Skip to content

Commit 8799e4f

Browse files
authored
fix: unplugin-typegpu should parse files with TS syntax support (#1516)
1 parent 2d1d95a commit 8799e4f

File tree

5 files changed

+69
-50
lines changed

5 files changed

+69
-50
lines changed

apps/typegpu-docs/src/content/docs/tooling/unplugin-typegpu.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,16 @@ module.exports = (api) => {
116116

117117
```ts
118118
interface Options {
119+
/** @default [/\.m?[jt]sx?$/] */
119120
include?: FilterPattern;
121+
/** @default undefined */
120122
exclude?: FilterPattern;
123+
/** @default undefined */
121124
enforce?: 'post' | 'pre' | undefined;
125+
/** @default undefined */
122126
forceTgpuAlias?: string;
127+
/** @default true */
128+
autoNamingEnabled?: boolean;
123129
}
124130
```
125131

packages/unplugin-typegpu/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@
110110
"@babel/standalone": "^7.27.0",
111111
"defu": "^6.1.4",
112112
"estree-walker": "^3.0.3",
113-
"magic-string-ast": "^0.9.1",
114-
"picomatch": "^4.0.2",
113+
"magic-string-ast": "^1.0.0",
114+
"picomatch": "^4.0.3",
115115
"tinyest": "workspace:~0.1.1",
116116
"tinyest-for-wgsl": "workspace:~0.1.2",
117-
"unplugin": "^2.3.1"
117+
"unplugin": "^2.3.5"
118118
},
119119
"peerDependencies": {
120120
"typegpu": "workspace:^0.6.0"
@@ -127,7 +127,7 @@
127127
"@types/babel__standalone": "^7.1.9",
128128
"@types/babel__template": "^7.4.4",
129129
"@types/babel__traverse": "^7.20.7",
130-
"@types/picomatch": "^4.0.0",
130+
"@types/picomatch": "^4.0.1",
131131
"acorn": "^8.14.1",
132132
"rollup": "~4.37.0",
133133
"tsup": "catalog:build",

packages/unplugin-typegpu/src/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface Options {
1616
include?: FilterPattern;
1717
exclude?: FilterPattern;
1818
enforce?: 'post' | 'pre' | undefined;
19-
forceTgpuAlias?: string;
20-
autoNamingEnabled?: boolean;
19+
forceTgpuAlias?: string | undefined;
20+
autoNamingEnabled?: boolean | undefined;
2121
}
2222

2323
export const defaultOptions = {

packages/unplugin-typegpu/src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,22 @@ const typegpu: UnpluginInstance<Options, false> = createUnplugin(
100100
autoNamingEnabled: options.autoNamingEnabled,
101101
};
102102

103-
const ast = this.parse(code, {
104-
allowReturnOutsideFunction: true,
105-
}) as Node;
103+
let ast: Node;
104+
try {
105+
ast = this.parse(code, {
106+
lang: 'ts',
107+
allowReturnOutsideFunction: true,
108+
}) as Node;
109+
} catch (cause) {
110+
console.warn(
111+
`[unplugin-typegpu] Failed to parse ${id}. Cause: ${
112+
typeof cause === 'object' && cause && 'message' in cause
113+
? cause.message
114+
: cause
115+
}`,
116+
);
117+
return undefined;
118+
}
106119

107120
const tgslFunctionDefs: {
108121
def: FunctionNode;
@@ -163,20 +176,14 @@ const typegpu: UnpluginInstance<Options, false> = createUnplugin(
163176
},
164177
});
165178

166-
for (
167-
const {
168-
def,
169-
name,
170-
} of tgslFunctionDefs
171-
) {
179+
for (const { def, name } of tgslFunctionDefs) {
172180
const { params, body, externalNames } = transpileFn(def);
173181
const isFunctionStatement = def.type === 'FunctionDeclaration';
174182

175183
if (
176184
isFunctionStatement &&
177185
name &&
178-
code
179-
.slice(0, def.start)
186+
code.slice(0, def.start)
180187
.search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1
181188
) {
182189
console.warn(

pnpm-lock.yaml

Lines changed: 39 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)