Skip to content

Commit ff570bd

Browse files
committed
Use typegpu from source
1 parent 8279714 commit ff570bd

File tree

5 files changed

+93
-31
lines changed

5 files changed

+93
-31
lines changed

apps/typegpu-docs/src/components/CodeEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function handleEditorWillMount(monaco: Monaco) {
1717
entries(SANDBOX_MODULES),
1818
map(([key, moduleDef]) => {
1919
if ('reroute' in moduleDef.typeDef) {
20-
return [key, moduleDef.typeDef.reroute] as const;
20+
return [key, [moduleDef.typeDef.reroute]] as [string, string[]];
2121
}
2222
return null;
2323
}),

apps/typegpu-docs/src/components/translator/lib/editorConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function setupMonacoEditor(monaco: Monaco) {
4444
entries(SANDBOX_MODULES),
4545
map(([key, moduleDef]) => {
4646
if ('reroute' in moduleDef.typeDef) {
47-
return [key, moduleDef.typeDef.reroute] as const;
47+
return [key, [moduleDef.typeDef.reroute]] as [string, string[]];
4848
}
4949
return null;
5050
}),

apps/typegpu-docs/src/components/translator/lib/rolldown.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { InputOptions, OutputOptions } from '@rolldown/browser';
2-
import { resolve } from 'pathe';
2+
import { join } from 'pathe';
33

44
export interface BundleResult {
55
output: Record<string, string | Uint8Array>;
@@ -12,7 +12,14 @@ export interface SourceFile {
1212
isEntry?: boolean;
1313
}
1414

15-
export type FileMap = Record<string, string>;
15+
export type FileMap = Record<
16+
string,
17+
{
18+
content: string;
19+
} | {
20+
reroute: string;
21+
} | undefined
22+
>;
1623

1724
export async function bundle(
1825
files: FileMap,
@@ -39,18 +46,32 @@ export async function bundle(
3946
{
4047
name: 'virtual-fs',
4148
resolveId(source, importer) {
42-
if (source[0] === '/') {
43-
// Absolute import
44-
return source;
49+
if (source.includes('typegpu') || importer?.includes('typegpu')) {
50+
debug = true;
4551
}
46-
if (source[0] === '.') {
47-
// Relative import
48-
return resolve(importer || '/', '..', source);
52+
53+
const id = source[0] === '.'
54+
? join(importer || '/', '..', source)
55+
: source;
56+
57+
if (files[id] && 'reroute' in files[id]) {
58+
// Rerouting
59+
return files[id].reroute;
4960
}
61+
62+
return id;
5063
},
5164
load(id) {
52-
if (id[0] !== '/') return;
53-
return files[id];
65+
if (!files[id]) {
66+
return;
67+
}
68+
69+
if ('reroute' in files[id]) {
70+
// Reroutes are supposed to be resolved in `resolveId`
71+
throw new Error(`Unresolved reroute for ${id}`);
72+
}
73+
74+
return files[id].content;
5475
},
5576
},
5677
...(Array.isArray(config?.plugins)

apps/typegpu-docs/src/components/translator/lib/tgslExecutor.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
import { mapValues, pipe } from 'remeda';
12
import rolldownPlugin from 'unplugin-typegpu/rolldown-browser';
23
import { bundle } from './rolldown.ts';
4+
import { SANDBOX_MODULES } from '../../../utils/examples/sandboxModules.ts';
35

46
const moduleImports = {
5-
'typegpu': 'https://esm.sh/typegpu@latest/?bundle=false',
6-
'typegpu/data': 'https://esm.sh/typegpu@latest/data/?bundle=false',
7-
'typegpu/std': 'https://esm.sh/typegpu@latest/std/?bundle=false',
7+
'typed-binary': 'https://esm.sh/typed-binary@latest',
88
} as Record<string, string>;
99

1010
type TgslModule = Record<string, unknown>;
1111

1212
async function executeTgslModule(tgslCode: string): Promise<TgslModule> {
1313
const result = await bundle(
1414
{
15-
'/shader.js': tgslCode,
16-
'/index.ts': `
17-
import tgpu from 'typegpu';
18-
import * as exports from './shader.js';
15+
...pipe(SANDBOX_MODULES, mapValues((val) => val.import)),
16+
'/shader.js': { content: tgslCode },
17+
'/index.ts': {
18+
content: `
19+
import tgpu from 'typegpu';
20+
import * as exports from './shader.js';
1921
20-
const shaderCode = tgpu.resolve({ externals: exports });
21-
export default shaderCode;
22-
`,
22+
const shaderCode = tgpu.resolve({ externals: exports });
23+
export default shaderCode;
24+
`,
25+
},
2326
},
2427
['./index.ts'],
2528
{
2629
plugins: [rolldownPlugin({})],
30+
external: ['typed-binary'],
2731
},
2832
);
2933

@@ -40,7 +44,7 @@ async function executeTgslModule(tgslCode: string): Promise<TgslModule> {
4044
const userBlob = new Blob([output], { type: 'text/javascript' });
4145
const userModuleUrl = URL.createObjectURL(userBlob);
4246

43-
const module = await import(userModuleUrl);
47+
const module = await import(/* @vite-ignore */ userModuleUrl);
4448

4549
URL.revokeObjectURL(userModuleUrl);
4650
return module;

apps/typegpu-docs/src/utils/examples/sandboxModules.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ import dtsWebGPU from '@webgpu/types/dist/index.d.ts?raw';
44
import dtsWgpuMatrix from 'wgpu-matrix/dist/3.x/wgpu-matrix.d.ts?raw';
55

66
interface SandboxModuleDefinition {
7-
typeDef: { filename?: string; content: string } | { reroute: string[] };
7+
typeDef:
8+
| { filename?: string; content: string }
9+
| { reroute: string };
10+
import?:
11+
| { filename?: string; content: string }
12+
| { reroute: string }
13+
| undefined;
14+
}
15+
16+
function srcFileToModule(
17+
[filepath, content]: [string, string],
18+
baseUrl: string,
19+
): [moduleKey: string, moduleDef: SandboxModuleDefinition] {
20+
const filename = filepath.replace(baseUrl, '');
21+
const def = {
22+
filename,
23+
content,
24+
};
25+
26+
return [
27+
filename,
28+
{
29+
typeDef: def,
30+
import: def,
31+
},
32+
] as const;
833
}
934

1035
function dtsFileToModule(
@@ -26,13 +51,16 @@ function dtsFileToModule(
2651

2752
const allPackagesSrcFiles = pipe(
2853
entries(
29-
import.meta.glob('../../../../../packages/*/src/**/*.ts', {
54+
import.meta.glob([
55+
'../../../../../packages/*/src/**/*.ts',
56+
'../../../../../packages/*/package.json',
57+
], {
3058
query: 'raw',
3159
eager: true,
3260
import: 'default',
3361
}) as Record<string, string>,
3462
),
35-
map((dtsFile) => dtsFileToModule(dtsFile, '../../../../../packages/')),
63+
map((dtsFile) => srcFileToModule(dtsFile, '../../../../../packages/')),
3664
fromEntries(),
3765
);
3866

@@ -61,21 +89,30 @@ export const SANDBOX_MODULES: Record<string, SandboxModuleDefinition> = {
6189
'wgpu-matrix': {
6290
typeDef: { filename: 'wgpu-matrix.d.ts', content: dtsWgpuMatrix },
6391
},
92+
tinyest: {
93+
import: { reroute: 'tinyest/src/index.ts' },
94+
typeDef: { reroute: 'tinyest/src/index.ts' },
95+
},
6496
typegpu: {
65-
typeDef: { reroute: ['typegpu/src/index.ts'] },
97+
import: { reroute: 'typegpu/src/index.ts' },
98+
typeDef: { reroute: 'typegpu/src/index.ts' },
6699
},
67100
'typegpu/data': {
68-
typeDef: { reroute: ['typegpu/src/data/index.ts'] },
101+
import: { reroute: 'typegpu/src/data/index.ts' },
102+
typeDef: { reroute: 'typegpu/src/data/index.ts' },
69103
},
70104
'typegpu/std': {
71-
typeDef: { reroute: ['typegpu/src/std/index.ts'] },
105+
import: { reroute: 'typegpu/src/std/index.ts' },
106+
typeDef: { reroute: 'typegpu/src/std/index.ts' },
72107
},
73108

74109
// Utility modules
75110
'@typegpu/noise': {
76-
typeDef: { reroute: ['typegpu-noise/src/index.ts'] },
111+
import: { reroute: 'typegpu-noise/src/index.ts' },
112+
typeDef: { reroute: 'typegpu-noise/src/index.ts' },
77113
},
78114
'@typegpu/color': {
79-
typeDef: { reroute: ['typegpu-color/src/index.ts'] },
115+
import: { reroute: 'typegpu-color/src/index.ts' },
116+
typeDef: { reroute: 'typegpu-color/src/index.ts' },
80117
},
81118
};

0 commit comments

Comments
 (0)