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

Commit beb1d1a

Browse files
committed
exports spec
1 parent 770f566 commit beb1d1a

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

doc/api/esm.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ PACKAGE_RESOLVE(_packageSpecifier_, _parentURL_)
223223
> 1. Throw a _Module Not Found_ error.
224224
> 1. Return _url_.
225225
> 1. Otherwise,
226+
> 1. If **HAS_ESM_PROPERTIES**(_pjson_) is *true*, then
227+
> 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _packagePath_, _pjson_).
226228
> 1. Return the URL resolution of _packagePath_ in _packageURL_.
227229
> 1. Throw a _Module Not Found_ error.
228230
@@ -234,9 +236,58 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
234236
> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, returning
235237
> *undefined* for no resolution.
236238
> 1. Return _mainURL_.
237-
> 1. TODO: ESM main handling.
239+
> 1. If _pjson.exports_ is a String, then
240+
> 1. Return the URL of _pjson.exports_ within the parent path _packageURL_.
241+
> 1. Assert: _pjson.exports_ is an Object.
242+
> 1. If _pjson.exports["."]_ is a String, then
243+
> 1. Let _target_ be _pjson.exports["."]_.
244+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false**, then
245+
> 1. Emit an _"Invalid Exports Target"_ warning.
246+
> 1. Otherwise,
247+
> 1. Return the URL of _pjson.exports.default_ within the parent path
248+
> _packageURL_.
238249
> 1. Return **null**.
239250
251+
PACKAGE_EXPORTS_RESOLVE(_packageURL_, _packagePath_, _pjson_)
252+
> 1. Assert: _pjson_ is not **null**.
253+
> 1. If _pjson.exports_ is a String, then
254+
> 1. Throw a _Module Not Found_ error.
255+
> 1. Assert: _pjson.exports_ is an Object.
256+
> 1. Set _packagePath_ to _"./"_ concatenated with _packagePath_.
257+
> 1. If _packagePath_ is a key of _pjson.exports_, then
258+
> 1. Let _target_ be the value of _pjson.exports[packagePath]_.
259+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false**, then
260+
> 1. Emit an _"Invalid Exports Target"_ warning.
261+
> 1. Throw a _Module Not Found_ error.
262+
> 1. Return the URL resolution of the concatenation of _packageURL_ and
263+
> _target_.
264+
> 1. Let _directoryKeys_ be the list of keys of _pjson.exports_ ending in
265+
> _"/"_, sorted by length descending.
266+
> 1. For each key _directory_ in _directoryKeys_, do
267+
> 1. If _packagePath_ starts with _directory_, then
268+
> 1. Let _target_ be the value of _pjson.exports[directory]_.
269+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false** or _target_
270+
> does not end in _"/"_, then
271+
> 1. Emit an _"Invalid Exports Target"_ warning.
272+
> 1. Continue the loop.
273+
> 1. Let _subpath_ be the substring of _target_ starting at the index of
274+
> the length of _directory_.
275+
> 1. Return the URL resolution of the concatenation of _packageURL_,
276+
> _target_ and _subpath_.
277+
> 1. Throw a _Module Not Found_ error.
278+
279+
IS_VALID_EXPORTS_TARGET(_target_)
280+
> 1. If _target_ is not a valid String, then
281+
> 1. Return **false**.
282+
> 1. If _target_ does not start with _"./"_, then
283+
> 1. Return **false**.
284+
> 1. If _target_ contains any _".."_ or _"."_ path segments, then
285+
> 1. Return **false**.
286+
> 1. If _target_ contains any percent-encoded characters for _"/"_ or _"\"_,
287+
> then
288+
> 1. Return **false**.
289+
> 1. Return **true**.
290+
240291
**ESM_FORMAT(_url_)**
241292
> 1. Assert: _url_ corresponds to an existing file.
242293
> 1. Let _pjson_ be the result of **READ_PACKAGE_BOUNDARY**(_url_).
@@ -268,7 +319,9 @@ READ_JSON_FILE(_url_)
268319
> 1. Return _pjson_.
269320
270321
HAS_ESM_PROPERTIES(_pjson_)
271-
> Note: To be specified.
322+
> 1. If _pjson_ is not **null** and _pjson.exports_ is a String or Object, then
323+
> 1. Return *true*.
324+
> 1. Return *false*.
272325
273326
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
274327
[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename

0 commit comments

Comments
 (0)