Repository navigation
esm: Add support for pjson cache and main without extensions #18728
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f14420e
esm: support main without extension and pjson cache
guybedford adfe1ac
pr feedback
guybedford ebdb3cb
explicit isMain checks
guybedford 5855e5c
loader setup promise tweaks
guybedford 05c9941
further tweaks
guybedford 2d6d4e6
c++ feedback
guybedford a06db2d
better todo for JSON parse failure
guybedford 081aab4
invalid json handling todo
guybedford ed9df8a
naming adjustments, simplifications
guybedford 8b4d85f
move PackageConfig into loader namespace
guybedford 4468e2c
make PackageConfig consts
guybedford b4dc802
clean up emplace logic a little
guybedford 9f7aabd
boolean aliasing
guybedford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
loader setup promise tweaks
- Loading branch information
commit 5855e5ca839100fb8fb23d4b878e46ef61dc1708
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,29 +21,35 @@ function initializeImportMetaObject(wrap, meta) { | |
| meta.url = wrap.url; | ||
| } | ||
|
|
||
| let loaderResolve, loaderReject; | ||
| exports.loaderPromise = new Promise((resolve, reject) => { | ||
| exports.setup = function() { | ||
| setInitializeImportMetaObjectCallback(initializeImportMetaObject); | ||
|
|
||
| let ESMLoader = new Loader(); | ||
| const loaderPromise = (async () => { | ||
| const userLoader = process.binding('config').userLoader; | ||
| if (userLoader) { | ||
| const hooks = await ESMLoader.import( | ||
| userLoader, getURLFromFilePath(`${process.cwd()}/`).href); | ||
| ESMLoader = new Loader(); | ||
| ESMLoader.hook(hooks); | ||
| exports.ESMLoader = ESMLoader; | ||
| } | ||
| return ESMLoader; | ||
| })(); | ||
| loaderPromise.then(resolve, reject); | ||
|
|
||
| setImportModuleDynamicallyCallback(async (referrer, specifier) => { | ||
| const loader = await loaderPromise; | ||
| return loader.import(specifier, normalizeReferrerURL(referrer)); | ||
| }); | ||
|
|
||
| exports.ESMLoader = ESMLoader; | ||
| }; | ||
| loaderResolve = resolve; | ||
| loaderReject = reject; | ||
| }); | ||
|
|
||
| exports.ESMLoader = undefined; | ||
|
|
||
| exports.setup = function() { | ||
| setInitializeImportMetaObjectCallback(initializeImportMetaObject); | ||
|
|
||
| let ESMLoader = new Loader(); | ||
| const loaderPromise = (async () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't a great pattern and errors here are suppressed unless explicitly asked for |
||
| const userLoader = process.binding('config').userLoader; | ||
| if (userLoader) { | ||
| const hooks = await ESMLoader.import( | ||
| userLoader, getURLFromFilePath(`${process.cwd()}/`).href); | ||
| ESMLoader = new Loader(); | ||
| ESMLoader.hook(hooks); | ||
| exports.ESMLoader = ESMLoader; | ||
| } | ||
| return ESMLoader; | ||
| })(); | ||
| loaderPromise.then(loaderResolve, loaderReject); | ||
|
|
||
| setImportModuleDynamicallyCallback(async (referrer, specifier) => { | ||
| const loader = await loaderPromise; | ||
| return loader.import(specifier, normalizeReferrerURL(referrer)); | ||
| }); | ||
|
|
||
| exports.ESMLoader = ESMLoader; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, elaborating on what I wrote before:
Can be written as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also just use createPromise/promiseReject/promiseResolve, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devsnek that's technically true but I'm not sure it would help clean the code and that's an internal API - I doubt the cost of the extra closure here is worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked through this code at all, so take this with a grain of salt, but I recently did a lot of benchmarking on using the Promise constructor vs the internal createPromise/promiseReject/promiseResolve and the latter is definitely quite a bit faster.
But again, I don't know about the usage here, whether this is performance sensitive code, if these functions end up being user-land exposed, etc.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just meant for readability heh, i was actually under the impression that the api was slower than the promise constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devsnek @apapirovski not allocating a closure and it being faster is actually why we have this API to begin with (as well as
util.promisify). There's a bit of history involved but if it wasn't faster I'd be surprised. That said - this code does I/O and the impact of an extra closure isn't something we should optimize for.@devsnek I think it makes the code easier for people more familiar with promises in core to read but overall I personally prefer standard to nonstandard when we have the choice.
Namely, there are subtle differences between
promiseResolveandresolvein the promise constructor which I'd rather avoid in areas that are not performance sensitive. I don't feel strongly about it though.