1
- import * as Babel from '@babel/standalone' ;
2
- import plugin from 'unplugin-typegpu/babel' ;
1
+ import rolldownPlugin from 'unplugin-typegpu/rolldown-browser' ;
3
2
import { bundle } from './rolldown.ts' ;
4
3
5
- function translateTGSL (
6
- code : string ,
7
- ) : string {
8
- const result = Babel . transform ( code , {
9
- 'presets' : [ 'typescript' ] ,
10
- 'filename' : 'example.ts' ,
11
- plugins : [ plugin ] ,
12
- } ) . code ;
13
- return result || '' ;
14
- }
15
-
16
4
const moduleImports = {
17
5
'typegpu' : 'https://esm.sh/typegpu@latest/?bundle=false' ,
18
6
'typegpu/data' : 'https://esm.sh/typegpu@latest/data/?bundle=false' ,
@@ -22,19 +10,24 @@ const moduleImports = {
22
10
type TgslModule = Record < string , unknown > ;
23
11
24
12
async function executeTgslModule ( tgslCode : string ) : Promise < TgslModule > {
25
- const translatedCode = translateTGSL ( tgslCode ) ;
26
-
27
- const { output } = await bundle (
13
+ const result = await bundle (
28
14
{
29
- '/utils.ts' :
30
- 'export function add(a: number, b: number): number { return a + b; }' ,
31
- '/index.ts' :
32
- `import {add} from './utils.ts'; console.log('Hello, World!');` ,
15
+ '/shader.js' : tgslCode ,
16
+ '/index.ts' : `
17
+ import tgpu from 'typegpu';
18
+ import * as exports from './shader.js';
19
+
20
+ const shaderCode = tgpu.resolve({ externals: exports });
21
+ export default shaderCode;
22
+ ` ,
33
23
} ,
34
24
[ './index.ts' ] ,
25
+ {
26
+ plugins : [ rolldownPlugin ( { } ) ] ,
27
+ } ,
35
28
) ;
36
29
37
- console . log ( output ) ;
30
+ const output = result . output [ 'index.js' ] ;
38
31
39
32
const importMap = { imports : moduleImports } ;
40
33
@@ -44,7 +37,7 @@ async function executeTgslModule(tgslCode: string): Promise<TgslModule> {
44
37
document . head . appendChild ( importMapScript ) ;
45
38
46
39
try {
47
- const userBlob = new Blob ( [ translatedCode ] , { type : 'text/javascript' } ) ;
40
+ const userBlob = new Blob ( [ output ] , { type : 'text/javascript' } ) ;
48
41
const userModuleUrl = URL . createObjectURL ( userBlob ) ;
49
42
50
43
const module = await import ( userModuleUrl ) ;
@@ -58,16 +51,8 @@ async function executeTgslModule(tgslCode: string): Promise<TgslModule> {
58
51
59
52
export async function executeTgslCode ( tgslCode : string ) : Promise < string > {
60
53
try {
61
- const exports = await executeTgslModule ( tgslCode ) ;
62
-
63
- const tgpuModule = await import (
64
- //@ts -expect-error
65
- 'https://esm.sh/typegpu@latest?bundle=false'
66
- ) ;
67
-
68
- return tgpuModule . default . resolve ( {
69
- externals : exports as Record < string , object > ,
70
- } ) ;
54
+ const shaderCode = await executeTgslModule ( tgslCode ) ;
55
+ return shaderCode . default as string ;
71
56
} catch ( error ) {
72
57
throw new Error (
73
58
`Failed to execute TGSL code: ${
0 commit comments