Skip to content

Commit c760c13

Browse files
authoredAug 20, 2020
test: realContentHash (#304)
1 parent 479d28b commit c760c13

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
 

‎test/TerserPlugin.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getCompiler,
1919
getErrors,
2020
getWarnings,
21+
readAsset,
2122
readsAssets,
2223
removeCache,
2324
} from './helpers';
@@ -843,4 +844,52 @@ describe('TerserPlugin', () => {
843844
expect(getErrors(stats)).toMatchSnapshot('errors');
844845
expect(getWarnings(stats)).toMatchSnapshot('warnings');
845846
});
847+
848+
it('should work and generate real content hash', async () => {
849+
if (getCompiler.isWebpack4()) {
850+
expect(true).toBe(true);
851+
} else {
852+
const compiler = getCompiler({
853+
entry: {
854+
app: path.resolve(__dirname, './fixtures/async-import-export/entry'),
855+
},
856+
output: {
857+
pathinfo: false,
858+
path: path.resolve(__dirname, 'dist'),
859+
filename: '[name].[contenthash].[chunkhash].[fullhash].js',
860+
chunkFilename: '[name].[contenthash].[chunkhash].[fullhash].js',
861+
},
862+
optimization: {
863+
minimize: false,
864+
realContentHash: true,
865+
},
866+
});
867+
868+
new TerserPlugin().apply(compiler);
869+
870+
const stats = await compile(compiler);
871+
const {
872+
compilation: {
873+
assets,
874+
options: { output },
875+
},
876+
} = stats;
877+
878+
for (const assetName of Object.keys(assets)) {
879+
const [, webpackHash] = assetName.match(/^.+?\.(.+?)\..+$/);
880+
const { hashDigestLength, hashDigest, hashFunction } = output;
881+
const cryptoHash = crypto
882+
.createHash(hashFunction)
883+
.update(readAsset(assetName, compiler, stats))
884+
.digest(hashDigest)
885+
.slice(0, hashDigestLength);
886+
887+
expect(webpackHash).toBe(cryptoHash);
888+
}
889+
890+
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
891+
expect(getErrors(stats)).toMatchSnapshot('errors');
892+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
893+
}
894+
});
846895
});

‎test/__snapshots__/TerserPlugin.test.js.snap.webpack5

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ exports[`TerserPlugin should work (without options): errors 1`] = `Array []`;
9898

9999
exports[`TerserPlugin should work (without options): warnings 1`] = `Array []`;
100100

101+
exports[`TerserPlugin should work and generate real content hash: assets 1`] = `
102+
Object {
103+
"598.ce0b7cde053504e39acf.6deaf1ba7f53eff889b0.d40767e553c518550dec.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\\"}}]);",
104+
"app.d1f36e00a3e46fd652ec.11a095fd3ecd51f2f6b2.d40767e553c518550dec.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function n(e){if(o[e])return o[e].exports;var r=o[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.m=t,n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.f={},n.e=e=>Promise.all(Object.keys(n.f).reduce((r,t)=>(n.f[t](e,r),r),[])),n.u=e=>e+\\".ce0b7cde053504e39acf.6deaf1ba7f53eff889b0.\\"+n.h()+\\".js\\",n.h=()=>\\"d40767e553c518550dec\\",n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",n.l=(t,o,a)=>{if(e[t])e[t].push(o);else{var i,u;if(void 0!==a)for(var d=document.getElementsByTagName(\\"script\\"),l=0;l<d.length;l++){var s=d[l];if(s.getAttribute(\\"src\\")==t||s.getAttribute(\\"data-webpack\\")==r+a){i=s;break}}i||(u=!0,(i=document.createElement(\\"script\\")).charset=\\"utf-8\\",i.timeout=120,n.nc&&i.setAttribute(\\"nonce\\",n.nc),i.setAttribute(\\"data-webpack\\",r+a),i.src=t),e[t]=[o];var p=(r,o)=>{i.onerror=i.onload=null,clearTimeout(c);var n=e[t];if(delete e[t],i.parentNode&&i.parentNode.removeChild(i),n&&n.forEach(e=>e(o)),r)return r(o)},c=setTimeout(p.bind(null,void 0,{type:\\"timeout\\",target:i}),12e4);i.onerror=p.bind(null,i.onerror),i.onload=p.bind(null,i.onload),u&&document.head.appendChild(i)}},n.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.p=\\"\\",(()=>{var e={143:0};n.f.j=(r,t)=>{var o=n.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var a=new Promise((t,n)=>{o=e[r]=[t,n]});t.push(o[2]=a);var i=n.p+n.u(r),u=new Error;n.l(i,t=>{if(n.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var a=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;u.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+a+\\": \\"+i+\\")\\",u.name=\\"ChunkLoadError\\",u.type=a,u.request=i,o[1](u)}},\\"chunk-\\"+r)}};var r=window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[],t=r.push.bind(r);r.push=function(r){for(var t,a,i=r[0],u=r[1],d=r[3],l=0,s=[];l<i.length;l++)a=i[l],n.o(e,a)&&e[a]&&s.push(e[a][0]),e[a]=0;for(t in u)n.o(u,t)&&(n.m[t]=u[t]);for(d&&d(n),o&&o(r);s.length;)s.shift()()};var o=t})(),n.e(598).then(n.bind(n,598)).then(()=>{console.log(\\"Good\\")})})();",
105+
}
106+
`;
107+
108+
exports[`TerserPlugin should work and generate real content hash: errors 1`] = `Array []`;
109+
110+
exports[`TerserPlugin should work and generate real content hash: warnings 1`] = `Array []`;
111+
101112
exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "false"): errors 1`] = `
102113
Array [
103114
"Error: main.js from Terser

0 commit comments

Comments
 (0)
Please sign in to comment.