-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.config.js
260 lines (255 loc) · 7.81 KB
/
webpack.config.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
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
const fs = require("fs");
const { resolve, basename } = require("path");
const autoprefixer = require("autoprefixer");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ForkTsCheckerPlugin = require("fork-ts-checker-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const OfflinePlugin = require("@lcdp/offline-plugin");
const ZipPlugin = require("zip-webpack-plugin");
const ReactRefreshPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
// these are the only date-fns locales we'll bother to bundle
const supportedLocales = ["en-US", "en-AU", "en-GB", "ja", "id", "es"];
const packageJson = require("./package.json");
/** @returns {import("webpack").Configuration} */
module.exports = function (env = {}, argv = {}) {
const isProd = !env.dev;
const serve = !!env.dev;
const version = env.version || "custom";
const zip = !!env.zip;
return {
target: "web",
mode: isProd ? "production" : "development",
devtool: isProd ? "source-map" : "inline-cheap-module-source-map",
devServer: !serve
? undefined
: {
static: "./dist",
hot: true,
host: "0.0.0.0",
},
entry: "./src/index.tsx",
output: {
filename: "[name].[chunkhash:5].js",
path: resolve(__dirname, "./dist"),
},
optimization: {
minimize: isProd,
minimizer: ["...", new CssMinimizerPlugin()],
},
performance: {
hints: false,
},
stats: {
colors: true,
logging: "warn",
children: false,
assets: false,
modules: false,
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".css", ".json"],
alias: {
peerjs$: resolve(__dirname, "node_modules/peerjs/dist/peerjs.esm.js"),
},
},
module: {
rules: [
// babel transformation for my own typescript files
{
enforce: "pre",
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
[
require("@babel/preset-env"),
{ targets: { browsers: [">1%"], ios: "12.5" } },
],
require("@babel/preset-typescript"),
],
plugins: [
require("@babel/plugin-transform-optional-chaining"),
require("@babel/plugin-transform-class-properties"),
require("@babel/plugin-syntax-dynamic-import"),
[
require("@babel/plugin-transform-react-jsx"),
{ runtime: "automatic" },
],
require("@babel/plugin-transform-react-jsx-source"),
!isProd ? require("react-refresh/babel") : null,
].filter(Boolean),
cacheDirectory: true,
},
},
},
// pass my own css files through css-loader with modules enabled and postcss+autoprefixer
{
test: /\.css$/,
exclude: /node_modules/,
use: [
isProd ? MiniCssExtractPlugin.loader : "style-loader",
{
loader: "css-loader",
options: {
modules: {
localIdentName: "[local]__[hash:base64:5]",
namedExport: false,
},
importLoaders: 1,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [autoprefixer],
},
},
},
],
},
// pass 3rd party css through css-loader without transforms
{
test: /\.css$/,
include: resolve(__dirname, "node_modules"),
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
// other asset files
{
test: /\.(woff2?|ttf|svg|eot|jpe?g|png|gif|mp4|mov|ogg|webm)(\?.*)?$/i,
type: "asset/resource",
},
// process the html template too
{
test: /\.html$/i,
use: {
loader: "html-loader",
options: {
sources: {
/**
*
* @param {string} attribute
* @param {string} value
* @param {string} resourcePath
* @returns {boolean}
*/
urlFilter(attribute, value, resourcePath) {
if (value.startsWith("/favicons/")) {
return false;
}
return true;
},
},
},
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new ForkTsCheckerPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: "jackets/**/*",
context: "src/assets",
globOptions: {
ignore: ["**/.DS_Store"],
},
},
],
}),
new webpack.ProgressPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
// exclude locale bundles we don't use
new webpack.ContextReplacementPlugin(
/^date-fns[/\\]locale$/,
new RegExp(
`\\.[/\\\\](${supportedLocales.join("|")})[/\\\\]index\\.js$`,
),
),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(
isProd ? "production" : "development",
),
"process.env.DATA_FILES": JSON.stringify(
fs.readdirSync(resolve(__dirname, "src/songs")).map((file) => {
const fileContents = JSON.parse(
fs.readFileSync(resolve(__dirname, "src/songs", file)),
);
return {
name: basename(file, ".json"),
display: fileContents.i18n.en.name,
parent: fileContents.meta.menuParent || "",
};
}),
),
}),
new MiniCssExtractPlugin({
filename: "[name].[chunkhash:5].css",
}),
new FaviconsWebpackPlugin({
logo: "./src/assets/ddr-tools-256.png",
inject: true,
prefix: "favicons/",
favicons: {
appName: "DDR Tools - card draw and more!",
appShortName: "DDR Tools",
theme_color: "#28b6ea",
display: "standalone",
scope: "/",
start_url: "/",
manifestMaskable: true,
icons: {
windows: false,
yandex: false,
firefox: false,
coast: false,
appleStartup: false,
android: {
offset: 15,
},
appleIcon: {
offset: 10,
},
},
},
}),
new HtmlWebpackPlugin({
title: "DDR Tools - card draw and more!",
filename: "index.html",
meta: {
description: packageJson.description,
viewport: "width=device-width, initial-scale=1",
},
template: "src/index.html",
}),
].concat(
!isProd
? [new ReactRefreshPlugin({ overlay: false })]
: zip
? [
new ZipPlugin({
path: __dirname,
filename: `DDRCardDraw-${version}.zip`,
exclude: "__offline_serviceworker",
}),
]
: [
new OfflinePlugin({
ServiceWorker: {
events: true,
},
excludes: ["../*.zip", "jackets/**/*", "favicons/*"],
}),
],
),
};
};