Skip to content

Commit

Permalink
fix: sync script loading with fallback for appending extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 29, 2022
1 parent f6091fa commit b988e0f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
14 changes: 5 additions & 9 deletions src/loader/file-type/script/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
37 changes: 30 additions & 7 deletions src/loader/file-type/script/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/loader/file-type/script/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
export * from './error';
export * from './file-name';
export * from './has-own-property';
export * from './object';
export * from './to-array';
14 changes: 14 additions & 0 deletions src/utils/object.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> {
return (
!!item &&
typeof item === 'object' &&
!Array.isArray(item)
);
}
File renamed without changes.

0 comments on commit b988e0f

Please sign in to comment.