@@ -2,6 +2,7 @@ import fs from 'fs';
2
2
import path from 'path' ;
3
3
import webpack from 'webpack' ;
4
4
import sources from 'webpack-sources' ;
5
+ import loaderUtils from 'loader-utils' ;
5
6
6
7
const { ConcatSource, SourceMapSource, OriginalSource } = sources ;
7
8
const { Template } = webpack ;
@@ -95,6 +96,19 @@ class MiniCssExtractPlugin {
95
96
}
96
97
97
98
apply ( compiler ) {
99
+ // add contenthash support
100
+ compiler . hooks . emit . tap ( pluginName , ( compilation ) => {
101
+ const regexp = / \[ (?: ( \w + ) : ) ? c o n t e n t h a s h (?: : ( [ 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
+ } ) ;
98
112
compiler . hooks . thisCompilation . tap ( pluginName , ( compilation ) => {
99
113
compilation . hooks . normalModuleLoader . tap ( pluginName , ( lc , m ) => {
100
114
const loaderContext = lc ;
0 commit comments