-
Notifications
You must be signed in to change notification settings - Fork 569
/
webpack.config.js
129 lines (126 loc) · 3.67 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var pkg = require('./package');
const TerserPlugin = require('terser-webpack-plugin');
// replaceVersion
var replaceVersion = function () {
var filePath = path.resolve(__dirname, 'src/cos.js');
var content = fs.readFileSync(filePath).toString();
if (content) {
var newContent = content.replace(/(COS\.version) *= *['"]\d+\.\d+\.\d+['"];/, "$1 = '" + pkg.version + "';");
if (newContent !== content) {
fs.writeFileSync(filePath, newContent);
console.log('cos.js version updated.');
}
}
};
var replaceDevCode = function (list) {
list.forEach(function (fileName) {
var filePath = path.resolve(__dirname, fileName);
var content = fs.readFileSync(filePath).toString();
var newContent = content;
newContent = newContent.replace(/https:\/\/\w+\.com\/[\w\-]+\/server\//, 'https://example.com/');
newContent = newContent.replace(/test-125\d{7}/, 'test-1250000000');
newContent = newContent.replace(/'proxy' => 'http:\/\/[^']+',/, "'proxy' => '',");
newContent = newContent.replace(/proxy: 'http:\/\/[^']+',/, "proxy: '',");
newContent = newContent.replace(/AKID\w+/, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
newContent = newContent.replace(/'secretKey' => '[^']+',/, "'secretKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',");
newContent = newContent.replace(/secretKey: '[^']+',/, "secretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',");
newContent = newContent.replace(/'allowActions' *=> *array\([^)]+\)/, `'allowActions' => array(
// 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
// 简单上传
'name/cos:PutObject',
// 分片上传
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload'
)`);
if (newContent !== content) {
console.log('replace ' + filePath);
fs.writeFileSync(filePath, newContent);
}
});
};
replaceVersion();
module.exports = {
mode: process.env.NODE_ENV,
entry: path.resolve(__dirname, './index.js'),
devtool: 'none',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'cos-js-sdk-v5.js',
libraryTarget: 'umd',
library: 'COS',
globalObject: 'this',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
}
]
},
optimization: {
minimize: false,
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
extractComments: false,
}),
],
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
};
if (process.env.NODE_ENV === 'production') {
replaceDevCode([
'demo/demo.js',
'demo/queue/index.js',
'test/test.js',
'server/sts.js',
]);
module.exports.output.filename = 'cos-js-sdk-v5.min.js';
module.exports.optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
extractComments: false,
terserOptions: {
compress: {
drop_debugger: true,
drop_console: true,
},
format: {
comments: false, // 删除注释
},
},
}),
],
};
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
}),
]);
}