-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
88 lines (84 loc) · 2.66 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
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const createHtml = require("./config/create-html");
const getEntry = require("./config/get-entry");
const defaultApps = ["index"]
const getPagePath = require("./config/get-page-path");
const vendorEntry = require("./vendor")
const defaultEntry = getEntry("./src/pages");
const defaultHtmlArr = createHtml(getPagePath("./src/pages"))
const getEntryFromApps = (apps)=>{
return apps.reduce((res,app)=>{
res[`${app}/${app}`] = `./src/pages/${app}/index.js`
return res
},{})
}
module.exports = (env, argv) => {
const {env:{}} = argv;
const { apps="", buildVendor, analyzeBundle } = env
const appsArr = [...apps.split(","), ...defaultApps];
const entry = buildVendor? {vendor: vendorEntry} : apps ? getEntryFromApps(appsArr): defaultEntry;
const htmlArr = buildVendor? []: apps ? createHtml(appsArr): defaultHtmlArr;
apps && console.log(`Webpack Build Apps: ${apps}`)
return ({
entry,
output: { path: path.join(__dirname, "build"), filename: "[name].js" },
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
alias: {
src: path.resolve(__dirname, "src/"),
component: path.resolve(__dirname, "src/component/"),
store: path.resolve(__dirname, "src/store/"),
},
},
devServer: { contentBase: path.join(__dirname, "src") }, // https://webpack.js.org/guides/hot-module-replacement/
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.(css|scss)$/,
use: ["style-loader", "css-loader", "sass-loader"],
},
{
test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
use: ["file-loader"],
},
],
},
plugins: [
...htmlArr,
new BundleAnalyzerPlugin({
analyzerMode: analyzeBundle? "static" : "disabled",
}),
// new CleanWebpackPlugin(),
// new IgnorePlugin({
// resourceRegExp: apps ? /react|react-dom/: /./,
// })
],
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
}),
//https://webpack.js.org/plugins/terser-webpack-plugin/
],
// splitChunks: {
// cacheGroups: {
// // Split vendor code to its own chunk(s)
// vendors: {
// name: "vendor",
// test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
// chunks: "all",
// },
// },
// },
},
})}