1
1
/* eslint-disable
2
2
import/order,
3
3
import/first,
4
+ arrow-parens,
4
5
no-undefined,
5
6
no-param-reassign,
6
7
no-useless-escape,
@@ -15,18 +16,21 @@ import minifier from 'html-minifier';
15
16
16
17
const schema = require ( './options' ) ;
17
18
18
- function randomIdent ( ) {
19
- return `xxxHTMLLINKxxx ${ Math . random ( ) } ${ Math . random ( ) } xxx ` ;
19
+ function randomize ( ) {
20
+ return `link__ ${ Math . random ( ) } ` ;
20
21
}
21
22
22
23
export default function loader ( html ) {
23
24
const options = loaderUtils . getOptions ( this ) || { } ;
24
25
25
26
validateOptions ( schema , options , 'HTML Loader' ) ;
26
27
28
+ // eslint-disable-next-line
29
+ const root = options . root ;
30
+
27
31
let attributes = [ 'img:src' ] ;
28
32
29
- if ( options . attrs = == undefined ) {
33
+ if ( options . attrs ! == undefined ) {
30
34
if ( typeof options . attrs === 'string' ) attributes = options . attrs . split ( ' ' ) ;
31
35
else if ( Array . isArray ( options . attrs ) ) attributes = options . attrs ;
32
36
else if ( options . attrs === false ) attributes = [ ] ;
@@ -38,11 +42,12 @@ export default function loader(html) {
38
42
}
39
43
}
40
44
41
- const { root } = options ;
42
-
43
- // eslint-disable-next-line
44
45
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 ;
46
51
} ) ;
47
52
48
53
links . reverse ( ) ;
@@ -62,14 +67,15 @@ export default function loader(html) {
62
67
link . value = uri . format ( ) ;
63
68
link . length = link . value . length ;
64
69
}
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 ] ) ;
68
73
data [ ident ] = link . value ;
69
74
70
75
const item = html . pop ( ) ;
71
76
72
77
html . push ( item . substr ( link . start + link . length ) ) ;
78
+ // eslint-disable-next-line
73
79
html . push ( ident ) ;
74
80
html . push ( item . substr ( 0 , link . start ) ) ;
75
81
} ) ;
@@ -79,7 +85,7 @@ export default function loader(html) {
79
85
if ( options . interpolate === 'require' ) {
80
86
const regex = / \$ \{ r e q u i r e \( [ ^ ) ] * \) \} / g;
81
87
// eslint-disable-next-line
82
- var result ;
88
+ let result ;
83
89
84
90
const requires = [ ] ;
85
91
@@ -98,42 +104,41 @@ export default function loader(html) {
98
104
99
105
requires . forEach ( ( link ) => {
100
106
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 ] ) ;
104
110
data [ ident ] = link . value . substring ( 11 , link . length - 3 ) ;
105
111
106
112
html . push ( item . substr ( link . start + link . length ) ) ;
113
+ // eslint-disable-next-line
107
114
html . push ( ident ) ;
108
115
html . push ( item . substr ( 0 , link . start ) ) ;
109
116
} ) ;
110
117
111
118
html = html . reverse ( ) . join ( '' ) ;
112
119
}
113
120
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 ,
134
135
} ) ;
135
136
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 ) ;
137
142
}
138
143
139
144
// TODO
@@ -148,8 +153,10 @@ export default function loader(html) {
148
153
html = JSON . stringify ( html ) ;
149
154
}
150
155
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 ) => {
152
157
if ( ! data [ match ] ) return match ;
153
158
return `"require('${ JSON . stringify ( loaderUtils . urlToRequest ( data [ match ] , root ) ) } ')"` ;
154
- } ) } ;`;
159
+ } ) ;
160
+
161
+ return `export default ${ html } ;` ;
155
162
}
0 commit comments