RollupCache

Rollup 默认是开启了内存 cache 的。 resolveId , load 和 transform 的结果都会缓存在内存中不会重新解析。

Rollup 在每次构建之间(每调用一次 rollup.rollup() 就是一次构建)也预留了 cache 的口子。

constructor(private readonly options: NormalizedInputOptions, watcher: RollupWatcher | null) {
        if (options.cache !== false) {
            if (options.cache?.modules) {
                for (const module of options.cache.modules) this.cachedModules.set(module.id, module);
            }
            this.pluginCache = options.cache?.plugins || Object.create(null);

可以看到 Rollup 主要有 2 种类型的 cache
moduleCache : 缓存的是每个 module 文件的 ast ,code , sourceMap ,resolve ,transform 结果等内容。
pluginCache: 允许三方插件自定义一些 cache ,在插件中通过 this.cache.set(key,value), this.cache.get(key) 实现。

const rollup = require('rollup');
let cache;

async function buildWithCache() {
  const bundle = await rollup.rollup({
    cache // 传入上次构建结果的 cache 
    // ... other input options
  });
  // 这里可以把本次构建结果的cache写入到文件或全局变量等
  cache = bundle.cache; 
  return bundle;
}

buildWithCache()

Rollup 在每次构建时如果 options 中 (options.cache)传入了上次构建结果的缓存就会尝试复用上次构建的结果。我们可以把上次构建结果写入到json 文件下次构建时传入,这样可以一定程度改善 Rollup 冷启动的时间。 watch 模式不用担心,因为 watch 模式一直都有内存缓存。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容