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

module: add preImport loader hook #43245

Closed
wants to merge 2 commits into from
Closed

Conversation

guybedford
Copy link
Contributor

@guybedford guybedford commented May 29, 2022

Adds a new preImport loader hook which is called before each top-level import operation (dynamic and host-defined).

In the chaining model these hooks run in parallel and block subsequent module pipeline work for the graph until they all resolve. This allows tracking and resolution cache preparation.

The goal for this PR is to land an acceptable API that can then lead to the async resolve hook being entirely deprecated. If we can get to such a point we will then be able to obtain full alignment on import.meta.resolve being sync.

This is a draft PR for now, with the docs and API changes untested simply to illustrate the concept. Tests will be fleshed out after further review if there is agreement on the overall shape.

//cc @nodejs/modules @nodejs/loaders

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/modules

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels May 29, 2022
@GeoffreyBooth
Copy link
Member

Can you open a PR on the loaders repo to suggest a change to the design doc? We should discuss and agree upon a design first before landing new APIs.

@guybedford
Copy link
Contributor Author

guybedford commented Jun 18, 2022

With the sync resolve PR now merged, I've marked this PR as ready for further review on the API.

The loaders repo API proposal has now been open for three weeks as well in nodejs/loaders#83.

Based on feedback I will then prepare the final work for landing further in terms of fleshing out tests for the various edge cases.

@guybedford guybedford changed the title module: add preImport loader hook module: add import loader hook Jun 18, 2022
@guybedford guybedford changed the title module: add import loader hook module: add preImport loader hook Jun 19, 2022
Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

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

Blocking for now since we should go through the design review process in the loaders repo first. If/when this is accepted as part of the loaders API then we can proceed with this PR.

@guybedford
Copy link
Contributor Author

@GeoffreyBooth having gone through the process as requested, and had three weeks of zero response, I would appreciate if you could advise with respect to next steps.

I would have blocked the resolve hook sync PR from landing had I know we would ship that without this.

Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

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

I discussed this with @guybedford. I’d like this hook added to the loaders API design doc via a PR on that repo that gets reviewed by folks interested in loaders who may be missing the now-removed async resolve hook. I want to make sure the design of the proposed hook satisfies the use cases that they’re trying to achieve. Most (possibly all) of the relevant stakeholders are the people who participated in the discussion on this thread: nodejs/loaders#64.

Comment on lines +777 to +782
export async function preImport(specifier, context) {
if (context.topLevel)
console.log(`Top-level load of ${specifier}`);
else
console.log(`Dynamic import of ${specifier} in ${context.parentURL}`);
}
Copy link
Member

Choose a reason for hiding this comment

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

Can this please be a realistic example that shows how this hook would be used for one of its primary use cases? Since the hook doesn’t return anything, I’m confused as to how this would affect a resolve hook.

@GeoffreyBooth GeoffreyBooth self-requested a review June 19, 2022 03:27
@JakobJingleheimer
Copy link
Contributor

Woh! Sorry @guybedford! I don't know how I missed both this PR and the node/loaders PR. I'll try to take a look at them today/tomorrow.

Comment on lines +197 to +210
/**
* Whether this loader has userland hooks.
* @private
* @property {boolean} hasLoaderHooks
*/
#hasLoaderHooks = false;

/**
* Prior to ESM loading. These are called once before any modules are started.
* @private
* @property {KeyedHook[]} globalPreloaders Last-in-first-out
* @property {KeyedHook[]} globalPreloadHooks Last-in-first-out
* list of preload hooks.
*/
#globalPreloaders = [];
#globalPreloadHooks = [];
Copy link
Member

@GeoffreyBooth GeoffreyBooth Jun 23, 2022

Choose a reason for hiding this comment

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

@guybedford I like the refactoring you did in this PR. Can it be split off into its own PR, without the added preImport hook? cc @JakobJingleheimer

And per https://github.com/nodejs/loaders/blob/main/doc/meetings/2022-06-21.md#plan-per-today we should probably close this PR, at least for now while we pursue the threads approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oow. #hasCustomHooks 🙂 (but actually, I think that's not as useful as this.#HOOK_NAME.length > 1?)

FYI, I was about to tidy the hooks up to something like:

#chains = {
  globalPreload: [],
  resolve: [],
  load: [],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants