@@ -340,10 +340,6 @@ This feature can be detected by checking if
340340To get the exact filename that will be loaded when ` require() ` is called, use
341341the ` require.resolve() ` function.
342342
343- When the [ ` --experimental-package-map ` ] [ ] flag is enabled, bare specifier
344- resolution first consults the package map before searching ` node_modules `
345- directories. See [ Package maps] [ ] for details.
346-
347343Putting together all of the above, here is the high-level algorithm
348344in pseudocode of what ` require() ` does:
349345
@@ -361,8 +357,12 @@ require(X) from module at path Y
3613574. If X begins with '#'
362358 a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
3633595. LOAD_PACKAGE_SELF(X, dirname(Y))
364- 6. LOAD_NODE_MODULES(X, dirname(Y))
365- 7. THROW "not found"
360+ 6. If a package map PACKAGE_MAP exists,
361+ a. Find the package ID for the package owning Y
362+ 1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
363+ b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
364+ 7. LOAD_NODE_MODULES(X, dirname(Y))
365+ 8. THROW "not found"
366366
367367MAYBE_DETECT_AND_LOAD(X)
3683681. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -407,9 +407,11 @@ LOAD_AS_DIRECTORY(X)
4074072. LOAD_INDEX(X)
408408
409409LOAD_NODE_MODULES(X, START)
410- 1. let DIRS = NODE_MODULES_PATHS(START)
411- 2. for each DIR in DIRS:
412- a. LOAD_PACKAGE_EXPORTS(X, DIR)
410+ 1. Try to interpret X as a combination of NAME and SUBPATH where the name
411+ may have a @scope/ prefix and the subpath begins with a slash (`/`).
412+ 2. let DIRS = NODE_MODULES_PATHS(START)
413+ 3. for each DIR in DIRS:
414+ a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
413415 b. LOAD_AS_FILE(DIR/X)
414416 c. LOAD_AS_DIRECTORY(DIR/X)
415417
@@ -424,6 +426,25 @@ NODE_MODULES_PATHS(START)
424426 d. let I = I - 1
4254275. return DIRS + GLOBAL_FOLDERS
426428
429+ FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
430+ 1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
431+ 2. If multiple entries are found, THROW "ambiguous resolution"
432+ 3. If no entry was found, THROW "external file".
433+ 4. return PACKAGE_ID
434+
435+ LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
436+ 1. Try to interpret X as a combination of NAME and SUBPATH where the name
437+ may have a @scope/ prefix and the subpath begins with a slash (`/`).
438+ 2. Find the package map entry for key PARENT_PACKAGE_ID
439+ 3. Look up NAME in the entry's "dependencies" map.
440+ 4. If NAME is not found, THROW "not found".
441+ 5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
442+ 6. Let PACKAGE_PATH be the resolved path of TARGET.
443+ 7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
444+ 8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
445+ 9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
446+ 10. THROW "not found"
447+
427448LOAD_PACKAGE_IMPORTS(X, DIR)
4284491. Find the closest package scope SCOPE to DIR.
4294502. If no scope was found, return.
@@ -435,19 +456,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
435456 CONDITIONS) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
4364576. RESOLVE_ESM_MATCH(MATCH).
437458
438- LOAD_PACKAGE_EXPORTS(X, DIR)
439- 1. Try to interpret X as a combination of NAME and SUBPATH where the name
440- may have a @scope/ prefix and the subpath begins with a slash (`/`).
441- 2. If X does not match this pattern or DIR/NAME/package.json is not a file,
442- return.
443- 3. Parse DIR/NAME/package.json, and look for "exports" field.
444- 4. If "exports" is null or undefined, return.
445- 5. If `--no-require-module` is not enabled
459+ LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
460+ 1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
461+ 2. If "exports" is null or undefined, return.
462+ 3. If `--no-require-module` is not enabled
446463 a. let CONDITIONS = ["node", "require", "module-sync"]
447464 b. Else, let CONDITIONS = ["node", "require"]
448- 6 . let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME ), "." + SUBPATH,
465+ 4 . let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR ), "." + SUBPATH,
449466 `package.json` "exports", CONDITIONS) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
450- 7 . RESOLVE_ESM_MATCH(MATCH)
467+ 5 . RESOLVE_ESM_MATCH(MATCH)
451468
452469LOAD_PACKAGE_SELF(X, DIR)
4534701. Find the closest package scope SCOPE to DIR.
0 commit comments