-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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) { | ||
|
@@ -589,10 +595,20 @@ Maybe<URL> ResolveMain(Environment* env, const URL& search) { | |
pjson.has_main == HasMain::No) { | ||
return Nothing<URL>(); | ||
} | ||
if (!ShouldBeTreatedAsRelativeOrAbsolutePath(pjson.main)) { | ||
return Resolve(env, "./" + pjson.main, search, IgnoreMain); | ||
|
||
URL resolved; | ||
if (ShouldBeTreatedAsRelativeOrAbsolutePath(pjson.main)) { | ||
resolved = URL(pjson.main, search); | ||
} else { | ||
resolved = URL("./" + pjson.main, search); | ||
} | ||
Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(resolved); | ||
if (!file.IsNothing()) | ||
return file; | ||
if (pjson.main.back() != '/') { | ||
resolved = URL(pjson.main + "/", search); | ||
} | ||
return Resolve(env, pjson.main, search, IgnoreMain); | ||
return ResolveIndex(resolved); | ||
} | ||
|
||
Maybe<URL> ResolveModule(Environment* env, | ||
|
@@ -603,7 +619,7 @@ Maybe<URL> ResolveModule(Environment* env, | |
do { | ||
dir = parent; | ||
Maybe<URL> check = | ||
Resolve(env, "./node_modules/" + specifier, dir, CheckMain); | ||
Resolve(env, "./node_modules/" + specifier, dir); | ||
if (!check.IsNothing()) { | ||
const size_t limit = specifier.find('/'); | ||
const size_t spec_len = | ||
|
@@ -623,23 +639,11 @@ Maybe<URL> ResolveModule(Environment* env, | |
return Nothing<URL>(); | ||
} | ||
|
||
Maybe<URL> ResolveDirectory(Environment* env, | ||
const URL& search, | ||
PackageMainCheck check_pjson_main) { | ||
if (check_pjson_main) { | ||
Maybe<URL> main = ResolveMain(env, search); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this is never called now. This is likely my fault - rushed the refactoring a bit last time. Will take another look. |
||
if (!main.IsNothing()) | ||
return main; | ||
} | ||
return ResolveIndex(search); | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
Maybe<URL> Resolve(Environment* env, | ||
const std::string& specifier, | ||
const URL& base, | ||
PackageMainCheck check_pjson_main) { | ||
const URL& base) { | ||
URL pure_url(specifier); | ||
if (!(pure_url.flags() & URL_FLAGS_FAILED)) { | ||
// just check existence, without altering | ||
|
@@ -654,13 +658,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's all @zenparsing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, this C++ code is a pleasure to work with 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the implementation work of @bmeck, with many various refactoring contribs. |
||
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'; |
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.