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

Commit 1392f6c

Browse files
committed
remove main and package parsing
1 parent a5f3284 commit 1392f6c

File tree

2 files changed

+7
-86
lines changed

2 files changed

+7
-86
lines changed

doc/api/esm.md

+4-38
Original file line numberDiff line numberDiff line change
@@ -207,50 +207,16 @@ _ESM_RESOLVE_:
207207
208208
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
209209
> 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,
210+
> 1. If _packageSpecifier_ is a Node.js builtin module then,
230211
> 1. Return _"node:${packageName}"_.
231212
> 1. Set _parentURL_ to the parent folder URL of _parentURL_.
232213
> 1. While _parentURL_ contains a non-empty _pathname_,
233214
> 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_.
215+
> _"${parentURL}/node_modules/${packageSpecifier}"_.
216+
> 1. If the file at _packageURL_ exists then,
217+
> 1. Return _packageURL_.
247218
> 1. Throw a _Module Not Found_ error.
248219
249-
**PACKAGE_MAIN_RESOLVE**(_packageURL_)
250-
> 1. Note: Main resolution to be implemented here.
251-
> 1. Return _undefined_.
252-
253-
254220
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
255221
[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename
256222
[ESM Minimal Kernel]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md

src/module_wrap.cc

+3-48
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)