|
@@ -0,0 +1,348 @@
|
|
1
|
+const path = require('path');
|
|
2
|
+const webpack = require('webpack');
|
|
3
|
+const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
|
|
4
|
+const glob = require("glob");
|
|
5
|
+const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
|
6
|
+const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
7
|
+// const CopyWebpackPlugin = require("copy-webpack-plugin");
|
|
8
|
+const CleanWebpackPlugin = require('clean-webpack-plugin');
|
|
9
|
+const packageJson = require("./../package.json");
|
|
10
|
+const HappyPack = require('happypack');
|
|
11
|
+const os = require('os');
|
|
12
|
+const happyThreadPool = HappyPack.ThreadPool({size: os.cpus().length});
|
|
13
|
+let pathVars = require("./pathVars");
|
|
14
|
+let config = {};
|
|
15
|
+
|
|
16
|
+/*----------------------------------- page ---------------------------------*/
|
|
17
|
+//测试页面
|
|
18
|
+
|
|
19
|
+// let files = glob.sync(pathVars.testPath + `/pie/ChartPie?(9|21|32)/index.html`);
|
|
20
|
+let files = glob.sync(pathVars.testPath + `/pie/ChartPie38/index.html`);
|
|
21
|
+// files = files.concat(glob.sync(pathVars.testPath + "/center/ChartCenterTop1/index.html"));
|
|
22
|
+// files = files.concat(glob.sync(pathVars.testPath + "/digit/ChartDigit5/index.html"));
|
|
23
|
+// files = files.concat(glob.sync(pathVars.testPath + "/digit/ChartDigit21/index.html"));
|
|
24
|
+
|
|
25
|
+// files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie38/index.html"));
|
|
26
|
+// files = files.concat(glob.sync(pathVars.testPath + "/map/ChartChinaMap5/index.html"));
|
|
27
|
+// files = files.concat(glob.sync(pathVars.testPath + "/map/ChartChinaMap1/index.html"));
|
|
28
|
+// files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie31/index.html"));
|
|
29
|
+// files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie39/index.html"));
|
|
30
|
+// files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie40/index.html"));
|
|
31
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartPie38blue/index.html"));
|
|
32
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar5/index.html"));
|
|
33
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar8/index.html"));
|
|
34
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar40/index.html"));
|
|
35
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar41/index.html"));
|
|
36
|
+// files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartLine3/index.html"));
|
|
37
|
+
|
|
38
|
+// files = files.concat(glob.sync(pathVars.testPath + "/bar/ChartBar46/index.html"));
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+console.log("测试页面\n", files);
|
|
43
|
+let entryDev = {
|
|
44
|
+ vendor: Object.keys(packageJson.dependencies)
|
|
45
|
+};
|
|
46
|
+let pageAry = [];
|
|
47
|
+files.forEach(function (filePath) {
|
|
48
|
+ //将单个文件路径(/test/ChartBar3D/index.htsml)路径拆分成数组
|
|
49
|
+ let tempAry = filePath.split('/');
|
|
50
|
+ //demo文件夹名
|
|
51
|
+ let typeName = tempAry[tempAry.length - 3];
|
|
52
|
+ let chunkName = tempAry[tempAry.length - 2];
|
|
53
|
+ let htmlName = chunkName + "/index.html";
|
|
54
|
+
|
|
55
|
+ let obj = {};
|
|
56
|
+ obj["filePath"] = filePath;
|
|
57
|
+ obj["chunkName"] = chunkName;
|
|
58
|
+ obj["htmlName"] = htmlName;
|
|
59
|
+ obj["entryJs"] = pathVars.testPath + "/" + typeName + "/" + chunkName + "/index.js";
|
|
60
|
+ entryDev[chunkName] = obj["entryJs"];
|
|
61
|
+ pageAry.push(obj);
|
|
62
|
+});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+//图表主js文件
|
|
66
|
+let chartFiles = [
|
|
67
|
+ glob.sync(pathVars.srcPath + "/chart/pie/ChartPie38/ChartPie38.js")[0],
|
|
68
|
+ // chartFiles = glob.sync(pathVars.srcPath + "/chart/digit/ChartDigit5/ChartDigit5.js"),
|
|
69
|
+ // chartFiles = glob.sync(pathVars.srcPath + "/chart/pie/ChartPie39/ChartPie39.js"),
|
|
70
|
+ // chartFiles = glob.sync(pathVars.srcPath + "/chart/zaibei/ChartBar40/ChartBar40.js"),
|
|
71
|
+ // chartFiles = glob.sync(pathVars.srcPath + "/chart/zaibei/ChartLine3/ChartLine3.js"),
|
|
72
|
+ // glob.sync(pathVars.srcPath + "/chart/map/ChartChinaMap5/ChartChinaMap5.js"),
|
|
73
|
+ // chartFiles = glob.sync(pathVars.srcPath + "/chart/center/ChartCenterTop1/ChartCenterTop1.js")
|
|
74
|
+]
|
|
75
|
+
|
|
76
|
+console.log("图表主js文件\n", chartFiles);
|
|
77
|
+let chartAry = [];
|
|
78
|
+let entryDist = {};
|
|
79
|
+chartFiles.forEach(function (filePath) {
|
|
80
|
+ //将单个文件路径(/src/chart/ChartEarth/ChartEarth.js)路径拆分成数组
|
|
81
|
+ let tempAry = filePath.split('/');
|
|
82
|
+ let chunkName = tempAry[tempAry.length - 2];
|
|
83
|
+ let obj = {};
|
|
84
|
+ obj["chunkName"] = chunkName;
|
|
85
|
+ chartAry.push(obj);
|
|
86
|
+ //
|
|
87
|
+ entryDist[chunkName] = filePath;
|
|
88
|
+});
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+/*----------------------------- entry ------------------------------*/
|
|
92
|
+
|
|
93
|
+config["entryDev"] = entryDev;
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+config["entryDist"] = entryDist;
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+/*----------------------------- devServer ------------------------------*/
|
|
100
|
+let devServer = {
|
|
101
|
+ // open: true,
|
|
102
|
+ publicPath: "/",
|
|
103
|
+ contentBase: pathVars.devPath,
|
|
104
|
+ hot: true,
|
|
105
|
+ inline: true,
|
|
106
|
+ port: 3007,
|
|
107
|
+ host: 'localhost'
|
|
108
|
+};
|
|
109
|
+config["devServer"] = devServer;
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+/*----------------------------- output ------------------------------*/
|
|
113
|
+let outputDev = {
|
|
114
|
+ path: pathVars.distPath,
|
|
115
|
+ publicPath: "/", // 表示资源的发布地址,当配置过该属性后,打包文件中所有通过相对路径引用的资源都会被配置的路径所替换(如:css中背景图的路径)
|
|
116
|
+ filename: "[name]/main.js"
|
|
117
|
+};
|
|
118
|
+config["outputDev"] = outputDev;
|
|
119
|
+
|
|
120
|
+let outputDist = {
|
|
121
|
+ path: pathVars.distPath,
|
|
122
|
+ publicPath: "/",
|
|
123
|
+ filename: "[name]_[chunkhash:5].js",
|
|
124
|
+ library: "[name]",
|
|
125
|
+ libraryTarget: "umd"
|
|
126
|
+// libraryExport: "default"
|
|
127
|
+};
|
|
128
|
+config["outputDist"] = outputDist;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+/*----------------------------- externals ------------------------------*/
|
|
132
|
+if (process.env.NODE_ENV === "noThird") {
|
|
133
|
+ console.log("==================", process.env.NODE_ENV);
|
|
134
|
+ config["externals"] = {
|
|
135
|
+ 'three': {
|
|
136
|
+ commonjs: 'THREE',
|
|
137
|
+ commonjs2: 'THREE',
|
|
138
|
+ amd: 'THREE',
|
|
139
|
+ root: 'THREE'
|
|
140
|
+ },
|
|
141
|
+ 'zrender': {
|
|
142
|
+ commonjs: 'zrender',
|
|
143
|
+ commonjs2: 'zrender',
|
|
144
|
+ amd: 'zrender',
|
|
145
|
+ root: 'zrender'
|
|
146
|
+ },
|
|
147
|
+ 'gsap': {
|
|
148
|
+ commonjs: 'gsap',
|
|
149
|
+ commonjs2: 'gsap',
|
|
150
|
+ amd: 'gsap',
|
|
151
|
+ root: 'gsap'
|
|
152
|
+ }
|
|
153
|
+ };
|
|
154
|
+} else {
|
|
155
|
+ config["externals"] = {};
|
|
156
|
+ config["outputDist"]["filename"]="[name].js"
|
|
157
|
+}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+/*----------------------------- module ------------------------------*/
|
|
161
|
+let rules = [
|
|
162
|
+ // {
|
|
163
|
+ // test: /\.js$/,
|
|
164
|
+ // exclude: pathVars.nodeModulesPath,
|
|
165
|
+ //
|
|
166
|
+ // //use: ['babel-loader'],
|
|
167
|
+ // use: ["happypack/loader?id=babel"]
|
|
168
|
+ // },
|
|
169
|
+ {
|
|
170
|
+ test: /\.js$/,
|
|
171
|
+ // exclude: pathVars.nodeModulesPath,
|
|
172
|
+ loader: "babel-loader",
|
|
173
|
+ },
|
|
174
|
+ // {
|
|
175
|
+ // test: /\.glsl$/,
|
|
176
|
+ // loader: 'webpack-glsl-loader'
|
|
177
|
+ // },
|
|
178
|
+ {
|
|
179
|
+ test: /\.(glsl|vs|fs|vert|frag)$/,
|
|
180
|
+ // exclude: [pathVars.nodeModulesPath, pathVars.dllPath],
|
|
181
|
+ use: ["raw-loader", "glslify-loader"]
|
|
182
|
+ }
|
|
183
|
+]
|
|
184
|
+// let jsonRuleDist={
|
|
185
|
+// test:/\.json$/,
|
|
186
|
+// type: 'javascript/auto',
|
|
187
|
+// use:[
|
|
188
|
+// {
|
|
189
|
+// loader: 'json-loader',
|
|
190
|
+//
|
|
191
|
+// options: {
|
|
192
|
+// publicPath: '../../static/',
|
|
193
|
+// name: 'json/[name]_[hash:5].[ext]'
|
|
194
|
+// }
|
|
195
|
+// }
|
|
196
|
+// ]
|
|
197
|
+// };
|
|
198
|
+let imgRuleDev = {
|
|
199
|
+ test: /\.(png|jpg|gif)$/,
|
|
200
|
+ // loader: 'url-loader?limit=1000&name=images/[name]_[hash:5].[ext]',
|
|
201
|
+ use: [
|
|
202
|
+ {
|
|
203
|
+ loader: 'file-loader',
|
|
204
|
+ options: {
|
|
205
|
+ publicPath: '/',
|
|
206
|
+ name: 'imgInChart/[name]_[hash:5].[ext]'
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+ ]
|
|
210
|
+}
|
|
211
|
+let imgRuleDist = {
|
|
212
|
+ test: /\.(png|jpg|gif)$/,
|
|
213
|
+ // loader: 'url-loader?limit=1000&name=images/[name]_[hash:5].[ext]',
|
|
214
|
+ use: [
|
|
215
|
+ {
|
|
216
|
+ loader: 'file-loader',
|
|
217
|
+ options: {
|
|
218
|
+ publicPath: '../../static/',
|
|
219
|
+ name: 'imgInChart/[name]_[hash:5].[ext]'
|
|
220
|
+ }
|
|
221
|
+ }
|
|
222
|
+ ]
|
|
223
|
+};
|
|
224
|
+
|
|
225
|
+config["moduleBaseDev"] = {rules: rules.concat(imgRuleDev)};
|
|
226
|
+config["moduleBaseDist"] = {rules: rules.concat(imgRuleDist)};
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+//---------------------------------- plugin -----------------------------------//
|
|
230
|
+let pluginBase = [
|
|
231
|
+ /*new CopyWebpackPlugin([{
|
|
232
|
+ from: pathVars.staticPath,
|
|
233
|
+ //to 起始位置为发布目录
|
|
234
|
+ to: "static"
|
|
235
|
+ }]),*/
|
|
236
|
+ new webpack.DllReferencePlugin({
|
|
237
|
+ context: pathVars.rootPath,
|
|
238
|
+ manifest: require(pathVars.dllPath + '/manifest.json')
|
|
239
|
+ }),
|
|
240
|
+ new HappyPack({
|
|
241
|
+ id: 'babel',
|
|
242
|
+ loaders: ["babel-loader"],
|
|
243
|
+ threadPool: happyThreadPool
|
|
244
|
+ }),
|
|
245
|
+ new webpack.DefinePlugin({
|
|
246
|
+ my_ENV: JSON.stringify(process.env.my_ENV)
|
|
247
|
+ })
|
|
248
|
+];
|
|
249
|
+
|
|
250
|
+const htmlBase = [];
|
|
251
|
+pageAry.forEach(function (page) {
|
|
252
|
+ const htmlPlugin = new HtmlWebpackPlugin({
|
|
253
|
+ title: '',
|
|
254
|
+ template: page["filePath"],
|
|
255
|
+ filename: page["htmlName"],
|
|
256
|
+ inject: 'body',
|
|
257
|
+ chunks: ["manifest", "vendor", "common", page["chunkName"]],
|
|
258
|
+ chunksSortMode: 'none'
|
|
259
|
+ });
|
|
260
|
+ htmlBase.push(htmlPlugin);
|
|
261
|
+});
|
|
262
|
+// pageVars.forEach(function (page) {
|
|
263
|
+// let htmlPlugin = new HtmlWebpackPlugin({
|
|
264
|
+// title: 'page1',
|
|
265
|
+// template: page["filePath"],
|
|
266
|
+// filename: page["htmlName"],
|
|
267
|
+// inject: 'body',
|
|
268
|
+// chunks: /*isDev ? [page["chunkName"]] :*/ ["manifest", "vendor", "common", page["chunkName"]],
|
|
269
|
+// chunksSortMode: 'dependency'
|
|
270
|
+// });
|
|
271
|
+// pluginBase.push(htmlPlugin);
|
|
272
|
+// });
|
|
273
|
+
|
|
274
|
+//------------------- dev
|
|
275
|
+let pluginsDev = [
|
|
276
|
+ new webpack.HotModuleReplacementPlugin()
|
|
277
|
+];
|
|
278
|
+config["pluginsDev"] = pluginBase.concat(pluginsDev, htmlBase);
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+//------------------- dist
|
|
282
|
+let pluginDist = [
|
|
283
|
+ new CleanWebpackPlugin(pathVars.distPath,
|
|
284
|
+ {
|
|
285
|
+ root: pathVars.rootPath,
|
|
286
|
+ verbose: true
|
|
287
|
+ }
|
|
288
|
+ ),
|
|
289
|
+ new webpack.NoEmitOnErrorsPlugin(),
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+];
|
|
293
|
+config["pluginDist"] = pluginBase.concat(pluginDist);
|
|
294
|
+
|
|
295
|
+//----------------------------------- optimization ---------------------------//
|
|
296
|
+config["optimizationDev"] = {
|
|
297
|
+ minimize: false,
|
|
298
|
+ splitChunks: {
|
|
299
|
+ chunks: "all", //initial
|
|
300
|
+ minSize: 30 * 1024, //模块大于30k会被抽离到公共模块 也就是说每个页面的js不会大于30k
|
|
301
|
+ minChunks: 1, //模块出现1次就会被抽离到公共模块
|
|
302
|
+ maxAsyncRequests: 5, //异步模块,一次最多只能被加载5个
|
|
303
|
+ maxInitialRequests: 3, //入口模块最多只能加载3个
|
|
304
|
+ name: true,
|
|
305
|
+ cacheGroups: {
|
|
306
|
+ default: {
|
|
307
|
+ chunks: "all",
|
|
308
|
+ name: "common",
|
|
309
|
+ test: /[\\/]src[\\/]/,
|
|
310
|
+ minChunks: 3,
|
|
311
|
+ priority: -20,
|
|
312
|
+ reuseExistingChunk: true
|
|
313
|
+ },
|
|
314
|
+ vendor: {
|
|
315
|
+ chunks: "all",
|
|
316
|
+ name: "vendor",
|
|
317
|
+ test: /[\\/]node_modules[\\/]/,
|
|
318
|
+ minChunks: 1,
|
|
319
|
+ priority: -10
|
|
320
|
+ }
|
|
321
|
+ }
|
|
322
|
+ },
|
|
323
|
+ runtimeChunk: {
|
|
324
|
+ name: "manifest"
|
|
325
|
+ }
|
|
326
|
+};
|
|
327
|
+//
|
|
328
|
+config["optimizationDist"] = {
|
|
329
|
+ minimize: true,
|
|
330
|
+ minimizer: [
|
|
331
|
+ new UglifyJsPlugin({
|
|
332
|
+ cache: true,
|
|
333
|
+ parallel: true,
|
|
334
|
+ uglifyOptions: {
|
|
335
|
+ warnings: false,
|
|
336
|
+ compress: {
|
|
337
|
+ // warnings: false,
|
|
338
|
+ drop_debugger: true,
|
|
339
|
+ drop_console: true
|
|
340
|
+ }
|
|
341
|
+ }
|
|
342
|
+ }),
|
|
343
|
+ new OptimizeCssAssetsPlugin({})
|
|
344
|
+ ],
|
|
345
|
+ splitChunks: {}
|
|
346
|
+
|
|
347
|
+};
|
|
348
|
+module.exports = config;
|