module.enableCompileCache([options])


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active Development

  • options <string> | <Object> 可选的。如果传递了字符串,则该字符串被视为 options.directory

    ¥options <string> | <Object> Optional. If a string is passed, it is considered to be options.directory.

    • directory <string> 可选的。存储编译缓存的目录。如果未指定,则如果设置了 NODE_COMPILE_CACHE=dir 环境变量,则使用其指定的目录;否则使用 path.join(os.tmpdir(), 'node-compile-cache')

      ¥directory <string> Optional. Directory to store the compile cache. If not specified, the directory specified by the NODE_COMPILE_CACHE=dir environment variable will be used if it's set, or path.join(os.tmpdir(), 'node-compile-cache') otherwise.

    • portable <boolean> 可选的。如果设置为 true,则启用可移植编译缓存,以便即使项目目录被移动,缓存也可以重用。这是一项尽力而为的功能。如果未指定,则取决于环境变量 NODE_COMPILE_CACHE_PORTABLE=1 是否设置。

      ¥portable <boolean> Optional. If true, enables portable compile cache so that the cache can be reused even if the project directory is moved. This is a best-effort feature. If not specified, it will depend on whether the environment variable NODE_COMPILE_CACHE_PORTABLE=1 is set.

  • 返回:<Object>

    ¥Returns: <Object>

    • status <integer> module.constants.compileCacheStatus 之一

      ¥status <integer> One of the module.constants.compileCacheStatus

    • message <string> | <undefined> 如果 Node.js 无法启用编译缓存,则包含错误消息。仅当 statusmodule.constants.compileCacheStatus.FAILED 时才设置。

      ¥message <string> | <undefined> If Node.js cannot enable the compile cache, this contains the error message. Only set if status is module.constants.compileCacheStatus.FAILED.

    • directory <string> | <undefined> 如果启用了编译缓存,则包含存储编译缓存的目录。仅当 statusmodule.constants.compileCacheStatus.ENABLEDmodule.constants.compileCacheStatus.ALREADY_ENABLED 时才设置。

      ¥directory <string> | <undefined> If the compile cache is enabled, this contains the directory where the compile cache is stored. Only set if status is module.constants.compileCacheStatus.ENABLED or module.constants.compileCacheStatus.ALREADY_ENABLED.

在当前 Node.js 实例中启用 模块编译缓存

¥Enable module compile cache in the current Node.js instance.

对于一般用例,建议在不指定 options.directory 的情况下调用 module.enableCompileCache(),以便目录可以在必要时被 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().