-
Notifications
You must be signed in to change notification settings - Fork 264
/
rsbuild.config.ts
118 lines (115 loc) · 3.24 KB
/
rsbuild.config.ts
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
import { defineConfig, type RequestHandler } from '@rsbuild/core';
import { pluginTypeCheck } from '@rsbuild/plugin-type-check';
import { execSync } from 'node:child_process';
import { readdir } from 'node:fs';
import type { ServerResponse } from 'node:http';
import parseUrl from 'parseurl';
import send from 'send';
let gitCommit = '(unknown)';
try {
gitCommit = execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
console.warn('Failed to fetch Git commit hash', e);
}
export default defineConfig({
source: {
entry: {
index: './src/main.ts',
embed: './src/main.ts',
},
// Legacy decorators are used with `reflect-metadata`.
// TODO: Migrate to TypeScript 5.0 / TC39 decorators.
decorators: {
version: 'legacy',
},
define: {
__COMMIT_HASH: JSON.stringify(gitCommit),
},
},
html: {
template: './src/index.html',
},
output: {
target: 'web',
// Mark Node.js built-in modules as external.
externals: ['fs', 'path', 'url'],
// TODO: These should be converted to use `new URL('./file.wasm', import.meta.url)`
// so that the bundler can resolve them. In the meantime, they're expected to be
// at the root.
copy: [
{ from: 'src/**/*.wasm', to: '[name][ext]' },
{ from: 'node_modules/librw/lib/librw.wasm', to: 'static/js/[name][ext]' },
{ from: 'src/vendor/basis_universal/basis_transcoder.wasm', to: 'static/js/[name][ext]' },
],
},
// Enable async TypeScript type checking.
plugins: [pluginTypeCheck()],
tools: {
// Add a rule to treat .glsl files as source assets.
rspack(_config, { addRules }) {
addRules([
{
test: /\.glsl$/,
type: 'asset/source',
},
]);
},
// Disable standards-compliant class field transforms.
swc: {
jsc: {
transform: {
useDefineForClassFields: false,
},
},
},
},
// Disable fallback to index for 404 responses.
server: {
htmlFallback: false,
},
// Setup middleware to serve the `data` directory.
dev: {
setupMiddlewares: [
(middlewares, _server) => {
middlewares.unshift(serveData);
return middlewares;
},
],
},
});
// Serve files from the `data` directory.
const serveData: RequestHandler = (req, res, next) => {
if (req.method !== 'GET' && req.method !== 'HEAD') {
next();
return;
}
const matches = parseUrl(req)?.pathname?.match(/^\/data(\/.*)?$/);
if (!matches) {
next();
return;
}
// The `send` package handles Range requests, conditional GET,
// ETag generation, Cache-Control, Last-Modified, and more.
const stream = send(req, matches[1] || '', {
index: false,
root: 'data',
});
stream.on(
'directory',
function handleDirectory(
this: send.SendStream,
res: ServerResponse,
path: string,
) {
// Print directory listing
readdir(path, (err, list) => {
if (err) return this.error(500, err);
const filtered = list.filter((file) => !file.startsWith('.'));
if (filtered.length === 0) return this.error(404);
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.end(`${filtered.join('\n')}\n`);
});
},
);
stream.pipe(res);
};