-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.common.js
124 lines (114 loc) · 2.58 KB
/
webpack.common.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
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const isDEV = process.env.NODE_ENV === "development"
function findPara(param) {
let result = ""
process.argv.forEach(argv => {
if (argv.indexOf(`--${param}`) === -1) return
// eslint-disable-next-line prefer-destructuring
result = argv.split("=")[1]
})
return result
}
const isServerBuild = findPara("build") === "server"
const setWebpackPlugins = () => {
const plugins = []
if (!isDEV) {
plugins.push(new CleanWebpackPlugin())
}
return plugins
}
exports.isDEV = isDEV
exports.babelPlugins = [
"babel-plugin-styled-components",
"@babel/plugin-proposal-class-properties",
]
exports.webpackResolve = {
extensions: [".jsx", ".js", ".mjs"],
}
exports.webpackPlugins = setWebpackPlugins()
exports.webpackSplitChunks = {
chunks: "all",
// minSize: 30000,
// maxSize: 0,
minChunks: 1,
maxAsyncRequests: 6,
maxInitialRequests: 4,
automaticNameDelimiter: "-",
// automaticNameMaxLength: 30,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: 2,
chunks: "all",
name: "vendors",
},
default: {
name: "commmon",
minChunks: 2,
priority: 1,
reuseExistingChunk: true,
},
},
}
exports.commonRules = () => {
const rules = [
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
{
loader: "file-loader",
options: {
name: "[name]-[hash:6].[ext]",
publicPath: isDEV ? "/images" : "/public/images",
outputPath: "images",
emitFile: !isServerBuild,
},
},
// {
// loader: "image-webpack-loader",
// options: {
// // optipng.enabled: false will disable optipng
// optipng: {
// enabled: true,
// },
// pngquant: {
// quality: [0.65, 0.9],
// speed: 4,
// },
// },
// },
],
},
]
return rules
}
exports.babelPresets = env => {
const common = [
"@babel/preset-env",
{
// targets: { esmodules: true },
useBuiltIns: "usage",
modules: false,
debug: false,
bugfixes: true,
corejs: { version: 3, proposals: true },
},
]
if (env === "node") {
common[1].targets = {
node: "14",
}
} else if (env === "legacy") {
common[1].targets = {
ios: "9",
safari: "9",
}
common[1].bugfixes = false
} else {
common[1].targets = {
esmodules: true,
}
}
return common
}
exports.getCommandArg = findPara