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
+47-47Lines changed: 47 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
@@ -169,55 +165,59 @@ from the command line, there is absolutely no way for Node to tell whether "my-m
169
165
$ node --module my-module.js
170
166
```
171
167
168
+
## Resolver variations
169
+
170
+
This specification specifies only how to handle the problem of determining the source text type.
171
+
172
+
There are many other aspects of the resolver which can be specified separately including:
173
+
174
+
* what file formats are supported by import
175
+
* .mjs support
176
+
* whether to automatically add file extensions or not for the ES resolver
177
+
* whether to provide directory index imports for the ES resolver
178
+
172
179
## Lookup Algorithm Psuedo-Code
173
180
174
-
### LOAD_MODULE(X, Y, T)
181
+
This spec covers only the ES resolver, with the CommonJS resolver remaining exactly as currently implemented.
175
182
176
-
Loads _X_ from a module at path _Y_. _T_ is either "require" or "import".
183
+
The function LOAD_LEGACY(X) refers to delegating resolution to the existing CommonJS resolver implementation.
184
+
185
+
### LOAD_ES_MODULE(X, Y)
186
+
187
+
Loads _X_ from a module at path _Y_.
177
188
178
189
1. If X is a core module, then
179
190
1. return the core module
180
191
1. STOP
181
192
1. If X begins with './' or '/' or '../'
182
-
1. LOAD_AS_FILE(Y + X, T)
183
-
1. LOAD_AS_DIRECTORY(Y + X, T)
184
-
1. LOAD_NODE_MODULES(X, dirname(Y), T)
193
+
1. LOAD_ES_FILE(Y + X)
194
+
1. LOAD_ES_NODE_MODULES(X, dirname(Y))
185
195
1. THROW "not found"
186
196
187
-
### LOAD_AS_FILE(X, T)
188
-
189
-
1. If T is "import",
190
-
1. If X is a file, then
191
-
1. If extname(X) is ".js", load X as ES module text. STOP
192
-
1. If extname(X) is ".json", parse X to a JavaScript Object. STOP
193
-
1. If extname(X) is ".node", load X as binary addon. STOP
194
-
1. THROW "not found"
195
-
1. Else,
196
-
1. Assert: T is "require"
197
-
1. If X is a file, load X as CJS module text. STOP
198
-
1. If X.js is a file, load X.js as CJS module text. STOP
199
-
1. If X.json is a file, parse X.json to a JavaScript Object. STOP
200
-
1. If X.node is a file, load X.node as binary addon. STOP
201
-
202
-
### LOAD_AS_DIRECTORY(X, T)
203
-
204
-
1. If T is "import",
205
-
1. If X/default.js is a file, load X/default.js as ES module text. STOP
206
-
1. If X/package.json is a file,
207
-
1. Parse X/package.json, and look for "module" field.
208
-
1. load X/(json module field) as ES module text. STOP
209
-
1. NOTE: If neither of the above are a file, then fallback to legacy behavior
210
-
1. If X/package.json is a file,
211
-
1. Parse X/package.json, and look for "main" field.
212
-
1. let M = X + (json main field)
213
-
1. LOAD_AS_FILE(M, "require")
214
-
1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
215
-
1. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
216
-
1. If X/index.node is a file, load X/index.node as binary addon. STOP
217
-
218
-
### LOAD_NODE_MODULES(X, START, T)
197
+
### LOAD_ES_FILE(X)
198
+
199
+
This method loads the file at X, treating ".js" files as ES module source texts and
200
+
calling STOP on resolution.
201
+
202
+
Exact semantics to be specified elsewhere (extension adding, directory lookups,
203
+
extension interpretations).
204
+
205
+
### LOAD_ES_NODE_MODULES(X, START, T)
219
206
220
207
1. let DIRS=NODE_MODULES_PATHS(START)
221
-
2. for each DIR in DIRS:
222
-
1. LOAD_AS_FILE(DIR/X, T)
223
-
1. LOAD_AS_DIRECTORY(DIR/X, T)
208
+
1. for each DIR in DIRS:
209
+
1. If the directory at DIR/X exists then,
210
+
1. LOAD_ES_PACKAGE(DIR/X)
211
+
212
+
### LOAD_ES_PACKAGE(X)
213
+
214
+
1. let C be an empty object
215
+
1. If the file at X/package.json exists then,
216
+
1. If the file at X/package.json is invalid JSON then,
217
+
1. THROW "resolution error"
218
+
1. Set C to the parsed JSON for X/package.json
219
+
1. If C has a "module" field string then,
220
+
1. let M be the "module" field of C.
221
+
1. LOAD_ES_FILE(X/M)
222
+
1. If X/default.js exists, load X/default.js as ES module text. STOP
0 commit comments