-
Notifications
You must be signed in to change notification settings - Fork 37
/
webpack.config.js
186 lines (180 loc) · 5.29 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
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const path = require('path');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const PROD = process.env.NODE_ENV === 'production';
const SOURCE_MAPS = process.env.SOURCE_MAPS ?? false;
const ANALYZE = process.env.ANALYZE ?? false;
const NO_MANIFEST = !!process.env.NO_MANIFEST ?? false;
const PROJECT_ROOT = __dirname;
console.log(PROD ? 'PRODUCTION BUILD' : 'DEVELOPMENT BUILD');
const BUNDLE_TO_TEMPLATE_MAP = {
admin: PROJECT_ROOT + '/tracker/templates/ui/index.html',
tracker: PROJECT_ROOT + '/tracker/templates/ui/index.html',
processing: PROJECT_ROOT + '/tracker/templates/ui/minimal.html',
};
function generateHTMLWebpackPlugins() {
return Object.entries(BUNDLE_TO_TEMPLATE_MAP).map(([bundle, template]) => {
return new HTMLWebpackPlugin({
template,
chunks: [bundle],
filename: PROJECT_ROOT + `/tracker/templates/ui/generated/${bundle}.html`,
minify: false,
inject: false,
});
});
}
function compact(array) {
return [...array].filter(n => !!n);
}
module.exports = {
context: PROJECT_ROOT,
mode: PROD ? 'production' : 'development',
entry: {
admin: './bundles/admin',
tracker: './bundles/tracker',
processing: './bundles/processing',
},
output: {
filename: PROD ? 'tracker-[name]-[contenthash].js' : 'tracker-[name].js',
pathinfo: true,
path: PROJECT_ROOT + '/tracker/static/gen',
publicPath: '/static/gen',
},
stats: 'minimal',
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
{
loader: 'swc-loader',
options: {
jsc: {
assumptions: {
iterableIsArray: false,
},
parser: {
syntax: 'typescript',
tsx: true,
},
loose: false,
transform: {
react: {
refresh: !PROD,
runtime: 'classic',
},
},
},
},
},
],
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
modules: false,
},
},
'postcss-loader',
],
exclude: /\.mod\.css$/,
},
{
test: /\.mod\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: {
mode: 'local',
localIdentName: '[local]--[contenthash:base64:10]',
},
},
},
'postcss-loader',
],
},
{
test: /\.(png|jpg|svg)$/,
use: ['asset/resource'],
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: 'asset',
generator: {
mimetype: 'application/font-woff',
},
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: 'asset/resource',
},
],
},
resolve: {
alias: {
'@admin': path.resolve('bundles', 'admin'),
'@common': path.resolve('bundles', 'common'),
'@processing': path.resolve('bundles', 'processing'),
'@public': path.resolve('bundles', 'public'),
'@tracker': path.resolve('bundles', 'tracker'),
'@uikit': path.resolve('bundles', 'uikit'),
},
extensions: ['.js', '.ts', '.tsx'],
fallback: {
fs: false,
path: false,
util: false,
},
},
devServer: PROD
? {}
: {
proxy: [
{
context: ['/admin', '/logout', '/api', '/ui', '/static', '/tracker', '/donate', '/media'],
target: process.env.TRACKER_HOST || 'http://127.0.0.1:8000/',
ws: true,
},
],
allowedHosts: ['localhost', '127.0.0.1', '.ngrok.io', '.ngrok.app'],
hot: true,
// HTMLWebpackPlugin generates Django templates for the backend to load
// for each bundle. Because Django still needs to interpret those and
// turn them into real HTML, it needs to have the files on disk, even
// during development. Without this, webpack-dev-server just keeps the
// files in memory and Django won't be able to find the templates.
devMiddleware: {
writeToDisk: true,
},
},
plugins: compact([
...(NO_MANIFEST ? [] : generateHTMLWebpackPlugins()),
new MiniCssExtractPlugin({
filename: PROD ? 'tracker-[name]-[contenthash].css' : 'tracker-[name].css',
chunkFilename: PROD ? '[id].[contenthash].css' : '[id].css',
ignoreOrder: false,
}),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
}),
!PROD && new ReactRefreshWebpackPlugin(),
new webpack.ProgressPlugin(),
ANALYZE && new BundleAnalyzerPlugin(),
]),
devtool: SOURCE_MAPS ? (PROD ? 'source-map' : 'eval-source-map') : false,
};