Skip to content

Commit

Permalink
fix: compatibility cache feature with webpack@5
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 3, 2020
1 parent 2a5acaa commit 5d2bd29
Show file tree
Hide file tree
Showing 14 changed files with 677 additions and 1,668 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ npm-debug.log*
/local
/reports
/node_modules
/test/outputs

.DS_Store
Thumbs.db
Expand Down
1,770 changes: 265 additions & 1,505 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"prettier": "^2.0.5",
"standard-version": "^8.0.2",
"uglify-js": "^3.10.0",
"webpack": "^4.44.1",
"webpack": "^5.0.0-beta.23",

This comment has been minimized.

Copy link
@chicoxyzzy

chicoxyzzy Aug 3, 2020

Contributor

I think this was added by accident

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Aug 3, 2020

Member

Yep, will fix it, but it is not affected, because CI always reinstall webpack

"worker-loader": "^2.0.0"
},
"keywords": [
Expand Down
10 changes: 5 additions & 5 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ export default class Webpack4Cache {
return Boolean(this.cacheDir);
}

get(task) {
async 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));
const { data } = await cacache.get(this.cacheDir, task.cacheIdent);

return JSON.parse(data);
}

store(task, data) {
async store(task, data) {
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
}
}
69 changes: 11 additions & 58 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,28 @@
// 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;
this.cache = compilation.getCache('TerserWebpackPlugin');
}

// eslint-disable-next-line class-methods-use-this
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}/TerserWebpackPlugin/${cacheKeys}/${task.file}`;
return true;
}

get(task) {
async get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
task.cacheIdent =
task.cacheIdent || `${task.file}|${serialize(task.cacheKeys)}`;
// eslint-disable-next-line no-param-reassign
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);
task.cacheETag =
task.cacheETag || this.cache.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();
}
}
);
});
return this.cache.getPromise(task.cacheIdent, task.cacheETag);
}

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);
}
}
);
});
async store(task, data) {
return this.cache.storePromise(task.cacheIdent, task.cacheETag, data);
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ class TerserPlugin {
return enqueue(task);
}

// Webpack@5 return `undefined` when cache is not found
if (!taskResult) {
return enqueue(task);
}

task.callback(taskResult);

return Promise.resolve();
Expand Down
8 changes: 8 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ describe('TerserPlugin', () => {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
},
optimization: {
minimize: false,
noEmitOnErrors: false,
},
});

new BrokenCodePlugin().apply(compiler);
Expand All @@ -694,6 +698,10 @@ describe('TerserPlugin', () => {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
},
optimization: {
minimize: false,
noEmitOnErrors: false,
},
});

new BrokenCodePlugin().apply(compiler);
Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ exports[`TerserPlugin should emit an error on a broken code in parallel mode: wa

exports[`TerserPlugin should regenerate hash: assets 1`] = `
Object {
"598.598.70ff6985755ae80c6ef7.js": "(window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[]).push([[598],{598:(e,p,s)=>{\\"use strict\\";s.r(p),s.d(p,{default:()=>n});const n=\\"async-dep\\"}}]);",
"AsyncImportExport.853ebc7a6b79b4a9b16b.js": "(()=>{\\"use strict\\";var e,r={},t={};function o(e){if(t[e])return t[e].exports;var a=t[e]={exports:{}};return r[e](a,a.exports,o),a.exports}o.m=r,o.d=(e,r)=>{for(var t in r)o.o(r,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},o.f={},o.e=e=>Promise.all(Object.keys(o.f).reduce((r,t)=>(o.f[t](e,r),r),[])),o.u=e=>e+\\".\\"+e+\\".70ff6985755ae80c6ef7.js\\",o.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},o.l=(r,t,a)=>{if(e[r])e[r].push(t);else{var n,i;if(void 0!==a)for(var p=document.getElementsByTagName(\\"script\\"),s=0;s<p.length;s++){var u=p[s];if(u.getAttribute(\\"src\\")==r||u.getAttribute(\\"data-webpack\\")==\\"terser-webpack-plugin:\\"+a){n=u;break}}n||(i=!0,(n=document.createElement(\\"script\\")).charset=\\"utf-8\\",n.timeout=120,o.nc&&n.setAttribute(\\"nonce\\",o.nc),n.setAttribute(\\"data-webpack\\",\\"terser-webpack-plugin:\\"+a),n.src=r),e[r]=[t];var c=t=>{c=()=>{},n.onerror=n.onload=null,clearTimeout(l);var o=e[r];delete e[r],n.parentNode&&n.parentNode.removeChild(n),o&&o.forEach(e=>e(t))},l=setTimeout(()=>{c({type:\\"timeout\\",target:n})},12e4);n.onerror=n.onload=c,i&&document.head.appendChild(n)}},o.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.p=\\"\\",(()=>{var e={988:0};o.f.j=(r,t)=>{var a=o.o(e,r)?e[r]:void 0;if(0!==a)if(a)t.push(a[2]);else{var n=new Promise((t,o)=>{a=e[r]=[t,o]});t.push(a[2]=n);var i=o.p+o.u(r),p=new Error;o.l(i,t=>{if(o.o(e,r)&&(0!==(a=e[r])&&(e[r]=void 0),a)){var n=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;p.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+n+\\": \\"+i+\\")\\",p.name=\\"ChunkLoadError\\",p.type=n,p.request=i,a[1](p)}},\\"chunk-\\"+r)}};var r=window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[],t=r.push.bind(r);r.push=function(r){for(var t,n,i=r[0],p=r[1],s=r[3],u=0,c=[];u<i.length;u++)n=i[u],o.o(e,n)&&e[n]&&c.push(e[n][0]),e[n]=0;for(t in p)o.o(p,t)&&(o.m[t]=p[t]);for(s&&s(o),a&&a(r);c.length;)c.shift()()};var a=t})(),o.e(598).then(o.bind(o,598)).then(()=>{console.log(\\"Good\\")})})();",
"importExport.4dbd083d0540b53c8bf4.js": "(()=>{\\"use strict\\"})();",
"js.d1bc0c3718c3f8ac4e41.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
"mjs.2b79abcd1a51a2d3d42f.js": "(()=>{var r={631:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(631)})();",
"598.598.5d72557de264fa10f306.js": "(window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[]).push([[598],{598:(e,p,s)=>{\\"use strict\\";s.r(p),s.d(p,{default:()=>n});const n=\\"async-dep\\"}}]);",
"AsyncImportExport.4f0bde0b02c99cb3336a.js": "(()=>{\\"use strict\\";var e,r={},t={};function o(e){if(t[e])return t[e].exports;var a=t[e]={exports:{}};return r[e](a,a.exports,o),a.exports}o.m=r,o.d=(e,r)=>{for(var t in r)o.o(r,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},o.f={},o.e=e=>Promise.all(Object.keys(o.f).reduce((r,t)=>(o.f[t](e,r),r),[])),o.u=e=>e+\\".\\"+e+\\".5d72557de264fa10f306.js\\",o.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},o.l=(r,t,a)=>{if(e[r])e[r].push(t);else{var n,i;if(void 0!==a)for(var p=document.getElementsByTagName(\\"script\\"),s=0;s<p.length;s++){var u=p[s];if(u.getAttribute(\\"src\\")==r||u.getAttribute(\\"data-webpack\\")==\\"terser-webpack-plugin:\\"+a){n=u;break}}n||(i=!0,(n=document.createElement(\\"script\\")).charset=\\"utf-8\\",n.timeout=120,o.nc&&n.setAttribute(\\"nonce\\",o.nc),n.setAttribute(\\"data-webpack\\",\\"terser-webpack-plugin:\\"+a),n.src=r),e[r]=[t];var d=t=>{d=()=>{},n.onerror=n.onload=null,clearTimeout(l);var o=e[r];delete e[r],n.parentNode&&n.parentNode.removeChild(n),o&&o.forEach(e=>e(t))},l=setTimeout(()=>{d({type:\\"timeout\\",target:n})},12e4);n.onerror=n.onload=d,i&&document.head.appendChild(n)}},o.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.p=\\"\\",(()=>{var e={988:0};o.f.j=(r,t)=>{var a=o.o(e,r)?e[r]:void 0;if(0!==a)if(a)t.push(a[2]);else{var n=new Promise((t,o)=>{a=e[r]=[t,o]});t.push(a[2]=n);var i=o.p+o.u(r),p=new Error;o.l(i,t=>{if(o.o(e,r)&&(0!==(a=e[r])&&(e[r]=void 0),a)){var n=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;p.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+n+\\": \\"+i+\\")\\",p.name=\\"ChunkLoadError\\",p.type=n,p.request=i,a[1](p)}},\\"chunk-\\"+r)}};var r=window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[],t=r.push.bind(r);r.push=function(r){for(var t,n,i=r[0],p=r[1],s=r[3],u=0,d=[];u<i.length;u++)n=i[u],o.o(e,n)&&e[n]&&d.push(e[n][0]),e[n]=0;for(t in p)o.o(p,t)&&(o.m[t]=p[t]);for(s&&s(o),a&&a(r);d.length;)d.shift()()};var a=t})(),o.e(598).then(o.bind(o,598)).then(()=>{console.log(\\"Good\\")})})();",
"importExport.c597e72d1fd1a5b91b6d.js": "(()=>{\\"use strict\\"})();",
"js.12e2a1e4f39d61d6de1b.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
"mjs.ff069f70188e3ab5ecd4.js": "(()=>{var r={631:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(631)})();",
}
`;

Expand Down
Loading

0 comments on commit 5d2bd29

Please sign in to comment.