1
- /* eslint-disable */
2
- import loaderUtils from "loader-utils" ;
1
+ /* eslint-disable
2
+ import/order,
3
+ import/first,
4
+ no-undefined,
5
+ no-param-reassign,
6
+ no-useless-escape,
7
+ */
8
+ import LoaderError from './Error' ;
9
+ import loaderUtils from 'loader-utils' ;
3
10
import validateOptions from 'schema-utils' ;
4
11
5
- import url from " url" ;
6
- import attrs from " ./lib/attrs" ;
7
- import minifier from " html-minifier" ;
12
+ import url from ' url' ;
13
+ import attrs from ' ./lib/attrs' ;
14
+ import minifier from ' html-minifier' ;
8
15
9
16
const schema = require ( './options' ) ;
10
17
11
18
function randomIdent ( ) {
12
- return " xxxHTMLLINKxxx" + Math . random ( ) + Math . random ( ) + " xxx" ;
19
+ return ` xxxHTMLLINKxxx${ Math . random ( ) } ${ Math . random ( ) } xxx` ;
13
20
}
14
21
15
- export default function loader ( html ) {
16
- var options = loaderUtils . getOptions ( this ) || { } ;
22
+ export default function loader ( html ) {
23
+ const options = loaderUtils . getOptions ( this ) || { } ;
17
24
18
25
validateOptions ( schema , options , 'HTML Loader' ) ;
19
26
20
- var attributes = [ " img:src" ] ;
27
+ let attributes = [ ' img:src' ] ;
21
28
22
- if ( options . attrs ! == undefined ) {
23
- if ( typeof options . attrs === " string" ) attributes = options . attrs . split ( " " ) ;
29
+ if ( options . attrs = == undefined ) {
30
+ if ( typeof options . attrs === ' string' ) attributes = options . attrs . split ( ' ' ) ;
24
31
else if ( Array . isArray ( options . attrs ) ) attributes = options . attrs ;
25
- else if ( options . attrs === false ) attributes = [ ] ;
26
- else throw new Error ( "Invalid value to options parameter attrs" ) ;
32
+ else if ( options . attrs === false ) attributes = [ ] ;
33
+ else {
34
+ throw new LoaderError ( {
35
+ name : 'AttributesError' ,
36
+ message : 'Invalid attribute value found' ,
37
+ } ) ;
38
+ }
27
39
}
28
40
29
- var root = options . root ;
41
+ const { root } = options ;
30
42
31
- var links = attrs ( html , ( tag , attr ) => {
32
- return attributes . indexOf ( tag + ":" + attr ) >= 0 ;
43
+ // eslint-disable-next-line
44
+ const links = attrs ( html , ( tag , attr ) => {
45
+ return attributes . indexOf ( `${ tag } :${ attr } ` ) >= 0 ;
33
46
} ) ;
34
47
35
48
links . reverse ( ) ;
36
49
37
- var data = { } ;
50
+ const data = { } ;
38
51
39
52
html = [ html ] ;
40
53
41
54
links . forEach ( ( link ) => {
42
- if ( ! loaderUtils . isUrlRequest ( link . value , root ) ) return ;
55
+ if ( ! loaderUtils . isUrlRequest ( link . value , root ) ) return ;
43
56
44
- var uri = url . parse ( link . value ) ;
57
+ const uri = url . parse ( link . value ) ;
45
58
46
59
if ( uri . hash !== null && uri . hash !== undefined ) {
47
60
uri . hash = null ;
48
61
49
62
link . value = uri . format ( ) ;
50
63
link . length = link . value . length ;
51
64
}
52
-
53
- do {
54
- var ident = randomIdent ( ) ;
55
- } while ( data [ ident ] ) ;
56
-
65
+ // eslint-disable-next-line
66
+ var ident ;
67
+ do { ident = randomIdent ( ) ; } while ( data [ ident ] ) ;
57
68
data [ ident ] = link . value ;
58
69
59
- var x = html . pop ( ) ;
70
+ const item = html . pop ( ) ;
60
71
61
- html . push ( x . substr ( link . start + link . length ) ) ;
72
+ html . push ( item . substr ( link . start + link . length ) ) ;
62
73
html . push ( ident ) ;
63
- html . push ( x . substr ( 0 , link . start ) ) ;
74
+ html . push ( item . substr ( 0 , link . start ) ) ;
64
75
} ) ;
65
76
66
- html . reverse ( ) ;
67
-
68
- html = html . join ( "" ) ;
77
+ html = html . reverse ( ) . join ( '' ) ;
69
78
70
79
if ( options . interpolate === 'require' ) {
71
- var regex = / \$ \{ r e q u i r e \( [ ^ ) ] * \) \} / g;
80
+ const regex = / \$ \{ r e q u i r e \( [ ^ ) ] * \) \} / g;
81
+ // eslint-disable-next-line
72
82
var result ;
73
83
74
- var reqList = [ ] ;
84
+ const requires = [ ] ;
75
85
76
- while ( result = regex . exec ( html ) ) {
77
- reqList . push ( {
78
- length : result [ 0 ] . length ,
79
- start : result . index ,
80
- value : result [ 0 ]
81
- } )
86
+ // eslint-disable-next-line
87
+ while ( result = regex . exec ( html ) ) {
88
+ requires . push ( {
89
+ length : result [ 0 ] . length ,
90
+ start : result . index ,
91
+ value : result [ 0 ] ,
92
+ } ) ;
82
93
}
83
94
84
- reqList . reverse ( ) ;
95
+ requires . reverse ( ) ;
85
96
86
97
html = [ html ] ;
87
98
88
- reqList . forEach ( ( link ) => {
89
- var x = html . pop ( ) ;
99
+ requires . forEach ( ( link ) => {
100
+ const item = html . pop ( ) ;
101
+ // eslint-disable-next-line
102
+ var ident
103
+ do { ident = randomIdent ( ) ; } while ( data [ ident ] ) ;
104
+ data [ ident ] = link . value . substring ( 11 , link . length - 3 ) ;
90
105
91
- do {
92
- var ident = randomIdent ( ) ;
93
- } while ( data [ ident ] ) ;
94
-
95
- data [ ident ] = link . value . substring ( 11 , link . length - 3 )
96
-
97
- html . push ( x . substr ( link . start + link . length ) ) ;
106
+ html . push ( item . substr ( link . start + link . length ) ) ;
98
107
html . push ( ident ) ;
99
- html . push ( x . substr ( 0 , link . start ) ) ;
108
+ html . push ( item . substr ( 0 , link . start ) ) ;
100
109
} ) ;
101
110
102
- html . reverse ( ) ;
103
- html = html . join ( "" ) ;
111
+ html = html . reverse ( ) . join ( '' ) ;
104
112
}
105
113
106
- if ( typeof options . minimize === " boolean" ? options . minimize : this . minimize ) {
107
- var minimizeOptions = Object . assign ( { } , options ) ;
114
+ if ( typeof options . minimize === ' boolean' ? options . minimize : this . minimize ) {
115
+ const minimizeOptions = Object . assign ( { } , options ) ;
108
116
109
117
[
110
- " removeComments" ,
111
- " removeCommentsFromCDATA" ,
112
- " removeCDATASectionsFromCDATA" ,
113
- " collapseWhitespace" ,
114
- " conservativeCollapse" ,
115
- " removeAttributeQuotes" ,
116
- " useShortDoctype" ,
117
- " keepClosingSlash" ,
118
- " minifyJS" ,
119
- " minifyCSS" ,
120
- " removeScriptTypeAttributes" ,
121
- " removeStyleTypeAttributes" ,
118
+ ' removeComments' ,
119
+ ' removeCommentsFromCDATA' ,
120
+ ' removeCDATASectionsFromCDATA' ,
121
+ ' collapseWhitespace' ,
122
+ ' conservativeCollapse' ,
123
+ ' removeAttributeQuotes' ,
124
+ ' useShortDoctype' ,
125
+ ' keepClosingSlash' ,
126
+ ' minifyJS' ,
127
+ ' minifyCSS' ,
128
+ ' removeScriptTypeAttributes' ,
129
+ ' removeStyleTypeAttributes' ,
122
130
] . forEach ( ( name ) => {
123
- if ( typeof minimizeOptions [ name ] === " undefined" ) {
131
+ if ( typeof minimizeOptions [ name ] === ' undefined' ) {
124
132
minimizeOptions [ name ] = true ;
125
133
}
126
134
} ) ;
@@ -129,7 +137,7 @@ export default function loader (html) {
129
137
}
130
138
131
139
// TODO
132
- // Support exporting a template function
140
+ // #120 - Support exporting a template function
133
141
//
134
142
// import template from 'file.html'
135
143
//
@@ -141,8 +149,7 @@ export default function loader (html) {
141
149
}
142
150
143
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 ) => {
144
- if ( ! data [ match ] ) return match ;
145
- return '" + require(' + JSON . stringify ( loaderUtils . urlToRequest ( data [ match ] , root ) ) + ') + "' ;
152
+ if ( ! data [ match ] ) return match ;
153
+ return `" require('${ JSON . stringify ( loaderUtils . urlToRequest ( data [ match ] , root ) ) } ')"` ;
146
154
} ) } ;`;
147
-
148
155
}
0 commit comments