-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
206 lines (194 loc) · 5.34 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//launching .env
require('dotenv').config();
const path = require('path');
const tsconfig = require('./tsconfig.json');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const {
mode,
isDevelopment,
isProduction,
definedVariables,
isElectron,
} = require('./electron-config/env.config');
const aliasResolver = (alias) => {
const resAlias = {};
Object.entries(alias).forEach((en) => {
resAlias[en[0].replace('/*', '')] = path.resolve(
__dirname,
en[1][0].replace('/*', ''),
);
});
return resAlias;
};
module.exports = {
mode: mode,
entry: ['./src/renderer/index.tsx'],
output: {
path: `${__dirname}/build/renderer`,
publicPath: '/',
filename: isProduction ? '[name].[contenthash:8].js' : '[name].bundle.js',
chunkFilename: isProduction
? '[name].[contenthash:8].chunk.js'
: '[name].chunk.js',
assetModuleFilename: 'assets/[contenthash][ext][query]',
},
module: {
rules: [
//Type Script
{
test: /\.tsx?$/,
use: [
{
loader: 'babel-loader',
options: {
plugins: [
isDevelopment && require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
// { loader: 'ts-loader' },disabled for now.(testing babel instead)
],
exclude: /node_modules/,
},
//scss|sass file rules
...[
//SCSS files
//load sass files and ignore module extension
{
test: /\.s?(a|c)ss$/,
sideEffects: true,
exclude: /\.module.(s(a|c)ss)$/,
use: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { sourceMap: true, importLoaders: 1 },
},
'postcss-loader',
{ loader: 'sass-loader', options: { sourceMap: true } },
].filter(Boolean),
},
//module style files
//load module style files only
{
test: /\.module\.s(a|c)ss$/,
sideEffects: true,
use: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { sourceMap: true, importLoaders: 1, modules: true },
},
'postcss-loader',
{ loader: 'sass-loader', options: { sourceMap: true } },
].filter(Boolean),
},
],
//SVG rules
...[
//load *.svg?url as svg file
{
test: /\.svg$/i,
type: 'asset',
resourceQuery: /url/, // *.svg ?url
},
//load *.svg as react components
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/, // only work ib jsx/tsx file
resourceQuery: { not: [/url/, /icon/] },
use: ['@svgr/webpack'],
},
//load *.svg?icon as react components with viewbox.
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: /icon/,
use: [
{
loader: '@svgr/webpack',
options: {
icon: true,
},
},
],
},
],
{
test: /\.png/,
type: 'asset/resource',
},
{
test: /\.mp3/,
type: 'asset/resource',
generator: {
filename: 'audio/[contenthash][ext][query]',
},
},
{
test: [/\.(woff|ttf)$/],
type: 'asset/resource',
generator: {
filename: 'font/[hash][ext][query]',
},
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.scss'],
alias: {
...aliasResolver({
...tsconfig.compilerOptions.paths,
styles: ['./src/renderer/assets/styles'],
font: ['./src/renderer/assets/font'],
icon: ['./src/renderer/assets/icon'],
}),
},
},
plugins: [
//Ts error
isDevelopment && new ForkTsCheckerWebpackPlugin(),
definedVariables,
new HtmlWebpackPlugin({
favicon: 'public/favicon.ico',
template: 'public/index.html',
}),
new MiniCssExtractPlugin({
filename: isDevelopment ? '[name].css' : '[name].[contenthash].css',
chunkFilename: isDevelopment ? '[id].css' : '[id].[contenthash].css',
}),
,
isDevelopment && new ReactRefreshWebpackPlugin({ overlay: false }),
].filter(Boolean),
devServer: {
static: path.resolve(__dirname, 'public'),
port: 4000,
hot: true,
historyApiFallback: true,
open: true,
},
devtool: isDevelopment ? 'eval-source-map' : 'source-map',
target: isDevelopment ? 'web' : 'browserslist',
optimization: {
minimize: isProduction,
minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
nodeEnv: mode,
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
experiments: {
topLevelAwait: true,
},
};