Skip to content

Commit d17c0ea

Browse files
committed
Get commonjs-extension-resolution-loader working
1 parent 35765c7 commit d17c0ea

File tree

4 files changed

+149
-21
lines changed

4 files changed

+149
-21
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
}

commonjs-extension-resolution-loader/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const child = spawn(execPath, [
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();
21+
child.on('close', (_code, _signal) => {
22+
stdout = stdout.toString();
2323
match(stdout, /hello from file\.js/);
2424
match(stdout, /hello from folder\/index\.js/);
2525
});

0 commit comments

Comments
 (0)