Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
451 changes: 305 additions & 146 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"cacache": "^15.0.5",
"cssnano": "^4.1.10",
"find-cache-dir": "^3.3.1",
"jest-worker": "^26.1.0",
"p-limit": "^3.0.2",
"schema-utils": "^2.7.0",
"serialize-javascript": "^4.0.0",
"source-map": "^0.6.1",
"webpack-sources": "^1.4.3"
},
Expand Down
35 changes: 35 additions & 0 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os from 'os';

import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';

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

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

isEnabled() {
return Boolean(this.cacheDir);
}

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

return cacache
.get(this.cacheDir, task.cacheIdent)
.then(({ data }) => JSON.parse(data));
}

store(task, data) {
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
}
}
75 changes: 75 additions & 0 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// eslint-disable-next-line import/extensions,import/no-unresolved
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
import serialize from 'serialize-javascript';

import { util } from 'webpack';

export default class Cache {
// eslint-disable-next-line no-unused-vars
constructor(compilation, ignored) {
this.compilation = compilation;
}

isEnabled() {
return Boolean(this.compilation.cache);
}

createCacheIdent(task) {
const {
outputOptions: { hashSalt, hashDigest, hashDigestLength, hashFunction },
} = this.compilation;

const hash = util.createHash(hashFunction);

if (hashSalt) {
hash.update(hashSalt);
}

hash.update(serialize(task.cacheKeys));

const digest = hash.digest(hashDigest);
const cacheKeys = digest.substr(0, hashDigestLength);

return `${this.compilation.compilerPath}/CssnanoWebpackPlugin/${cacheKeys}/${task.file}`;
}

get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
// eslint-disable-next-line no-param-reassign
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);

return new Promise((resolve, reject) => {
this.compilation.cache.get(
task.cacheIdent,
task.cacheETag,
(err, result) => {
if (err) {
reject(err);
} else if (result) {
resolve(result);
} else {
reject();
}
}
);
});
}

store(task, data) {
return new Promise((resolve, reject) => {
this.compilation.cache.store(
task.cacheIdent,
task.cacheETag,
data,
(err) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}
);
});
}
}
Loading