This repository has been archived by the owner on Jun 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
webpack.config.base.js
176 lines (150 loc) · 3.7 KB
/
webpack.config.base.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
/* eslint-disable */
const { resolve, join } = require('path');
const { writeFileSync, readFileSync, existsSync } = require('fs');
const { merge } = require('webpack-merge');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const createStyledComponentsTransformer = require('typescript-plugin-styled-components')
.default;
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
/* eslint-enable */
const INCLUDE = resolve(__dirname, 'src');
const BUILD_FLAGS = {
ENABLE_EXTENSIONS: true,
ENABLE_AUTOFILL: false,
};
process.env = {
...process.env,
...BUILD_FLAGS,
};
const dev = process.env.DEV === '1';
process.env.NODE_ENV = dev ? 'development' : 'production';
const styledComponentsTransformer = createStyledComponentsTransformer({
minify: !dev,
displayName: dev,
});
const config = {
mode: dev ? 'development' : 'production',
devtool: dev ? 'eval-source-map' : false,
output: {
path: resolve(__dirname, 'build'),
filename: '[name].bundle.js',
},
module: {
rules: [
{
test: /\.(png|gif|jpg|woff2|ttf|svg)$/,
include: INCLUDE,
use: [
{
loader: 'file-loader',
options: {
esModule: false,
outputPath: 'res',
},
},
],
},
{
test: /\.tsx|ts$/,
use: [
{
loader: 'ts-loader',
options: {
experimentalWatchApi: dev,
transpileOnly: true, // |dev| to throw CI when a ts error occurs
getCustomTransformers: () => ({
before: [styledComponentsTransformer],
}),
},
},
],
include: INCLUDE,
},
],
},
node: {
__dirname: false,
__filename: false,
},
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.jsx', '.tsx', '.ts', '.json'],
alias: {
'~': INCLUDE,
},
plugins: [new TsconfigPathsPlugin()],
},
plugins: [
new webpack.EnvironmentPlugin(['NODE_ENV', ...Object.keys(BUILD_FLAGS)]),
],
externals: {
keytar: `require('keytar')`,
electron: 'require("electron")',
fs: 'require("fs")',
os: 'require("os")',
path: 'require("path")',
},
optimization: {
minimizer: !dev
? [
new TerserPlugin({
extractComments: true,
terserOptions: {
ecma: 8,
output: {
comments: false,
},
},
parallel: true,
}),
]
: [],
},
};
if (dev) {
config.module.rules[1].use.splice(0, 0, {
loader: 'babel-loader',
options: { plugins: ['react-refresh/babel'] },
});
}
function getConfig(...cfg) {
return merge(config, ...cfg);
}
const getHtml = (name) => {
return new HtmlWebpackPlugin({
title: 'Wexond',
template: 'static/pages/app.html',
filename: `${name}.html`,
chunks: [name],
});
};
const applyEntries = (config, entries) => {
for (const entry of entries) {
config.entry[entry] = [
`./src/renderer/pre-entry`,
`./src/renderer/views/${entry}`,
];
config.plugins.push(getHtml(entry));
}
};
const getBaseConfig = (name) => {
const config = {
plugins: [],
output: {},
entry: {},
optimization: {
runtimeChunk: {
name: `runtime.${name}`,
},
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
},
},
};
return config;
};
module.exports = { getConfig, dev, getHtml, applyEntries, getBaseConfig };