Skip to content

Fix commonjs-extension-resolution-loader #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think there's module.isBuiltin now

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'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if loaders should be able to expose their own hooks, for example to accept new extensions ... without that, a TS loader would have to implement the same thing but with a different extension, which would require to perform the same filesystem checks multiple times 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So ambient loaders weren’t enough, now we need loaders for our loaders . . .

Copy link
Member Author

@GeoffreyBooth GeoffreyBooth Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept new extensions

I think the way to achieve this is for loaders to optionally load config files. There’s nothing stopping me from adding some code to the top of loader.js to look for a possibly existing config file, and if found to import it, and read the extensions list from there.

One loader could communicate with the following ones by attaching something to globalThis, I think, since that should be shared within the loaders thread (?). So that’s another way to define/share configuration.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the sort of problem that I'm referring to when I say that the "helper functions" need to compose like hooks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they'll share globalThis (as long as we keep ambient loaders and lay loaders in the same thread).

But context may be a better vehicle, and will is guaranteed to work regardless of threading.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the blocker right now for the helper functions is the API design. I took a stab at it in nodejs/loaders#94 but I feel like that pass leaves a lot to be desired. A similar example is in https://github.com/browserify/resolve#resolveid-opts-cb: see the functions it accepts in the options to override its own lower-level operations.

});
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"
}
}
16 changes: 8 additions & 8 deletions commonjs-extension-resolution-loader/test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { ok } from 'assert';
import { match } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';


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

const child = spawn(execPath, [
'--experimental-loader',
'--loader',
'./loader.js',
'./fixtures/index.js'
]);

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();
ok(stdout.includes('hello from file.js'));
ok(stdout.includes('hello from folder/index.js'));
child.on('close', (_code, _signal) => {
stdout = stdout.toString();
match(stdout, /hello from file\.js/);
match(stdout, /hello from folder\/index\.js/);
});