Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/typegpu/src/shared/normalizeMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RawMetadataV2 {
name: string;
ast: { params: FuncParameter[]; body: Block | SourceMappedNode };
externals: { [key: string]: () => unknown };
filename?: string;
}

/**
Expand All @@ -28,6 +29,7 @@ export type RawMetadata = RawMetadataV1 | RawMetadataV2;
export interface Metadata {
ast: { params: FuncParameter[]; body: Block | SourceMappedNode };
externals: () => Record<string, unknown>;
filename?: string;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/unplugin-typegpu/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function assignMetadata(
t.objectProperty(i('name'), t.valueToNode(name)),
t.objectProperty(i('ast'), t.valueToNode({ params: ast.params, body: ast.body })),
t.objectProperty(i('externals'), externalsToNode(ast.externalNames)),
...(this.opts.unstable_sourceMaps && this.filename
? [t.objectProperty(i('filename'), t.stringLiteral(this.filename))]
: []),
]);

let expression: t.Expression;
Expand Down
8 changes: 6 additions & 2 deletions packages/unplugin-typegpu/src/core/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ function assignMetadata(
v: ${METADATA_FORMAT_VERSION},
name: ${name ? `"${name}"` : 'undefined'},
ast: ${embedJSON({ params: ast.params, body: ast.body })},
externals: ${externalsToString(ast.externalNames)}
externals: ${externalsToString(ast.externalNames)}${
this.opts.unstable_sourceMaps && this.filename
? `,\n filename: ${embedJSON(this.filename)}`
: ''
}
Comment thread
aleksanderkatan marked this conversation as resolved.
}`;

const visibility = t.isFunctionDeclaration(path.node)
Expand Down Expand Up @@ -262,7 +266,7 @@ export const unpluginFactory = ((rawOptions, _meta) => {
return magicString.generateMap({
source: id,
includeContent: true,
hires: 'boundary',
hires: options.unstable_sourceMaps ? true : 'boundary',
});
},
};
Expand Down
39 changes: 34 additions & 5 deletions packages/unplugin-typegpu/test/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ describe('source maps', () => {
test('[BABEL]', () => {
const transformed = babelTransform(code, { unstable_sourceMaps: true });

expect(transformed).toMatchInlineSnapshot(
`
expect(transformed).toMatchInlineSnapshot(`
"import { tgpu } from 'typegpu';
const external = {
n: 1
Expand All @@ -124,8 +123,7 @@ describe('source maps', () => {
"external.n": () => external.n
}
}) && $.f)({});"
`,
);
`);
});

test('[ROLLUP]', async () => {
Expand All @@ -145,7 +143,8 @@ describe('source maps', () => {
v: 2,
name: "fn",
ast: {"params":[{"type":"i","name":"argument"}],"body":[-1,5,38,[0,[[-1,7,8,[13,[-1,7,14,[9,"variable"]],[-1,7,25,[5,"3"]]]],[-1,8,8,[10,[-1,8,15,[1,[-1,8,15,[1,[-1,8,15,[9,"external.n"]],"+",[-1,8,28,[9,"argument"]]]],"+",[-1,8,39,[9,"variable"]]]]]]]]]},
externals: {"external.n":() => external.n}
externals: {"external.n":() => external.n},
filename: "\\u0000virtual:code"
}) && $.f)({}));

export { fn };
Expand Down Expand Up @@ -302,4 +301,34 @@ describe('source maps', () => {
});
});
});

describe('file name', () => {
const code = `const fn = () => { 'use gpu'; return 1; }; console.log(fn)`;

describe('is included when source maps are enabled', () => {
test('[BABEL]', () => {
const transformed = babelTransform(code, { unstable_sourceMaps: true }, [], [], 'test.ts');
const filename = transformed?.match(/.*filename(.*)\n/)?.[1];
expect(filename).toMatchInlineSnapshot(`": "/test.ts""`);
});
Comment thread
aleksanderkatan marked this conversation as resolved.

test('[ROLLUP]', async () => {
const transformed = await rollupTransform(code, { unstable_sourceMaps: true });
const filename = transformed?.match(/.*filename(.*)\n/)?.[1];
expect(filename).toMatchInlineSnapshot(`": "\\u0000virtual:code""`);
});
});

describe('is omitted when source maps are disabled', () => {
test('[BABEL]', () => {
const transformed = babelTransform(code, { unstable_sourceMaps: false }, [], [], 'test.ts');
expect(transformed).not.toContain('filename:');
});

test('[ROLLUP]', async () => {
const transformed = await rollupTransform(code, { unstable_sourceMaps: false });
expect(transformed).not.toContain('filename:');
});
});
});
});
2 changes: 2 additions & 0 deletions packages/unplugin-typegpu/test/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export const babelTransform = (
options?: Options,
prePlugins: BabelTestPlugin[] = [],
postPlugins: BabelTestPlugin[] = [],
filename?: string,
) =>
Babel.transform(code, {
filename,
plugins: [...prePlugins, [babelPlugin, { ...defaultOptions, ...options }], ...postPlugins],
parserOpts: { plugins: ['typescript'] },
}).code;
Expand Down
Loading