Skip to content

Commit ef1073a

Browse files
Merge pull request #11 from nodejs/specifier-resolution-tests
2 parents 1dfa865 + ed9a48e commit ef1073a

File tree

21 files changed

+250
-7
lines changed

21 files changed

+250
-7
lines changed

commonjs-extension-resolution-loader/loader.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ export async function resolve(specifier, context, next) {
2424
}
2525
const parentPath = fileURLToPath(parentURL);
2626

27-
const resolution = await resolveAsync(specifier, {
28-
basedir: dirname(parentPath),
29-
extensions: ['.js', '.json', '.node'],
30-
});
31-
const url = pathToFileURL(resolution).href;
27+
let url;
28+
try {
29+
const resolution = await resolveAsync(specifier, {
30+
basedir: dirname(parentPath),
31+
// For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions
32+
// but it does search for index.mjs files within directories
33+
extensions: ['.js', '.json', '.node', '.mjs'],
34+
});
35+
url = pathToFileURL(resolution).href;
36+
} catch (error) {
37+
if (error.code === 'MODULE_NOT_FOUND') {
38+
// Match Node's error code
39+
error.code = 'ERR_MODULE_NOT_FOUND';
40+
}
41+
throw error;
42+
}
3243

3344
return next(url, context);
3445
}

commonjs-extension-resolution-loader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "./loader.js",
77
"scripts": {
88
"start": "npm test",
9-
"test": "node test.js"
9+
"test": "node test/basic.js && node test/test-esm-specifiers.js && node test/test-esm-specifiers-symlink.js"
1010
},
1111
"author": "Geoffrey Booth <[email protected]>",
1212
"license": "MIT",
File renamed without changes.
File renamed without changes.
File renamed without changes.

commonjs-extension-resolution-loader/test.js renamed to commonjs-extension-resolution-loader/test/basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { execPath } from 'process';
99
const child = spawn(execPath, [
1010
'--loader',
1111
'./loader.js',
12-
'./fixtures/index.js'
12+
'./test/basic-fixtures/index.js'
1313
]);
1414

1515
let stdout = '';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Ported from https://github.com/nodejs/node/blob/45f2258f74117e27ffced434a98ad6babc14f7fa/test/common/index.js
2+
3+
import { spawn } from 'node:child_process';
4+
5+
6+
export function spawnPromisified(...args) {
7+
let stderr = '';
8+
let stdout = '';
9+
10+
const child = spawn(...args);
11+
child.stderr.setEncoding('utf8');
12+
child.stderr.on('data', (data) => { stderr += data; });
13+
child.stdout.setEncoding('utf8');
14+
child.stdout.on('data', (data) => { stdout += data; });
15+
16+
return new Promise((resolve, reject) => {
17+
child.on('close', (code, signal) => {
18+
resolve({
19+
code,
20+
signal,
21+
stderr,
22+
stdout,
23+
});
24+
});
25+
child.on('error', (code, signal) => {
26+
reject({
27+
code,
28+
signal,
29+
stderr,
30+
stdout,
31+
});
32+
});
33+
});
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import explicit from 'explicit-main';
2+
import implicit from 'implicit-main';
3+
import implicitModule from 'implicit-main-type-module';
4+
import noMain from 'no-main-field';
5+
6+
function getImplicitCommonjs () {
7+
return import('implicit-main-type-commonjs');
8+
}
9+
10+
export {explicit, implicit, implicitModule, getImplicitCommonjs, noMain};
11+
export default 'success';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'a';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 'b';

0 commit comments

Comments
 (0)