Skip to content

Commit

Permalink
Better escaping during shader compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
flatpickles committed Nov 10, 2023
1 parent 2c1ace1 commit 8b1f850
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sketchbook2",
"version": "0.2.1",
"version": "0.2.2",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down
24 changes: 21 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ function viteGlslify() {
throw new Error(`Unexpected shader file format for ${id}: ${code}`);
}
// Translate escaped characters for glslify
const glslifiedCode = glslify.compile(
code.replace(/\\n/g, '\n').replace(/\\t/g, '\t')
);
const glslifiedCode = glslify.compile(unescapeString(code));
// Return the compiled code as a default export
return `export default ${JSON.stringify(glslifiedCode)}`;
}
Expand All @@ -38,6 +36,26 @@ function viteGlslify() {
};
}

// 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: {
Expand Down

0 comments on commit 8b1f850

Please sign in to comment.