Skip to content

Commit 676b365

Browse files
committed
fix: 离线包问题解决.
1 parent b52d7e2 commit 676b365

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

lib/builder.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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');
1414
const HtmlWebpackPlugin = require('html-webpack-plugin');
1515
const ExtractTextPlugin = require('extract-text-webpack-plugin');
1616
const CleanWebpackPlugin = require('clean-webpack-plugin');
17-
const ZipWebpackPlugin = require('zip-webpack-plugin');
1817
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
1918
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
2019
const ReplaceBundleStringPlugin = require('replace-bundle-webpack-plugin');
2120
const HtmlStringReplace = require('html-string-replace-webpack-plugin');
2221
const StringReplaceWebpackPlugin = require('string-replace-webpack-plugin');
2322
const SriPlugin = require('webpack-subresource-integrity');
23+
const OfflineWebpackPlugin = require('offline-webpack-plugin');
2424
const { deepCopy, listDir, merge, isEmpty } = require('./util');
2525
const Config = require('./config');
2626
const 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(/\/\/11\.url\.cn\/now\/([^"'\)\`]+)(["'\`\)])/g, '//now.qq.com/$1$2');
623+
// 去掉 crossorigin="annoymous"
624+
source = source.replace(/crossorigin=\"anonymous\"/g, '');
625+
// 去掉 业务 js 文件的 ?_bid=152
626+
source = source.replace(/\?_bid=152/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;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "builder-webpack3",
3-
"version": "0.2.19",
3+
"version": "0.3.0",
44
"description": "webpack项目构建器.",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)