编译缓存的可移植性
¥Portability of the compile cache
默认情况下,当被缓存模块的绝对路径发生更改时,缓存将失效。要在移动项目目录后保持缓存正常工作,请启用可移植编译缓存。这允许之前编译的模块在不同的目录位置重复使用,只要相对于缓存目录的布局保持不变。这将在尽力而为的基础上完成。如果 Node.js 无法计算模块相对于缓存目录的位置,则不会缓存该模块。
¥By default, caches are invalidated when the absolute paths of the modules being cached are changed. To keep the cache working after moving the project directory, enable portable compile cache. This allows previously compiled modules to be reused across different directory locations as long as the layout relative to the cache directory remains the same. This would be done on a best-effort basis. If Node.js cannot compute the location of a module relative to the cache directory, the module will not be cached.
有两种方法可以启用便携模式:
¥There are two ways to enable the portable mode:
-
在
module.enableCompileCache()
中使用可移植选项:¥Using the portable option in
module.enableCompileCache()
:// Non-portable cache (default): cache breaks if project is moved module.enableCompileCache({ directory: '/path/to/cache/storage/dir' }); // Portable cache: cache works after the project is moved module.enableCompileCache({ directory: '/path/to/cache/storage/dir', portable: true });
-
设置环境变量:
NODE_COMPILE_CACHE_PORTABLE=1
¥Setting the environment variable:
NODE_COMPILE_CACHE_PORTABLE=1