11/* eslint-disable
22 import/order,
33 import/first,
4+ arrow-parens,
45 no-undefined,
56 no-param-reassign,
67 no-useless-escape,
@@ -15,18 +16,21 @@ import minifier from 'html-minifier';
1516
1617const schema = require ( './options' ) ;
1718
18- function randomIdent ( ) {
19- return `xxxHTMLLINKxxx ${ Math . random ( ) } ${ Math . random ( ) } xxx ` ;
19+ function randomize ( ) {
20+ return `link__ ${ Math . random ( ) } ` ;
2021}
2122
2223export default function loader ( html ) {
2324 const options = loaderUtils . getOptions ( this ) || { } ;
2425
2526 validateOptions ( schema , options , 'HTML Loader' ) ;
2627
28+ // eslint-disable-next-line
29+ const root = options . root ;
30+
2731 let attributes = [ 'img:src' ] ;
2832
29- if ( options . attrs = == undefined ) {
33+ if ( options . attrs ! == undefined ) {
3034 if ( typeof options . attrs === 'string' ) attributes = options . attrs . split ( ' ' ) ;
3135 else if ( Array . isArray ( options . attrs ) ) attributes = options . attrs ;
3236 else if ( options . attrs === false ) attributes = [ ] ;
@@ -38,11 +42,12 @@ export default function loader(html) {
3842 }
3943 }
4044
41- const { root } = options ;
42-
43- // eslint-disable-next-line
4445 const links = attrs ( html , ( tag , attr ) => {
45- return attributes . indexOf ( `${ tag } :${ attr } ` ) >= 0 ;
46+ const item = `${ tag } :${ attr } ` ;
47+
48+ const result = attributes . find ( ( a ) => item . indexOf ( a ) >= 0 ) ;
49+
50+ return ! ! result ;
4651 } ) ;
4752
4853 links . reverse ( ) ;
@@ -62,14 +67,15 @@ export default function loader(html) {
6267 link . value = uri . format ( ) ;
6368 link . length = link . value . length ;
6469 }
65- // eslint-disable-next-line
66- var ident ;
67- do { ident = randomIdent ( ) ; } while ( data [ ident ] ) ;
70+
71+ let ident ;
72+ do { ident = randomize ( ) ; } while ( data [ ident ] ) ;
6873 data [ ident ] = link . value ;
6974
7075 const item = html . pop ( ) ;
7176
7277 html . push ( item . substr ( link . start + link . length ) ) ;
78+ // eslint-disable-next-line
7379 html . push ( ident ) ;
7480 html . push ( item . substr ( 0 , link . start ) ) ;
7581 } ) ;
@@ -79,7 +85,7 @@ export default function loader(html) {
7985 if ( options . interpolate === 'require' ) {
8086 const regex = / \$ \{ r e q u i r e \( [ ^ ) ] * \) \} / g;
8187 // eslint-disable-next-line
82- var result ;
88+ let result ;
8389
8490 const requires = [ ] ;
8591
@@ -98,42 +104,41 @@ export default function loader(html) {
98104
99105 requires . forEach ( ( link ) => {
100106 const item = html . pop ( ) ;
101- // eslint-disable-next-line
102- var ident
103- do { ident = randomIdent ( ) ; } while ( data [ ident ] ) ;
107+
108+ let ident ;
109+ do { ident = randomize ( ) ; } while ( data [ ident ] ) ;
104110 data [ ident ] = link . value . substring ( 11 , link . length - 3 ) ;
105111
106112 html . push ( item . substr ( link . start + link . length ) ) ;
113+ // eslint-disable-next-line
107114 html . push ( ident ) ;
108115 html . push ( item . substr ( 0 , link . start ) ) ;
109116 } ) ;
110117
111118 html = html . reverse ( ) . join ( '' ) ;
112119 }
113120
114- if ( typeof options . minimize === 'boolean' ? options . minimize : this . minimize ) {
115- const minimizeOptions = Object . assign ( { } , options ) ;
116-
117- [
118- 'removeComments' ,
119- 'removeCommentsFromCDATA' ,
120- 'removeCDATASectionsFromCDATA' ,
121- 'collapseWhitespace' ,
122- 'conservativeCollapse' ,
123- 'removeAttributeQuotes' ,
124- 'useShortDoctype' ,
125- 'keepClosingSlash' ,
126- 'minifyJS' ,
127- 'minifyCSS' ,
128- 'removeScriptTypeAttributes' ,
129- 'removeStyleTypeAttributes' ,
130- ] . forEach ( ( name ) => {
131- if ( typeof minimizeOptions [ name ] === 'undefined' ) {
132- minimizeOptions [ name ] = true ;
133- }
121+ if ( options . minimize || this . minimize ) {
122+ let minimize = Object . create ( {
123+ collapseWhitespace : true ,
124+ conservativeCollapse : true ,
125+ useShortDoctype : true ,
126+ keepClosingSlash : true ,
127+ minifyJS : true ,
128+ minifyCSS : true ,
129+ removeComments : true ,
130+ removeAttributeQuotes : true ,
131+ removeStyleTypeAttributes : true ,
132+ removeScriptTypeAttributes : true ,
133+ removeCommentsFromCDATA : true ,
134+ removeCDATASectionsFromCDATA : true ,
134135 } ) ;
135136
136- html = minifier . minify ( html , minimizeOptions ) ;
137+ if ( typeof options . minimize === 'object' ) {
138+ minimize = Object . assign ( minimize , options . minimize ) ;
139+ }
140+
141+ html = minifier . minify ( html , minimize ) ;
137142 }
138143
139144 // TODO
@@ -148,8 +153,10 @@ export default function loader(html) {
148153 html = JSON . stringify ( html ) ;
149154 }
150155
151- return `export default ${ html . replace ( / x x x H T M L L I N K x x x [ 0 - 9 \. ] + x x x / g, ( match ) => {
156+ html = html . replace ( / l i n k _ _ [ 0 - 9 \. ] + / g, ( match ) => {
152157 if ( ! data [ match ] ) return match ;
153158 return `"require('${ JSON . stringify ( loaderUtils . urlToRequest ( data [ match ] , root ) ) } ')"` ;
154- } ) } ;`;
159+ } ) ;
160+
161+ return `export default ${ html } ;` ;
155162}
0 commit comments