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

Commit e69ac4e

Browse files
committed
exports spec
1 parent 7a5cb42 commit e69ac4e

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

doc/api/esm.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ PACKAGE_RESOLVE(_packageSpecifier_, _parentURL_)
219219
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_,
220220
> _pjson_).
221221
> 1. Otherwise,
222-
> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
222+
> 1. If **HAS_ESM_PROPERTIES**(_pjson_) is *true*, then
223+
> 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _packagePath_,
224+
> _pjson_).
225+
> 1. Return the URL resolution of _packagePath_ in _packageURL_.
223226
> 1. Throw a _Module Not Found_ error.
224227
225228
PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
@@ -230,9 +233,58 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
230233
> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
231234
> _Module Not Found_ error for no resolution.
232235
> 1. Return _mainURL_.
233-
> 1. TODO: ESM main handling.
236+
> 1. If _pjson.exports_ is a String, then
237+
> 1. Return the URL of _pjson.exports_ within the parent path _packageURL_.
238+
> 1. Assert: _pjson.exports_ is an Object.
239+
> 1. If _pjson.exports["."]_ is a String, then
240+
> 1. Let _target_ be _pjson.exports["."]_.
241+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false**, then
242+
> 1. Emit an _"Invalid Exports Target"_ warning.
243+
> 1. Otherwise,
244+
> 1. Return the URL of _pjson.exports.default_ within the parent path
245+
> _packageURL_.
246+
> 1. Return **null**.
247+
248+
PACKAGE_EXPORTS_RESOLVE(_packageURL_, _packagePath_, _pjson_)
249+
> 1. Assert: _pjson_ is not **null**.
250+
> 1. If _pjson.exports_ is a String, then
251+
> 1. Throw a _Module Not Found_ error.
252+
> 1. Assert: _pjson.exports_ is an Object.
253+
> 1. Set _packagePath_ to _"./"_ concatenated with _packagePath_.
254+
> 1. If _packagePath_ is a key of _pjson.exports_, then
255+
> 1. Let _target_ be the value of _pjson.exports[packagePath]_.
256+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false**, then
257+
> 1. Emit an _"Invalid Exports Target"_ warning.
258+
> 1. Throw a _Module Not Found_ error.
259+
> 1. Return the URL resolution of the concatenation of _packageURL_ and
260+
> _target_.
261+
> 1. Let _directoryKeys_ be the list of keys of _pjson.exports_ ending in
262+
> _"/"_, sorted by length descending.
263+
> 1. For each key _directory_ in _directoryKeys_, do
264+
> 1. If _packagePath_ starts with _directory_, then
265+
> 1. Let _target_ be the value of _pjson.exports[directory]_.
266+
> 1. If **IS_VALID_EXPORTS_TARGET**(_target_) is **false** or _target_
267+
> does not end in _"/"_, then
268+
> 1. Emit an _"Invalid Exports Target"_ warning.
269+
> 1. Continue the loop.
270+
> 1. Let _subpath_ be the substring of _target_ starting at the index of
271+
> the length of _directory_.
272+
> 1. Return the URL resolution of the concatenation of _packageURL_,
273+
> _target_ and _subpath_.
234274
> 1. Throw a _Module Not Found_ error.
235275
276+
IS_VALID_EXPORTS_TARGET(_target_)
277+
> 1. If _target_ is not a valid String, then
278+
> 1. Return **false**.
279+
> 1. If _target_ does not start with _"./"_, then
280+
> 1. Return **false**.
281+
> 1. If _target_ contains any _".."_ or _"."_ path segments, then
282+
> 1. Return **false**.
283+
> 1. If _target_ contains any percent-encoded characters for _"/"_ or _"\"_,
284+
> then
285+
> 1. Return **false**.
286+
> 1. Return **true**.
287+
236288
**ESM_FORMAT(_url_, _isMain_)**
237289
> 1. Assert: _url_ corresponds to an existing file.
238290
> 1. Let _pjson_ be the result of **READ_PACKAGE_BOUNDARY**(_url_).
@@ -266,7 +318,9 @@ READ_PACKAGE_JSON(_packageURL_)
266318
> 1. Return the parsed JSON source of the file at _url_.
267319
268320
HAS_ESM_PROPERTIES(_pjson_)
269-
> Note: To be specified.
321+
> 1. If _pjson_ is not **null** and _pjson.exports_ is a String or Object, then
322+
> 1. Return *true*.
323+
> 1. Return *false*.
270324
271325
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
272326
[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename

0 commit comments

Comments
 (0)