-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.js
164 lines (156 loc) · 4.07 KB
/
vite.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
import { defineConfig } from "vite";
import topLevelAwait from "vite-plugin-top-level-await";
import glsl from "vite-plugin-glsl";
import wasm from "vite-plugin-wasm";
import { resolve } from "path";
import fs from "fs";
import path from "path";
// Worker Chunk Plugin
function workerChunkPlugin() {
return {
name: "worker-chunk-plugin",
apply: "build",
enforce: "pre",
async resolveId(source, importer, options) {
if (source.endsWith("?worker")) {
const resolved = await this.resolve(source.split("?")[0], importer, {
...options,
skipSelf: true,
});
return resolved ? "\0" + resolved.id + "?worker-chunk" : null;
}
},
load(id) {
if (id.startsWith("\0") && id.endsWith("?worker-chunk")) {
const referenceId = this.emitFile({
type: "chunk",
id: id.slice(1).split("?")[0],
preserveSignature: "strict",
});
return `
export default function WorkerWrapper() {
return new Worker(
import.meta.ROLLUP_FILE_URL_${referenceId},
{ type: "module" }
);
}
`;
}
},
outputOptions(options) {
return {
...options,
sourcemap: true,
inlineDynamicImports: false,
};
},
};
}
// Define your plugins
const plugins = [
topLevelAwait({
promiseExportName: "__tla",
promiseImportName: (i) => `__tla_${i}`,
}),
glsl(),
wasm(),
workerChunkPlugin(),
];
// Export a function that returns the configuration based on the mode
export default defineConfig(({ mode }) => {
// Common configuration shared between builds
const commonConfig = {
target: "es6",
plugins,
worker: {
format: "es",
plugins,
},
assetsInclude: ["**/*.obj"],
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis",
},
},
include: ["gpu-io", "three"],
},
server: {
fs: {
allow: ["../router", "../gpu-io", "."],
},
},
resolve: {
alias: {
"three/webgpu": path.resolve(
__dirname,
"node_modules/three/build/three.webgpu",
),
"three-obj-loader": path.resolve(
__dirname,
"node_modules/three/examples/jsm/loaders/OBJLoader.js",
),
"webscape-wanderer": path.resolve(__dirname, "src/main.ts"),
},
},
build: {
minify: true, // Consider enabling for production
modulePreload: false,
sourcemap: true,
target: "module",
},
};
if (mode === "lib") {
// Configuration for building the library
return {
...commonConfig,
build: {
...commonConfig.build,
lib: {
entry: resolve(__dirname, "src/main.ts"),
name: "WebscapeWanderer",
fileName: "webscape-wanderer",
formats: ["es"],
},
},
};
} else {
// Configuration for building the examples
return {
...commonConfig,
base: "/webscape-wanderer/",
build: {
...commonConfig.build,
rollupOptions: {
input: {
// Automatically include all HTML files in the ./examples directory
...getExampleInputs(),
},
output: {
manualChunks(id) {
// You can define manual chunks here if needed
// For example, to vendor large dependencies:
// if (id.includes('node_modules/large-dependency')) {
// return 'vendor-large-dependency';
// }
},
},
},
},
publicDir: resolve(__dirname, "assets"),
};
}
});
// Helper function to get all HTML files in the ./examples directory
function getExampleInputs() {
const examplesDir = path.resolve(__dirname, "examples");
const exampleFiles = fs.readdirSync(examplesDir);
const inputs = {};
exampleFiles.forEach((file) => {
if (file.endsWith(".html")) {
const name = path.basename(file, ".html");
inputs[name] = path.resolve(examplesDir, file);
}
});
return inputs;
}