This repository was archived by the owner on Mar 17, 2021. It is now read-only.
File tree 2 files changed +42
-10
lines changed
2 files changed +42
-10
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,14 @@ export default function loader(content) {
14
14
} ) ;
15
15
16
16
const context = options . context || this . rootContext ;
17
+ const name = options . name || '[contenthash].[ext]' ;
18
+ const immutable = / \[ ( [ ^ : \] ] + : ) ? ( h a s h | c o n t e n t h a s h ) ( : [ ^ \] ] + ) ? \] / gi. test ( name ) ;
17
19
18
- const url = loaderUtils . interpolateName (
19
- this ,
20
- options . name || '[contenthash].[ext]' ,
21
- {
22
- context,
23
- content,
24
- regExp : options . regExp ,
25
- }
26
- ) ;
20
+ const url = loaderUtils . interpolateName ( this , name , {
21
+ context,
22
+ content,
23
+ regExp : options . regExp ,
24
+ } ) ;
27
25
28
26
let outputPath = url ;
29
27
@@ -56,7 +54,7 @@ export default function loader(content) {
56
54
}
57
55
58
56
if ( typeof options . emitFile === 'undefined' || options . emitFile ) {
59
- this . emitFile ( outputPath , content ) ;
57
+ this . emitFile ( outputPath , content , null , { immutable } ) ;
60
58
}
61
59
62
60
const esModule =
Original file line number Diff line number Diff line change @@ -82,4 +82,38 @@ describe('"name" option', () => {
82
82
) ;
83
83
expect ( normalizeErrors ( stats . compilation . errors ) ) . toMatchSnapshot ( 'errors' ) ;
84
84
} ) ;
85
+
86
+ it ( 'should mark hashed asset as immutable' , async ( ) => {
87
+ const compiler = getCompiler ( 'simple.js' , {
88
+ name : '[md5:hash:hex:8].asset.[ext]' ,
89
+ } ) ;
90
+ const stats = await compile ( compiler ) ;
91
+
92
+ let assetInfo ;
93
+ for ( const [ name , info ] of stats . compilation . assetsInfo ) {
94
+ if ( name . match ( 'asset.' ) ) {
95
+ assetInfo = info ;
96
+ break ;
97
+ }
98
+ }
99
+
100
+ expect ( assetInfo . immutable ) . toBe ( true ) ;
101
+ } ) ;
102
+
103
+ it ( 'should not mark unhashed asset as immutable' , async ( ) => {
104
+ const compiler = getCompiler ( 'simple.js' , {
105
+ name : 'asset.[ext]' ,
106
+ } ) ;
107
+ const stats = await compile ( compiler ) ;
108
+
109
+ let assetInfo ;
110
+ for ( const [ name , info ] of stats . compilation . assetsInfo ) {
111
+ if ( name . match ( 'asset.' ) ) {
112
+ assetInfo = info ;
113
+ break ;
114
+ }
115
+ }
116
+
117
+ expect ( assetInfo . immutable ) . toBe ( false ) ;
118
+ } ) ;
85
119
} ) ;
You can’t perform that action at this time.
0 commit comments