编译缓存的可移植性
【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()中使用便携选项:// 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 });