-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
166 lines (153 loc) · 3.85 KB
/
webpack.common.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
const {
DefinePlugin
} = require('webpack')
const path = require('path')
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const WebpackBar = require('webpackbar');
const {
CleanWebpackPlugin
} = require('clean-webpack-plugin')
const webpaclAlias = {
'@': path.resolve(__dirname, 'src'),
}
const isSSR = process.env.SSR === 'true'
const isDEV = process.env.NODE_ENV === 'development'
const isCSRDEV = isDEV && !isSSR
const isSSRDEV = isDEV && isSSR
const isWebpackBuildServer = process.env.BUILD === 'server'
const babelPresets = (env) => {
const common = [
'@babel/preset-env',
{
useBuiltIns: 'usage',
shippedProposals: true,
modules: false,
debug: false,
bugfixes: true,
corejs: {
version: '3.21.1'
},
},
]
if (env === 'node') {
common[1].targets = {
node: '14',
}
} else {
common[1].targets = {
esmodules: true,
}
}
return common
}
const webpackPlugins = () => {
const webpackCommonPlugins = isCSRDEV ? [
new DefinePlugin({
'process.env.SSR': JSON.stringify(process.env.SSR),
}),
new ReactRefreshWebpackPlugin(),
] : [
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.SSR': JSON.stringify(process.env.SSR),
}),
]
const webpackpPlugins = [
...webpackCommonPlugins,
!isWebpackBuildServer ?
new HtmlWebpackPlugin(
isCSRDEV ? {
title: 'ssr',
template: path.resolve(__dirname, 'index.html'),
} : {
minify: {
collapseWhitespace: true,
removeComments: false,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
inject: true,
template: path.resolve(__dirname, 'index.html'),
}
) :
null,
!isWebpackBuildServer ? new ForkTsCheckerWebpackPlugin() : null,
!isCSRDEV && new CleanWebpackPlugin(),
].filter((p) => !!p)
if (!isCSRDEV) {
const mincssOptions = {
filename: isSSRDEV ? '[name].css' : '[name]-[contenthash].css',
chunkFilename: isSSRDEV ?
'[name].chunk.css' : '[name].[contenthash].chunk.css',
ignoreOrder: true,
}
webpackpPlugins.push(
new MiniCssExtractPlugin(
!isWebpackBuildServer ? {
linkType: 'text/css',
...mincssOptions,
} : {
linkType: false,
runtime: false,
...mincssOptions
}
)
)
}
if (!isWebpackBuildServer) {
!isDEV &&
webpackpPlugins.push(
new CopyPlugin({
patterns: [{
from: './src/assets/favicon.ico',
to: './favicon.ico',
}, ],
})
)
}
webpackpPlugins.push(new WebpackBar({}))
return webpackpPlugins
}
const cssProcessLoader = () => {
const cssProcessLoaders = [{
loader: 'css-loader',
options: {
importLoaders: 1,
sourceMap: !isDEV,
},
},
'postcss-loader',
]
if (isCSRDEV) {
cssProcessLoaders.unshift('style-loader')
} else {
cssProcessLoaders.unshift(MiniCssExtractPlugin.loader)
}
return cssProcessLoaders
}
const babelPlugin = [
isCSRDEV ? 'react-refresh/babel' : '',
!isDEV ? ['transform-remove-console', {
exclude: ['error', 'warn', 'info']
}] :
'',
].filter((p) => !!p)
const webpackpPlugins = webpackPlugins()
const cssProcessLoaders = cssProcessLoader()
module.exports = {
cssProcessLoaders,
webpackpPlugins,
babelPlugin,
babelPresets,
webpaclAlias,
isSSR,
isDEV,
isCSRDEV,
isSSRDEV
}