Skip to content

Commit 1dfa865

Browse files
Merge pull request #10 from nodejs/commonjs-resolution-loader
2 parents 4d6413f + d17c0ea commit 1dfa865

File tree

4 files changed

+154
-26
lines changed

4 files changed

+154
-26
lines changed
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
import { existsSync } from 'fs';
2-
import { createRequire } from 'module';
1+
import { builtinModules } from 'node:module';
32
import { dirname } from 'path';
4-
import { URL, fileURLToPath, pathToFileURL } from 'url';
3+
import { cwd } from 'process';
4+
import { fileURLToPath, pathToFileURL } from 'url';
5+
import { promisify } from 'util';
56

6-
const require = createRequire(import.meta.url);
7-
const baseURL = pathToFileURL(process.cwd() + '/').href;
7+
import resolveCallback from 'resolve/async.js';
88

9-
export function resolve(specifier, context, defaultResolve) {
9+
const resolveAsync = promisify(resolveCallback);
10+
11+
const baseURL = pathToFileURL(cwd() + '/').href;
12+
13+
14+
export async function resolve(specifier, context, next) {
1015
const { parentURL = baseURL } = context;
1116

12-
// `require.resolve` works with paths, not URLs, so convert to and from
17+
if (specifier.startsWith('node:') || builtinModules.includes(specifier)) {
18+
return next(specifier, context);
19+
}
20+
21+
// `resolveAsync` works with paths, not URLs
1322
if (specifier.startsWith('file://')) {
1423
specifier = fileURLToPath(specifier);
1524
}
16-
const basePath = dirname(fileURLToPath(parentURL));
17-
const resolvedPath = require.resolve(specifier, {paths: [basePath]});
25+
const parentPath = fileURLToPath(parentURL);
1826

19-
if (existsSync(resolvedPath)) {
20-
return {
21-
url: pathToFileURL(resolvedPath).href
22-
};
23-
}
27+
const resolution = await resolveAsync(specifier, {
28+
basedir: dirname(parentPath),
29+
extensions: ['.js', '.json', '.node'],
30+
});
31+
const url = pathToFileURL(resolution).href;
2432

25-
// Let Node.js handle all other specifiers, such as package names
26-
return defaultResolve(specifier, context, defaultResolve);
33+
return next(url, context);
2734
}

commonjs-extension-resolution-loader/package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commonjs-extension-resolution-loader/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"start": "npm test",
99
"test": "node test.js"
1010
},
11-
"author": "Geoffrey Booth <[email protected]>",
12-
"license": "MIT"
11+
"author": "Geoffrey Booth <[email protected]>",
12+
"license": "MIT",
13+
"dependencies": {
14+
"resolve": "^1.22.1"
15+
}
1316
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { ok } from 'assert';
1+
import { match } from 'assert';
22
import { spawn } from 'child_process';
33
import { execPath } from 'process';
44

55

66
// Run this test yourself with debugging mode via:
7-
// node --inspect-brk --experimental-loader ./loader.js ./fixtures/index.js
7+
// node --inspect-brk --loader ./loader.js ./fixtures/index.js
88

99
const child = spawn(execPath, [
10-
'--experimental-loader',
10+
'--loader',
1111
'./loader.js',
1212
'./fixtures/index.js'
1313
]);
1414

1515
let stdout = '';
1616
child.stdout.setEncoding('utf8');
17-
child.stdout.on('data', (data) => {
17+
child.stdout.on('data', data => {
1818
stdout += data;
1919
});
2020

21-
child.on('close', (code, signal) => {
22-
stdout = stdout.toString();
23-
ok(stdout.includes('hello from file.js'));
24-
ok(stdout.includes('hello from folder/index.js'));
21+
child.on('close', (_code, _signal) => {
22+
stdout = stdout.toString();
23+
match(stdout, /hello from file\.js/);
24+
match(stdout, /hello from folder\/index\.js/);
2525
});

0 commit comments

Comments
 (0)