@@ -385,20 +385,33 @@ changes:
385385
386386The module compile cache can be enabled either using the [` module .enableCompileCache ()` ][]
387387method or the [` NODE_COMPILE_CACHE = dir` ][] environment variable. After it is enabled,
388- whenever Node.js compiles a CommonJS or a ECMAScript Module, it will use on-disk
389- [V8 code cache][] persisted in the specified directory to speed up the compilation.
388+ whenever Node.js compiles a CommonJS, a ECMAScript Module, or a TypeScript module, it will
389+ use on-disk [V8 code cache][] persisted in the specified directory to speed up the compilation.
390390This may slow down the first load of a module graph, but subsequent loads of the same module
391391graph may get a significant speedup if the contents of the modules do not change.
392392
393393To clean up the generated compile cache on disk, simply remove the cache directory. The cache
394394directory will be recreated the next time the same directory is used for for compile cache
395395storage. To avoid filling up the disk with stale cache, it is recommended to use a directory
396396under the [` os .tmpdir ()` ][]. If the compile cache is enabled by a call to
397- [` module .enableCompileCache ()` ][] without specifying the directory, Node.js will use
397+ [` module .enableCompileCache ()` ][] without specifying the ` directory` , Node.js will use
398398the [` NODE_COMPILE_CACHE = dir` ][] environment variable if it's set, or defaults
399399to ` path .join (os .tmpdir (), ' node-compile-cache' )` otherwise. To locate the compile cache
400400directory used by a running Node.js instance, use [` module .getCompileCacheDir ()` ][].
401401
402+ The enabled module compile cache can be disabled by the [` NODE_DISABLE_COMPILE_CACHE = 1 ` ][]
403+ environment variable. This can be useful when the compile cache leads to unexpected or
404+ undesired behaviors (e.g. less precise test coverage).
405+
406+ At the moment, when the compile cache is enabled and a module is loaded afresh, the
407+ code cache is generated from the compiled code immediately, but will only be written
408+ to disk when the Node.js instance is about to exit. This is subject to change. The
409+ [` module .flushCompileCache ()` ][] method can be used to ensure the accumulated code cache
410+ is flushed to disk in case the application wants to spawn other Node.js instances
411+ and let them share the cache long before the parent exits.
412+
413+ ### Portability of the compile cache
414+
402415By default, caches are invalidated when the absolute paths of the modules being
403416cached are changed. To keep the cache working after moving the
404417project directory, enable portable compile cache. This allows previously compiled
@@ -409,38 +422,29 @@ will not be cached.
409422
410423There are two ways to enable the portable mode:
411424
412- 1. Using the portable option in module.enableCompileCache():
425+ 1. Using the portable option in [ ` module .enableCompileCache ()` ][] :
413426
414427 ` ` ` js
415428 // Non-portable cache (default): cache breaks if project is moved
416- module .enableCompileCache ({ path : ' /path/to/cache/storage/dir' });
429+ module .enableCompileCache ({ directory : ' /path/to/cache/storage/dir' });
417430
418431 // Portable cache: cache works after the project is moved
419- module .enableCompileCache ({ path : ' /path/to/cache/storage/dir' , portable: true });
432+ module .enableCompileCache ({ directory : ' /path/to/cache/storage/dir' , portable: true });
420433 ` ` `
421434
4224352. Setting the environment variable: [` NODE_COMPILE_CACHE_PORTABLE = 1 ` ][]
423436
437+ ### Limitations of the compile cache
438+
424439Currently when using the compile cache with [V8 JavaScript code coverage][], the
425440coverage being collected by V8 may be less precise in functions that are
426441deserialized from the code cache. It's recommended to turn this off when
427442running tests to generate precise coverage.
428443
429- The enabled module compile cache can be disabled by the [` NODE_DISABLE_COMPILE_CACHE = 1 ` ][]
430- environment variable. This can be useful when the compile cache leads to unexpected or
431- undesired behaviors (e.g. less precise test coverage).
432-
433444Compilation cache generated by one version of Node.js can not be reused by a different
434445version of Node.js. Cache generated by different versions of Node.js will be stored
435446separately if the same base directory is used to persist the cache, so they can co-exist.
436447
437- At the moment, when the compile cache is enabled and a module is loaded afresh, the
438- code cache is generated from the compiled code immediately, but will only be written
439- to disk when the Node.js instance is about to exit. This is subject to change. The
440- [` module .flushCompileCache ()` ][] method can be used to ensure the accumulated code cache
441- is flushed to disk in case the application wants to spawn other Node.js instances
442- and let them share the cache long before the parent exits.
443-
444448### ` module .constants .compileCacheStatus `
445449
446450<!-- YAML
@@ -494,16 +498,30 @@ The following constants are returned as the `status` field in the object returne
494498 </tr>
495499</table>
496500
497- ### ` module .enableCompileCache ([cacheDir ])`
501+ ### ` module .enableCompileCache ([options ])`
498502
499503<!-- YAML
500504added: v22.8.0
505+ changes:
506+ - version: REPLACEME
507+ pr-url: https://github.com/nodejs/node/pull/58797
508+ description: Add ` portable` option to enable portable compile cache.
509+ - version: REPLACEME
510+ pr-url: REPLACEME
511+ description: Rename the unreleased ` path` option to ` directory` to maintain consistency.
501512-->
502513
503514> Stability: 1.1 - Active Development
504515
505- * ` cacheDir` {string|undefined} Optional path to specify the directory where the compile cache
506- will be stored/retrieved.
516+ * ` options` {string|Object} Optional. If a string is passed, it is considered to be ` options .directory ` .
517+ * ` directory` {string} Optional. Directory to store the compile cache. If not specified,
518+ the directory specified by the [` NODE_COMPILE_CACHE = dir` ][] environment variable
519+ will be used if it's set, or ` path .join (os .tmpdir (), ' node-compile-cache' )`
520+ otherwise.
521+ * ` portable` {boolean} Optional. If ` true ` , enables portable compile cache so that
522+ the cache can be reused even if the project directory is moved. This is a best-effort
523+ feature. If not specified, it will depend on whether the environment variable
524+ [` NODE_COMPILE_CACHE_PORTABLE = 1 ` ][] is set.
507525* Returns: {Object}
508526 * ` status` {integer} One of the [` module .constants .compileCacheStatus ` ][]
509527 * ` message` {string|undefined} If Node.js cannot enable the compile cache, this contains
@@ -515,20 +533,16 @@ added: v22.8.0
515533
516534Enable [module compile cache][] in the current Node.js instance.
517535
518- If ` cacheDir` is not specified, Node.js will either use the directory specified by the
519- [` NODE_COMPILE_CACHE = dir` ][] environment variable if it's set, or use
520- ` path .join (os .tmpdir (), ' node-compile-cache' )` otherwise. For general use cases, it's
521- recommended to call ` module .enableCompileCache ()` without specifying the ` cacheDir` ,
522- so that the directory can be overridden by the ` NODE_COMPILE_CACHE ` environment
523- variable when necessary.
524-
525- Since compile cache is supposed to be a quiet optimization that is not required for the
526- application to be functional, this method is designed to not throw any exception when the
527- compile cache cannot be enabled. Instead, it will return an object containing an error
528- message in the ` message` field to aid debugging.
529- If compile cache is enabled successfully, the ` directory` field in the returned object
530- contains the path to the directory where the compile cache is stored. The ` status`
531- field in the returned object would be one of the ` module .constants .compileCacheStatus `
536+ For general use cases, it's recommended to call ` module .enableCompileCache ()` without
537+ specifying the ` options .directory ` , so that the directory can be overridden by the
538+ ` NODE_COMPILE_CACHE ` environment variable when necessary.
539+
540+ Since compile cache is supposed to be a optimization that is not mission critical, this
541+ method is designed to not throw any exception when the compile cache cannot be enabled.
542+ Instead, it will return an object containing an error message in the ` message` field to
543+ aid debugging. If compile cache is enabled successfully, the ` directory` field in the
544+ returned object contains the path to the directory where the compile cache is stored. The
545+ ` status` field in the returned object would be one of the ` module .constants .compileCacheStatus `
532546values to indicate the result of the attempt to enable the [module compile cache][].
533547
534548This method only affects the current Node.js instance. To enable it in child worker threads,
0 commit comments