Skip to content

Commit

Permalink
test: realContentHash (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 20, 2020
1 parent 479d28b commit c760c13
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/TerserPlugin.test.js
Expand Up @@ -18,6 +18,7 @@ import {
getCompiler,
getErrors,
getWarnings,
readAsset,
readsAssets,
removeCache,
} from './helpers';
Expand Down Expand Up @@ -843,4 +844,52 @@ describe('TerserPlugin', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work and generate real content hash', async () => {
if (getCompiler.isWebpack4()) {
expect(true).toBe(true);
} else {
const compiler = getCompiler({
entry: {
app: path.resolve(__dirname, './fixtures/async-import-export/entry'),
},
output: {
pathinfo: false,
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].[chunkhash].[fullhash].js',
chunkFilename: '[name].[contenthash].[chunkhash].[fullhash].js',
},
optimization: {
minimize: false,
realContentHash: true,
},
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);
const {
compilation: {
assets,
options: { output },
},
} = stats;

for (const assetName of Object.keys(assets)) {
const [, webpackHash] = assetName.match(/^.+?\.(.+?)\..+$/);
const { hashDigestLength, hashDigest, hashFunction } = output;
const cryptoHash = crypto
.createHash(hashFunction)
.update(readAsset(assetName, compiler, stats))
.digest(hashDigest)
.slice(0, hashDigestLength);

expect(webpackHash).toBe(cryptoHash);
}

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
}
});
});
11 changes: 11 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Expand Up @@ -98,6 +98,17 @@ exports[`TerserPlugin should work (without options): errors 1`] = `Array []`;

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

exports[`TerserPlugin should work and generate real content hash: assets 1`] = `
Object {
"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\\"}}]);",
"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\\")})})();",
}
`;

exports[`TerserPlugin should work and generate real content hash: errors 1`] = `Array []`;

exports[`TerserPlugin should work and generate real content hash: warnings 1`] = `Array []`;

exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "false"): errors 1`] = `
Array [
"Error: main.js from Terser
Expand Down

0 comments on commit c760c13

Please sign in to comment.