diff --git a/src/loader/file-type/script/async.ts b/src/loader/file-type/script/async.ts index b94d2a27..d42ec6e2 100644 --- a/src/loader/file-type/script/async.ts +++ b/src/loader/file-type/script/async.ts @@ -7,15 +7,11 @@ import { pathToFileURL } from 'url'; import { LocatorInfo, pathToLocatorInfo } from '../../../locator'; -import { handleFileLoadError, hasOwnProperty } from '../../../utils'; +import { handleFileLoadError, hasOwnProperty, isObject } from '../../../utils'; import { buildLoaderFilePath } from '../../utils'; -import { LoaderFilterFn, ScriptFileExportItem } from './type'; +import { LoaderFilterFn, ScriptFileExportItem, ScriptFileLoadOptions } from './type'; import { getExportItem } from './utils'; -type ScriptFileLoadOptions = { - withExtension?: boolean, - withFilePrefix?: boolean -}; export async function loadScriptFile( data: LocatorInfo | string, options?: ScriptFileLoadOptions, @@ -39,17 +35,17 @@ export async function loadScriptFile( return await import(filePath); } catch (e) { if ( - e instanceof Error && + isObject(e) && hasOwnProperty(e, 'code') ) { - if (e.code === 'ERR_MODULE_NOT_FOUND') { + if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') { return loadScriptFile(locatorInfo, { ...options, withExtension: true, }); } - if (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME') { + if (e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME') { return loadScriptFile(locatorInfo, { ...options, withFilePrefix: true, diff --git a/src/loader/file-type/script/sync.ts b/src/loader/file-type/script/sync.ts index ea21fe8f..41954fd8 100644 --- a/src/loader/file-type/script/sync.ts +++ b/src/loader/file-type/script/sync.ts @@ -5,21 +5,44 @@ * view the LICENSE file that was distributed with this source code. */ -import { LoaderFilterFn, ScriptFileExportItem } from './type'; +import { LoaderFilterFn, ScriptFileExportItem, ScriptFileLoadOptions } from './type'; import { getExportItem } from './utils'; -import { LocatorInfo, isLocatorInfo } from '../../../locator'; +import { LocatorInfo, isLocatorInfo, pathToLocatorInfo } from '../../../locator'; import { buildLoaderFilePath } from '../../utils'; -import { handleFileLoadError } from '../../../utils'; +import { handleFileLoadError, hasOwnProperty, isObject } from '../../../utils'; -export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undefined { - const filePath = isLocatorInfo(data) ? - buildLoaderFilePath(data) : - data; +export function loadScriptFileSync( + data: LocatorInfo | string, + options?: ScriptFileLoadOptions, +) : unknown | undefined { + let locatorInfo : LocatorInfo; + + if (typeof data === 'string') { + locatorInfo = pathToLocatorInfo(data); + } else { + locatorInfo = data; + } + + options = options || {}; + + const filePath = buildLoaderFilePath(locatorInfo, options.withExtension); try { // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require return require(filePath); } catch (e) { + if ( + isObject(e) && + hasOwnProperty(e, 'code') + ) { + if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') { + return loadScriptFileSync(locatorInfo, { + ...options, + withExtension: true, + }); + } + } + return handleFileLoadError(e); } } diff --git a/src/loader/file-type/script/type.ts b/src/loader/file-type/script/type.ts index bfd3214f..aee5d84c 100644 --- a/src/loader/file-type/script/type.ts +++ b/src/loader/file-type/script/type.ts @@ -6,7 +6,13 @@ */ export type LoaderFilterFn = (key: string, value: unknown) => boolean; + export type ScriptFileExportItem = { key: string, value: unknown }; + +export type ScriptFileLoadOptions = { + withExtension?: boolean, + withFilePrefix?: boolean +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index c13a07f0..ff606028 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -8,4 +8,5 @@ export * from './error'; export * from './file-name'; export * from './has-own-property'; +export * from './object'; export * from './to-array'; diff --git a/src/utils/object.ts b/src/utils/object.ts new file mode 100644 index 00000000..73599a1e --- /dev/null +++ b/src/utils/object.ts @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +export function isObject(item: unknown) : item is Record { + return ( + !!item && + typeof item === 'object' && + !Array.isArray(item) + ); +} diff --git a/test/data/file-default.js b/test/data/file-default.mjs similarity index 100% rename from test/data/file-default.js rename to test/data/file-default.mjs