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

esm: port loader code to JS #32201

Closed
wants to merge 7 commits into from
Closed

Conversation

addaleax
Copy link
Member

There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

@nodejs/modules-active-members

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.
@addaleax addaleax added the esm Issues and PRs related to the ECMAScript Modules implementation. label Mar 11, 2020
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Mar 11, 2020
@MylesBorins
Copy link
Member

@addaleax thanks much for this awesome work.

It seems like the work might overlap a bit with some error improvements I was trying to do in #32052 (which ended up being complicated due to the split logic between js + c++)

Would it make sense to fold the logic from my PR into here?

@bmeck
Copy link
Member

bmeck commented Mar 11, 2020

Note: last time we benchmarked this, sync fs ops were significantly faster during resolution. We should benchmark before jumping onto async.

Copy link
Contributor

@jkrems jkrems left a comment

Choose a reason for hiding this comment

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

Thanks so much for taking this on! I did an initial pass and it LGTM overall.

test/es-module/test-esm-exports.mjs Show resolved Hide resolved
@bmeck
Copy link
Member

bmeck commented Mar 11, 2020

@addaleax This should use primordials and cached builtins. If desired, I can make the changes and push them onto this PR.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Sorry only some of these are suggestions; it’s really hard to type them out on mobile :-)

lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
@addaleax
Copy link
Member Author

@MylesBorins If you want, I can try to integrate your changes – at the moment, I was trying to go for a 1:1 conversion from ESM, but if there’s consensus for further changes, I’m okay with that.

@addaleax
Copy link
Member Author

@bmeck I’ve added primordials usage where I could find it – let me know if there’s anything else. (You can feel free to push changes if you think that they make sense, too.)

Copy link
Member

@bmeck bmeck left a comment

Choose a reason for hiding this comment

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

some nits, will need to re-review due to size

lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
@MylesBorins
Copy link
Member

@addaleax there was consensus on the behavior in the PR I mentioned, just needed work to ensure it covered both CJS + ESM loader. LMK if you prefer I try and add it to this PR and push a commit, since it seems like you have your work cut out for you here

lib/internal/modules/esm/get_format.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
@addaleax
Copy link
Member Author

LMK if you prefer I try and add it to this PR and push a commit, since it seems like you have your work cut out for you here

Yeah, that would be great.

To be honest, I’m not clear on the exact semantics here everywhere, although it’s easy enough to translate the code into JS.

addaleax and others added 2 commits March 12, 2020 12:31
Co-Authored-By: Bradley Farias <bradley.meck@gmail.com>
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Mar 12, 2020

CI: https://ci.nodejs.org/job/node-test-pull-request/29786/ (:white_check_mark:)

}
}

function getPackageConfig(path, base) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be possible to update this to be shared with https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L245 now. That can be a further refactoring certainly, but it's ripe to share the package.json caches and C++ JSON parser code optimized for package.json reading.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd like to move that refactoring to a later point. In a similar vein: We could also share the exports logic now!

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Mar 13, 2020
@addaleax
Copy link
Member Author

Landed in 605615e

@addaleax addaleax closed this Mar 13, 2020
addaleax added a commit that referenced this pull request Mar 13, 2020
There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

PR-URL: #32201
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
@addaleax addaleax deleted the modulewrap-js branch March 13, 2020 16:35
BridgeAR pushed a commit that referenced this pull request Mar 17, 2020
There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

PR-URL: #32201
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Mar 19, 2020
MylesBorins pushed a commit that referenced this pull request Mar 24, 2020
There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

PR-URL: #32201
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
@MylesBorins
Copy link
Member

hey @addaleax do you think we should backport this to 12.x? Seems to me like if we don't future module updates will be hard to land.

@targos targos removed author ready PRs that have at least one approval, no pending requests for changes, and a CI started. backport-requested-v12.x labels Apr 25, 2020
targos pushed a commit to targos/node that referenced this pull request Apr 25, 2020
There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

PR-URL: nodejs#32201
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
targos pushed a commit that referenced this pull request Apr 28, 2020
There is no reason for this to be in C++. Using JavaScript means that
the code is more accessible to more developers, which is important
for any Node.js feature. This also simplifies the code significantly
in some areas. On the technical side, this potentially also enables
making some of the file system operations that are involved
asynchronous.

PR-URL: #32201
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants