Skip to content

module: use cjsCache over esm injection

This is the replacement for https://github.com/nodejs/node/pull/34467 as an alternative mechanism to ensure that named exports loaders (such as https://github.com/guybedford/cjs-named-exports-loader) are fully possible without issues. Without this PR such loaders cannot work for CJS loaded via require() before it is loaded via import().

As discussed, this PR retains the existing CJS snapshotting behaviour so that the module.exports value is captured for all CommonJS modules and stored in the ESMLoader.cjsCache map. This map is changed to be a weakmap keyed by the module object so that deletions in Module._cache allow this map to be GC'd.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

Merge request reports

Loading