-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
modules: remove file extension and directory resolving for esm #21729
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
this build on top of the limitations of the package name map proposal. It removes file extensions and directory resolution in the resolver. This is only currently limited for local development. When resolving dependencies file extension and directory resolution will still work. Refs: https://github.com/domenic/package-name-maps
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -552,6 +552,12 @@ enum ResolveExtensionsOptions { | |
| ONLY_VIA_EXTENSIONS | ||
| }; | ||
|
|
||
| inline bool ResolvesToFile(const URL& search) { | ||
| std::string filePath = search.ToFilePath(); | ||
| Maybe<uv_file> check = CheckFile(filePath); | ||
| return !check.IsNothing(); | ||
| } | ||
|
|
||
| template <ResolveExtensionsOptions options> | ||
| Maybe<URL> ResolveExtensions(const URL& search) { | ||
| if (options == TRY_EXACT_NAME) { | ||
|
|
@@ -590,9 +596,9 @@ Maybe<URL> ResolveMain(Environment* env, const URL& search) { | |
| return Nothing<URL>(); | ||
| } | ||
| if (!ShouldBeTreatedAsRelativeOrAbsolutePath(pjson.main)) { | ||
| return Resolve(env, "./" + pjson.main, search, IgnoreMain); | ||
| return ResolveRecurse(env, "./" + pjson.main, search, IgnoreMain); | ||
|
||
| } | ||
| return Resolve(env, pjson.main, search, IgnoreMain); | ||
| return ResolveRecurse(env, pjson.main, search, IgnoreMain); | ||
| } | ||
|
|
||
| Maybe<URL> ResolveModule(Environment* env, | ||
|
|
@@ -603,7 +609,7 @@ Maybe<URL> ResolveModule(Environment* env, | |
| do { | ||
| dir = parent; | ||
| Maybe<URL> check = | ||
| Resolve(env, "./node_modules/" + specifier, dir, CheckMain); | ||
| ResolveRecurse(env, "./node_modules/" + specifier, dir, CheckMain); | ||
|
||
| if (!check.IsNothing()) { | ||
| const size_t limit = specifier.find('/'); | ||
| const size_t spec_len = | ||
|
|
@@ -636,10 +642,27 @@ Maybe<URL> ResolveDirectory(Environment* env, | |
|
|
||
| } // anonymous namespace | ||
|
|
||
| Maybe<URL> Resolve(Environment* env, | ||
| Maybe<URL> ResolveRecurse(Environment* env, | ||
|
||
| const std::string& specifier, | ||
| const URL& base, | ||
| PackageMainCheck check_pjson_main) { | ||
| if (ShouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | ||
| URL resolved(specifier, base); | ||
| Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(resolved); | ||
| if (!file.IsNothing()) | ||
| return file; | ||
| if (specifier.back() != '/') { | ||
| resolved = URL(specifier + "/", base); | ||
| } | ||
| return ResolveDirectory(env, resolved, check_pjson_main); | ||
| } else { | ||
| return ResolveModule(env, specifier, base); | ||
| } | ||
| } | ||
|
|
||
| Maybe<URL> Resolve(Environment* env, | ||
| const std::string& specifier, | ||
| const URL& base) { | ||
| URL pure_url(specifier); | ||
| if (!(pure_url.flags() & URL_FLAGS_FAILED)) { | ||
| // just check existence, without altering | ||
|
|
@@ -654,13 +677,9 @@ Maybe<URL> Resolve(Environment* env, | |
| } | ||
| if (ShouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | ||
| URL resolved(specifier, base); | ||
| Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(resolved); | ||
| if (!file.IsNothing()) | ||
| return file; | ||
| if (specifier.back() != '/') { | ||
| resolved = URL(specifier + "/", base); | ||
| } | ||
| return ResolveDirectory(env, resolved, check_pjson_main); | ||
| if (ResolvesToFile(resolved)) | ||
| return Just(resolved); | ||
|
||
| return Nothing<URL>(); | ||
| } else { | ||
| return ResolveModule(env, specifier, base); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Flags: --experimental-modules | ||
| import '../common'; | ||
| import('./test-esm-cyclic-dynamic-import'); | ||
| /* eslint-disable node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
| import('./test-esm-cyclic-dynamic-import.mjs'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Flags: --experimental-modules | ||
| import '../common'; | ||
| /* eslint-disable node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
|
|
||
| // Assert we can import files with `%` in their pathname. | ||
|
|
||
| import '../fixtures/es-modules/test-esm-double-encoding-native%2520.js'; | ||
| import '../fixtures/es-modules/test-esm-double-encoding-native%2520.mjs'; |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/not-found-assert-loader.mjs | ||
| /* eslint-disable node-core/required-modules */ | ||
| import './not-found'; | ||
| import './not-found.mjs'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #! }]) // isn't js | ||
| // Flags: --experimental-modules | ||
| import '../common'; | ||
| /* eslint-disable node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
|
|
||
| const isJs = true; | ||
| export default isJs; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| // Flags: --experimental-modules | ||
| import '../common'; | ||
| import '../fixtures/es-modules/esm-snapshot-mutator'; | ||
| import one from '../fixtures/es-modules/esm-snapshot'; | ||
| /* eslint-disable node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
| import '../fixtures/es-modules/esm-snapshot-mutator.js'; | ||
| import one from '../fixtures/es-modules/esm-snapshot.js'; | ||
| import assert from 'assert'; | ||
|
|
||
| assert.strictEqual(one, 1); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| import { foo, notfound } from './module-named-exports'; | ||
| import { foo, notfound } from './module-named-exports.mjs'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { message } from './message'; | ||
| import { message } from './message.mjs'; | ||
|
|
||
| var t = 1; | ||
| var k = 1; | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| 'use strict'; | ||
|
|
||
| // Trivial test to assert we can load files with `%` in their pathname. | ||
| // Imported by `test-esm-double-encoding.mjs`. | ||
|
|
||
| export default 42; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // Flags: --experimental-modules | ||
| /* eslint-disable no-unused-vars */ | ||
| import '../common'; | ||
| /* eslint-disable no-unused-vars, node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
| import { | ||
| foo, | ||
| notfound | ||
| } from '../fixtures/es-module-loaders/module-named-exports'; | ||
| } from '../fixtures/es-module-loaders/module-named-exports.mjs'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Flags: --experimental-modules | ||
| import '../common'; | ||
| import '../fixtures/es-module-loaders/syntax-error-import'; | ||
| /* eslint-disable node-core/required-modules */ | ||
| import '../common/index.mjs'; | ||
| import '../fixtures/es-module-loaders/syntax-error-import.mjs'; |
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.
I would prefer if we could move this filtering logic into the resolver itself still, to ensure a single code path for resolution.