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' ;
310import validateOptions from 'schema-utils' ;
411
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' ;
815
916const schema = require ( './options' ) ;
1017
1118function randomIdent ( ) {
12- return " xxxHTMLLINKxxx" + Math . random ( ) + Math . random ( ) + " xxx" ;
19+ return ` xxxHTMLLINKxxx${ Math . random ( ) } ${ Math . random ( ) } xxx` ;
1320}
1421
15- export default function loader ( html ) {
16- var options = loaderUtils . getOptions ( this ) || { } ;
22+ export default function loader ( html ) {
23+ const options = loaderUtils . getOptions ( this ) || { } ;
1724
1825 validateOptions ( schema , options , 'HTML Loader' ) ;
1926
20- var attributes = [ " img:src" ] ;
27+ let attributes = [ ' img:src' ] ;
2128
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 ( ' ' ) ;
2431 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+ }
2739 }
2840
29- var root = options . root ;
41+ const { root } = options ;
3042
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 ;
3346 } ) ;
3447
3548 links . reverse ( ) ;
3649
37- var data = { } ;
50+ const data = { } ;
3851
3952 html = [ html ] ;
4053
4154 links . forEach ( ( link ) => {
42- if ( ! loaderUtils . isUrlRequest ( link . value , root ) ) return ;
55+ if ( ! loaderUtils . isUrlRequest ( link . value , root ) ) return ;
4356
44- var uri = url . parse ( link . value ) ;
57+ const uri = url . parse ( link . value ) ;
4558
4659 if ( uri . hash !== null && uri . hash !== undefined ) {
4760 uri . hash = null ;
4861
4962 link . value = uri . format ( ) ;
5063 link . length = link . value . length ;
5164 }
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 ] ) ;
5768 data [ ident ] = link . value ;
5869
59- var x = html . pop ( ) ;
70+ const item = html . pop ( ) ;
6071
61- html . push ( x . substr ( link . start + link . length ) ) ;
72+ html . push ( item . substr ( link . start + link . length ) ) ;
6273 html . push ( ident ) ;
63- html . push ( x . substr ( 0 , link . start ) ) ;
74+ html . push ( item . substr ( 0 , link . start ) ) ;
6475 } ) ;
6576
66- html . reverse ( ) ;
67-
68- html = html . join ( "" ) ;
77+ html = html . reverse ( ) . join ( '' ) ;
6978
7079 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
7282 var result ;
7383
74- var reqList = [ ] ;
84+ const requires = [ ] ;
7585
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+ } ) ;
8293 }
8394
84- reqList . reverse ( ) ;
95+ requires . reverse ( ) ;
8596
8697 html = [ html ] ;
8798
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 ) ;
90105
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 ) ) ;
98107 html . push ( ident ) ;
99- html . push ( x . substr ( 0 , link . start ) ) ;
108+ html . push ( item . substr ( 0 , link . start ) ) ;
100109 } ) ;
101110
102- html . reverse ( ) ;
103- html = html . join ( "" ) ;
111+ html = html . reverse ( ) . join ( '' ) ;
104112 }
105113
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 ) ;
108116
109117 [
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' ,
122130 ] . forEach ( ( name ) => {
123- if ( typeof minimizeOptions [ name ] === " undefined" ) {
131+ if ( typeof minimizeOptions [ name ] === ' undefined' ) {
124132 minimizeOptions [ name ] = true ;
125133 }
126134 } ) ;
@@ -129,7 +137,7 @@ export default function loader (html) {
129137 }
130138
131139 // TODO
132- // Support exporting a template function
140+ // #120 - Support exporting a template function
133141 //
134142 // import template from 'file.html'
135143 //
@@ -141,8 +149,7 @@ export default function loader (html) {
141149 }
142150
143151 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 ) ) } ')"` ;
146154 } ) } ;`;
147-
148155}
0 commit comments