-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.babel.js
252 lines (238 loc) · 5.51 KB
/
webpack.config.babel.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { spawn } from "child_process";
import path from "path";
import webpack from "webpack";
import merge from "webpack-merge";
import HtmlWebpackPlugin from "html-webpack-plugin";
import postcssPresetEnv from "postcss-preset-env";
import AntdScssThemePlugin from "antd-scss-theme-plugin";
import BabiliPlugin from "babili-webpack-plugin";
import TerserPlugin from "terser-webpack-plugin";
import UglifyJsPlugin from "uglifyjs-webpack-plugin";
import BrotliPlugin from "brotli-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import OptimizeCSSAssetsPlugin from "optimize-css-assets-webpack-plugin";
const host = `0.0.0.0`;
const port = 3100;
const src = path.resolve(__dirname, "src");
const isDev = process.env.NODE_ENV === "development";
const cssModuleLoader = {
loader: "css-loader",
options: {
importLoaders: 2,
modules: true,
camelCase: true,
sourceMap: isDev,
localIdentName: isDev
? "[folder]__[name]__[local]__[hash:base64:5]"
: "[hash:base64:5]"
}
};
const cssLoader = {
loader: "css-loader",
options: {
importLoaders: 2,
modules: false,
sourceMap: isDev
}
};
const postCssLoader = {
loader: "postcss-loader",
options: {
ident: "postcss",
sourceMap: isDev,
plugins: () => [postcssPresetEnv()]
}
};
const sassLoader = {
loader: "sass-loader",
options: {
sourceMap: isDev
}
};
const lessLoader = AntdScssThemePlugin.themify({
loader: "less-loader",
options: {
sourceMap: isDev,
javascriptEnabled: true
}
});
const sassHotLoader = {
loader: "css-hot-loader"
};
const sassHotModuleLoader = {
loader: "css-hot-loader",
options: {
cssModule: true
}
};
const assetsLoader = {
loader: "file-loader?name=[name]__[hash:base64:5].[ext]"
};
const babelLoader = [
{
loader: "thread-loader"
},
{
loader: "babel-loader",
options: {
cacheDirectory: true
}
}
];
const babelDevLoader = babelLoader.concat([
"react-hot-loader/webpack",
"eslint-loader"
]);
const config = {
target: "electron-renderer",
base: {
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: isDev ? babelDevLoader : babelLoader,
exclude: /node_modules/
},
{
test: /\.(jpe?g|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/,
use: [assetsLoader]
},
{
test: /\.global|vars\.scss$/,
use: [
sassHotLoader,
MiniCssExtractPlugin.loader,
cssLoader,
postCssLoader,
sassLoader
]
},
{
test: /\.scss$/,
exclude: /\.global|vars\.scss$/,
use: [
sassHotModuleLoader,
MiniCssExtractPlugin.loader,
cssModuleLoader,
postCssLoader,
sassLoader
]
},
{
test: /\.less$/,
use: [
sassHotLoader,
MiniCssExtractPlugin.loader,
cssLoader,
lessLoader
]
}
]
},
plugins: [
new webpack.DefinePlugin({
NODE_ENV: process.env.NODE_ENV
}),
new AntdScssThemePlugin(
path.join(__dirname, "src", "themes/Ant.vars.scss")
),
new MiniCssExtractPlugin({
filename: isDev ? "[name].css" : "[name].[chunkhash].css",
chunkFilename: isDev ? "[id].css" : "[name].[chunkhash].css",
reload: false
}),
new HtmlWebpackPlugin({
template: "public/index.html",
minify: {
collapseWhitespace: !isDev
}
})
]
},
development: {
mode: "development",
plugins: [new webpack.HotModuleReplacementPlugin()],
entry: [
"react-hot-loader/patch",
"webpack/hot/only-dev-server",
src
],
devtool: "cheap-module-source-map",
cache: true,
devServer: {
host,
port,
hot: true,
contentBase: "public",
compress: true,
inline: true,
lazy: false,
stats: "errors-only",
historyApiFallback: {
verbose: true,
disableDotRule: false
},
headers: { "Access-Control-Allow-Origin": "*" },
stats: {
colors: true,
chunks: false,
children: false
},
before() {
spawn("electron", ["."], {
shell: true,
env: process.env,
stdio: "inherit"
})
.on("close", code => process.exit(0))
.on("error", spawnError => console.error(spawnError));
}
},
optimization: {
namedModules: true
},
resolve: {
extensions: [".js", ".jsx", ".json"],
modules: [].concat(src, ["node_modules"]),
alias: {
"react-dom": "@hot-loader/react-dom"
}
}
},
production: {
mode: "production",
entry: {
app: src
},
plugins: [
new BrotliPlugin({
asset: "[path].br[query]",
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8
})
],
output: {
path: __dirname + "/dist",
filename: "[name].[chunkhash].js"
},
optimization: {
minimizer: [
new UglifyJsPlugin(),
new TerserPlugin(),
new BabiliPlugin(),
new OptimizeCSSAssetsPlugin()
],
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all"
}
}
}
}
}
};
export default merge(config.base, config[process.env.NODE_ENV]);