summaryrefslogtreecommitdiff
path: root/doc/api/esm.md
diff options
context:
space:
mode:
authorJan Krems <jan.krems@gmail.com>2019-08-19 20:59:25 -0700
committerJan Krems <jan.krems@gmail.com>2019-10-24 15:14:38 -0700
commit71bcd05232b4fc21db20e5acf019f97780050568 (patch)
treeff58c1a5c27e3e28755395e1b07b0394583d96e1 /doc/api/esm.md
parentd53dd8b0a00d3e00e97f46ae4ae67afa31c10526 (diff)
downloadandroid-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.tar.gz
android-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.tar.bz2
android-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.zip
module: resolve self-references
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: https://github.com/nodejs/node/pull/29327 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'doc/api/esm.md')
-rw-r--r--doc/api/esm.md28
1 files changed, 24 insertions, 4 deletions
diff --git a/doc/api/esm.md b/doc/api/esm.md
index e1002f11fb..298420465b 100644
--- a/doc/api/esm.md
+++ b/doc/api/esm.md
@@ -839,9 +839,6 @@ _isMain_ is **true** when resolving the Node.js application entry point.
> 1. Let _packageSubpath_ be *undefined*.
> 1. If _packageSpecifier_ is an empty string, then
> 1. Throw an _Invalid Specifier_ error.
-> 1. If _packageSpecifier_ does not start with _"@"_, then
-> 1. Set _packageName_ to the substring of _packageSpecifier_ until the
-> first _"/"_ separator or the end of the string.
> 1. Otherwise,
> 1. If _packageSpecifier_ does not contain a _"/"_ separator, then
> 1. Throw an _Invalid Specifier_ error.
@@ -855,7 +852,7 @@ _isMain_ is **true** when resolving the Node.js application entry point.
> 1. Set _packageSubpath_ to _"."_ concatenated with the substring of
> _packageSpecifier_ from the position at the length of _packageName_.
> 1. If _packageSubpath_ contains any _"."_ or _".."_ segments or percent
-> encoded strings for _"/"_ or _"\\"_ then,
+> encoded strings for _"/"_ or _"\\"_, then
> 1. Throw an _Invalid Specifier_ error.
> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
> module, then
@@ -878,8 +875,31 @@ _isMain_ is **true** when resolving the Node.js application entry point.
> 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_,
> _packageSubpath_, _pjson.exports_).
> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
+> 1. Set _selfUrl_ to the result of
+> **SELF_REFERENCE_RESOLE**(_packageSpecifier_, _parentURL_).
+> 1. If _selfUrl_ isn't empty, return _selfUrl_.
> 1. Throw a _Module Not Found_ error.
+**SELF_REFERENCE_RESOLVE**(_specifier_, _parentURL_)
+
+> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
+> 1. If _packageURL_ is **null**, then
+> 1. Return an empty result.
+> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
+> 1. Set _name_ to _pjson.name_.
+> 1. If _name_ is empty, then return an empty result.
+> 1. If _name_ is equal to _specifier_, then
+> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_).
+> 1. If _specifier_ starts with _name_ followed by "/", then
+> 1. Set _subpath_ to everything after the "/".
+> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
+> 1. Let _exports_ be _pjson.exports_.
+> 1. If _exports_ is not **null** or **undefined**, then
+> 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _subpath_,
+> _pjson.exports_).
+> 1. Return the URL resolution of _subpath_ in _packageURL_.
+> 1. Otherwise return an empty result.
+
**PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_)
> 1. If _pjson_ is **null**, then