-
Notifications
You must be signed in to change notification settings - Fork 14
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
Private name mappings (Imports) #47
Comments
The suggestions I'm aware of are:
My personal preference is to build on top of self-reference and conditionals and use a new condition to express this. Imports"name": "pkg",
"imports": { "#fetch": "./lib/fetch.mjs" }
import '#fetch'; // only visible to code within the package boundary Exports Sigil"name": "pkg",
"exports": { "./#fetch": "./lib/fetch.mjs" }
import 'pkg/#fetch'; // only visible to code when using self-reference imports Exports Condition"name": "pkg",
"exports": { "./fetch": { "internal": "./lib/fetch.mjs" } }
import 'pkg/fetch'; // only visible to code when using self-reference imports |
If you look at the example posted in uuidjs/uuid#462 (comment) the first thing one notices is that "exports" is already more verbose than the browser field (and obviously because it carries the ability to hold more information than just browser mappings). If we use an internal condition name I would be concerned with the level of verbosity in that case, since the internal mapping would then need to additionally compose with the browser mapping and any other mappings, leading to quite deep nesting. I think it would be beneficial to try and ship something here especially given the resistance @sokra indicated to wanting to implement private mappings on the public API. I would like to suggest that a variation on the sigil to use a
By retaining the leading Usage would be via Note: this variation of the feature is fully valid under current exports - you can ship it now and it will work in Node.js. That is, the |
Updated the sigil bit to include the |
The nice thing is that private names are well-enough adopted now for the meaning to be obvious to JS users. |
Using Note: I'm only referring to usage in imports like |
Something like that would also be possible: "name": "pkg",
"exports": { "#/fetch": "./lib/fetch.mjs" }
"exports": { "#./fetch": "./lib/fetch.mjs" }
import 'pkg/fetch'; // only visible to code when using self-reference imports But I dislike that as it's not visible from importing site that this is private. I also dislike using self reference import as you have to repeat the package name. |
So to summarize my wishlist would be:
It's only a wishlist, I'm also fine if not everything works. Here are some options that would fullfill that:
|
A clarification here: The bare specifiers in import maps aren't URLs and The |
Yes I can confirm that I also really like the backwards compatibility approach to implementation that came up here. |
If that's the case that's great and I'll take back my critique on that. In this case I prefer the |
My preference is for the "Exports Sigil" where Can we specifically mention that deep private exports work? For example One detail this "Exports Sigil" works today but is not restricted to self-reference. {
"name": "pkg",
"version": "0.1.0",
"exports": {
"./#/": "./",
"./#package.json": "./package.json"
}
} This package.json allows the following from inside or outside console.log('via "./#/" export', require('pkg/#/package.json'));
console.log('via "./#package.json" export', require('pkg/#package.json')); This is a good and bad thing. Technically making |
I've put together a PR here in nodejs/node#33780. Feedback welcome. |
First of all: the `pkg.exports` spec defines that object key order is being used when resolving for the targeted environment, see uuidjs/uuid#462 (comment) In order for `webpack@5` to pick up the `browser` overrides they must come _before_ `import` and `require`, see https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62 for preliminary documentation. To make full use of `pkg.exports` in node as well we can use package self reference instead of local paths, see the discussion in jkrems/proposal-pkg-exports#47
First of all: the `pkg.exports` spec defines that object key order is being used when resolving for the targeted environment, see uuidjs/uuid#462 (comment) In order for `webpack@5` to pick up the `browser` overrides they must come _before_ `import` and `require`, see https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62 for preliminary documentation. To make full use of `pkg.exports` in node as well we can use package self reference instead of local paths, see the discussion in jkrems/proposal-pkg-exports#47 Unfortunately the browser testing tool polendina doesn't play nice with self reference since it only looks for bare module specifiers in `node_modules` and `node_modules/polendina/node_modules`, see https://github.com/rvagg/polendina/blob/master/lib/webpack.config.js#L28-L39 We would need a way to set up an `resolve.alias` for webpack in polendina to make package self reference work.
The proposal currently lists a secondary
imports
field. This hasn't been implemented anywhere afaik. We should decide what the solution for that use case is.The text was updated successfully, but these errors were encountered: