-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
107 lines (101 loc) · 3.56 KB
/
vite.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
import { sveltekit } from '@sveltejs/kit/vite';
import glslify from 'glslify';
import glsl from 'vite-plugin-glsl';
import { defineConfig } from 'vitest/config';
// List any dependencies here that you want to be pre-bundled. This avoids page reloads when loading
// projects that use these dependencies.
const bundleDeps = [
'js-cookie',
'regl',
'three',
'p5',
'canvas-sketch-util/penplot',
'canvas-sketch-util/color',
'canvas-sketch',
'alea',
'simplex-noise',
'bezier-spline',
'regl-camera',
'angle-normals',
'gl-matrix',
'd3-path'
];
// viteGlslify is a lightweight Vite plugin that compiles GLSL files with glslify. It will compile
// any .vert, .frag, or .glsl files that include the string "#pragma glslify" in the file, and can
// work with shaders that are already transformed into importable JS by vite.
function viteGlslify() {
return {
name: 'vite-glslify',
enforce: 'pre' as const,
transform: async (code: string, id: string) => {
const shaderFileMatcher = /\.(vert|frag|glsl)/g;
if (shaderFileMatcher.test(id) && code.includes('#pragma glslify')) {
// Match a couple different formats of parsed files
if (code.startsWith('export default ')) {
code = code.replace('export default "', '').slice(0, -1);
} else if (/export\s*{/.test(code)) {
const afterDef = code.split(/=\s*"/)[1];
code = afterDef.split(/";/)[0];
} else {
throw new Error(`Unexpected shader file format for ${id}: ${code}`);
}
// Translate escaped characters for glslify
const glslifiedCode = glslify.compile(unescapeString(code));
// Return the compiled code as a default export
return `export default ${JSON.stringify(glslifiedCode)}`;
}
return code;
}
};
}
// Apply escape characters within a string
function unescapeString(str: string): string {
return str
.replace(/\\n/g, '\n')
.replace(/\\t/g, '\t')
.replace(/\\r/g, '\r')
.replace(/\\b/g, '\b')
.replace(/\\f/g, '\f')
.replace(/\\v/g, '\v')
.replace(/\\'/g, "'")
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\')
.replace(/\\x([0-9A-Fa-f]{2})/g, (_: unknown, hex: string) =>
String.fromCharCode(parseInt(hex, 16))
)
.replace(/\\u([0-9A-Fa-f]{4})/g, (_: unknown, hex: string) =>
String.fromCharCode(parseInt(hex, 16))
);
}
export default defineConfig({
plugins: [sveltekit(), glsl(), viteGlslify()],
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "src/config/theme.scss" as *;'
}
}
},
test: {
include: [
'tests/unit/**/*.{test,spec}.{js,ts}',
'tests/component/**/*.{test,spec}.{js,ts}'
],
environment: 'jsdom',
// https://stackoverflow.com/a/76615709/280404
alias: [{ find: /^svelte$/, replacement: 'svelte/internal' }]
},
server: {
fs: {
allow: ['src/art']
}
},
optimizeDeps: {
include: bundleDeps
},
// This is an attempted workaround for a bug in Vite that causes import errors for cached
// chunks. See https://github.com/vitejs/vite/issues/11804. This may need to be revised.
build: {
rollupOptions: { output: { entryFileNames: '[name].js', chunkFileNames: '[name].js' } }
}
});