-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
82 lines (76 loc) · 2.09 KB
/
webpack.config.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
import path from "path";
import { type Configuration, DefinePlugin } from "webpack";
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import { loadEnvs, mapEnvsToConfig } from "./config/env";
import { createDevServerConfig } from "./config/devServer";
const createBundleAnalyzerConfig = () => {
const isAnalyzeMode = process.env.ANALYZE_BUILD;
const config: BundleAnalyzerPlugin["opts"] = isAnalyzeMode
? { analyzerMode: "server" }
: { analyzerMode: "disabled" };
return config;
};
const createWebpackConfig = (mode: Configuration["mode"]): Configuration => {
const envs = loadEnvs();
const envsConfig = mapEnvsToConfig(envs);
const bundleAnalyzerConfig = createBundleAnalyzerConfig();
return {
mode: mode,
entry: path.resolve(process.cwd(), "src/index.ts"),
module: {
rules: [
{
test: /\.(js|mjs|ts|tsx)$/,
include: path.resolve(process.cwd(), "src"),
loader: "babel-loader",
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: "asset/resource",
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: "asset/resource",
},
{
test: /\.css$/i,
use: [
mode === "production"
? MiniCssExtractPlugin.loader
: "style-loader",
"css-loader",
"postcss-loader",
],
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
...(mode === "development"
? {
devServer: createDevServerConfig(),
}
: {}),
output: {
path: path.resolve(process.cwd(), "dist"),
publicPath: "/",
clean: true,
},
plugins: [
new DefinePlugin(envsConfig),
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(process.cwd(), "public/index.html"),
}),
mode === "production" && new MiniCssExtractPlugin(),
mode === "production" && new CssMinimizerPlugin(),
mode === "production" &&
new BundleAnalyzerPlugin(bundleAnalyzerConfig),
],
};
};
export default createWebpackConfig;