Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Feb 21, 2022
1 parent 5db54d3 commit 203325b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ export function createEsmHooks(tsNodeService: Service) {
if (!isProbablyEntrypoint(specifier, context.parentURL))
throw esmResolverError;
try {
let cjsSpecifier = specifier;
// Attempt to convert from ESM file:// to CommonJS path
try {
if (specifier.startsWith('file://'))
cjsSpecifier = fileURLToPath(specifier);
} catch {}
const resolution = pathToFileURL(
createRequire(process.cwd()).resolve(specifier)
createRequire(process.cwd()).resolve(cjsSpecifier)
).toString();
rememberIsProbablyEntrypoint.add(resolution);
rememberResolvedViaCommonjsFallback.add(resolution);
Expand Down
17 changes: 16 additions & 1 deletion src/test/esm-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,22 @@ test.suite('esm', (test) => {

test.suite(
'Entrypoint resolution falls back to CommonJS resolver and format',
(test) => {}
(test) => {
test('extensionless entrypoint', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/extensionless-entrypoint`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});
test('relies upon CommonJS resolution', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/relies-upon-cjs-resolution`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});
}
);
});

Expand Down

0 comments on commit 203325b

Please sign in to comment.