unplugin-typegpu 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/babel.cjs +161 -2
- package/babel.d.cts +8 -7
- package/babel.d.cts.map +1 -0
- package/babel.d.mts +12 -0
- package/babel.d.mts.map +1 -0
- package/babel.mjs +159 -0
- package/babel.mjs.map +1 -0
- package/bun.cjs +39 -2
- package/bun.d.cts +6 -3
- package/bun.d.cts.map +1 -0
- package/bun.d.mts +7 -0
- package/bun.d.mts.map +1 -0
- package/bun.mjs +37 -0
- package/bun.mjs.map +1 -0
- package/common-CRPQ2KE6.d.mts +26 -0
- package/common-CRPQ2KE6.d.mts.map +1 -0
- package/common-Dpl8jppn.d.cts +26 -0
- package/common-Dpl8jppn.d.cts.map +1 -0
- package/esbuild.cjs +8 -2
- package/esbuild.d.cts +3 -14
- package/esbuild.d.mts +4 -0
- package/esbuild.mjs +8 -0
- package/esbuild.mjs.map +1 -0
- package/farm.cjs +8 -2
- package/farm.d.cts +3 -14
- package/farm.d.mts +4 -0
- package/farm.mjs +8 -0
- package/farm.mjs.map +1 -0
- package/filter-6T-5w4kG.mjs +235 -0
- package/filter-6T-5w4kG.mjs.map +1 -0
- package/filter-CKmD4HRk.cjs +327 -0
- package/index-legacy-DRI9LQiG.d.mts +1489 -0
- package/index-legacy-DRI9LQiG.d.mts.map +1 -0
- package/index-legacy-xKi5C2ga.d.cts +1489 -0
- package/index-legacy-xKi5C2ga.d.cts.map +1 -0
- package/index.cjs +29 -2
- package/index.d.cts +12 -48
- package/index.d.cts.map +1 -0
- package/index.d.mts +18 -0
- package/index.d.mts.map +1 -0
- package/index.mjs +18 -0
- package/index.mjs.map +1 -0
- package/package.json +95 -51
- package/rolldown-browser-DKeiFM_e.mjs +130 -0
- package/rolldown-browser-DKeiFM_e.mjs.map +1 -0
- package/rolldown-browser-DUanNY-R.cjs +141 -0
- package/rolldown-browser-JYqfNLQ8.d.mts +1248 -0
- package/rolldown-browser-JYqfNLQ8.d.mts.map +1 -0
- package/rolldown.cjs +8 -2
- package/rolldown.d.cts +3 -14
- package/rolldown.d.mts +4 -0
- package/rolldown.mjs +8 -0
- package/rolldown.mjs.map +1 -0
- package/rollup-impl-DqC_d-_W.d.cts +1247 -0
- package/rollup-impl-DqC_d-_W.d.cts.map +1 -0
- package/rollup.cjs +8 -2
- package/rollup.d.cts +3 -14
- package/rollup.d.mts +4 -0
- package/rollup.mjs +8 -0
- package/rollup.mjs.map +1 -0
- package/rspack.cjs +8 -2
- package/rspack.d.cts +3 -14
- package/rspack.d.mts +4 -0
- package/rspack.mjs +8 -0
- package/rspack.mjs.map +1 -0
- package/vite.cjs +8 -2
- package/vite.d.cts +3 -14
- package/vite.d.mts +4 -0
- package/vite.mjs +8 -0
- package/vite.mjs.map +1 -0
- package/webpack.cjs +8 -2
- package/webpack.d.cts +3 -14
- package/webpack.d.mts +4 -0
- package/webpack.mjs +8 -0
- package/webpack.mjs.map +1 -0
- package/babel.cjs.map +0 -1
- package/babel.d.ts +0 -10
- package/babel.js +0 -2
- package/babel.js.map +0 -1
- package/bun-Dq3TUCfa.d.cts +0 -27
- package/bun-Dq3TUCfa.d.ts +0 -27
- package/bun.cjs.map +0 -1
- package/bun.d.ts +0 -3
- package/bun.js +0 -2
- package/bun.js.map +0 -1
- package/chunk-DEVVEJF4.cjs +0 -2
- package/chunk-DEVVEJF4.cjs.map +0 -1
- package/chunk-EESBB3Y4.js +0 -2
- package/chunk-EESBB3Y4.js.map +0 -1
- package/chunk-LJXNN2RM.cjs +0 -7
- package/chunk-LJXNN2RM.cjs.map +0 -1
- package/chunk-NQODVO3L.cjs +0 -2
- package/chunk-NQODVO3L.cjs.map +0 -1
- package/chunk-RIZD7OMC.js +0 -7
- package/chunk-RIZD7OMC.js.map +0 -1
- package/chunk-V4Q4PLLO.js +0 -2
- package/chunk-V4Q4PLLO.js.map +0 -1
- package/esbuild.cjs.map +0 -1
- package/esbuild.d.ts +0 -14
- package/esbuild.js +0 -2
- package/esbuild.js.map +0 -1
- package/farm.cjs.map +0 -1
- package/farm.d.ts +0 -14
- package/farm.js +0 -2
- package/farm.js.map +0 -1
- package/index.cjs.map +0 -1
- package/index.d.ts +0 -54
- package/index.js +0 -2
- package/index.js.map +0 -1
- package/rolldown.cjs.map +0 -1
- package/rolldown.d.ts +0 -14
- package/rolldown.js +0 -2
- package/rolldown.js.map +0 -1
- package/rollup.cjs.map +0 -1
- package/rollup.d.ts +0 -14
- package/rollup.js +0 -2
- package/rollup.js.map +0 -1
- package/rspack.cjs.map +0 -1
- package/rspack.d.ts +0 -14
- package/rspack.js +0 -2
- package/rspack.js.map +0 -1
- package/vite.cjs.map +0 -1
- package/vite.d.ts +0 -14
- package/vite.js +0 -2
- package/vite.js.map +0 -1
- package/webpack.cjs.map +0 -1
- package/webpack.d.ts +0 -14
- package/webpack.js +0 -2
- package/webpack.js.map +0 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { a as gatherTgpuAliases, c as operators, i as embedJSON, l as performExpressionNaming, n as defaultOptions, o as getFunctionName, r as earlyPruneRegex, s as isShellImplementationCall, u as useGpuDirective } from "./filter-6T-5w4kG.mjs";
|
|
2
|
+
import defu from "defu";
|
|
3
|
+
import { MagicStringAST, generateTransform } from "magic-string-ast";
|
|
4
|
+
import { walk } from "estree-walker";
|
|
5
|
+
import { transpileFn } from "tinyest-for-wgsl";
|
|
6
|
+
import { FORMAT_VERSION } from "tinyest";
|
|
7
|
+
|
|
8
|
+
//#region src/rollup-impl.ts
|
|
9
|
+
function containsUseGpuDirective(node) {
|
|
10
|
+
if (node.body.type === "BlockStatement") {
|
|
11
|
+
for (const statement of node.body.body) if (statement.type === "ExpressionStatement" && statement.directive === useGpuDirective) return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
function removeUseGpuDirective(node) {
|
|
16
|
+
const cloned = structuredClone(node);
|
|
17
|
+
if (cloned.body.type === "BlockStatement") cloned.body.body = cloned.body.body.filter((statement) => !(statement.type === "ExpressionStatement" && statement.directive === useGpuDirective));
|
|
18
|
+
return cloned;
|
|
19
|
+
}
|
|
20
|
+
function assignMetadata(magicString, node, metadata) {
|
|
21
|
+
magicString.prependLeft(node.start, "(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (").appendRight(node.end, `), ${metadata}) && $.f)({}))`);
|
|
22
|
+
}
|
|
23
|
+
function wrapInAutoName(magicString, node, name) {
|
|
24
|
+
magicString.prependLeft(node.start, "((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").appendRight(node.end, `, "${name}"))`);
|
|
25
|
+
}
|
|
26
|
+
const rollUpImpl = (rawOptions) => {
|
|
27
|
+
const options = defu(rawOptions, defaultOptions);
|
|
28
|
+
return {
|
|
29
|
+
name: "unplugin-typegpu",
|
|
30
|
+
enforce: options.enforce,
|
|
31
|
+
transform: {
|
|
32
|
+
filter: options.earlyPruning ? {
|
|
33
|
+
id: options,
|
|
34
|
+
code: earlyPruneRegex
|
|
35
|
+
} : { id: options },
|
|
36
|
+
handler(code, id) {
|
|
37
|
+
const ctx = {
|
|
38
|
+
tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
|
|
39
|
+
fileId: id,
|
|
40
|
+
autoNamingEnabled: options.autoNamingEnabled
|
|
41
|
+
};
|
|
42
|
+
let ast;
|
|
43
|
+
try {
|
|
44
|
+
ast = this.parse(code, {
|
|
45
|
+
lang: "ts",
|
|
46
|
+
allowReturnOutsideFunction: true
|
|
47
|
+
});
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const tgslFunctionDefs = [];
|
|
53
|
+
const magicString = new MagicStringAST(code);
|
|
54
|
+
walk(ast, { enter(_node, _parent) {
|
|
55
|
+
const node = _node;
|
|
56
|
+
const parent = _parent;
|
|
57
|
+
performExpressionNaming(ctx, node, (node, name) => {
|
|
58
|
+
wrapInAutoName(magicString, node, name);
|
|
59
|
+
});
|
|
60
|
+
if (node.type === "ImportDeclaration") gatherTgpuAliases(node, ctx);
|
|
61
|
+
if (node.type === "CallExpression") {
|
|
62
|
+
if (isShellImplementationCall(node, ctx)) {
|
|
63
|
+
const implementation = node.arguments[0];
|
|
64
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
|
|
65
|
+
tgslFunctionDefs.push({ def: removeUseGpuDirective(implementation) });
|
|
66
|
+
this.skip();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
|
|
71
|
+
if (containsUseGpuDirective(node)) {
|
|
72
|
+
tgslFunctionDefs.push({
|
|
73
|
+
def: removeUseGpuDirective(node),
|
|
74
|
+
name: getFunctionName(node, parent)
|
|
75
|
+
});
|
|
76
|
+
this.skip();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} });
|
|
80
|
+
for (const { def, name } of tgslFunctionDefs) {
|
|
81
|
+
const { params, body, externalNames } = transpileFn(def);
|
|
82
|
+
const isFunctionStatement = def.type === "FunctionDeclaration";
|
|
83
|
+
walk(def, { leave(_node) {
|
|
84
|
+
const node = _node;
|
|
85
|
+
if (node.type === "AssignmentExpression") {
|
|
86
|
+
const runtimeFn = operators[node.operator];
|
|
87
|
+
if (runtimeFn) {
|
|
88
|
+
const left = node.left;
|
|
89
|
+
const right = node.right;
|
|
90
|
+
const lhs = magicString.sliceNode(left);
|
|
91
|
+
const rhs = magicString.sliceNode(right);
|
|
92
|
+
magicString.overwriteNode(node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
|
|
93
|
+
}
|
|
94
|
+
} else if (node.type === "BinaryExpression") {
|
|
95
|
+
const runtimeFn = operators[node.operator];
|
|
96
|
+
if (runtimeFn) {
|
|
97
|
+
const left = node.left;
|
|
98
|
+
const right = node.right;
|
|
99
|
+
const lhs = magicString.sliceNode(left);
|
|
100
|
+
const rhs = magicString.sliceNode(right);
|
|
101
|
+
magicString.overwriteNode(node, `${runtimeFn}(${lhs}, ${rhs})`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} });
|
|
105
|
+
if (isFunctionStatement && name && code.slice(0, def.start).search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1) console.warn(`File ${id}: function "${name}" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);
|
|
106
|
+
assignMetadata(magicString, def, `{
|
|
107
|
+
v: ${FORMAT_VERSION},
|
|
108
|
+
name: ${name ? `"${name}"` : "undefined"},
|
|
109
|
+
ast: ${embedJSON({
|
|
110
|
+
params,
|
|
111
|
+
body,
|
|
112
|
+
externalNames
|
|
113
|
+
})},
|
|
114
|
+
externals: () => ({${externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
|
|
115
|
+
}`);
|
|
116
|
+
if (isFunctionStatement && name) magicString.prependLeft(def.start, `const ${name} = `);
|
|
117
|
+
}
|
|
118
|
+
return generateTransform(magicString, id);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/rolldown-browser.ts
|
|
126
|
+
var rolldown_browser_default = rollUpImpl;
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
export { rollUpImpl as n, rolldown_browser_default as t };
|
|
130
|
+
//# sourceMappingURL=rolldown-browser-DKeiFM_e.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rolldown-browser-DKeiFM_e.mjs","names":[],"sources":["../src/rollup-impl.ts","../src/rolldown-browser.ts"],"sourcesContent":["import type * as acorn from 'acorn';\nimport defu from 'defu';\nimport { generateTransform, MagicStringAST } from 'magic-string-ast';\nimport {\n type Context,\n defaultOptions,\n earlyPruneRegex,\n embedJSON,\n gatherTgpuAliases,\n getFunctionName,\n isShellImplementationCall,\n operators,\n type Options,\n performExpressionNaming,\n useGpuDirective,\n} from './common.ts';\nimport type { UnpluginBuildContext, UnpluginContext } from 'unplugin';\nimport { type Node, walk } from 'estree-walker';\nimport { transpileFn } from 'tinyest-for-wgsl';\nimport { FORMAT_VERSION } from 'tinyest';\n\nexport type FunctionNode =\n | acorn.FunctionDeclaration\n | acorn.AnonymousFunctionDeclaration\n | acorn.FunctionExpression\n | acorn.ArrowFunctionExpression;\n\nexport function containsUseGpuDirective(node: FunctionNode): boolean {\n if (node.body.type === 'BlockStatement') {\n for (const statement of node.body.body) {\n if (\n statement.type === 'ExpressionStatement' &&\n statement.directive === useGpuDirective\n ) {\n return true;\n }\n }\n }\n return false;\n}\n\nexport function removeUseGpuDirective(node: FunctionNode) {\n const cloned = structuredClone(node);\n\n if (cloned.body.type === 'BlockStatement') {\n cloned.body.body = cloned.body.body.filter(\n (statement) =>\n !(\n statement.type === 'ExpressionStatement' &&\n statement.directive === useGpuDirective\n ),\n );\n }\n\n return cloned;\n}\n\nexport function assignMetadata(\n magicString: MagicStringAST,\n node: acorn.AnyNode,\n metadata: string,\n) {\n magicString.prependLeft(\n node.start,\n '(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (',\n ).appendRight(\n node.end,\n `), ${metadata}) && $.f)({}))`,\n );\n}\n\nexport function wrapInAutoName(\n magicString: MagicStringAST,\n node: acorn.Node,\n name: string,\n) {\n magicString\n .prependLeft(\n node.start,\n '((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(',\n )\n .appendRight(node.end, `, \"${name}\"))`);\n}\n\nexport const rollUpImpl = (rawOptions: Options) => {\n const options = defu(rawOptions, defaultOptions);\n\n return {\n name: 'unplugin-typegpu' as const,\n enforce: options.enforce,\n transform: {\n filter: options.earlyPruning\n ? {\n id: options,\n code: earlyPruneRegex,\n }\n : {\n id: options,\n },\n handler(\n this: UnpluginBuildContext & UnpluginContext,\n code: string,\n id: string,\n ) {\n const ctx: Context = {\n tgpuAliases: new Set<string>(\n options.forceTgpuAlias ? [options.forceTgpuAlias] : [],\n ),\n fileId: id,\n autoNamingEnabled: options.autoNamingEnabled,\n };\n\n let ast: Node;\n try {\n ast = this.parse(code, {\n lang: 'ts',\n allowReturnOutsideFunction: true,\n }) as Node;\n } catch (cause) {\n console.warn(\n `[unplugin-typegpu] Failed to parse ${id}. Cause: ${\n typeof cause === 'object' && cause && 'message' in cause\n ? cause.message\n : cause\n }`,\n );\n return undefined;\n }\n\n const tgslFunctionDefs: {\n def: FunctionNode;\n name?: string | undefined;\n }[] = [];\n\n const magicString = new MagicStringAST(code);\n\n walk(ast, {\n enter(_node, _parent) {\n const node = _node as acorn.AnyNode;\n const parent = _parent as acorn.AnyNode;\n\n performExpressionNaming(ctx, node, (node, name) => {\n wrapInAutoName(magicString, node, name);\n });\n\n if (node.type === 'ImportDeclaration') {\n gatherTgpuAliases(node, ctx);\n }\n\n if (node.type === 'CallExpression') {\n if (isShellImplementationCall(node, ctx)) {\n const implementation = node.arguments[0];\n\n if (\n implementation &&\n (implementation.type === 'FunctionExpression' ||\n implementation.type === 'ArrowFunctionExpression')\n ) {\n tgslFunctionDefs.push({\n def: removeUseGpuDirective(implementation),\n });\n this.skip();\n }\n }\n }\n\n if (\n node.type === 'ArrowFunctionExpression' ||\n node.type === 'FunctionExpression' ||\n node.type === 'FunctionDeclaration'\n ) {\n if (containsUseGpuDirective(node)) {\n tgslFunctionDefs.push({\n def: removeUseGpuDirective(node),\n name: getFunctionName(node, parent),\n });\n this.skip();\n }\n }\n },\n });\n\n for (const { def, name } of tgslFunctionDefs) {\n const { params, body, externalNames } = transpileFn(def);\n const isFunctionStatement = def.type === 'FunctionDeclaration';\n\n walk(def as Node, {\n leave(_node) {\n const node = _node as acorn.AnyNode;\n\n if (node.type === 'AssignmentExpression') {\n const runtimeFn =\n operators[node.operator as keyof typeof operators];\n\n if (runtimeFn) {\n const left = node.left;\n const right = node.right;\n\n const lhs = magicString.sliceNode(left);\n const rhs = magicString.sliceNode(right);\n magicString.overwriteNode(\n node,\n `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`,\n );\n }\n } else if (node.type === 'BinaryExpression') {\n const runtimeFn =\n operators[node.operator as keyof typeof operators];\n\n if (runtimeFn) {\n const left = node.left;\n const right = node.right;\n\n const lhs = magicString.sliceNode(left);\n const rhs = magicString.sliceNode(right);\n magicString.overwriteNode(\n node,\n `${runtimeFn}(${lhs}, ${rhs})`,\n );\n }\n }\n },\n });\n\n if (\n isFunctionStatement &&\n name &&\n code.slice(0, def.start)\n .search(new RegExp(`(?<![\\\\w_.])${name}(?![\\\\w_])`)) !== -1\n ) {\n console.warn(\n `File ${id}: function \"${name}\" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`,\n );\n }\n\n const metadata = `{\n v: ${FORMAT_VERSION},\n name: ${name ? `\"${name}\"` : 'undefined'},\n ast: ${embedJSON({ params, body, externalNames })},\n externals: () => ({${\n externalNames.map((e) => e === 'this' ? '\"this\": this' : e).join(\n ', ',\n )\n }}),\n }`;\n\n assignMetadata(magicString, def, metadata);\n\n if (isFunctionStatement && name) {\n magicString.prependLeft(def.start, `const ${name} = `);\n }\n }\n\n return generateTransform(magicString, id);\n },\n },\n };\n};\n","import { rollUpImpl } from './rollup-impl.ts';\n\n// The unplugin API is based on the rollup/rolldown APIs, so it\n// should just be a compatible rolldown plugin.\nexport default rollUpImpl;\n"],"mappings":";;;;;;;;AA2BA,SAAgB,wBAAwB,MAA6B;AACnE,KAAI,KAAK,KAAK,SAAS,kBACrB;OAAK,MAAM,aAAa,KAAK,KAAK,KAChC,KACE,UAAU,SAAS,yBACnB,UAAU,cAAc,gBAExB,QAAO;;AAIb,QAAO;;AAGT,SAAgB,sBAAsB,MAAoB;CACxD,MAAM,SAAS,gBAAgB,KAAK;AAEpC,KAAI,OA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,KACN,QAAQ,GAAG,cAAc,KAAK,iIAC/B;AAcH,oBAAe,aAAa,KAXX;mBACR,eAAe;sBACZ,OAAO,IAAI,KAAK,KAAK,YAAY;qBAClC,UAAU;MAAE;MAAQ;MAAM;MAAe,CAAC,CAAC;mCAEpD,cAAc,KAAK,MAAM,MAAM,SAAS,mBAAiB,EAAE,CAAC,KAC1D,KACD,CACF;eAGyC;AAE1C,SAAI,uBAAuB,KACzB,aAAY,YAAY,IAAI,OAAO,SAAS,KAAK,KAAK;;AAI1D,WAAO,kBAAkB,aAAa,GAAG;;GAE5C;EACF;;;;;AC5PH,+BAAe"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const require_filter = require('./filter-CKmD4HRk.cjs');
|
|
2
|
+
let defu = require("defu");
|
|
3
|
+
defu = require_filter.__toESM(defu);
|
|
4
|
+
let magic_string_ast = require("magic-string-ast");
|
|
5
|
+
let estree_walker = require("estree-walker");
|
|
6
|
+
let tinyest_for_wgsl = require("tinyest-for-wgsl");
|
|
7
|
+
let tinyest = require("tinyest");
|
|
8
|
+
|
|
9
|
+
//#region src/rollup-impl.ts
|
|
10
|
+
function containsUseGpuDirective(node) {
|
|
11
|
+
if (node.body.type === "BlockStatement") {
|
|
12
|
+
for (const statement of node.body.body) if (statement.type === "ExpressionStatement" && statement.directive === require_filter.useGpuDirective) return true;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
function removeUseGpuDirective(node) {
|
|
17
|
+
const cloned = structuredClone(node);
|
|
18
|
+
if (cloned.body.type === "BlockStatement") cloned.body.body = cloned.body.body.filter((statement) => !(statement.type === "ExpressionStatement" && statement.directive === require_filter.useGpuDirective));
|
|
19
|
+
return cloned;
|
|
20
|
+
}
|
|
21
|
+
function assignMetadata(magicString, node, metadata) {
|
|
22
|
+
magicString.prependLeft(node.start, "(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (").appendRight(node.end, `), ${metadata}) && $.f)({}))`);
|
|
23
|
+
}
|
|
24
|
+
function wrapInAutoName(magicString, node, name) {
|
|
25
|
+
magicString.prependLeft(node.start, "((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").appendRight(node.end, `, "${name}"))`);
|
|
26
|
+
}
|
|
27
|
+
const rollUpImpl = (rawOptions) => {
|
|
28
|
+
const options = (0, defu.default)(rawOptions, require_filter.defaultOptions);
|
|
29
|
+
return {
|
|
30
|
+
name: "unplugin-typegpu",
|
|
31
|
+
enforce: options.enforce,
|
|
32
|
+
transform: {
|
|
33
|
+
filter: options.earlyPruning ? {
|
|
34
|
+
id: options,
|
|
35
|
+
code: require_filter.earlyPruneRegex
|
|
36
|
+
} : { id: options },
|
|
37
|
+
handler(code, id) {
|
|
38
|
+
const ctx = {
|
|
39
|
+
tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
|
|
40
|
+
fileId: id,
|
|
41
|
+
autoNamingEnabled: options.autoNamingEnabled
|
|
42
|
+
};
|
|
43
|
+
let ast;
|
|
44
|
+
try {
|
|
45
|
+
ast = this.parse(code, {
|
|
46
|
+
lang: "ts",
|
|
47
|
+
allowReturnOutsideFunction: true
|
|
48
|
+
});
|
|
49
|
+
} catch (cause) {
|
|
50
|
+
console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const tgslFunctionDefs = [];
|
|
54
|
+
const magicString = new magic_string_ast.MagicStringAST(code);
|
|
55
|
+
(0, estree_walker.walk)(ast, { enter(_node, _parent) {
|
|
56
|
+
const node = _node;
|
|
57
|
+
const parent = _parent;
|
|
58
|
+
require_filter.performExpressionNaming(ctx, node, (node, name) => {
|
|
59
|
+
wrapInAutoName(magicString, node, name);
|
|
60
|
+
});
|
|
61
|
+
if (node.type === "ImportDeclaration") require_filter.gatherTgpuAliases(node, ctx);
|
|
62
|
+
if (node.type === "CallExpression") {
|
|
63
|
+
if (require_filter.isShellImplementationCall(node, ctx)) {
|
|
64
|
+
const implementation = node.arguments[0];
|
|
65
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
|
|
66
|
+
tgslFunctionDefs.push({ def: removeUseGpuDirective(implementation) });
|
|
67
|
+
this.skip();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
|
|
72
|
+
if (containsUseGpuDirective(node)) {
|
|
73
|
+
tgslFunctionDefs.push({
|
|
74
|
+
def: removeUseGpuDirective(node),
|
|
75
|
+
name: require_filter.getFunctionName(node, parent)
|
|
76
|
+
});
|
|
77
|
+
this.skip();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} });
|
|
81
|
+
for (const { def, name } of tgslFunctionDefs) {
|
|
82
|
+
const { params, body, externalNames } = (0, tinyest_for_wgsl.transpileFn)(def);
|
|
83
|
+
const isFunctionStatement = def.type === "FunctionDeclaration";
|
|
84
|
+
(0, estree_walker.walk)(def, { leave(_node) {
|
|
85
|
+
const node = _node;
|
|
86
|
+
if (node.type === "AssignmentExpression") {
|
|
87
|
+
const runtimeFn = require_filter.operators[node.operator];
|
|
88
|
+
if (runtimeFn) {
|
|
89
|
+
const left = node.left;
|
|
90
|
+
const right = node.right;
|
|
91
|
+
const lhs = magicString.sliceNode(left);
|
|
92
|
+
const rhs = magicString.sliceNode(right);
|
|
93
|
+
magicString.overwriteNode(node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
|
|
94
|
+
}
|
|
95
|
+
} else if (node.type === "BinaryExpression") {
|
|
96
|
+
const runtimeFn = require_filter.operators[node.operator];
|
|
97
|
+
if (runtimeFn) {
|
|
98
|
+
const left = node.left;
|
|
99
|
+
const right = node.right;
|
|
100
|
+
const lhs = magicString.sliceNode(left);
|
|
101
|
+
const rhs = magicString.sliceNode(right);
|
|
102
|
+
magicString.overwriteNode(node, `${runtimeFn}(${lhs}, ${rhs})`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} });
|
|
106
|
+
if (isFunctionStatement && name && code.slice(0, def.start).search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1) console.warn(`File ${id}: function "${name}" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);
|
|
107
|
+
assignMetadata(magicString, def, `{
|
|
108
|
+
v: ${tinyest.FORMAT_VERSION},
|
|
109
|
+
name: ${name ? `"${name}"` : "undefined"},
|
|
110
|
+
ast: ${require_filter.embedJSON({
|
|
111
|
+
params,
|
|
112
|
+
body,
|
|
113
|
+
externalNames
|
|
114
|
+
})},
|
|
115
|
+
externals: () => ({${externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
|
|
116
|
+
}`);
|
|
117
|
+
if (isFunctionStatement && name) magicString.prependLeft(def.start, `const ${name} = `);
|
|
118
|
+
}
|
|
119
|
+
return (0, magic_string_ast.generateTransform)(magicString, id);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/rolldown-browser.ts
|
|
127
|
+
var rolldown_browser_default = rollUpImpl;
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
Object.defineProperty(exports, 'rollUpImpl', {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
get: function () {
|
|
133
|
+
return rollUpImpl;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
Object.defineProperty(exports, 'rolldown_browser_default', {
|
|
137
|
+
enumerable: true,
|
|
138
|
+
get: function () {
|
|
139
|
+
return rolldown_browser_default;
|
|
140
|
+
}
|
|
141
|
+
});
|