-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
module: package imports targets outside package #52641
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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
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.
Overall looks great, thanks for contributing this!
@@ -485,7 +485,7 @@ where `import '#dep'` does not get the resolution of the external package | |||
file `./dep-polyfill.js` relative to the package in other environments. | |||
|
|||
Unlike the `"exports"` field, the `"imports"` field permits mapping to external | |||
packages. | |||
packages and locations. |
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.
Does https://nodejs.org/api/esm.html#resolution-algorithm-specification need to be updated as well?
lib/internal/modules/esm/resolve.js
Outdated
@@ -414,7 +416,8 @@ function resolvePackageTargetString( | |||
const resolvedPath = resolved.pathname; | |||
const packagePath = new URL('.', packageJSONUrl).pathname; | |||
|
|||
if (!StringPrototypeStartsWith(resolvedPath, packagePath)) { | |||
// if (mustBeInternalTarget && !StringPrototypeStartsWith(resolvedPath, packagePath)) { |
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.
Unsure why this is here.
// if (mustBeInternalTarget && !StringPrototypeStartsWith(resolvedPath, packagePath)) { |
lib/internal/modules/esm/resolve.js
Outdated
@@ -465,14 +468,15 @@ function isArrayIndex(key) { | |||
* @param {boolean} internal - Whether the package is internal. | |||
* @param {boolean} isPathMap - Whether the package is a path map. | |||
* @param {Set<string>} conditions - The conditions to match. | |||
* @param {boolean} mustBeInternalTarget - If the target must be in the package boundary. |
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.
This might be a bit much for a JSDoc comment, but is it possible to briefly explain when we would want this restriction? Is it like If the target must be in the package scope: yes for "exports", no for "imports"
?
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.
@RafaelGSS does this needs to do anything specific for permissions?
this allows the imports field of package.json to target a location outside of the package boundary. doing so allows for imports to work in directories just altering things like the type field of package.json and to enable monorepo workspace workflows.
7def52e
to
a2eed40
Compare
this allows the imports field of package.json to target a location outside of the package boundary. doing so allows for imports to work in directories just altering things like the type field of package.json and to enable monorepo workspace workflows.
Node already allows resolving to external packages for
"imports"
; this fixes some usability problems of mixing monorepos and directories containing a{"type":"module"}
(or vice versa)package.json
preventing the ability to properly use"imports"
to resolve to reasonable locations. It does not enable..
in a specifiers to escape still per the tests it must be in the target of thepackage.json
to allow escaping.An example of a case where this feature is high friction is:
Note: this still requires duplication of imports for this use case with this PR.
Another example is in a monorepo situation: