-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
114 lines (112 loc) · 3.19 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
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const BundleTracker = require("webpack-bundle-tracker");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
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.js"],
output: isDev ? localhostOutput : productionOutput,
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: nodeModulesDir,
use: [
{
loader: require.resolve("babel-loader"),
options: {
plugins: [
isDev && require.resolve("react-refresh/babel"),
].filter(Boolean),
},
},
],
},
{
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"],
},
optimization: {
minimize: !isDev,
splitChunks: {
// include all types of chunks
chunks: "all",
},
},
};
};