-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildPlugins.ts
78 lines (70 loc) · 2 KB
/
buildPlugins.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
import HtmlWebpackPlugin from 'html-webpack-plugin'
import webpack, { ProgressPlugin } from 'webpack'
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
import MiniCSSExtractPlugin from 'mini-css-extract-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import CircularDependencyPlugin from 'circular-dependency-plugin'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import type { BuildOptions } from './types/config'
export default ({
paths,
isDev,
isAnalyze,
apiURL,
project,
}: BuildOptions): webpack.WebpackPluginInstance[] => {
const isProd = !isDev
const plugins: webpack.WebpackPluginInstance[] = [
new HtmlWebpackPlugin({
template: paths.html,
}),
new ProgressPlugin(),
new webpack.DefinePlugin({
/* eslint-disable @typescript-eslint/naming-convention */
__IS_DEV__: JSON.stringify(isDev),
__API__: JSON.stringify(apiURL),
__PROJECT__: JSON.stringify(project),
/* eslint-enable @typescript-eslint/naming-convention */
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
}),
]
const devPlugins: webpack.WebpackPluginInstance[] = [
new ReactRefreshWebpackPlugin(),
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true,
}),
]
const prodPlugins: webpack.WebpackPluginInstance[] = [
new MiniCSSExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].css',
}),
new CopyWebpackPlugin({
patterns: [
{
from: paths.locales,
to: paths.buildLocales,
},
],
}),
]
if (isDev) {
plugins.push(...devPlugins)
}
if (isProd) {
plugins.push(...prodPlugins)
}
if (isAnalyze) {
plugins.push(new BundleAnalyzerPlugin())
}
return plugins
}