-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwebpack.config.js
More file actions
276 lines (265 loc) · 10.5 KB
/
Copy pathwebpack.config.js
File metadata and controls
276 lines (265 loc) · 10.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* @author hexxone / https://hexx.one
*
* @license
* Copyright (c) 2026 hexxone All rights reserved.
* Licensed under the GNU GENERAL PUBLIC LICENSE.
* See LICENSE file in the project root for full license information.
*
* Webpack build config for AudiOrbits.
* Probably not a good example to start from :D
*/
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const { networkInterfaces } = require('os');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
// custom plugins
const OfflinePlugin = require('./src/we_utils/src/offline/OfflinePlugin');
const WascBuilderPlugin = require('./src/we_utils/src/wasc-worker/WascBuilderPlugin');
const RenamerPlugin = require('./src/we_utils/src/renamer/RenamerPlugin');
const lanIp = Object.values(networkInterfaces())
.flat()
.find((item) => {
return item.family === 'IPv4' && !item.internal && item.address;
})
?.address ?? '0.0.0.0';
console.log(`Got Lan IP: ${lanIp}`);
module.exports = (env) => {
const prod = env.production || false;
const stringMode = prod ? 'production' : 'development';
return {
mode: stringMode,
entry: {
ao: {
import: './src/AudiOrbi.ts'
}
},
output: {
chunkFormat: 'module',
path: path.resolve(__dirname, 'dist', stringMode),
library: {
name: 'ao',
type: 'var' // window - exposes the entry point as window.ao
},
filename: '[name].js', // Main output file, e.g., ao.js
chunkFilename: '[name].js', // For dynamically imported chunks
globalObject: 'this' // Ensures compatibility in various environments for the UMD wrapper / library type 'var'
},
optimization: {
minimize: prod, // Only minimize in production
nodeEnv: stringMode, // Sets process.env.NODE_ENV, Webpack does this by default based on mode
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false
},
mangle: {
properties: {
keep_quoted: true,
regex: /_(private|internal)_/
}
},
module: true,
toplevel: true,
sourceMap: !prod,
keep_classnames: !prod,
keep_fnames: !prod
// Aggressive compression can be enabled if needed and tested
// compress: prod ? { unsafe: true, hoist_funs: true, passes: 2 } : false,
},
extractComments: false
})
],
// Following options are generally good defaults for production builds
chunkIds: prod ? 'size' : 'named',
concatenateModules: prod, // Scope hoisting
moduleIds: prod ? 'size' : 'named',
mangleExports: prod ? 'size' : false,
mangleWasmImports: true, // Good for Wasm size
providedExports: true,
usedExports: true, // Essential for tree-shaking
innerGraph: prod // More effective tree shaking in prod
},
devtool: prod ? false : 'source-map',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
compiler: 'typescript', // Specify compiler if using ttypescript or similar
transpileOnly: true // Speeds up compilation; type checking can be a separate step (e.g. `tsc --noEmit`)
}
},
{
test: /\.jsx?$/, // If you are using TypeScript: /\.tsx?$/
include: path.resolve(__dirname, 'src'),
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
]
},
// shader loader
{
test: /\.(glsl)$/,
loader: require.resolve(
'./src/we_utils/src/three/shader/loader'
)
},
// exclude .asc from the bundle
{
test: /.*\.asc$/i,
loader: 'null-loader'
}
]
},
resolveLoader: {
alias: {
'worker-loader': path.resolve(
__dirname,
'./src/we_utils/src/worker-loader-fork/dist'
)
}
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.glsl'],
alias: {
'we_utils/src': path.resolve(__dirname, './src/we_utils/src'),
'three.ts/src': path.resolve(__dirname, './src/we_utils/src/three.ts/src')
}
},
plugins: [
// copy static files
new CopyWebpackPlugin({
patterns: [
{
from: 'public'
},
{
from: path.resolve(
__dirname,
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.wasm'
),
to: 'ort/[name][ext]'
},
{
from: path.resolve(
__dirname,
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm'
),
to: 'ort/[name][ext]'
},
{
from: path.resolve(
__dirname,
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.mjs'
),
to: 'ort/[name][ext]'
},
{
from: path.resolve(
__dirname,
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.mjs'
),
to: 'ort/[name][ext]'
}
]
}),
// manual wasm module build
// just so you don't have to modify the process everytime...
// this will compile all modules in 'rootpath' (recursive)
// where the 'include' (regex) matches a filename.
new WascBuilderPlugin({
production: prod,
basedir: path.resolve(__dirname, 'assembly'),
modules: ['BasicGeometry.ts', 'FractalGeometry.ts'],
cleanup: true,
shared: true
}),
new WascBuilderPlugin({
production: prod,
basedir: path.resolve(__dirname, 'src/we_utils/src/weas/assembly'),
modules: ['WEAS.ts'],
cleanup: true,
shared: true
}),
new OfflinePlugin({
staticdir: path.resolve(__dirname, 'public'),
outfile: 'offlinefiles.json',
extrafiles: ['/'], // Cache the root path
pretty: !prod
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: path.resolve(__dirname, 'dist', 'report', 'ao_bundle_report.html'),
openAnalyzer: false
}),
// custom renamer
new RenamerPlugin({
regex: /[a-z0-9_]*_webpack_[a-z0-9_]*/gi
}),
new CircularDependencyPlugin({
exclude: /node_modules/,
include: /src/,
failOnError: true,
cwd: process.cwd()
})
],
devServer: {
allowedHosts: ['localhost'],
static: {
directory: path.resolve(__dirname, 'dist', stringMode), // Serve from the output directory
watch: true // Watch for file changes
},
client: false, // Disable webpack client logging in the browser console
liveReload: false, // Disable live reload (can be enabled if preferred)
compress: true, // Enable gzip compression
https: {
// See README.md for instructions on how to create these keys.
key: fs.readFileSync(path.resolve(__dirname, 'localhost+2-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'localhost+2.pem'))
},
server: 'https', // Use HTTPS
hot: false, // Disable Hot Module Replacement (HMR)
port: 8443, // Dev server port
headers: {
'Access-Control-Allow-Origin': '*',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Resource-Policy': 'cross-origin',
'Content-Security-Policy': [
"worker-src 'self' blob:",
"script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval' 'unsafe-inline' blob:"
].join('; ')
},
setupMiddlewares: (middlewares, devServer) => {
devServer.app.use((req, res, next) => {
// res.setHeader(
// 'Content-Security-Policy',
// "default-src 'self' blob:; worker-src 'self' blob:"
// );
next();
});
return middlewares;
}
},
// print statistics
stats: {
children: true,
errorDetails: true,
// Display bailout reasons
optimizationBailout: true
}
};
};