-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.common.ts
227 lines (212 loc) · 5.88 KB
/
webpack.common.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
import * as webpack from "webpack"
import path from "path"
import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin"
import MiniCssExtractPlugin from "mini-css-extract-plugin"
import WorkboxPlugin from "workbox-webpack-plugin"
import WebpackPwaManifest from "webpack-pwa-manifest"
import CopyPlugin from "copy-webpack-plugin"
import { CleanWebpackPlugin } from "clean-webpack-plugin"
import HtmlWebpackPlugin from "html-webpack-plugin"
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"
import { version } from "./package.json"
const webpaclAlias = {
"@": path.resolve(__dirname, "src"),
}
export const isSSR = process.env.SSR === "true"
export const isDEV = process.env.NODE_ENV === "development"
export const isCSRDEV = isDEV && !isSSR
export const isSSRDEV = isDEV && isSSR
const isWebpackBuildServer = process.env.BUILD === "server"
const babelPresets = (env?: "node" | "legacy") => {
const common: [string, any] = [
"@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 if (env === "legacy") {
common[1].targets = {
ios: "9",
safari: "9",
}
common[1].bugfixes = false
} else {
common[1].targets = {
esmodules: true,
}
}
return common
}
const webpackPlugins = () => {
const webpackCommonPlugins = isCSRDEV
? [
new webpack.DefinePlugin({
"process.env.SSR": JSON.stringify(process.env.SSR),
__VERSION__: JSON.stringify(version).replaceAll(".", "-"),
}),
new ReactRefreshWebpackPlugin(),
]
: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
"process.env.SSR": JSON.stringify(process.env.SSR),
__VERSION__: JSON.stringify(version).replaceAll(".", "-"),
}),
]
const webpackpPlugins: any[] = [
...webpackCommonPlugins,
!isWebpackBuildServer
? new HtmlWebpackPlugin(
isCSRDEV
? {
title: "music-motion",
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 WorkboxPlugin.InjectManifest({
swSrc: path.join(process.cwd(), "./src/src-service-worker.js"),
swDest: "service-worker.js",
exclude: [
/\.map$/,
/manifest$/,
/\.htaccess$/,
/service-worker\.js$/,
/\.webp$/,
],
}),
new WebpackPwaManifest({
name: "pika-music",
short_name: "pika-music",
description: "A PWA Music Web Site",
display: "standalone",
start_url: "/",
background_color: "#ffffff",
theme_color: "#FEDD27",
inject: true,
fingerprints: false,
ios: true,
scope: "/",
icons: [
{
src: path.resolve(
__dirname,
"./src/assets/pika_tail_192x192.png",
),
sizes: [192, 192],
type: "image/png",
ios: true,
},
{
src: path.resolve(
__dirname,
"./src/assets/pika_tail_512x512.png",
),
sizes: [512, 512],
type: "image/png",
ios: true,
},
{
src: path.resolve(
__dirname,
"./src/assets/pika_tail_152x152.png",
),
sizes: [152, 152],
type: "image/png",
ios: true,
},
],
}),
)
webpackpPlugins.push(
new CopyPlugin({
patterns: [
{
from: "./src/assets/favicon.ico",
to: "./favicon.ico",
},
],
}),
)
}
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: any[] = [
isCSRDEV ? "react-refresh/babel" : "",
!isDEV
? ["transform-remove-console", { exclude: ["error", "warn", "info"] }]
: "",
].filter((p) => !!p)
const webpackpPlugins = webpackPlugins()
const cssProcessLoaders = cssProcessLoader()
export {
cssProcessLoaders,
webpackpPlugins,
babelPlugin,
babelPresets,
webpaclAlias,
}