You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+65-47Lines changed: 65 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -17,19 +17,15 @@ This ensures 100% backward-compatibility, while still allowing some freedom of d
17
17
18
18
A distinct entry point ("default.js") allows us to distinguish when a user is attempting to import from a legacy package or a folder containing CommonJS modules.
19
19
20
-
**3. When `import`ing a file path, file extensions are not automatically appended.**
21
-
22
-
The default resolution algorithm used by web browsers will not automatically append file extensions.
23
-
24
-
**4. When `import`ing a directory, if a "default.js" file cannot be found, the algorithm will attempt to find an entry point using legacy `require` rules, by consulting "package.json" and looking for "index.*" files.**
20
+
**3. When `import`ing a package, if a "module" and "default.js" file cannot be found, the algorithm will attempt to find an entry point using legacy `require` rules, by consulting "package.json" and looking for "index.*" files.**
25
21
26
22
This provides users with the ability to `import` from legacy packages.
27
23
28
-
**5. `import(modulePath)` asynchronously imports an ES module from CommonJS.**
24
+
**4. `import(modulePath)` asynchronously imports an ES module from CommonJS.**
29
25
30
26
This allows old-style modules to `import` from new-style modules.
31
27
32
-
**6. Node will support a `--module` flag.**
28
+
**5. Node will support a `--module` flag.**
33
29
34
30
This provides the context that the module being loaded is a module, where in future this could be set by default.
35
31
@@ -172,55 +168,77 @@ from the command line, there is absolutely no way for Node to tell whether "my-m
172
168
$ node --module my-module.js
173
169
```
174
170
171
+
## Resolver variations
172
+
173
+
This specification specifies only how to handle the problem of determining the source text type.
174
+
175
+
There are many other aspects of the resolver which can be specified separately including:
176
+
177
+
* what file formats are supported by import
178
+
* .mjs support
179
+
* whether to automatically add file extensions or not for the ES resolver
180
+
* whether to provide directory index imports for the ES resolver
181
+
175
182
## Lookup Algorithm Psuedo-Code
176
183
177
-
### LOAD_MODULE(X, Y, T)
184
+
This spec covers only the ES resolver, with the CommonJS resolver remaining exactly as currently implemented.
185
+
186
+
The function LEGACY_LOAD(X) refers to delegating resolution to the existing CommonJS resolver implementation.
187
+
188
+
The LOAD_AS_DIRECTORY function from the CommonJS resolver is also referenced here.
178
189
179
-
Loads _X_ from a module at path _Y_. _T_ is either "require" or "import".
190
+
### ES_LOAD_MODULE(X, Y)
191
+
192
+
Loads _X_ from a module at path _Y_.
180
193
181
194
1. If X is a core module, then
182
195
1. return the core module
183
196
1. STOP
184
197
1. If X begins with './' or '/' or '../'
185
-
1.LOAD_AS_FILE(Y + X, T)
186
-
1.LOAD_AS_DIRECTORY(Y + X, T)
187
-
1.LOAD_NODE_MODULES(X, dirname(Y), T)
198
+
1.ES_LOAD_FILE(Y + X)
199
+
1.ES_LOAD_DIRECTORY(Y + X)
200
+
1.ES_LOAD_NODE_MODULES(X, dirname(Y))
188
201
1. THROW "not found"
189
202
190
-
### LOAD_AS_FILE(X, T)
191
-
192
-
1. If T is "import",
193
-
1. If X is a file, then
194
-
1. If extname(X) is ".js", load X as ES module text. STOP
195
-
1. If extname(X) is ".json", parse X to a JavaScript Object. STOP
196
-
1. If extname(X) is ".node", load X as binary addon. STOP
197
-
1. THROW "not found"
198
-
1. Else,
199
-
1. Assert: T is "require"
200
-
1. If X is a file, load X as CJS module text. STOP
201
-
1. If X.js is a file, load X.js as CJS module text. STOP
202
-
1. If X.json is a file, parse X.json to a JavaScript Object. STOP
203
-
1. If X.node is a file, load X.node as binary addon. STOP
204
-
205
-
### LOAD_AS_DIRECTORY(X, T)
206
-
207
-
1. If T is "import",
208
-
1. If X/default.js is a file, load X/default.js as ES module text. STOP
209
-
1. If X/package.json is a file,
210
-
1. Parse X/package.json, and look for "default" field.
211
-
1. load X/(json module field) as ES module text. STOP
212
-
1. NOTE: If neither of the above are a file, then fallback to legacy behavior
213
-
1. If X/package.json is a file,
214
-
1. Parse X/package.json, and look for "main" field.
215
-
1. let M = X + (json main field)
216
-
1. LOAD_AS_FILE(M, "require")
217
-
1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
218
-
1. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
219
-
1. If X/index.node is a file, load X/index.node as binary addon. STOP
220
-
221
-
### LOAD_NODE_MODULES(X, START, T)
203
+
### ES_LOAD_FILE(X)
204
+
205
+
This method loads the file at X, treating ".js" files as ES module source texts and
206
+
calling STOP on resolution.
207
+
208
+
Exact semantics to be specified elsewhere for default file extension adding, and determining
209
+
non-JS source text interpretations based on file extension.
210
+
211
+
If the file is not found, no STOP call is made resulting in fall-through here.
212
+
213
+
### ES_LOAD_DIRECTORY(X)
214
+
215
+
1. If X/default.js is a file, load X/default.js as an ES module text. STOP
216
+
1. Let C be the result of LOAD_PACKAGE_JSON(X).
217
+
1. If C has a "default" field string then,
218
+
1. let M be the "default" field of C.
219
+
1. ES_LOAD_FILE(X/M)
220
+
1. Defer to legacy CommonJS resolver function, LOAD_AS_DIRECTORY(X).
221
+
222
+
### ES_LOAD_NODE_MODULES(X, START, T)
222
223
223
224
1. let DIRS=NODE_MODULES_PATHS(START)
224
-
2. for each DIR in DIRS:
225
-
1. LOAD_AS_FILE(DIR/X, T)
226
-
1. LOAD_AS_DIRECTORY(DIR/X, T)
225
+
1. for each DIR in DIRS:
226
+
1. If the directory at DIR/X exists then,
227
+
1. ES_LOAD_PACKAGE(DIR/X)
228
+
229
+
### ES_LOAD_PACKAGE(X)
230
+
231
+
1. Let C be the result of LOAD_PACKAGE_JSON(X).
232
+
1. If C has a "default" field string then,
233
+
1. let M be the "default" field of C.
234
+
1. ES_LOAD_FILE(X/M)
235
+
1. If X/default.js exists, load X/default.js as ES module text. STOP
236
+
1. LEGACY_LOAD(X)
237
+
238
+
### LOAD_PACKAGE_JSON(X)
239
+
240
+
1. If the file at X/package.json exists then,
241
+
1. If the file at X/package.json is invalid JSON then,
242
+
1. THROW "resolution error"
243
+
1. Return the parsed JSON object for X/package.json
0 commit comments