-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
191 lines (179 loc) · 5.49 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
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
const lodash = require('lodash');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const webpack = require('webpack');
function srcPaths(src) {
return path.join(__dirname, src);
}
const isEnvProduction = process.env.NODE_ENV === 'production';
const isEnvDevelopment = process.env.NODE_ENV === 'development';
const isWeb = process.env.PLAT_ENV === 'web';
const isElectron = process.env.PLAT_ENV === 'electron';
// #region Common settings
const commonConfig = {
devtool: isEnvDevelopment ? 'source-map' : false,
mode: isEnvProduction ? 'production' : 'development',
output: { path: srcPaths('dist') },
node: { __dirname: false, __filename: false },
resolve: {
alias: {
_: srcPaths('src'),
_main: srcPaths('src/main'),
_models: srcPaths('src/models'),
_public: srcPaths('public'),
_renderer: srcPaths('src/renderer'),
_utils: srcPaths('src/utils'),
_reducers: srcPaths('src/reducers'),
_components: srcPaths('src/components'),
_hooks: srcPaths('src/hooks'),
},
extensions: ['.js', '.json', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.(woff|woff2)$/,
use: {
loader: 'url-loader',
},
},
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
{
test: /\.(scss|css)$/,
use: [
isEnvDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
],
},
{
test: /\.(jpg|png|svg|ico|icns)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
};
// #endregion
if (isElectron) {
const mainConfig = lodash.cloneDeep(commonConfig);
mainConfig.entry = './src/main/electron.ts';
mainConfig.target = 'electron-main';
mainConfig.output.filename = 'main.bundle.js';
mainConfig.plugins = [
new CopyPlugin({
patterns: [
{
from: 'package.json',
to: 'package.json',
transform: (content, _path) => { // eslint-disable-line no-unused-vars
const jsonContent = JSON.parse(content);
delete jsonContent.devDependencies;
delete jsonContent.scripts;
delete jsonContent.build;
jsonContent.main = './main.bundle.js';
jsonContent.scripts = { start: 'electron ./main.bundle.js' };
jsonContent.postinstall = 'electron-builder install-app-deps';
return JSON.stringify(jsonContent, undefined, 2);
},
},
],
}),
];
const preloadConfig = lodash.cloneDeep(commonConfig);
preloadConfig.entry = './src/main/preload.ts';
preloadConfig.target = 'electron-preload';
preloadConfig.output.filename = 'preload.bundle.js';
const rendererConfig = lodash.cloneDeep(commonConfig);
rendererConfig.entry = './src/renderer/index.tsx';
rendererConfig.target = 'electron-renderer';
rendererConfig.output.filename = 'renderer.bundle.js';
rendererConfig.plugins = [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './public/index.html'),
inject: true,
hash: true,
}),
new webpack.DefinePlugin({
'process.env.PLAT_ENV': JSON.stringify(process.env.PLAT_ENV),
}),
];
if (!isEnvDevelopment) {
// enable in production only
rendererConfig.plugins.push(new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
}));
}
module.exports = [mainConfig, preloadConfig, rendererConfig];
}
if (isWeb) {
const webConfig = lodash.cloneDeep(commonConfig);
webConfig.entry = {
renderer: ['./src/renderer/index.tsx'],
};
webConfig.output = {
filename: 'static/js/[name].[contenthash:8].js',
clean: true,
};
webConfig.performance = {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
};
webConfig.optimization = {
minimize: isEnvProduction,
splitChunks: {
cacheGroups: {
vendor: {
name: 'node_vendors',
test: /[\\/]node_modules[\\/]/,
chunks: 'all',
},
},
},
runtimeChunk: {
name: (entrypoint) => `runtime-${entrypoint.name}`,
},
};
webConfig.plugins = [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './public/index.html'),
favicon: path.resolve(__dirname, './public/favicon.ico'),
// inject: true,
// hash: true,
}),
new CopyPlugin({
patterns: [
{ from: 'public/manifest.json' },
{ from: 'public/robots.txt' },
{ from: 'public/logo192.png' },
{ from: 'public/logo512.png' },
],
}),
new webpack.DefinePlugin({
'process.env.PLAT_ENV': JSON.stringify(process.env.PLAT_ENV),
}),
// new BundleAnalyzerPlugin(),
];
if (!isEnvDevelopment) {
// enable in production only
webConfig.plugins.push(new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
}));
}
module.exports = [webConfig];
}