forked from My-Project-Template/solid-ts-webpack5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
312 lines (298 loc) · 10.7 KB
/
index.ts
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { resolve as pathResolve } from 'path';
import Config from 'webpack-chain';
import compose from 'compose-function';
import { loadStyles, loadJs } from './modules';
// plugins
import { DefinePlugin } from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import ESLintPlugin from 'eslint-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import { EsbuildPlugin } from 'esbuild-loader';
// types
import type { MinifyOptions } from 'terser';
import type { LoaderOptions as EsbuildLoaderOptions } from 'esbuild-loader';
type ConditionalConfigurationComposeCallback = (conf: Config) => Config;
/**
* @description Modify the relative path to the root path of the project using `path.resolve`
* @param suffix the relative path relative to the root path of the project
* @returns the real path
*/
const withBasePath = (suffix = '') => pathResolve(__dirname, `../${suffix}`);
/**
* @description get kb
* @param kbNum kb's num, default 1
* @returns kb
*/
const kb = (kbNum = 1) => 1024 * kbNum;
const { uglifyJsMinify: minify } = TerserPlugin;
/** @description Self-defined options. */
type SelfDefineOptions = Partial<{
/** HTML Title */
title: string;
/** Language of the project */
lang: string;
/** For development conf */
isDev: boolean;
/** For production conf */
isProd: boolean;
/** Whether open esbuild when At dev or not */
isEsbuildInDev: boolean;
/** for esbuild loader options */
esbuildLoaderOptions: EsbuildLoaderOptions;
/** Whether open source map for styles or not */
isOpenStyleSourceMap: (() => boolean) | boolean;
/** babel only compile, which is more important than `babelNotCompiles` */
babelOnlyCompiles: (string | RegExp)[];
/** babel not compile */
babelNotCompiles: (string | RegExp)[];
}>;
/**
* Generate a basic config
* @param options config options
* @returns basic webpack conf
*/
export const createBasicConfig = (options: SelfDefineOptions = {}): Config => {
const {
title = 'react-ts-webpack-starter',
lang = 'en',
isDev = true,
isProd = false,
isEsbuildInDev = true,
isOpenStyleSourceMap = false,
esbuildLoaderOptions = { target: 'es2020' },
babelOnlyCompiles = [],
babelNotCompiles = [/node_modules/],
} = options || {};
// basic configuration for styles
const basicStyleConf = {
isDev,
isOpenSourceMap: isOpenStyleSourceMap,
};
// configuration of loading styles
const takeConditionalConfiguration: ConditionalConfigurationComposeCallback = compose(
(conf: Config) =>
loadStyles(conf, {
...basicStyleConf,
styleType: 'sass',
}),
(conf: Config) =>
loadStyles(conf, {
...basicStyleConf,
styleType: 'scss',
styleResourcePatterns: [
// use scss
withBasePath('/src/assets/scss/_globals.scss'),
],
}),
(conf: Config) =>
loadStyles(conf, {
...basicStyleConf,
styleType: 'css',
}),
(conf: Config) =>
loadJs(conf, {
isProd,
isEsbuildInDev,
esbuildLoaderOptions,
notCompiles: babelNotCompiles,
onlyCompiles: babelOnlyCompiles,
})
);
return takeConditionalConfiguration(
new Config()
// set context
.context(withBasePath())
// set entry
.entry('index')
.add(withBasePath('src/index.tsx'))
.end()
// output
.output.path(withBasePath('dist'))
.hashFunction('xxhash64')
.filename('js/[name].[contenthash].bundle.js')
.chunkFilename('js/[name].[contenthash].js')
// Set output.clean to replace CleanWebpackPlugin. See: https://webpack.js.org/configuration/output/#outputclean
.set('clean', true)
.end()
// set alias
.resolve.alias.set('@', withBasePath('src'))
.end()
.extensions.add('.js')
.add('.jsx')
.add('.ts')
.add('.tsx')
.add('.json')
.add('.cjs')
.add('.mjs')
.end()
.end()
.module.rule('fonts')
.test(/\.(woff2?|eot|[ot]tf)$/i)
.set('type', 'asset/resource')
.end()
.rule('pics')
.test(/\.(png|jpe?g|gif)$/i)
.set('type', 'asset/resource')
.set('generator', {
filename: 'static/[hash][ext][query]',
})
.parser({
dataUrlCondition: {
// 10kb
maxSize: kb(10),
},
})
.end()
.end()
// set plugins
.plugin('HtmlWebpackPlugin')
.use(HtmlWebpackPlugin, [
{
template: withBasePath('html/index.htm'),
templateParameters: { lang },
inject: 'body',
favicon: withBasePath('html/favicon.ico'),
title,
},
])
.end()
.plugin('DefinePlugin')
.use(DefinePlugin, [{ isDev, isProd }])
.end()
// check ts
.plugin('ForkTsCheckerWebpackPlugin')
.use(ForkTsCheckerWebpackPlugin, [{ devServer: false }])
.end()
// split chunks
.optimization.splitChunks({
chunks: 'all',
minSize: 15000,
})
.end()
// set in development mode
.when(isDev, configure => {
configure
.devtool('source-map')
.mode('development')
// set devServer
.devServer.compress(true)
.port(9222)
.hot(true)
.open(false)
.set('client', {
overlay: {
errors: true,
warnings: false,
runtimeErrors: true,
},
})
.end()
// check ts in dev environment
.plugin('ForkTsCheckerWebpackPlugin')
.tap(args => {
const [oldConf] = args;
return [
{
...oldConf,
devServer: true,
},
];
})
.end()
.plugin('ESLintPlugin')
.use(ESLintPlugin, [
{
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json', '.mjs', '.cjs'],
fix: true,
threads: true,
},
])
.end()
// config esbuild
.when(isEsbuildInDev, conf => {
conf.optimization.minimizer('EsbuildPlugin').use(EsbuildPlugin, [
{
target: esbuildLoaderOptions?.target || 'es2020',
},
]);
});
})
// set in production mode
.when(isProd, configure => {
configure
.devtool('eval')
.mode('production')
.optimization.minimize(true)
.minimizer('TerserPlugin')
.use(TerserPlugin<MinifyOptions>, [
{
extractComments: true,
minify,
terserOptions: {
ecma: 5,
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
])
.end()
.minimizer('CssMinimizerPlugin')
.use(CssMinimizerPlugin)
.end()
.splitChunks({
cacheGroups: {
defaultVendors: {
name: 'chunk-vendors',
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'initial',
},
common: {
name: 'chunk-common',
minChunks: 2,
priority: -20,
chunks: 'initial',
reuseExistingChunk: true,
},
},
})
.set('realContentHash', false)
// html webpack plugin
.end()
.plugin('HtmlWebpackPlugin')
.tap(([oldConf]) => [
{
...oldConf,
minify: true,
},
])
.end()
.plugin('MiniCssExtractPlugin')
.use(MiniCssExtractPlugin, [{ filename: 'style/[name]-[contenthash].css' }])
.end()
// check ts in prod environment
.plugin('ForkTsCheckerWebpackPlugin')
.tap(args => {
const [oldConf] = args;
return [
{
...oldConf,
devServer: false,
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
},
];
})
.end();
})
);
};