module.enableCompileCache([options])
options<string> | <Object> 可选。如果传入字符串,则视为options.directory。directory<string> 可选。用于存储编译缓存的目录。如果未指定,将使用NODE_COMPILE_CACHE=dir环境变量指定的目录(如果已设置),否则使用path.join(os.tmpdir(), 'node-compile-cache')。portable<boolean> 可选。如果为true,则启用可移植编译缓存,使缓存即使在项目目录被移动后也可以重用。这是一个尽力而为的功能。如果未指定,则取决于环境变量NODE_COMPILE_CACHE_PORTABLE=1是否已设置。
- 返回:<Object>
status<integer>module.constants.compileCacheStatus之一message<string> | <undefined> 如果 Node.js 无法启用编译缓存,则此处包含错误信息。只有在status为module.constants.compileCacheStatus.FAILED时才会设置。directory<string> | <undefined> 如果启用了编译缓存,本字段包含存储编译缓存的目录。仅在status为module.constants.compileCacheStatus.ENABLED或module.constants.compileCacheStatus.ALREADY_ENABLED时设置。
在当前的 Node.js 实例中启用 模块编译缓存。
【Enable module compile cache in the current Node.js instance.】
对于一般使用情况,建议调用 module.enableCompileCache() 时不要指定 options.directory,这样在必要时可以通过 NODE_COMPILE_CACHE 环境变量覆盖该目录。
【For general use cases, it's recommended to call module.enableCompileCache() without
specifying the options.directory, so that the directory can be overridden by the
NODE_COMPILE_CACHE environment variable when necessary.】
由于编译缓存被认为是一种非关键任务的优化,因此当无法启用编译缓存时,该方法设计为不会抛出任何异常。相反,它将返回一个对象,其中 message 字段包含错误信息以便调试。如果编译缓存成功启用,返回对象中的 directory 字段将包含存储编译缓存的目录路径。返回对象中的 status 字段将是 module.constants.compileCacheStatus 的某个值,以指示尝试启用 模块编译缓存 的结果。
【Since compile cache is supposed to be a optimization that is not mission critical, this
method is designed to not throw any exception when the compile cache cannot be enabled.
Instead, it will return an object containing an error message in the message field to
aid debugging. If compile cache is enabled successfully, the directory field in the
returned object contains the path to the directory where the compile cache is stored. The
status field in the returned object would be one of the module.constants.compileCacheStatus
values to indicate the result of the attempt to enable the module compile cache.】
此方法仅影响当前的 Node.js 实例。要在子工作线程中启用它,你可以在子工作线程中也调用此方法,或者将 process.env.NODE_COMPILE_CACHE 设置为编译缓存目录,这样该行为就可以继承到子工作线程中。该目录可以通过此方法返回的 directory 字段获取,或者使用 module.getCompileCacheDir() 获取。
【This method only affects the current Node.js instance. To enable it in child worker threads,
either call this method in child worker threads too, or set the
process.env.NODE_COMPILE_CACHE value to compile cache directory so the behavior can
be inherited into the child workers. The directory can be obtained either from the
directory field returned by this method, or with module.getCompileCacheDir().】