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

commonjs-extension-resolution-loader: add experimental-specifier-resolution tests #11

Merged
merged 5 commits into from
Sep 25, 2022

Conversation

GeoffreyBooth
Copy link
Member

Extends #10 (merge that in first)

This PR follows up #10 with adding the Node codebase’s tests for --experimental-specifier-resolution=node, modified only to run without the full Node test harness (so path.join instead of fixtures.path, etc.) and replacing --experimental-specifier-resolution=node with --loader=./loader.js to use the loader in this repo.

Adding these tests led to two discoveries which caused minor changes in the loader:

  • --experimental-specifier-resolution=node includes .mjs as one of the extensions it searches for (who knew!)
  • The correct error code when a module isn’t found is ERR_MODULE_NOT_FOUND rather than MODULE_NOT_FOUND.

With those updates, all the --experimental-specifier-resolution=node tests pass with the commonjs-extension-resolution-loader loader in this repo. I think this means that this loader can be considered a feature-complete replacement for --experimental-specifier-resolution=node.

Copy link
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

🎉 🙌

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

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

Choose a reason for hiding this comment

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

TLDR: you might need to wrap the cwd() in a try/catch.

In internal/process/esm_loader.js, this cwd is wrapped in a try/catch. I don't remember why (I asked and Bradley answered, and I think I forgot document the reason in a comment).

basedir: dirname(parentPath),
// For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions
// but it does search for index.mjs files within directories
extensions: ['.js', '.json', '.node', '.mjs'],
Copy link
Member

@JakobJingleheimer JakobJingleheimer Sep 25, 2022

Choose a reason for hiding this comment

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

Nit: no need to re-construct this list every time resolve runs.

@GeoffreyBooth GeoffreyBooth merged commit ef1073a into main Sep 25, 2022
@GeoffreyBooth GeoffreyBooth deleted the specifier-resolution-tests branch September 25, 2022 15:12
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.

3 participants