-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typescript types are done; added support for wgsl loading and typescr…
…ipt parsing; lots more test coverage; space module cleaned up; rust cleaned up
- Loading branch information
Mr Martian
committed
Oct 3, 2024
1 parent
4b9fdc5
commit 42f8f76
Showing
42 changed files
with
2,072 additions
and
2,984 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { BunPlugin } from 'bun'; | ||
|
||
// TODO: Some shaders may #include inside an already #included file. This is not supported yet. | ||
|
||
const WgslPlugin: BunPlugin = { | ||
name: 'WGSL loader', | ||
/** | ||
* @param build - bun setup tool to parse wgsl files | ||
*/ | ||
setup(build) { | ||
build.onLoad({ filter: /\.wgsl$/ }, async (args) => { | ||
const { path } = args; | ||
// load the file: | ||
const file = await Bun.file(path).text(); | ||
const basePath = path.split('/').slice(0, -1).join('/'); | ||
// for each line, if a line starts with #include, replace it with the contents of the file | ||
const lines = file.split('\n'); | ||
const contents: string[] = []; | ||
for (const line of lines) { | ||
if (line.startsWith('#include')) { | ||
let includePath = line.split(' ')[1]; | ||
includePath = includePath.slice(0, -1); | ||
const includeFile = await Bun.file(`${basePath}/${includePath}`).text(); | ||
contents.push(includeFile); | ||
} else { | ||
contents.push(line); | ||
} | ||
} | ||
const result = contents.join('\n'); | ||
return { | ||
contents: `export default "${sanitizeStringForExport(result)}"`, | ||
loader: 'js', | ||
}; | ||
}); | ||
}, | ||
}; | ||
|
||
/** | ||
* @param str - string to sanitize | ||
* @returns sanitized string | ||
*/ | ||
function sanitizeStringForExport(str: string): string { | ||
// Remove single-line comments | ||
str = str.replace(/\/\/.*$/gm, ''); | ||
// Remove multi-line comments | ||
str = str.replace(/\/\*[\s\S]*?\*\//g, ''); | ||
// Replace line breaks with '\n' | ||
str = str.replace(/\n/g, '\\n'); | ||
// Escape double quotes | ||
str = str.replace(/"/g, '\\"'); | ||
return str; | ||
} | ||
|
||
export default WgslPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.