11'use strict' ;
22
33/**
4- * Copyright (c) 2017 Tencent Inc.
4+ * Copyright (c) 2018 Tencent Inc.
55 *
66 * Webpack构建器,适用于NOW直播IVWEB团队工程项目.
77 *
@@ -14,13 +14,13 @@ const HappyPack = require('happypack');
1414const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
1515const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ;
1616const CleanWebpackPlugin = require ( 'clean-webpack-plugin' ) ;
17- const ZipWebpackPlugin = require ( 'zip-webpack-plugin' ) ;
1817const HtmlWebpackExternalsPlugin = require ( 'html-webpack-externals-plugin' ) ;
1918const HtmlWebpackInlineSourcePlugin = require ( 'html-webpack-inline-source-plugin' ) ;
2019const ReplaceBundleStringPlugin = require ( 'replace-bundle-webpack-plugin' ) ;
2120const HtmlStringReplace = require ( 'html-string-replace-webpack-plugin' ) ;
2221const StringReplaceWebpackPlugin = require ( 'string-replace-webpack-plugin' ) ;
2322const SriPlugin = require ( 'webpack-subresource-integrity' ) ;
23+ const OfflineWebpackPlugin = require ( 'offline-webpack-plugin' ) ;
2424const { deepCopy, listDir, merge, isEmpty } = require ( './util' ) ;
2525const Config = require ( './config' ) ;
2626const projectRoot = Config . getPath ( ) ;
@@ -235,7 +235,7 @@ class Builder {
235235 }
236236
237237 return {
238- filename : `${ filename } [name]${ hash } .js` ,
238+ filename : `${ filename } [name]${ hash } .js?_bid=152 ` ,
239239 path : path . join ( projectRoot , 'public/' ) ,
240240 publicPath : publicPath ,
241241 crossOriginLoading : 'anonymous'
@@ -514,7 +514,7 @@ class Builder {
514514 * @private
515515 */
516516 static _setExternalPlugin ( externals ) {
517- externals = externals || [
517+ const newExternals = externals || [
518518 {
519519 module : 'react' ,
520520 entry : 'https://11.url.cn/now/lib/15.1.0/react-with-addons.min.js?_bid=3123' ,
@@ -527,19 +527,6 @@ class Builder {
527527 }
528528 ] ;
529529
530- const newExternals = [ ] ;
531- // 增加 cross-origin 的头
532- externals . map ( ( external ) => {
533- const obj = deepCopy ( external ) ;
534- obj . entry = {
535- path : external . entry ,
536- attributes : {
537- crossorigin : 'anonymous'
538- }
539- }
540- newExternals . push ( obj ) ;
541- } ) ;
542-
543530 return new HtmlWebpackExternalsPlugin ( {
544531 externals : newExternals
545532 } ) ;
@@ -617,19 +604,29 @@ class Builder {
617604 * @private
618605 */
619606 static _setOffline ( assetsPrefix , htmlPrefix , cdnUrl , serverUrl ) {
620- return new ZipWebpackPlugin ( {
607+ return new OfflineWebpackPlugin ( {
621608 path : path . join ( projectRoot , './public/offline' ) ,
622609 filename : 'offline.zip' ,
623610 pathMapper : ( assetPath ) => {
624611 if ( assetPath . indexOf ( htmlPrefix ) !== - 1 ) {
612+ // 所有资源都改成 now.qq.com 的域名, 防止跨域问题
625613 assetPath = assetPath . replace ( htmlPrefix , '' ) ;
626- return path . join ( serverUrl . replace ( '//' , '' ) , assetPath ) ;
627614 } else if ( assetPath . indexOf ( assetsPrefix ) !== - 1 ) {
628615 assetPath = assetPath . replace ( assetsPrefix , '' ) ;
629- return path . join ( cdnUrl . replace ( '//' , '' ) , assetPath ) ;
616+ }
617+ return path . join ( serverUrl . replace ( '//' , '' ) , assetPath ) ;
618+ } ,
619+ beforeAddBuffer ( source , assetPath ) {
620+ if ( assetPath . indexOf ( htmlPrefix ) !== - 1 ) {
621+ // 所有资源都改成 now.qq.com 的域名, 防止跨域问题
622+ source = source . replace ( / \/ \/ 1 1 \. u r l \. c n \/ n o w \/ ( [ ^ " ' \) \` ] + ) ( [ " ' \` \) ] ) / g, '//now.qq.com/$1$2' ) ;
623+ // 去掉 crossorigin="annoymous"
624+ source = source . replace ( / c r o s s o r i g i n = \" a n o n y m o u s \" / g, '' ) ;
625+ // 去掉 业务 js 文件的 ?_bid=152
626+ source = source . replace ( / \? _ b i d = 1 5 2 / g, '' ) ;
630627 }
631628
632- return assetPath ;
629+ return source ;
633630 }
634631 } )
635632 }
@@ -676,4 +673,4 @@ class Builder {
676673 }
677674}
678675
679- module . exports = Builder ;
676+ module . exports = Builder ;
0 commit comments