This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
webpack.config.js
159 lines (156 loc) · 4.74 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
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
const CopyPlugin = require("copy-webpack-plugin");
// const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
require("dotenv").config();
const isProduction = process.env.NODE_ENV === "production";
module.exports = {
mode: isProduction ? "production" : "development",
context: path.resolve(__dirname, "src"),
entry: "./index.js", // starting point - all dependent files/dependencies will be linked from here
output: {
path: path.resolve(__dirname, "./build"),
filename: "static/js/[name].[chunkhash:8].js",
assetModuleFilename: "static/media/[name].[hash][ext]",
publicPath: "/",
clean: true,
/**
* This is to show the source files in static/js folder instead of webpack://
* @param {*} info
* @returns
*/
devtoolModuleFilenameTemplate: info => {
return path
.relative(path.resolve(__dirname, "src"), info.absoluteResourcePath)
.replace(/\\/g, "/");
},
},
devtool: isProduction ? "source-map" : "cheap-module-source-map",
performance: false,
target: "web",
resolve: {
extensions: [".js", ".jsx", ".css", ".svg"],
fallback: {
path: false,
fs: false,
events: false,
},
},
ignoreWarnings: [/Failed to parse source map/], // some libraries do not provide proper source maps which throws this warning
module: {
rules: [
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
},
{
test: /\.jsx?$/,
use: [
{
loader: "esbuild-loader",
options: {
loader: "jsx",
target: "es6",
},
},
],
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"], // This order is to be maintained
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|svg)$/i,
type: "asset/resource", // This will be outputted as a separate file
},
{
test: /\.(woff(2)?|eot|ttf|otf|)$/,
type: "asset/inline", // These will be embedded in html itself.
},
],
},
optimization: {
splitChunks: {},
runtimeChunk: false,
minimize: process.env.NODE_ENV === "production",
minimizer: [
new TerserPlugin({
minify: TerserPlugin.esbuildMinify,
}),
],
},
plugins: [
new webpack.ProgressPlugin(),
new ESLintPlugin({ fix: true }),
/**
* The following plugin creates asset-manifest.json with links to all generated files
* .map(sourcemap) files are excluded.
*/
new WebpackManifestPlugin({
fileName: "asset-manifest.json",
publicPath: "/",
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
manifest[file.name] = file.path;
return manifest;
}, seed);
const entrypointFiles = entrypoints.main.filter(
fileName => !fileName.endsWith(".map")
);
return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
// ...(!isProduction ? [new BundleAnalyzerPlugin()] : []),
// This outputs css as file
new MiniCssExtractPlugin({
filename: "static/css/[name].[chunkhash:8].css",
}),
// This is to copy from public to build except index.html and manifest.json
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, "./public"),
to: path.resolve(__dirname, "./build"),
filter: async resourcePath => {
if (/index.html|manifest.json/.test(resourcePath)) {
return false;
}
return true;
},
},
],
}),
// This plugin injects generated files into index.html file
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "./public/index.html"),
base: "/",
}),
// React will be made available globally
new webpack.ProvidePlugin({
React: "react",
}),
// This handle env variables and provides them as process.env globally
// webpack5 doesn't support process, hence this is needed.
new webpack.DefinePlugin({
"process.env": JSON.stringify(process.env),
}),
],
devServer: {
historyApiFallback: true,
static: path.resolve(__dirname, "./public"),
open: true,
compress: true,
hot: true,
port: process.env.PORT || 3000,
allowedHosts: "all",
},
};