-
Notifications
You must be signed in to change notification settings - Fork 18
ESM_FORMAT: Avoid reading package.json when extension is unambiguous #33
Conversation
Refs: nodejs/modules#180 PR-URL: nodejs#6 Reviewed-By: Myles Borins <MylesBorins@google.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Refs: nodejs/modules#180 PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Refs: nodejs/modules#180 PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Refs: nodejs/modules#180 PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Myles Borins <mylesborins@google.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
PR-URL: nodejs#6 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Myles Borins <mylesborins@google.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
It’s wasteful to load the package.json even if the extension is unambiguous. This patch optimizes for the zero-config case by avoiding needless package.json loads. With this patch, the mental model becomes a lot simpler too: - `.cjs` means `commonjs` - `.mjs` means `module` See discussion thread: https://twitter.com/mathias/status/1096167102455709696
I don’t think this is a future-looking optimization; eventually package.json should be able to override any extension to parse in any way. |
That’s what’s under discussion. IMHO it’s worth carving out We need to weigh the cost of allowing |
This PR looks good. My view is that package.json is about defining package-scoped things, whereas extensions/pragmas are a way of specialising/overriding behaviour on a per-file basis. This locality simplifies the mental model and eliminates a data-dependency. |
Thanks @mathiasbynens for the PR. This is a simplification of the existing spec that seems sensible to me. There is effectively only one behavioural change here from what was written previously and that is that previously if there was no package boundary, we would load This sounds like a spec bug to me, where the fix happens to enable an optimization. We as a group can decide over time whether we want that optimization to remain, but what we're really deciding from a behavioural perspective is if |
+1 FWIW, there is precedent for this in both |
Note that the implementation already does this too, so that this is actually just a spec bug. |
This is fine for where the implementation is now, but if we add package exports we'll always need to read package.json to know what the valid paths inside a package are. Also package-level loaders, if added, would also require always reading package.json. Ditto for file extension configuration like @ljharb said. Another point is if But for right now, sure. |
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.
Just noticed this line is missing.
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.
Lgtm
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.
We don’t even have consensus that the cjs extension will be a thing, and no extension should be special - if any of them can’t br overridden, then none of them can (including js)
@ljharb please read #33 (comment) and then let me know if you want to revise your comment. |
@guybedford that comment hasn’t made anything clear to me about why I’d revise it. In the absence of a package.json, and perhaps when run as an entry point, it makes sense that a file extension unambiguously determines the parse goal, but if a package.json is present (and certainly when it’s not an entry point), it must always be consulted regardless of the file extension. No extension should be special; either they can all be overridden from package.json or none of them can. Separately, adding the cjs extension, without any consensus besides apparent side channel discussions about it, isn’t something that should be done via a PR. |
Currently, by the spec, The change here ONLY changes the above. That is the only behavioural change. |
bd5c877
to
0237465
Compare
bec588f
to
ea59221
Compare
484d1fb
to
7efc53d
Compare
c7fa700
to
d69f765
Compare
335d584
to
9a343ce
Compare
3a00b51
to
bc101f6
Compare
fd5b55a
to
3a40599
Compare
6fe40a4
to
d9cdfd8
Compare
I imagine this can/should be closed at this point? Or if not closed, then at least transferred to the main repository? |
It’s wasteful to load the package.json even if the extension is unambiguous. This patch optimizes for the zero-config case by avoiding needless package.json loads.
With this patch, the mental model becomes a lot simpler too:
.cjs
meanscommonjs
.mjs
meansmodule
See discussion thread: https://twitter.com/mathias/status/1096167102455709696
Checklist