Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit 528e26f

Browse files
committed
remove main and package parsing
1 parent a5f3284 commit 528e26f

File tree

2 files changed

+7
-116
lines changed

2 files changed

+7
-116
lines changed

doc/api/esm.md

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -154,36 +154,6 @@ The resolver has the following properties:
154154
* No default extensions
155155
* No folder mains
156156
* Bare specifier package resolution lookup through node_modules
157-
* Avoids the package fallback which breaks package isolation. This is the
158-
problem that an import to `x/y.js` will keep checking subsequent node_modules
159-
even if a package "x" is found without a "y.js". This is done through package
160-
name format detection.
161-
162-
The ESM resolver differs from the CommonJS resolver in detecting a package name
163-
based on it being either `package` or `@scoped/package`. This allows the
164-
resolver to know when a package has been matched, and then avoid a fallback for
165-
missing modules.
166-
167-
The main lookup is then provided by _CHECK_PACKAGE_MAIN_ which is still an
168-
experimental approach under discussion.
169-
170-
Currently, the main lookup will only check for _index.mjs_.
171-
172-
### Examples
173-
174-
Relative and absolute resolution without directory or extension resolution
175-
(URL resolution):
176-
177-
* ESM_RESOLVE("./a.asdf", "file:///parent/path") -> "file:///parent/a.asdf"
178-
* ESM_RESOLVE("/a", "file:///parent/path") -> "file:///a"
179-
* ESM_RESOLVE("file:///a", "file:///parent/path") -> "file:///a"
180-
181-
Package resolution:
182-
183-
1. ESM_RESOLVE("pkg/x", "file:///path/to/project") ->
184-
"file:///path/to/nodemodules/pkg/x" (if it exists)
185-
1. ESM_RESOLVE("pkg/x", "file:///path/to/project") ->
186-
Not Found otherwise, even if "file:///path/nodemodules/pkg/x" exists.
187157
188158
### Resolver Algorithm
189159
@@ -207,50 +177,16 @@ _ESM_RESOLVE_:
207177
208178
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
209179
> 1. Assert: _packageSpecifier_ is a bare specifier.
210-
> 1. Let _packageName_ be _undefined_.
211-
> 1. Let _packagePath_ be _undefined_.
212-
> 1. If _packageSpecifier_ does not start with _"@"_ then,
213-
> 1. If _packageSpecifier_ is an empty string then,
214-
> 1. Throw a _Invalid Package Name_ error.
215-
> 1. Set _packageName_ to the substring of _packageSpecifier_ until the
216-
> first _"/"_ separator or the end of the string.
217-
> 1. If _packageSpecifier_ starts with _"@"_ then,
218-
> 1. If _packageSpecifier_ does not contain a _"/"_ separator then,
219-
> 1. Throw a _Invalid Package Name_ error.
220-
> 1. Set _packageName_ to the substring of _packageSpecifier_
221-
> until the second _"/"_ separator or the end of the string.
222-
> 1. Let _packagePath_ be the substring of _packageSpecifier_ from the
223-
> position at the length of _packageName_ plus one, if any.
224-
> 1. Assert: _packageName_ is a valid package name or scoped package name.
225-
> 1. Assert: _packagePath_ is either empty, or a path without a leading
226-
> separator.
227-
> 1. Note: Further package name validations can be added here.
228-
> 1. If _packagePath_ is empty and _packageName_ is a Node.js builtin
229-
> module then,
180+
> 1. If _packageSpecifier_ is a Node.js builtin module then,
230181
> 1. Return _"node:${packageName}"_.
231182
> 1. Set _parentURL_ to the parent folder URL of _parentURL_.
232183
> 1. While _parentURL_ contains a non-empty _pathname_,
233184
> 1. Let _packageURL_ be the URL resolution of
234-
> _"${parentURL}/node_modules/${packageName}"_.
235-
> 1. If the folder at _packageURL_ does not exist then,
236-
> 1. Note: This check can be optimized out where possible in
237-
> implementation.
238-
> 1. Set _parentURL_ to the parent URL path of _parentURL_.
239-
> 1. Continue the next loop iteration.
240-
> 1. If _packagePath_ is empty then,
241-
> 1. Let _url_ be the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_).
242-
> 1. If _url_ is _undefined_ then,
243-
> 1. Throw a _Module Not Found_ error.
244-
> 1. Return _url_.
245-
> 1. Otherwise,
246-
> 1. Return the URL resolution of _packagePath_ in _packageURL_.
185+
> _"${parentURL}/node_modules/${packageSpecifier}"_.
186+
> 1. If the file at _packageURL_ exists then,
187+
> 1. Return _packageURL_.
247188
> 1. Throw a _Module Not Found_ error.
248189
249-
**PACKAGE_MAIN_RESOLVE**(_packageURL_)
250-
> 1. Note: Main resolution to be implemented here.
251-
> 1. Return _undefined_.
252-
253-
254190
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
255191
[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename
256192
[ESM Minimal Kernel]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md

src/module_wrap.cc

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -485,60 +485,15 @@ DescriptorType CheckDescriptor(const std::string& path) {
485485
return NONE;
486486
}
487487

488-
inline Maybe<URL> PackageMainResolve(const URL& search) {
489-
return Nothing<URL>();
490-
}
491-
492488
Maybe<URL> PackageResolve(Environment* env,
493489
const std::string& specifier,
494490
const URL& base) {
495-
size_t sep_index = specifier.find('/');
496-
if (specifier[0] == '@' && sep_index == std::string::npos ||
497-
specifier.length() == 0) {
498-
std::string msg = "Invalid package name '" + specifier +
499-
"' imported from " + base.ToFilePath();
500-
node::THROW_ERR_INVALID_PACKAGE_NAME(env, msg.c_str());
501-
return Nothing<URL>();
502-
}
503-
if (specifier[0] == '@') {
504-
sep_index = specifier.find('/', sep_index + 1);
505-
}
506-
std::string pkg_name = specifier.substr(0,
507-
sep_index == std::string::npos ? std::string::npos : sep_index);
508-
std::string pkg_path;
509-
if (sep_index == std::string::npos ||
510-
sep_index == specifier.length() - 1) {
511-
pkg_path = "";
512-
} else {
513-
pkg_path = specifier.substr(sep_index);
514-
}
515491
URL parent(".", base);
516492
std::string last_path;
517493
do {
518-
URL pkg_url("./node_modules/" + pkg_name, &parent);
519-
URL url;
520-
if (pkg_path.length()) {
521-
url = URL("./node_modules/" + pkg_name + pkg_path, &parent);
522-
} else {
523-
url = pkg_url;
524-
}
525-
DescriptorType check;
526-
if (pkg_path.length()) {
527-
DescriptorType check = CheckDescriptor(url.ToFilePath());
528-
if (check == FILE) return Just(url);
529-
if (check == NONE) check = CheckDescriptor(pkg_url.ToFilePath());
530-
} else {
531-
Maybe<URL> main_url = PackageMainResolve(url);
532-
if (!main_url.IsNothing()) return main_url;
533-
check = CheckDescriptor(pkg_url.ToFilePath());
534-
}
535-
// throw not found if we did match a package directory
536-
if (check == DIRECTORY) {
537-
std::string msg = "Cannot find module '" + url.ToFilePath() +
538-
"' imported from " + base.ToFilePath();
539-
node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
540-
return Nothing<URL>();
541-
}
494+
URL pkg_url("./node_modules/" + specifier, &parent);
495+
DescriptorType check = CheckDescriptor(pkg_url.ToFilePath());
496+
if (check == FILE) return Just(pkg_url);
542497
last_path = parent.path();
543498
parent = URL("..", &parent);
544499
// cross-platform root check

0 commit comments

Comments
 (0)