@@ -64,6 +64,152 @@ const require = createRequire(import.meta.url); |
64 | 64 | const siblingModule = require('./sibling-module'); |
65 | 65 | ``` |
66 | 66 |
|
| 67 | +### `module.constants.compileCacheStatus` |
| 68 | +
|
| 69 | +<!-- YAML |
| 70 | +added: REPLACEME |
| 71 | +--> |
| 72 | +
|
| 73 | +> Stability: 1.1 - Active Development |
| 74 | +
|
| 75 | +The following constants are returned as the `status` field in the object returned by |
| 76 | +[`module.enableCompileCache()`][] to indicate the result of the attempt to enable the |
| 77 | +[module compile cache][]. |
| 78 | +
|
| 79 | +<table> |
| 80 | + <tr> |
| 81 | + <th>Constant</th> |
| 82 | + <th>Description</th> |
| 83 | + </tr> |
| 84 | + <tr> |
| 85 | + <td><code>ENABLED</code></td> |
| 86 | + <td> |
| 87 | + Node.js has enabled the compile cache successfully. The directory used to store the |
| 88 | + compile cache will be returned in the <code>directory</code> field in the |
| 89 | + returned object. |
| 90 | + </td> |
| 91 | + </tr> |
| 92 | + <tr> |
| 93 | + <td><code>ALREADY_ENABLED</code></td> |
| 94 | + <td> |
| 95 | + The compile cache has already been enabled before, either by a previous call to |
| 96 | + <code>module.enableCompileCache()</code>, or by the <code>NODE_COMPILE_CACHE=dir</code> |
| 97 | + environment variable. The directory used to store the |
| 98 | + compile cache will be returned in the <code>directory</code> field in the |
| 99 | + returned object. |
| 100 | + </td> |
| 101 | + </tr> |
| 102 | + <tr> |
| 103 | + <td><code>FAILED</code></td> |
| 104 | + <td> |
| 105 | + Node.js fails to enable the compile cache. This can be caused by the lack of |
| 106 | + permission to use the specified directory, or various kinds of file system errors. |
| 107 | + The detail of the failure will be returned in the <code>message</code> field in the |
| 108 | + returned object. |
| 109 | + </td> |
| 110 | + </tr> |
| 111 | + <tr> |
| 112 | + <td><code>DISABLED</code></td> |
| 113 | + <td> |
| 114 | + Node.js cannot enable the compile cache because the environment variable |
| 115 | + <code>NODE_DISABLE_COMPILE_CACHE=1</code> has been set. |
| 116 | + </td> |
| 117 | + </tr> |
| 118 | +</table> |
| 119 | +
|
| 120 | +### `module.enableCompileCache([cacheDir])` |
| 121 | +
|
| 122 | +<!-- YAML |
| 123 | +added: REPLACEME |
| 124 | +--> |
| 125 | +
|
| 126 | +> Stability: 1.1 - Active Development |
| 127 | +
|
| 128 | +* `cacheDir` {string|undefined} Optional path to specify the directory where the compile cache |
| 129 | + will be stored/retrieved. |
| 130 | +* Returns: {Object} |
| 131 | + * `status` {integer} One of the [`module.constants.compileCacheStatus`][] |
| 132 | + * `message` {string|undefined} If Node.js cannot enable the compile cache, this contains |
| 133 | + the error message. Only set if `status` is `module.constants.compileCacheStatus.FAILED`. |
| 134 | + * `directory` {string|undefined} If the compile cache is enabled, this contains the directory |
| 135 | + where the compile cache is stored. Only set if `status` is |
| 136 | + `module.constants.compileCacheStatus.ENABLED` or |
| 137 | + `module.constants.compileCacheStatus.ALREADY_ENABLED`. |
| 138 | +
|
| 139 | +Enable [module compile cache][] in the current Node.js instance. |
| 140 | +
|
| 141 | +If `cacheDir` is not specified, Node.js will either use the directory specified by the |
| 142 | +[`NODE_COMPILE_CACHE=dir`][] environment variable if it's set, or use |
| 143 | +`path.join(os.tmpdir(), 'node-compile-cache')` otherwise. For general use cases, it's |
| 144 | +recommended to call `module.enableCompileCache()` without specifying the `cacheDir`, |
| 145 | +so that the directory can be overriden by the `NODE_COMPILE_CACHE` environment |
| 146 | +variable when necessary. |
| 147 | +
|
| 148 | +Since compile cache is supposed to be a quiet optimization that is not required for the |
| 149 | +application to be functional, this method is designed to not throw any exception when the |
| 150 | +compile cache cannot be enabled. Instead, it will return an object containing an error |
| 151 | +message in the `message` field to aid debugging. |
| 152 | +If compile cache is enabled successefully, the `directory` field in the returned object |
| 153 | +contains the path to the directory where the compile cache is stored. The `status` |
| 154 | +field in the returned object would be one of the `module.constants.compileCacheStatus` |
| 155 | +values to indicate the result of the attempt to enable the [module compile cache][]. |
| 156 | +
|
| 157 | +This method only affects the current Node.js instance. To enable it in child worker threads, |
| 158 | +either call this method in child worker threads too, or set the |
| 159 | +`process.env.NODE_COMPILE_CACHE` value to compile cache directory so the behavior can |
| 160 | +be inheritend into the child workers. The directory can be obtained either from the |
| 161 | +`directory` field returned by this method, or with [`module.getCompileCacheDir()`][]. |
| 162 | +
|
| 163 | +#### Module compile cache |
| 164 | +
|
| 165 | +<!-- YAML |
| 166 | +added: v22.1.0 |
| 167 | +changes: |
| 168 | + - version: REPLACEME |
| 169 | + pr-url: https://github.com/nodejs/node/pull/54501 |
| 170 | + description: add initial JavaScript APIs for runtime access. |
| 171 | +--> |
| 172 | +
|
| 173 | +The module compile cache can be enabled either using the [`module.enableCompileCache()`][] |
| 174 | +method or the [`NODE_COMPILE_CACHE=dir`][] environemnt variable. After it's enabled, |
| 175 | +whenever Node.js compiles a CommonJS or a ECMAScript Module, it will use on-disk |
| 176 | +[V8 code cache][] persisted in the specified directory to speed up the compilation. |
| 177 | +This may slow down the first load of a module graph, but subsequent loads of the same module |
| 178 | +graph may get a significant speedup if the contents of the modules do not change. |
| 179 | +
|
| 180 | +To clean up the generated compile cache on disk, simply remove the cache directory. The cache |
| 181 | +directory will be recreated the next time the same directory is used for for compile cache |
| 182 | +storage. To avoid filling up the disk with stale cache, it is recommended to use a directory |
| 183 | +under the [`os.tmpdir()`][]. If the compile cache is enabled by a call to |
| 184 | +[`module.enableCompileCache()`][] without specifying the directory, Node.js will use |
| 185 | +the [`NODE_DISABLE_COMPILE_CACHE=1`][] environment variable if it's set, or defaults |
| 186 | +to `path.join(os.tmpdir(), 'node-compile-cache')` otherwise. To locate the compile cache |
| 187 | +directory used by a running Node.js instance, use [`module.getCompileCacheDir()`][]. |
| 188 | +
|
| 189 | +Currently when using the compile cache with [V8 JavaScript code coverage][], the |
| 190 | +coverage being collected by V8 may be less precise in functions that are |
| 191 | +deserialized from the code cache. It's recommended to turn this off when |
| 192 | +running tests to generate precise coverage. |
| 193 | +
|
| 194 | +The enabled module compile cache can be disabled by the [`NODE_DISABLE_COMPILE_CACHE=1`][] |
| 195 | +environment variable. This can be useful when the compile cache leads to unexpected or |
| 196 | +undesired behaviors (e.g. less precise test coverage). |
| 197 | +
|
| 198 | +Compilation cache generated by one version of Node.js can not be reused by a different |
| 199 | +version of Node.js. Cache generated by different versions of Node.js will be stored |
| 200 | +separately if the same base directory is used to persist the cache, so they can co-exist. |
| 201 | +
|
| 202 | +### `module.getCompileCacheDir()` |
| 203 | +
|
| 204 | +<!-- YAML |
| 205 | +added: REPLACEME |
| 206 | +--> |
| 207 | +
|
| 208 | +> Stability: 1.1 - Active Development |
| 209 | +
|
| 210 | +* Returns: {string|undefined} Path to the [module compile cache][] directory if it is enabled, |
| 211 | + or `undefined` otherwise. |
| 212 | +
|
67 | 213 | ### `module.isBuiltin(moduleName)` |
68 | 214 |
|
69 | 215 | <!-- YAML |
@@ -1055,22 +1201,31 @@ returned object contains the following keys: |
1055 | 1201 | [Customization hooks]: #customization-hooks |
1056 | 1202 | [ES Modules]: esm.md |
1057 | 1203 | [Source map v3 format]: https://sourcemaps.info/spec.html#h.mofvlxcwqzej |
| 1204 | +[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html |
| 1205 | +[V8 code cache]: https://v8.dev/blog/code-caching-for-devs |
1058 | 1206 | [`"exports"`]: packages.md#exports |
1059 | 1207 | [`--enable-source-maps`]: cli.md#--enable-source-maps |
1060 | 1208 | [`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer |
| 1209 | +[`NODE_COMPILE_CACHE=dir`]: cli.md#node_compile_cachedir |
| 1210 | +[`NODE_DISABLE_COMPILE_CACHE=1`]: cli.md#node_disable_compile_cache1 |
1061 | 1211 | [`NODE_V8_COVERAGE=dir`]: cli.md#node_v8_coveragedir |
1062 | 1212 | [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer |
1063 | 1213 | [`SourceMap`]: #class-modulesourcemap |
1064 | 1214 | [`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
1065 | 1215 | [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array |
1066 | 1216 | [`initialize`]: #initialize |
1067 | | -[`module`]: modules.md#the-module-object |
| 1217 | +[`module.constants.compileCacheStatus`]: #moduleconstantscompilecachestatus |
| 1218 | +[`module.enableCompileCache()`]: #moduleenablecompilecachecachedir |
| 1219 | +[`module.getCompileCacheDir()`]: #modulegetcompilecachedir |
| 1220 | +[`module`]: #the-module-object |
| 1221 | +[`os.tmpdir()`]: os.md#ostmpdir |
1068 | 1222 | [`register`]: #moduleregisterspecifier-parenturl-options |
1069 | 1223 | [`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String |
1070 | 1224 | [`util.TextDecoder`]: util.md#class-utiltextdecoder |
1071 | 1225 | [chain]: #chaining |
1072 | 1226 | [hooks]: #customization-hooks |
1073 | 1227 | [load hook]: #loadurl-context-nextload |
| 1228 | +[module compile cache]: #module-compile-cache |
1074 | 1229 | [module wrapper]: modules.md#the-module-wrapper |
1075 | 1230 | [prefix-only modules]: modules.md#built-in-modules-with-mandatory-node-prefix |
1076 | 1231 | [realm]: https://tc39.es/ecma262/#realm |
|