@@ -250,40 +250,63 @@ HtmlResWebpackPlugin.prototype.buildStats = function(compilation) {
250
250
HtmlResWebpackPlugin . prototype . processAssets = function ( compilation ) {
251
251
var htmlContent = compilation . assets [ this . options . htmlFileName ] . source ( ) ,
252
252
publicPath = this . webpackOptions . output . publicPath ;
253
-
254
- // console.log(this.stats.assets);
255
- // console.log(htmlContent);
256
253
257
- // css inline
258
- let styleInlineRegex = new RegExp ( "<link.*href=[\"|\']*(.+)[\?]\_\_inline.*?[\"|\']>" , "ig" ) ;
259
- htmlContent = this . inlineHtmlRes ( htmlContent , styleInlineRegex , compilation , 'css' ) ;
260
-
261
- // js liline
262
- let scriptInlineRegex = new RegExp ( "<script.*src=[\"|\']*(.+)[\?]\_\_inline.*?[\"|\']><\/script>" , "ig" ) ;
263
- htmlContent = this . inlineHtmlRes ( htmlContent , scriptInlineRegex , compilation , 'js' ) ;
264
-
265
- // css
266
- let styleMd5Regex = new RegExp ( "<link.*href=[\"|\']*(.+).*?[\"|\']>" , "ig" ) ;
267
- let cssPublicPath = this . options . cssPublicPath || publicPath ;
268
- htmlContent = this . md5HtmlRes ( htmlContent , styleMd5Regex , cssPublicPath , "css" ) ;
269
-
270
- // favico
271
- htmlContent = this . md5HtmlRes ( htmlContent , styleMd5Regex , publicPath , "ico" ) ;
272
-
273
- // js
274
- let scriptMd5Regex = new RegExp ( "<script.*src=[\"|\']*(.+).*?[\"|\']><\/script>" , "ig" ) ;
275
- htmlContent = this . md5HtmlRes ( htmlContent , scriptMd5Regex , publicPath , "js" ) ;
254
+ htmlContent = this . checkResource ( htmlContent , publicPath , compilation ) ;
276
255
277
256
compilation . assets [ this . options . htmlFileName ] . source = ( ) => {
278
257
return this . options . templateContent . bind ( this ) ( htmlContent ) ;
279
258
} ;
280
259
} ;
281
260
282
- HtmlResWebpackPlugin . prototype . md5HtmlRes = function ( htmlContent , reg , publicPath , extension ) {
261
+ HtmlResWebpackPlugin . prototype . checkResource = function ( htmlContent , publicPath , compilation ) {
262
+ let linkRegex = new RegExp ( "(<link[^>]*href=([\'\"]*)(.*?)([\'\"]*).*?\>)" , "ig" ) ,
263
+ scriptRegex = new RegExp ( "(<script[^>]*src=([\'\"]*)(.*?)([\'\"]*).*?\>(<\/script>)?)" , "ig" ) ;
264
+
265
+ htmlContent = htmlContent . replace ( linkRegex , ( route ) => {
266
+ if ( ! ! ~ route . indexOf ( "__inline" ) ) {
267
+ // css inline
268
+ let styleInlineRegex = new RegExp ( "<link.*href=(\s*?)*(.+)[\?]\_\_inline.*?(\s*?)>" , "ig" ) ;
269
+ route = this . inlineHtmlRes ( route , styleInlineRegex , compilation , 'css' ) ;
270
+ }
271
+ else {
272
+ // css md5
273
+ let styleMd5Regex = new RegExp ( "<link.*href=(\s*?)*(.+).*?(\s*?)>" , "ig" ) ;
274
+ let cssPublicPath = this . options . cssPublicPath || publicPath ;
275
+ route = this . md5HtmlRes ( route , styleMd5Regex , cssPublicPath , "css" ) ;
276
+
277
+ route = this . md5HtmlRes ( route , styleMd5Regex , publicPath , "ico" ) ;
278
+ }
279
+
280
+ return route ;
281
+ } ) ;
282
+
283
+ htmlContent = htmlContent . replace ( scriptRegex , ( route ) => {
284
+ if ( ! ! ~ route . indexOf ( "__inline" ) ) {
285
+ // js inline
286
+ let scriptInlineRegex = new RegExp ( "<script.*src=(\s*?)*(.+)[\?]\_\_inline.*?(\s*?)><\/script>" , "ig" ) ;
287
+ route = this . inlineHtmlRes ( route , scriptInlineRegex , compilation , 'js' ) ;
288
+ }
289
+ else {
290
+ // js md5
291
+ let scriptMd5Regex = new RegExp ( "<script.*src=(\s*?)*(.+).*?(\s*?)><\/script>" , "ig" ) ;
292
+ route = this . md5HtmlRes ( route , scriptMd5Regex , publicPath , "js" ) ;
293
+ }
294
+
295
+ return route ;
296
+ } ) ;
297
+
298
+ return htmlContent ;
299
+ } ;
300
+
301
+
302
+ HtmlResWebpackPlugin . prototype . md5HtmlRes = function ( routeStr , reg , publicPath , extension ) {
283
303
let _this = this ;
284
304
285
- htmlContent = htmlContent . replace ( reg , function ( tag , route ) {
286
-
305
+ routeStr = routeStr . replace ( reg , function ( tag , gap , route ) {
306
+
307
+ route = route . replace ( / [ \" | ' ] / g, "" ) ;
308
+ // console.log(tag, gap, route);
309
+
287
310
if ( extension === "ico" && ! ! ~ route . indexOf ( "." + extension ) ) {
288
311
tag = tag . replace ( route , publicPath + route ) ;
289
312
return tag ;
@@ -307,14 +330,15 @@ HtmlResWebpackPlugin.prototype.md5HtmlRes = function(htmlContent, reg, publicPat
307
330
return tag ;
308
331
} ) ;
309
332
310
- return htmlContent ;
333
+ return routeStr ;
311
334
} ;
312
335
313
- HtmlResWebpackPlugin . prototype . inlineHtmlRes = function ( htmlContent , reg , compilation , extension ) {
336
+ HtmlResWebpackPlugin . prototype . inlineHtmlRes = function ( routeStr , reg , compilation , extension ) {
314
337
let _this = this ;
315
338
316
- htmlContent = htmlContent . replace ( reg , function ( tag , route ) {
317
- // console.log(tag, route);
339
+ routeStr = routeStr . replace ( reg , function ( tag , gap , route ) {
340
+ route = route . replace ( / [ \" | ' ] / g, "" ) ;
341
+ // console.log(tag, gap, route);
318
342
var assets = _this . stats . assets [ route ] || [ ] ,
319
343
file = "" ;
320
344
@@ -323,7 +347,6 @@ HtmlResWebpackPlugin.prototype.inlineHtmlRes = function(htmlContent, reg, compil
323
347
}
324
348
325
349
assets . forEach ( function ( item , index ) {
326
-
327
350
if ( ! ! ~ item . indexOf ( "." + extension ) && extension === "js" ) {
328
351
file = "<script>" + compilation . assets [ item ] . source ( ) + "</script>" ;
329
352
}
@@ -342,7 +365,7 @@ HtmlResWebpackPlugin.prototype.inlineHtmlRes = function(htmlContent, reg, compil
342
365
return tag ;
343
366
} ) ;
344
367
345
- return htmlContent ;
368
+ return routeStr ;
346
369
} ;
347
370
348
371
/**
0 commit comments