|
1 | 1 | import path from 'path';
|
| 2 | +import crypto from 'crypto'; |
2 | 3 |
|
3 | 4 | import { SourceMapConsumer } from 'source-map';
|
4 | 5 | import MiniCssExtractPlugin from 'mini-css-extract-plugin';
|
@@ -390,4 +391,58 @@ describe('CssMinimizerPlugin', () => {
|
390 | 391 | expect(getErrors(stats)).toMatchSnapshot('errors');
|
391 | 392 | expect(getWarnings(stats)).toMatchSnapshot('warnings');
|
392 | 393 | });
|
| 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 | + }); |
393 | 448 | });
|
0 commit comments