Skip to content

Commit

Permalink
fix: guarantee that option is not already set on load error
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 30, 2022
1 parent d7b31ef commit b1736dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/loader/file-type/script/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,26 @@ export async function loadScriptFile(
isObject(e) &&
hasOwnProperty(e, 'code')
) {
if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
if (
!options.withExtension &&
(
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' || e.code === 'UNSUPPORTED_ESM_URL_SCHEME') {
if (
!options.withFilePrefix &&
(
e.code === 'ERR_UNSUPPORTED_ESM_URL_SCHEME' ||
e.code === 'UNSUPPORTED_ESM_URL_SCHEME'
)
) {
return loadScriptFile(locatorInfo, {
...options,
withFilePrefix: true,
Expand Down
8 changes: 7 additions & 1 deletion src/loader/file-type/script/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ export function loadScriptFileSync(
isObject(e) &&
hasOwnProperty(e, 'code')
) {
if (e.code === 'ERR_MODULE_NOT_FOUND' || e.code === 'MODULE_NOT_FOUND') {
if (
!options.withExtension &&
(
e.code === 'ERR_MODULE_NOT_FOUND' ||
e.code === 'MODULE_NOT_FOUND'
)
) {
return loadScriptFileSync(locatorInfo, {
...options,
withExtension: true,
Expand Down
15 changes: 12 additions & 3 deletions test/unit/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import path from "path";
import {
buildLoaderFilePath, loadScriptFile,
buildLoaderFilePath,
loadFile,
loadFileSync,
loadScriptFile,
loadScriptFileExport,
loadScriptFileExportSync, loadScriptFileSync,
loadScriptFileExportSync,
loadScriptFileSync,
locateFile,
locateFileSync
} from "../../src";
import {loadFile, loadFileSync} from "../../src";

const basePath = path.join(__dirname, '..', 'data');

Expand Down Expand Up @@ -120,5 +123,11 @@ describe('src/loader/**', () => {
locatorInfo = locateFileSync( 'file.foo', {path: [basePath]});
loaderContent = loadFileSync(locatorInfo);
expect(loaderContent).toBeUndefined();

loaderContent = await loadFile('file.foo');
expect(loaderContent).toBeUndefined();

loaderContent = loadFileSync('file.foo');
expect(loaderContent).toBeUndefined();
});
});

0 comments on commit b1736dd

Please sign in to comment.