-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathwebpack.config.js
107 lines (104 loc) · 2.96 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
const path = require("path");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const BundleTracker = require("webpack-bundle-tracker");
module.exports = (env, argv) => {
const isDev = argv.mode === "development";
const nodeModulesDir = path.resolve(__dirname, "node_modules");
const localhostOutput = {
path: path.resolve("./frontend/webpack_bundles/"),
publicPath: "http://localhost:3000/frontend/webpack_bundles/",
filename: "[name].js",
};
const productionOutput = {
path: path.resolve("./frontend/webpack_bundles/"),
publicPath: "auto",
filename: "[name]-[chunkhash].js",
};
return {
mode: isDev ? "development" : "production",
devtool: "source-map",
devServer: {
hot: true,
historyApiFallback: true,
host: "0.0.0.0",
port: 3000,
// Allow CORS requests from the Django dev server domain:
headers: { "Access-Control-Allow-Origin": "*" },
},
context: __dirname,
entry: ["./frontend/js/index.tsx"],
output: isDev ? localhostOutput : productionOutput,
module: {
rules: [
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
use: {
loader: "swc-loader",
},
},
{
test: /\.css$/,
use: [
isDev && "style-loader",
!isDev && MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [["postcss-preset-env"]],
},
},
},
].filter(Boolean),
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
isDev && "style-loader",
// Optimizes CSS in chunks
!isDev && MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
].filter(Boolean),
},
{
test: /\.(svg)(\?v=\d+\.\d+\.\d+)?$/,
type: "asset",
},
{
test: /\.(woff(2)?|eot|ttf|otf)(\?v=\d+\.\d+\.\d+)?$/,
type: "asset",
},
{
test: /\.(png|jpg|jpeg|gif|webp)?$/,
type: "asset",
},
],
},
plugins: [
!isDev &&
new MiniCssExtractPlugin({ filename: "[name]-[chunkhash].css" }),
isDev && new ReactRefreshWebpackPlugin(),
new BundleTracker({
path: __dirname,
filename: "webpack-stats.json",
}),
].filter(Boolean),
resolve: {
modules: [nodeModulesDir, path.resolve(__dirname, "frontend/js/")],
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
optimization: {
minimize: !isDev,
splitChunks: {
// include all types of chunks
chunks: "all",
},
},
};
};