Skip to content
Permalink
Browse files

feat: improve caching

  • Loading branch information
evilebottnawi committed Sep 11, 2020
1 parent 92f53b6 commit defde640c8a2448fe7a5fee95a516af6e1bba8e4
@@ -5,31 +5,114 @@ import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';

export default class Webpack4Cache {
constructor(compilation, options) {
this.cacheDir =
constructor(compilation, options, weakCache) {
this.cache =
options.cache === true
? Webpack4Cache.getCacheDirectory()
: options.cache;
this.weakCache = weakCache;
}

static getCacheDirectory() {
return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir();
}

isEnabled() {
return Boolean(this.cacheDir);
}
async get(cacheData, { RawSource, ConcatSource, SourceMapSource }) {
if (!this.cache) {
// eslint-disable-next-line no-undefined
return undefined;
}

const weakOutput = this.weakCache.get(cacheData.inputSource);

if (weakOutput) {
return weakOutput;
}

async get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
cacheData.cacheIdent =
cacheData.cacheIdent || serialize(cacheData.cacheKeys);

let cachedResult;

try {
cachedResult = await cacache.get(this.cache, cacheData.cacheIdent);
} catch (ignoreError) {
// eslint-disable-next-line no-undefined
return undefined;
}

cachedResult = JSON.parse(cachedResult.data);

if (cachedResult.target === 'comments') {
return new ConcatSource(cachedResult.value);
}

const { data } = await cacache.get(this.cacheDir, task.cacheIdent);
const {
code,
name,
map,
input,
inputSourceMap,
extractedComments,
} = cachedResult;

return JSON.parse(data);
if (map) {
cachedResult.source = new SourceMapSource(
code,
name,
map,
input,
inputSourceMap,
true
);
} else {
cachedResult.source = new RawSource(code);
}

if (extractedComments) {
cachedResult.extractedCommentsSource = new RawSource(extractedComments);
}

return cachedResult;
}

async store(task, data) {
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
async store(cacheData) {
if (!this.cache) {
// eslint-disable-next-line no-undefined
return undefined;
}

if (!this.weakCache.has(cacheData.inputSource)) {
if (cacheData.target === 'comments') {
this.weakCache.set(cacheData.inputSource, cacheData.output);
} else {
this.weakCache.set(cacheData.inputSource, cacheData);
}
}

let data;

if (cacheData.target === 'comments') {
data = {
target: cacheData.target,
value: cacheData.output.source(),
};
} else {
data = {
code: cacheData.code,
name: cacheData.name,
map: cacheData.map,
input: cacheData.input,
inputSourceMap: cacheData.inputSourceMap,
};

if (cacheData.extractedCommentsSource) {
data.extractedComments = cacheData.extractedCommentsSource.source();
data.commentsFilename = cacheData.commentsFilename;
}
}

return cacache.put(this.cache, cacheData.cacheIdent, JSON.stringify(data));
}
}
@@ -1,25 +1,35 @@
export default class Cache {
// eslint-disable-next-line no-unused-vars
constructor(compilation, ignored) {
constructor(compilation) {
this.cache = compilation.getCache('TerserWebpackPlugin');
}

// eslint-disable-next-line class-methods-use-this
isEnabled() {
return true;
}

async get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || `${task.name}`;
async get(cacheData) {
// eslint-disable-next-line no-param-reassign
task.cacheETag =
task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource);
cacheData.eTag =
cacheData.eTag || Array.isArray(cacheData.inputSource)
? cacheData.inputSource
.map((item) => this.cache.getLazyHashedEtag(item))
.reduce((previousValue, currentValue) =>
this.cache.mergeEtags(previousValue, currentValue)
)
: this.cache.getLazyHashedEtag(cacheData.inputSource);

return this.cache.getPromise(task.cacheIdent, task.cacheETag);
return this.cache.getPromise(cacheData.name, cacheData.eTag);
}

async store(task, data) {
return this.cache.storePromise(task.cacheIdent, task.cacheETag, data);
async store(cacheData) {
let data;

if (cacheData.target === 'comments') {
data = cacheData.output;
} else {
data = {
source: cacheData.source,
extractedCommentsSource: cacheData.extractedCommentsSource,
commentsFilename: cacheData.commentsFilename,
};
}

return this.cache.storePromise(cacheData.name, cacheData.eTag, data);
}
}

0 comments on commit defde64

Please sign in to comment.
You can’t perform that action at this time.