Skip to content

Commit bfb75a7

Browse files
test: realContentHash (#24)
1 parent 6b00c65 commit bfb75a7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

test/CssMinimizerPlugin.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
import crypto from 'crypto';
23

34
import { SourceMapConsumer } from 'source-map';
45
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
@@ -390,4 +391,58 @@ describe('CssMinimizerPlugin', () => {
390391
expect(getErrors(stats)).toMatchSnapshot('errors');
391392
expect(getWarnings(stats)).toMatchSnapshot('warnings');
392393
});
394+
395+
it('should work and generate real content hash', async () => {
396+
if (getCompiler.isWebpack4()) {
397+
expect(true).toBe(true);
398+
} else {
399+
const compiler = getCompiler({
400+
entry: {
401+
entry: `${__dirname}/fixtures/entry.js`,
402+
},
403+
output: {
404+
pathinfo: false,
405+
path: path.resolve(__dirname, 'dist'),
406+
filename: '[name].[contenthash].[chunkhash].[fullhash].js',
407+
chunkFilename: '[name].[contenthash].[chunkhash].[fullhash].js',
408+
},
409+
optimization: {
410+
minimize: false,
411+
realContentHash: true,
412+
},
413+
plugins: [
414+
new MiniCssExtractPlugin({
415+
filename: '[name].[contenthash].[chunkhash].[fullhash].css',
416+
chunkFilename: '[name].[contenthash].[chunkhash].[fullhash].css',
417+
}),
418+
],
419+
});
420+
421+
new CssMinimizerPlugin().apply(compiler);
422+
423+
const stats = await compile(compiler);
424+
const {
425+
compilation: {
426+
assets,
427+
options: { output },
428+
},
429+
} = stats;
430+
431+
for (const assetName of Object.keys(assets)) {
432+
const [, webpackHash] = assetName.match(/^.+?\.(.+?)\..+$/);
433+
const { hashDigestLength, hashDigest, hashFunction } = output;
434+
const cryptoHash = crypto
435+
.createHash(hashFunction)
436+
.update(readAsset(assetName, compiler, stats))
437+
.digest(hashDigest)
438+
.slice(0, hashDigestLength);
439+
440+
expect(webpackHash).toBe(cryptoHash);
441+
}
442+
443+
expect(readAssets(compiler, stats, '.css')).toMatchSnapshot('assets');
444+
expect(getErrors(stats)).toMatchSnapshot('errors');
445+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
446+
}
447+
});
393448
});

test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ exports[`CssMinimizerPlugin should respect the hash options #1: errors 1`] = `Ar
5959

6060
exports[`CssMinimizerPlugin should respect the hash options #1: warnings 1`] = `Array []`;
6161

62+
exports[`CssMinimizerPlugin should work and generate real content hash: assets 1`] = `
63+
Object {
64+
"entry.19e4764f9c1d9fe130e2.655a1d8e7614dda9272e.1afdccbeaec4b0402d74.css": "body{color:red}a{color:#00f}",
65+
}
66+
`;
67+
68+
exports[`CssMinimizerPlugin should work and generate real content hash: errors 1`] = `Array []`;
69+
70+
exports[`CssMinimizerPlugin should work and generate real content hash: warnings 1`] = `Array []`;
71+
6272
exports[`CssMinimizerPlugin should work and show minimized assets in stats: assets 1`] = `
6373
Object {
6474
"foo.css": "body{color:red}a{color:#00f}",

0 commit comments

Comments
 (0)