@@ -19,6 +19,7 @@ const ZipWebpackPlugin = require('zip-webpack-plugin');
1919const HtmlWebpackExternalsPlugin = require ( 'html-webpack-externals-plugin' ) ;
2020const HtmlWebpackInlineSourcePlugin = require ( 'html-webpack-inline-source-plugin' ) ;
2121const ReplaceBundleStringPlugin = require ( 'replace-bundle-webpack-plugin' ) ;
22+ const HtmlStringReplace = require ( 'html-string-replace-webpack-plugin' ) ;
2223const { deepCopy, listDir } = require ( './util' ) ;
2324const Config = require ( './config' ) ;
2425const projectRoot = Config . getPath ( ) ;
@@ -113,6 +114,7 @@ class Builder {
113114 const assetsPrefix = `cdn/${ bizName } ` ;
114115 const cdnUrl = `//11.url.cn/now/${ moduleName } /${ bizName } ` ;
115116 const serverUrl = `//now.qq.com/${ moduleName } /${ bizName } ` ;
117+ const regex = new RegExp ( assetsPrefix + '/' , 'g' ) ;
116118
117119 // 设置打包规则
118120 const prodRules = [ ] ;
@@ -145,8 +147,10 @@ class Builder {
145147 const { newEntry, htmlWebpackPlugins} = Builder . _setMultiplePage ( prodConfig . entry , options . minifyHTML || true , options . inject , false , assetsPrefix , htmlPrefix ) ;
146148
147149 prodPlugins = prodPlugins . concat ( htmlWebpackPlugins ) ;
148- // 由于静态文件打包到了 cdn 目录,assets路径引用问题采用全局替换策略进行修复
149- prodPlugins . push ( Builder . _setReplacePlugin ( new RegExp ( assetsPrefix + '/' , 'g' ) , '' ) ) ;
150+ // 修复html引用css和js的路径问题
151+ prodPlugins . push ( Builder . _setReplaceHtmlPlugin ( regex , '' ) ) ;
152+ // 修复js和css中引用的图片路径问题
153+ prodPlugins . push ( Builder . _setReplaceBundlePlugin ( regex , '' ) ) ;
150154 prodPlugins . push ( Builder . _setOffline ( assetsPrefix , htmlPrefix , cdnUrl , serverUrl ) ) ;
151155
152156 prodConfig . entry = newEntry ;
@@ -291,13 +295,35 @@ class Builder {
291295
292296
293297 /**
294- * 打包构建后替换的内容
298+ * 打包构建后替换的内容, 此处替换html文件
295299 * @param regex 正则
296300 * @param replaceWith 替换成xx字符串
297- * @returns {ReplaceBundlePlugin }
301+ * @returns {HtmlStringReplace }
298302 * @private
299303 */
300- static _setReplacePlugin ( regex , replaceWith ) {
304+ static _setReplaceHtmlPlugin ( regex , replaceWith ) {
305+ return new HtmlStringReplace ( {
306+ enable : true ,
307+ patterns : [
308+ {
309+ match : regex ,
310+ replacement : ( ) => {
311+ return replaceWith ;
312+ }
313+ }
314+ ]
315+ } )
316+ }
317+
318+
319+ /**
320+ * 打包构建后替换的内容, 此处替换css和js文件
321+ * @param regex 正则
322+ * @param replaceWith 替换成xx字符串
323+ * @returns {ReplaceBundleStringPlugin }
324+ * @private
325+ */
326+ static _setReplaceBundlePlugin ( regex , replaceWith ) {
301327 return new ReplaceBundleStringPlugin ( [ {
302328 partten : regex ,
303329 replacement : ( ) => {
@@ -365,34 +391,34 @@ class Builder {
365391
366392 Object . keys ( entries ) . map ( ( index ) => {
367393 const entry = entries [ index ] ;
368- const match = entry . match ( / \/ p a g e s \/ ( .* ) \/ i n i t .j s / ) ;
369- const pageName = match && match [ 1 ] ;
370- let filename = '' ;
371- if ( htmlPrefix ) {
372- filename = htmlPrefix + '/' ;
373- }
394+ const match = entry . match ( / \/ p a g e s \/ ( .* ) \/ i n i t .j s / ) ;
395+ const pageName = match && match [ 1 ] ;
396+ let filename = '' ;
397+ if ( htmlPrefix ) {
398+ filename = htmlPrefix + '/' ;
399+ }
374400
375- newEntry [ pageName ] = entry ;
376-
377- htmlWebpackPlugins . push (
378- new HtmlWebpackPlugin ( {
379- inlineSource : inlineCSS ? '\\.css$' : undefined ,
380- template : path . join ( projectRoot , `src/pages/${ pageName } /index.html` ) ,
381- filename : `${ filename } ${ pageName } .html` ,
382- chunks : [ pageName ] ,
383- assetsPrefix : `${ assetsPrefix } /` ,
384- inject : inject ,
385- minify : minifyHtml ? {
386- html5 : true ,
387- collapseWhitespace : true ,
388- preserveLineBreaks : false ,
389- minifyCSS : true ,
390- minifyJS : true ,
391- removeComments : false
392- } : false
393- } )
394- ) ;
395- } ) ;
401+ newEntry [ pageName ] = entry ;
402+
403+ htmlWebpackPlugins . push (
404+ new HtmlWebpackPlugin ( {
405+ inlineSource : inlineCSS ? '\\.css$' : undefined ,
406+ template : path . join ( projectRoot , `src/pages/${ pageName } /index.html` ) ,
407+ filename : `${ filename } ${ pageName } .html` ,
408+ chunks : [ pageName ] ,
409+ assetsPrefix : `${ assetsPrefix } /` ,
410+ inject : inject ,
411+ minify : minifyHtml ? {
412+ html5 : true ,
413+ collapseWhitespace : true ,
414+ preserveLineBreaks : false ,
415+ minifyCSS : true ,
416+ minifyJS : true ,
417+ removeComments : false
418+ } : false
419+ } )
420+ ) ;
421+ } ) ;
396422
397423 return {
398424 newEntry,
@@ -410,20 +436,20 @@ class Builder {
410436 */
411437 static _setOffline ( assetsPrefix , htmlPrefix , cdnUrl , serverUrl ) {
412438 return new ZipWebpackPlugin ( {
413- path : path . join ( projectRoot , './public/offline' ) ,
414- filename : 'offline.zip' ,
415- pathMapper : ( assetPath ) => {
439+ path : path . join ( projectRoot , './public/offline' ) ,
440+ filename : 'offline.zip' ,
441+ pathMapper : ( assetPath ) => {
416442 if ( assetPath . indexOf ( htmlPrefix ) !== - 1 ) {
417- assetPath = assetPath . replace ( htmlPrefix , '' ) ;
418- return path . join ( serverUrl . replace ( '//' , '' ) , assetPath ) ;
419- } else if ( assetPath . indexOf ( assetsPrefix ) !== - 1 ) {
420- assetPath = assetPath . replace ( assetsPrefix , '' ) ;
421- return path . join ( cdnUrl . replace ( '//' , '' ) , assetPath ) ;
422- }
443+ assetPath = assetPath . replace ( htmlPrefix , '' ) ;
444+ return path . join ( serverUrl . replace ( '//' , '' ) , assetPath ) ;
445+ } else if ( assetPath . indexOf ( assetsPrefix ) !== - 1 ) {
446+ assetPath = assetPath . replace ( assetsPrefix , '' ) ;
447+ return path . join ( cdnUrl . replace ( '//' , '' ) , assetPath ) ;
448+ }
423449
424- return assetPath ;
425- }
426- } )
450+ return assetPath ;
451+ }
452+ } )
427453 }
428454
429455 /**
@@ -453,11 +479,11 @@ class Builder {
453479
454480 listDir ( path . join ( projectRoot , './src' ) , 1 )
455481 . forEach ( ( dir ) => {
456- const { name, dirPath } = dir ;
482+ const { name, dirPath } = dir ;
457483
458- aliasObject [ '/' + name ] = dirPath ;
459- aliasObject [ name ] = dirPath ;
460- } ) ;
484+ aliasObject [ '/' + name ] = dirPath ;
485+ aliasObject [ name ] = dirPath ;
486+ } ) ;
461487
462488 return aliasObject ;
463489 }
0 commit comments