@@ -353,8 +353,12 @@ require(X) from module at path Y |
353 | 353 | 4. If X begins with '#' |
354 | 354 | a. LOAD_PACKAGE_IMPORTS(X, dirname(Y)) |
355 | 355 | 5. LOAD_PACKAGE_SELF(X, dirname(Y)) |
356 | | -6. LOAD_NODE_MODULES(X, dirname(Y)) |
357 | | -7. THROW "not found" |
| 356 | +6. If a package map PACKAGE_MAP exists, |
| 357 | + a. Find the package ID for the package owning Y |
| 358 | + 1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP) |
| 359 | + b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP) |
| 360 | +7. LOAD_NODE_MODULES(X, dirname(Y)) |
| 361 | +8. THROW "not found" |
358 | 362 |
|
359 | 363 | MAYBE_DETECT_AND_LOAD(X) |
360 | 364 | 1. If X parses as a CommonJS module, load X as a CommonJS module. STOP. |
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X) |
398 | 402 | 2. LOAD_INDEX(X) |
399 | 403 |
|
400 | 404 | LOAD_NODE_MODULES(X, START) |
401 | | -1. let DIRS = NODE_MODULES_PATHS(START) |
402 | | -2. for each DIR in DIRS: |
403 | | - a. LOAD_PACKAGE_EXPORTS(X, DIR) |
| 405 | +1. Try to interpret X as a combination of NAME and SUBPATH where the name |
| 406 | + may have a @scope/ prefix and the subpath begins with a slash (`/`). |
| 407 | +2. let DIRS = NODE_MODULES_PATHS(START) |
| 408 | +3. for each DIR in DIRS: |
| 409 | + a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME) |
404 | 410 | b. LOAD_AS_FILE(DIR/X) |
405 | 411 | c. LOAD_AS_DIRECTORY(DIR/X) |
406 | 412 |
|
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START) |
415 | 421 | d. let I = I - 1 |
416 | 422 | 5. return DIRS + GLOBAL_FOLDERS |
417 | 423 |
|
| 424 | +FIND_PACKAGE_ID(PATH, PACKAGE_MAP) |
| 425 | +1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH |
| 426 | +2. If multiple entries are found, THROW "ambiguous resolution" |
| 427 | +3. If no entry was found, THROW "external file". |
| 428 | +4. return PACKAGE_ID |
| 429 | +
|
| 430 | +LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP) |
| 431 | +1. Try to interpret X as a combination of NAME and SUBPATH where the name |
| 432 | + may have a @scope/ prefix and the subpath begins with a slash (`/`). |
| 433 | +2. Find the package map entry for key PARENT_PACKAGE_ID |
| 434 | +3. Look up NAME in the entry's "dependencies" map. |
| 435 | +4. If NAME is not found, THROW "not found". |
| 436 | +5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]] |
| 437 | +6. Let PACKAGE_PATH be the resolved path of TARGET. |
| 438 | +7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH) |
| 439 | +8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH) |
| 440 | +9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH) |
| 441 | +10. THROW "not found" |
| 442 | +
|
418 | 443 | LOAD_PACKAGE_IMPORTS(X, DIR) |
419 | 444 | 1. Find the closest package scope SCOPE to DIR. |
420 | 445 | 2. If no scope was found, return. |
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR) |
426 | 451 | CONDITIONS) defined in the ESM resolver. |
427 | 452 | 6. RESOLVE_ESM_MATCH(MATCH). |
428 | 453 |
|
429 | | -LOAD_PACKAGE_EXPORTS(X, DIR) |
430 | | -1. Try to interpret X as a combination of NAME and SUBPATH where the name |
431 | | - may have a @scope/ prefix and the subpath begins with a slash (`/`). |
432 | | -2. If X does not match this pattern or DIR/NAME/package.json is not a file, |
433 | | - return. |
434 | | -3. Parse DIR/NAME/package.json, and look for "exports" field. |
435 | | -4. If "exports" is null or undefined, return. |
436 | | -5. If `--no-require-module` is not enabled |
| 454 | +LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR) |
| 455 | +1. Parse PACKAGE_DIR/package.json, and look for "exports" field. |
| 456 | +2. If "exports" is null or undefined, return. |
| 457 | +3. If `--no-require-module` is not enabled |
437 | 458 | a. let CONDITIONS = ["node", "require", "module-sync"] |
438 | 459 | b. Else, let CONDITIONS = ["node", "require"] |
439 | | -6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH, |
| 460 | +4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH, |
440 | 461 | `package.json` "exports", CONDITIONS) defined in the ESM resolver. |
441 | | -7. RESOLVE_ESM_MATCH(MATCH) |
| 462 | +5. RESOLVE_ESM_MATCH(MATCH) |
442 | 463 |
|
443 | 464 | LOAD_PACKAGE_SELF(X, DIR) |
444 | 465 | 1. Find the closest package scope SCOPE to DIR. |
|