Skip to content

module: deprecate trailing slash pattern mappings - #40039

Closed
guybedford wants to merge 5 commits into
nodejs:masterfrom
guybedford:restrict-exports-slash
Closed

module: deprecate trailing slash pattern mappings#40039
guybedford wants to merge 5 commits into
nodejs:masterfrom
guybedford:restrict-exports-slash

Conversation

@guybedford

@guybedfordguybedford commented Sep 8, 2021

Copy link
Copy Markdown
Contributor

This PR deprecates the ability for pattern mappings to be able to resolve import specifiers ending in / like import('pkg/subpath/'), with a new deprecation warning and message.

The primary motivation here is that the import maps specification does not support trailing / mappings resolving.

For example:

<!doctype html><scripttype="importmap">{"imports": {"pkg/subpath/": "/pkg/index.js"}}</script><scripttype="module">import'pkg/subpath/';</script>

will give the browser console error:

Ignored an import map value of "pkg/subpath/": Since specifierKey ended in a slash, so must the address: /pkg/index.js

See WICG/import-maps#244 for more background here.

Since we deprecated folder subpaths, there are no common patterns of trailing / usage yet. It is only with the introduction of pattern trailers in #39635 that it is now easy to define maps like:

{"exports": {"./*/": "./*/index.js"}}

to support import('pkg/asdf/') resolving to pkg/asdf/index.js for any name.

The risk is that users start defining packages like this and then these packages will never be supportable in import maps environments. By deprecating this now we can avoid this outcome.

@nodejs-github-botnodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Sep 8, 2021
@guybedford

Copy link
Copy Markdown
ContributorAuthor

@nodejs/modules

Comment threaddoc/api/deprecations.md
Comment threaddoc/api/deprecations.md Outdated
@aduh95aduh95 added deprecations Issues and PRs related to deprecations. semver-major PRs that contain breaking changes and should be released in the next major version. labels Sep 8, 2021
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@guybedford
guybedfordforce-pushed the restrict-exports-slash branch from 1dd5eaa to 5129ba6CompareSeptember 8, 2021 21:43
@guybedfordguybedford added deprecations Issues and PRs related to deprecations. and removed semver-major PRs that contain breaking changes and should be released in the next major version. deprecations Issues and PRs related to deprecations. labels Sep 8, 2021
Comment threaddoc/api/deprecations.md Outdated
Comment threaddoc/api/deprecations.md Outdated
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@ljharb

ljharb commented Sep 8, 2021

Copy link
Copy Markdown
Member

What about core modules? It's a very common pattern to require, eg fs/ to indicate that the user wants node_modules/fs and not the core fs module.

… or does this only apply to exports?

What about wanting to disambiguate foo.js from foo/index.js, and wanting the user to do $pkg/foo vs $pkg/foo/?

@guybedford

Copy link
Copy Markdown
ContributorAuthor

@ljharb now that we have node:fs I think that's the better pattern for distinction going forward.

And yes, disambiguation of pkg/foo versus pkg/foo/ in the import maps specification not being possible is exactly the issue here.

Comment threadtest/es-module/test-esm-exports-deprecations.mjs Outdated
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@ljharb

Copy link
Copy Markdown
Member

@guybedford for a core module, yes, i can use node: as a prefix, but i'd still need fs/ to be able to get at node_modules/fs - the prefix doesn't change that.

And yes, disambiguation of pkg/foo versus pkg/foo/ in the import maps specification not being possible is exactly the issue here.

Why are we reducing the usefulness of node because an entirely different specification - one node doesn't even follow - lacks functionality?

@aduh95

Copy link
Copy Markdown
Contributor

To be clear, you mean being able to do something like that, correct?

{
"imports": {
"#fs": "fs/"
}
}

And import '#fs' would load node_modules/fs package. But I don't think this PR forbids that, does it?

@guybedford

guybedford commented Sep 9, 2021

Copy link
Copy Markdown
ContributorAuthor

Note that import 'fs/' resolving node_modules/fs/index.js is deprecated due to folder mappings.

So this is just fs/subpath/ use cases.

Why are we reducing the usefulness of node because an entirely different specification - one node doesn't even follow - lacks functionality?

So this PR only reduces the functionality of a feature landed two days ago with pattern trailers.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@ljharb

Copy link
Copy Markdown
Member

By deprecated, you mean in ESM, or in CJS?

Either way, if I have a folder in node_modules/fs with a package.json with an "exports" with a dot in it, how do you propose i both require and import it?

In CJS, require('fs/') would be the answer; you seem to be suggesting that in ESM there's no non-deprecated answer. That seems like a problem.

@guybedford

Copy link
Copy Markdown
ContributorAuthor

By deprecated, you mean in ESM, or in CJS?

We deprecated it specifically for ESM per the folder mappings deprecation PR.

Yes, ESM does deprecate the ability to load npm packages with the same names as core modules, but this is completely off topic at this point for this PR - I'd suggest opening another issue to discuss that.

guybedford added a commit that referenced this pull request Sep 15, 2021
PR-URL: #40039
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
@guybedford

Copy link
Copy Markdown
ContributorAuthor

Landed in 7216fb1.

@guybedford
guybedford deleted the restrict-exports-slash branch September 15, 2021 06:59
BethGriggs pushed a commit that referenced this pull request Sep 21, 2021
PR-URL: #40039
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
BethGriggs pushed a commit that referenced this pull request Sep 21, 2021
PR-URL: #40039
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
@BethGriggsBethGriggs mentioned this pull request Sep 21, 2021
1 task
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecationsIssues and PRs related to deprecations.esmIssues and PRs related to the ECMAScript Modules implementation.needs-ciPRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@guybedford@ljharb@aduh95@nodejs-github-bot@GeoffreyBooth