@@ -55,27 +55,32 @@ module.exports = {
5555 } ,
5656 {
5757 test : / \. ( g i f | p n g | s v g | t t f ) $ / ,
58- loader : 'file-loader' ,
59- options : {
60- // Use a content-based hash in the name so that we can set a
61- // long cache lifetime for assets while still delivering
62- // changes quickly.
63- name : '[name].[hash:7].[ext]' ,
64- outputPath : function ( url , resourcePath , context ) {
65- // Merge assets found via CSS and imports into a single
66- // tree, while also preserving directories under **/res.
67- const prefix = / ^ .* \/ r e s \/ / ;
68- const outputDir = path . dirname ( resourcePath ) . replace ( prefix , "" ) ;
69- return path . join ( outputDir , path . basename ( url ) ) ;
58+ // Use a content-based hash in the name so that we can set a long cache
59+ // lifetime for assets while still delivering changes quickly.
60+ oneOf : [
61+ {
62+ // Images referenced in HTML files
63+ issuer : / \. h t m l $ / ,
64+ loader : 'file-loader' ,
65+ options : {
66+ name : '[name].[hash:7].[ext]' ,
67+ outputPath : getImgOutputPath ,
68+ } ,
7069 } ,
71- publicPath : function ( url , resourcePath , context ) {
72- // Merge assets found via CSS and imports into a single
73- // tree, while also preserving directories under **/res.
74- const prefix = / ^ .* \/ r e s \/ / ;
75- const outputDir = path . dirname ( resourcePath ) . replace ( prefix , "" ) ;
76- return path . join ( "../.." , outputDir , path . basename ( url ) ) ;
70+ {
71+ // Images referenced in JS and CSS files
72+ loader : 'file-loader' ,
73+ options : {
74+ name : '[name].[hash:7].[ext]' ,
75+ outputPath : getImgOutputPath ,
76+ publicPath : function ( url , resourcePath ) {
77+ // JS and CSS image usages end up the `bundles/[hash]` output
78+ // directory, so we adjust the final path to navigate up twice.
79+ return path . join ( "../.." , getImgOutputPath ( url , resourcePath ) ) ;
80+ } ,
81+ } ,
7782 } ,
78- } ,
83+ ] ,
7984 } ,
8085 ] ,
8186 noParse : [
@@ -178,3 +183,17 @@ module.exports = {
178183 inline : false ,
179184 } ,
180185} ;
186+
187+ /**
188+ * Merge assets found via CSS and imports into a single tree, while also preserving
189+ * directories under `res`.
190+ *
191+ * @param {string } url The adjusted name of the file, such as `warning.1234567.svg`.
192+ * @param {string } resourcePath The absolute path to the source file with unmodified name.
193+ * @return {string } The returned paths will look like `img/warning.1234567.svg`.
194+ */
195+ function getImgOutputPath ( url , resourcePath ) {
196+ const prefix = / ^ .* \/ r e s \/ / ;
197+ const outputDir = path . dirname ( resourcePath ) . replace ( prefix , "" ) ;
198+ return path . join ( outputDir , path . basename ( url ) ) ;
199+ }
0 commit comments