From ba17060e1623f7771fa55d1d9aab542e7425ce0a Mon Sep 17 00:00:00 2001 From: chenxsan Date: Fri, 9 Mar 2018 12:43:45 +0800 Subject: [PATCH 1/3] add contenthash support --- src/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.js b/src/index.js index 8449af47..4de41376 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; import webpack from 'webpack'; import sources from 'webpack-sources'; +import loaderUtils from 'loader-utils'; const { ConcatSource, SourceMapSource, OriginalSource } = sources; const { Template } = webpack; @@ -95,6 +96,19 @@ class MiniCssExtractPlugin { } apply(compiler) { + // add contenthash support + compiler.hooks.emit.tap(pluginName, (compilation) => { + const regexp = /\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig; + Object.keys(compilation.assets).forEach((filename) => { + if (regexp.test(filename)) { + const source = compilation.assets[filename].source(); + const getHashDigest = (...args) => loaderUtils.getHashDigest(source, args[1], args[2], parseInt(args[3], 10)); + const newFilename = filename.replace(regexp, getHashDigest); + compilation.assets[newFilename] = compilation.assets[filename]; // eslint-disable-line no-param-reassign + delete compilation.assets[filename]; // eslint-disable-line no-param-reassign + } + }); + }); compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => { const loaderContext = lc; From bc8fc816bd1f0aefda8c5dc7320383e2f3fb26fb Mon Sep 17 00:00:00 2001 From: chenxsan Date: Fri, 9 Mar 2018 12:43:53 +0800 Subject: [PATCH 2/3] add test --- .../main.b90cb67ebad35ad4d10bd6ba71a70638.css | 2 ++ test/cases/simple-contenthash/index.js | 1 + test/cases/simple-contenthash/style.css | 1 + .../simple-contenthash/webpack.config.js | 21 +++++++++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 test/cases/simple-contenthash/expected/main.b90cb67ebad35ad4d10bd6ba71a70638.css create mode 100644 test/cases/simple-contenthash/index.js create mode 100644 test/cases/simple-contenthash/style.css create mode 100644 test/cases/simple-contenthash/webpack.config.js diff --git a/test/cases/simple-contenthash/expected/main.b90cb67ebad35ad4d10bd6ba71a70638.css b/test/cases/simple-contenthash/expected/main.b90cb67ebad35ad4d10bd6ba71a70638.css new file mode 100644 index 00000000..aea53e43 --- /dev/null +++ b/test/cases/simple-contenthash/expected/main.b90cb67ebad35ad4d10bd6ba71a70638.css @@ -0,0 +1,2 @@ +body { background: red; } + diff --git a/test/cases/simple-contenthash/index.js b/test/cases/simple-contenthash/index.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/simple-contenthash/index.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/simple-contenthash/style.css b/test/cases/simple-contenthash/style.css new file mode 100644 index 00000000..31fc5b8a --- /dev/null +++ b/test/cases/simple-contenthash/style.css @@ -0,0 +1 @@ +body { background: red; } diff --git a/test/cases/simple-contenthash/webpack.config.js b/test/cases/simple-contenthash/webpack.config.js new file mode 100644 index 00000000..eed25f76 --- /dev/null +++ b/test/cases/simple-contenthash/webpack.config.js @@ -0,0 +1,21 @@ +const Self = require('../../../'); + +module.exports = { + entry: './index.js', + module: { + rules: [ + { + test: /\.css$/, + use: [ + Self.loader, + 'css-loader', + ], + }, + ], + }, + plugins: [ + new Self({ + filename: '[name].[contenthash].css', + }), + ], +}; From db8d63a33125fb9902aa34053a198b7018e59e05 Mon Sep 17 00:00:00 2001 From: chenxsan Date: Fri, 9 Mar 2018 20:19:52 +0800 Subject: [PATCH 3/3] add comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dc02a7f2..9b71fa86 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ module.exports = { new MiniCssExtractPlugin({ // Options similar to the same options in webpackOptions.output // both options are optional - filename: "[name].css", + filename: "[name].css", // [contenthash] is supported chunkFilename: "[id].css" }) ],