Skip to content

fix: unplugin-typegpu should parse files with TS syntax support #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 28, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,16 @@ module.exports = (api) => {

```ts
interface Options {
/** @default [/\.m?[jt]sx?$/] */
include?: FilterPattern;
/** @default undefined */
exclude?: FilterPattern;
/** @default undefined */
enforce?: 'post' | 'pre' | undefined;
/** @default undefined */
forceTgpuAlias?: string;
/** @default true */
autoNamingEnabled?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. While we're at it, could you include the default options here as well?

}
```

Expand Down
8 changes: 4 additions & 4 deletions packages/unplugin-typegpu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@
"@babel/standalone": "^7.27.0",
"defu": "^6.1.4",
"estree-walker": "^3.0.3",
"magic-string-ast": "^0.9.1",
"picomatch": "^4.0.2",
"magic-string-ast": "^1.0.0",
"picomatch": "^4.0.3",
"tinyest": "workspace:~0.1.1",
"tinyest-for-wgsl": "workspace:~0.1.2",
"unplugin": "^2.3.1"
"unplugin": "^2.3.5"
},
"peerDependencies": {
"typegpu": "workspace:^0.6.0"
Expand All @@ -127,7 +127,7 @@
"@types/babel__standalone": "^7.1.9",
"@types/babel__template": "^7.4.4",
"@types/babel__traverse": "^7.20.7",
"@types/picomatch": "^4.0.0",
"@types/picomatch": "^4.0.1",
"acorn": "^8.14.1",
"rollup": "~4.37.0",
"tsup": "catalog:build",
Expand Down
4 changes: 2 additions & 2 deletions packages/unplugin-typegpu/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface Options {
include?: FilterPattern;
exclude?: FilterPattern;
enforce?: 'post' | 'pre' | undefined;
forceTgpuAlias?: string;
autoNamingEnabled?: boolean;
forceTgpuAlias?: string | undefined;
autoNamingEnabled?: boolean | undefined;
}

export const defaultOptions = {
Expand Down
29 changes: 18 additions & 11 deletions packages/unplugin-typegpu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,22 @@ const typegpu: UnpluginInstance<Options, false> = createUnplugin(
autoNamingEnabled: options.autoNamingEnabled,
};

const ast = this.parse(code, {
allowReturnOutsideFunction: true,
}) as Node;
let ast: Node;
try {
ast = this.parse(code, {
lang: 'ts',
allowReturnOutsideFunction: true,
}) as Node;
} catch (cause) {
console.warn(
`[unplugin-typegpu] Failed to parse ${id}. Cause: ${
typeof cause === 'object' && cause && 'message' in cause
? cause.message
: cause
}`,
);
return undefined;
}

const tgslFunctionDefs: {
def: FunctionNode;
Expand Down Expand Up @@ -163,20 +176,14 @@ const typegpu: UnpluginInstance<Options, false> = createUnplugin(
},
});

for (
const {
def,
name,
} of tgslFunctionDefs
) {
for (const { def, name } of tgslFunctionDefs) {
const { params, body, externalNames } = transpileFn(def);
const isFunctionStatement = def.type === 'FunctionDeclaration';

if (
isFunctionStatement &&
name &&
code
.slice(0, def.start)
code.slice(0, def.start)
.search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1
) {
console.warn(
Expand Down
72 changes: 39 additions & 33 deletions pnpm-lock.yaml

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