Skip to content
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

Fix commonjs-extension-resolution-loader #10

Merged
merged 2 commits into from
Sep 25, 2022

Conversation

GeoffreyBooth
Copy link
Member

This loader should never have been part of the original repo, since it didn’t work 😄

This PR simply gets this loader working per its own test. As a follow-up I’d like to add all the Node codebase tests for --experimental-specifier-resolution=node to ensure that they all pass with this loader.

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

}
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.

@GeoffreyBooth GeoffreyBooth merged commit 1dfa865 into main Sep 25, 2022
@GeoffreyBooth GeoffreyBooth deleted the commonjs-resolution-loader branch September 25, 2022 13:08
@JakobJingleheimer
Copy link
Member

W00t! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants