Skip to content

Commit ba17060

Browse files
committed
add contenthash support
1 parent e062cd2 commit ba17060

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import webpack from 'webpack';
44
import sources from 'webpack-sources';
5+
import loaderUtils from 'loader-utils';
56

67
const { ConcatSource, SourceMapSource, OriginalSource } = sources;
78
const { Template } = webpack;
@@ -95,6 +96,19 @@ class MiniCssExtractPlugin {
9596
}
9697

9798
apply(compiler) {
99+
// add contenthash support
100+
compiler.hooks.emit.tap(pluginName, (compilation) => {
101+
const regexp = /\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig;
102+
Object.keys(compilation.assets).forEach((filename) => {
103+
if (regexp.test(filename)) {
104+
const source = compilation.assets[filename].source();
105+
const getHashDigest = (...args) => loaderUtils.getHashDigest(source, args[1], args[2], parseInt(args[3], 10));
106+
const newFilename = filename.replace(regexp, getHashDigest);
107+
compilation.assets[newFilename] = compilation.assets[filename]; // eslint-disable-line no-param-reassign
108+
delete compilation.assets[filename]; // eslint-disable-line no-param-reassign
109+
}
110+
});
111+
});
98112
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
99113
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
100114
const loaderContext = lc;

0 commit comments

Comments
 (0)