Skip to content

Commit

Permalink
Get commonjs-extension-resolution-loader working
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Sep 25, 2022
1 parent 35765c7 commit d17c0ea
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 21 deletions.
39 changes: 23 additions & 16 deletions commonjs-extension-resolution-loader/loader.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { existsSync } from 'fs';
import { createRequire } from 'module';
import { builtinModules } from 'node:module';
import { dirname } from 'path';
import { URL, fileURLToPath, pathToFileURL } from 'url';
import { cwd } from 'process';
import { fileURLToPath, pathToFileURL } from 'url';
import { promisify } from 'util';

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

export function resolve(specifier, context, defaultResolve) {
const resolveAsync = promisify(resolveCallback);

const baseURL = pathToFileURL(cwd() + '/').href;


export async function resolve(specifier, context, next) {
const { parentURL = baseURL } = context;

// `require.resolve` works with paths, not URLs, so convert to and from
if (specifier.startsWith('node:') || builtinModules.includes(specifier)) {
return next(specifier, context);
}

// `resolveAsync` works with paths, not URLs
if (specifier.startsWith('file://')) {
specifier = fileURLToPath(specifier);
}
const basePath = dirname(fileURLToPath(parentURL));
const resolvedPath = require.resolve(specifier, {paths: [basePath]});
const parentPath = fileURLToPath(parentURL);

if (existsSync(resolvedPath)) {
return {
url: pathToFileURL(resolvedPath).href
};
}
const resolution = await resolveAsync(specifier, {
basedir: dirname(parentPath),
extensions: ['.js', '.json', '.node'],
});
const url = pathToFileURL(resolution).href;

// Let Node.js handle all other specifiers, such as package names
return defaultResolve(specifier, context, defaultResolve);
return next(url, context);
}
118 changes: 118 additions & 0 deletions commonjs-extension-resolution-loader/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions commonjs-extension-resolution-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"start": "npm test",
"test": "node test.js"
},
"author": "Geoffrey Booth <[email protected]>",
"license": "MIT"
"author": "Geoffrey Booth <[email protected]>",
"license": "MIT",
"dependencies": {
"resolve": "^1.22.1"

This comment has been minimized.

Copy link
@ljharb

ljharb Oct 2, 2022

Member

You may want to update to next (v2 prerelease) here

}
}
6 changes: 3 additions & 3 deletions commonjs-extension-resolution-loader/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const child = spawn(execPath, [

let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
child.stdout.on('data', data => {
stdout += data;
});

child.on('close', (code, signal) => {
stdout = stdout.toString();
child.on('close', (_code, _signal) => {
stdout = stdout.toString();
match(stdout, /hello from file\.js/);
match(stdout, /hello from folder\/index\.js/);
});

0 comments on commit d17c0ea

Please sign in to comment.