From c760c13c5dc15429dc3d8fb476a456f4a30e498a Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Thu, 20 Aug 2020 20:31:11 +0300 Subject: [PATCH] test: `realContentHash` (#304) --- test/TerserPlugin.test.js | 49 +++++++++++++++++++ .../TerserPlugin.test.js.snap.webpack5 | 11 +++++ 2 files changed, 60 insertions(+) diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 5e63fb40..99350b2c 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -18,6 +18,7 @@ import { getCompiler, getErrors, getWarnings, + readAsset, readsAssets, removeCache, } from './helpers'; @@ -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'); + } + }); }); diff --git a/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 b/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 index ab48ade5..0454521f 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 +++ b/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 @@ -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{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{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