unplugin-typegpu 0.9.0 → 0.10.1

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.
Files changed (109) hide show
  1. package/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/babel.cjs +163 -2
  3. package/babel.d.cts +7 -7
  4. package/babel.d.mts +11 -0
  5. package/babel.mjs +159 -0
  6. package/bun.cjs +40 -2
  7. package/bun.d.cts +5 -3
  8. package/bun.d.mts +6 -0
  9. package/bun.mjs +36 -0
  10. package/common.cjs +183 -0
  11. package/common.d.cts +25 -0
  12. package/common.d.mts +25 -0
  13. package/common.mjs +174 -0
  14. package/esbuild.cjs +8 -2
  15. package/esbuild.d.cts +2 -14
  16. package/esbuild.d.mts +2 -0
  17. package/esbuild.mjs +7 -0
  18. package/farm.cjs +8 -2
  19. package/farm.d.cts +2 -14
  20. package/farm.d.mts +2 -0
  21. package/farm.mjs +7 -0
  22. package/filter.cjs +63 -0
  23. package/filter.mjs +61 -0
  24. package/index.cjs +30 -2
  25. package/index.d.cts +12 -48
  26. package/index.d.mts +19 -0
  27. package/index.mjs +18 -0
  28. package/package.json +110 -66
  29. package/rolldown-browser.cjs +7 -0
  30. package/rolldown-browser.d.mts +1 -0
  31. package/rolldown-browser.mjs +7 -0
  32. package/rolldown.cjs +8 -2
  33. package/rolldown.d.cts +2 -14
  34. package/rolldown.d.mts +2 -0
  35. package/rolldown.mjs +7 -0
  36. package/rollup-impl.cjs +127 -0
  37. package/rollup-impl.d.cts +38 -0
  38. package/rollup-impl.d.mts +39 -0
  39. package/rollup-impl.mjs +125 -0
  40. package/rollup.cjs +8 -2
  41. package/rollup.d.cts +2 -14
  42. package/rollup.d.mts +2 -0
  43. package/rollup.mjs +7 -0
  44. package/rspack.cjs +8 -2
  45. package/rspack.d.cts +2 -14
  46. package/rspack.d.mts +2 -0
  47. package/rspack.mjs +7 -0
  48. package/vite.cjs +8 -2
  49. package/vite.d.cts +2 -14
  50. package/vite.d.mts +2 -0
  51. package/vite.mjs +7 -0
  52. package/webpack.cjs +8 -2
  53. package/webpack.d.cts +2 -14
  54. package/webpack.d.mts +2 -0
  55. package/webpack.mjs +7 -0
  56. package/babel.cjs.map +0 -1
  57. package/babel.d.ts +0 -10
  58. package/babel.js +0 -2
  59. package/babel.js.map +0 -1
  60. package/bun-Dq3TUCfa.d.cts +0 -27
  61. package/bun-Dq3TUCfa.d.ts +0 -27
  62. package/bun.cjs.map +0 -1
  63. package/bun.d.ts +0 -3
  64. package/bun.js +0 -2
  65. package/bun.js.map +0 -1
  66. package/chunk-DEVVEJF4.cjs +0 -2
  67. package/chunk-DEVVEJF4.cjs.map +0 -1
  68. package/chunk-EESBB3Y4.js +0 -2
  69. package/chunk-EESBB3Y4.js.map +0 -1
  70. package/chunk-LJXNN2RM.cjs +0 -7
  71. package/chunk-LJXNN2RM.cjs.map +0 -1
  72. package/chunk-NQODVO3L.cjs +0 -2
  73. package/chunk-NQODVO3L.cjs.map +0 -1
  74. package/chunk-RIZD7OMC.js +0 -7
  75. package/chunk-RIZD7OMC.js.map +0 -1
  76. package/chunk-V4Q4PLLO.js +0 -2
  77. package/chunk-V4Q4PLLO.js.map +0 -1
  78. package/esbuild.cjs.map +0 -1
  79. package/esbuild.d.ts +0 -14
  80. package/esbuild.js +0 -2
  81. package/esbuild.js.map +0 -1
  82. package/farm.cjs.map +0 -1
  83. package/farm.d.ts +0 -14
  84. package/farm.js +0 -2
  85. package/farm.js.map +0 -1
  86. package/index.cjs.map +0 -1
  87. package/index.d.ts +0 -54
  88. package/index.js +0 -2
  89. package/index.js.map +0 -1
  90. package/rolldown.cjs.map +0 -1
  91. package/rolldown.d.ts +0 -14
  92. package/rolldown.js +0 -2
  93. package/rolldown.js.map +0 -1
  94. package/rollup.cjs.map +0 -1
  95. package/rollup.d.ts +0 -14
  96. package/rollup.js +0 -2
  97. package/rollup.js.map +0 -1
  98. package/rspack.cjs.map +0 -1
  99. package/rspack.d.ts +0 -14
  100. package/rspack.js +0 -2
  101. package/rspack.js.map +0 -1
  102. package/vite.cjs.map +0 -1
  103. package/vite.d.ts +0 -14
  104. package/vite.js +0 -2
  105. package/vite.js.map +0 -1
  106. package/webpack.cjs.map +0 -1
  107. package/webpack.d.ts +0 -14
  108. package/webpack.js +0 -2
  109. package/webpack.js.map +0 -1
@@ -0,0 +1,29 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
package/babel.cjs CHANGED
@@ -1,2 +1,163 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkDEVVEJF4cjs = require('./chunk-DEVVEJF4.cjs');exports.default = _chunkDEVVEJF4cjs.i;
2
- //# sourceMappingURL=babel.cjs.map
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
+ const require_common = require('./common.cjs');
4
+ const require_filter = require('./filter.cjs');
5
+ let defu = require("defu");
6
+ defu = require_runtime.__toESM(defu);
7
+ let tinyest_for_wgsl = require("tinyest-for-wgsl");
8
+ let tinyest = require("tinyest");
9
+ let _babel_standalone = require("@babel/standalone");
10
+ _babel_standalone = require_runtime.__toESM(_babel_standalone);
11
+
12
+ //#region src/babel.ts
13
+ const template = _babel_standalone.packages.template;
14
+ const types = _babel_standalone.packages.types;
15
+ function containsUseGpuDirective(node) {
16
+ var _node$body$directives, _node$body;
17
+ return ("directives" in node.body ? (_node$body$directives = (_node$body = node.body) === null || _node$body === void 0 ? void 0 : _node$body.directives) !== null && _node$body$directives !== void 0 ? _node$body$directives : [] : []).map((directive) => directive.value.value).includes(require_common.useGpuDirective);
18
+ }
19
+ function i(identifier) {
20
+ return types.identifier(identifier);
21
+ }
22
+ const fnNodeToOriginalMap = /* @__PURE__ */ new WeakMap();
23
+ function functionToTranspiled(node, parent) {
24
+ var _fnNodeToOriginalMap$;
25
+ const { params, body, externalNames } = (0, tinyest_for_wgsl.transpileFn)((_fnNodeToOriginalMap$ = fnNodeToOriginalMap.get(node)) !== null && _fnNodeToOriginalMap$ !== void 0 ? _fnNodeToOriginalMap$ : node);
26
+ const maybeName = require_common.getFunctionName(node, parent);
27
+ const metadata = types.objectExpression([
28
+ types.objectProperty(i("v"), types.numericLiteral(tinyest.FORMAT_VERSION)),
29
+ types.objectProperty(i("name"), maybeName ? types.stringLiteral(maybeName) : types.buildUndefinedNode()),
30
+ types.objectProperty(i("ast"), template.expression`${require_common.embedJSON({
31
+ params,
32
+ body,
33
+ externalNames
34
+ })}`()),
35
+ types.objectProperty(i("externals"), types.arrowFunctionExpression([], types.blockStatement([types.returnStatement(types.objectExpression(externalNames.map((name) => types.objectProperty(i(name), i(name), false, name !== "this"))))])))
36
+ ]);
37
+ return types.callExpression(types.arrowFunctionExpression([i("$")], types.logicalExpression("&&", types.callExpression(types.memberExpression(types.assignmentExpression("??=", types.memberExpression(i("globalThis"), i("__TYPEGPU_META__")), types.newExpression(i("WeakMap"), [])), i("set")), [types.assignmentExpression("=", types.memberExpression(i("$"), i("f")), node), metadata]), types.memberExpression(i("$"), i("f")))), [types.objectExpression([])]);
38
+ }
39
+ function wrapInAutoName(node, name) {
40
+ return types.callExpression(template.expression("globalThis.__TYPEGPU_AUTONAME__ ?? (a => a)", { placeholderPattern: false })(), [node, types.stringLiteral(name)]);
41
+ }
42
+ function functionVisitor(ctx) {
43
+ let inUseGpuScope = false;
44
+ return {
45
+ VariableDeclarator(path) {
46
+ require_common.performExpressionNaming(ctx, path.node, (node, name) => {
47
+ path.get("init").replaceWith(wrapInAutoName(node, name));
48
+ });
49
+ },
50
+ AssignmentExpression(path) {
51
+ if (inUseGpuScope) {
52
+ const runtimeFn = require_common.operators[path.node.operator];
53
+ if (runtimeFn) path.replaceWith(types.assignmentExpression("=", path.node.left, types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right])));
54
+ }
55
+ require_common.performExpressionNaming(ctx, path.node, (node, name) => {
56
+ path.get("right").replaceWith(wrapInAutoName(node, name));
57
+ });
58
+ },
59
+ ObjectProperty(path) {
60
+ require_common.performExpressionNaming(ctx, path.node, (node, name) => {
61
+ path.get("value").replaceWith(wrapInAutoName(node, name));
62
+ });
63
+ },
64
+ ClassProperty(path) {
65
+ require_common.performExpressionNaming(ctx, path.node, (node, name) => {
66
+ path.get("value").replaceWith(wrapInAutoName(node, name));
67
+ });
68
+ },
69
+ ImportDeclaration(path) {
70
+ require_common.gatherTgpuAliases(path.node, ctx);
71
+ },
72
+ BinaryExpression: { exit(path) {
73
+ if (!inUseGpuScope) return;
74
+ const runtimeFn = require_common.operators[path.node.operator];
75
+ if (runtimeFn) path.replaceWith(types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right]));
76
+ } },
77
+ ArrowFunctionExpression: {
78
+ enter(path) {
79
+ if (containsUseGpuDirective(path.node)) {
80
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
81
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
82
+ inUseGpuScope = true;
83
+ }
84
+ },
85
+ exit(path) {
86
+ const node = path.node;
87
+ if (containsUseGpuDirective(node)) {
88
+ inUseGpuScope = false;
89
+ const parent = path.parentPath.node;
90
+ path.replaceWith(functionToTranspiled(node, parent));
91
+ path.skip();
92
+ }
93
+ }
94
+ },
95
+ FunctionExpression: {
96
+ enter(path) {
97
+ if (containsUseGpuDirective(path.node)) {
98
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
99
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
100
+ inUseGpuScope = true;
101
+ }
102
+ },
103
+ exit(path) {
104
+ const node = path.node;
105
+ if (containsUseGpuDirective(node)) {
106
+ inUseGpuScope = false;
107
+ const parent = path.parentPath.node;
108
+ path.replaceWith(functionToTranspiled(node, parent));
109
+ path.skip();
110
+ }
111
+ }
112
+ },
113
+ FunctionDeclaration: {
114
+ enter(path) {
115
+ if (containsUseGpuDirective(path.node)) {
116
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
117
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
118
+ inUseGpuScope = true;
119
+ }
120
+ },
121
+ exit(path) {
122
+ var _fnNodeToOriginalMap$2;
123
+ const node = (_fnNodeToOriginalMap$2 = fnNodeToOriginalMap.get(path.node)) !== null && _fnNodeToOriginalMap$2 !== void 0 ? _fnNodeToOriginalMap$2 : path.node;
124
+ if (containsUseGpuDirective(node)) {
125
+ inUseGpuScope = false;
126
+ if (!node.id) return;
127
+ const parent = path.parentPath.node;
128
+ const expression = types.functionExpression(node.id, node.params, node.body);
129
+ path.replaceWith(types.variableDeclaration("const", [types.variableDeclarator(node.id, functionToTranspiled(expression, parent))]));
130
+ path.skip();
131
+ }
132
+ }
133
+ },
134
+ CallExpression: { exit(path) {
135
+ const node = path.node;
136
+ if (require_common.isShellImplementationCall(node, ctx)) {
137
+ const implementation = node.arguments[0];
138
+ if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
139
+ const transpiled = functionToTranspiled(implementation, null);
140
+ path.replaceWith(types.callExpression(node.callee, [transpiled]));
141
+ path.skip();
142
+ }
143
+ }
144
+ } }
145
+ };
146
+ }
147
+ function babel_default() {
148
+ return { visitor: { Program(path, state) {
149
+ const options = (0, defu.default)(state.opts, require_common.defaultOptions);
150
+ const id = state.filename;
151
+ const filter = require_filter.createFilterForId(options);
152
+ if (id && filter && !(filter === null || filter === void 0 ? void 0 : filter(id))) return;
153
+ const ctx = {
154
+ tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
155
+ fileId: id,
156
+ autoNamingEnabled: options.autoNamingEnabled
157
+ };
158
+ path.traverse(functionVisitor(ctx));
159
+ } } };
160
+ }
161
+
162
+ //#endregion
163
+ exports.default = babel_default;
package/babel.d.cts CHANGED
@@ -1,10 +1,10 @@
1
- import * as _babel_traverse from '@babel/traverse';
2
- import * as babel from '@babel/types';
1
+ import { Node, Program } from "./node_modules/.pnpm/@babel_types@7.26.5/node_modules/@babel/types/lib/index-legacy.cjs";
2
+ import * as _babel_traverse0 from "@babel/traverse";
3
3
 
4
+ //#region src/babel.d.ts
4
5
  declare function export_default(): {
5
- visitor: {
6
- Program(this: babel.Node, path: _babel_traverse.NodePath<babel.Program>, state: babel.Node): void;
7
- };
6
+ visitor: {
7
+ Program(this: Node, path: _babel_traverse0.NodePath<Program>, state: Node): void;
8
+ };
8
9
  };
9
-
10
- export { export_default as default };
10
+ export = export_default;
package/babel.d.mts ADDED
@@ -0,0 +1,11 @@
1
+ import { Node, Program } from "./node_modules/.pnpm/@babel_types@7.26.5/node_modules/@babel/types/lib/index-legacy.mjs";
2
+ import * as _babel_traverse0 from "@babel/traverse";
3
+
4
+ //#region src/babel.d.ts
5
+ declare function export_default(): {
6
+ visitor: {
7
+ Program(this: Node, path: _babel_traverse0.NodePath<Program>, state: Node): void;
8
+ };
9
+ };
10
+ //#endregion
11
+ export { export_default as default };
package/babel.mjs ADDED
@@ -0,0 +1,159 @@
1
+ import { defaultOptions, embedJSON, gatherTgpuAliases, getFunctionName, isShellImplementationCall, operators, performExpressionNaming, useGpuDirective } from "./common.mjs";
2
+ import { createFilterForId } from "./filter.mjs";
3
+ import defu from "defu";
4
+ import { transpileFn } from "tinyest-for-wgsl";
5
+ import { FORMAT_VERSION } from "tinyest";
6
+ import * as Babel from "@babel/standalone";
7
+
8
+ //#region src/babel.ts
9
+ const template = Babel.packages.template;
10
+ const types = Babel.packages.types;
11
+ function containsUseGpuDirective(node) {
12
+ var _node$body$directives, _node$body;
13
+ return ("directives" in node.body ? (_node$body$directives = (_node$body = node.body) === null || _node$body === void 0 ? void 0 : _node$body.directives) !== null && _node$body$directives !== void 0 ? _node$body$directives : [] : []).map((directive) => directive.value.value).includes(useGpuDirective);
14
+ }
15
+ function i(identifier) {
16
+ return types.identifier(identifier);
17
+ }
18
+ const fnNodeToOriginalMap = /* @__PURE__ */ new WeakMap();
19
+ function functionToTranspiled(node, parent) {
20
+ var _fnNodeToOriginalMap$;
21
+ const { params, body, externalNames } = transpileFn((_fnNodeToOriginalMap$ = fnNodeToOriginalMap.get(node)) !== null && _fnNodeToOriginalMap$ !== void 0 ? _fnNodeToOriginalMap$ : node);
22
+ const maybeName = getFunctionName(node, parent);
23
+ const metadata = types.objectExpression([
24
+ types.objectProperty(i("v"), types.numericLiteral(FORMAT_VERSION)),
25
+ types.objectProperty(i("name"), maybeName ? types.stringLiteral(maybeName) : types.buildUndefinedNode()),
26
+ types.objectProperty(i("ast"), template.expression`${embedJSON({
27
+ params,
28
+ body,
29
+ externalNames
30
+ })}`()),
31
+ types.objectProperty(i("externals"), types.arrowFunctionExpression([], types.blockStatement([types.returnStatement(types.objectExpression(externalNames.map((name) => types.objectProperty(i(name), i(name), false, name !== "this"))))])))
32
+ ]);
33
+ return types.callExpression(types.arrowFunctionExpression([i("$")], types.logicalExpression("&&", types.callExpression(types.memberExpression(types.assignmentExpression("??=", types.memberExpression(i("globalThis"), i("__TYPEGPU_META__")), types.newExpression(i("WeakMap"), [])), i("set")), [types.assignmentExpression("=", types.memberExpression(i("$"), i("f")), node), metadata]), types.memberExpression(i("$"), i("f")))), [types.objectExpression([])]);
34
+ }
35
+ function wrapInAutoName(node, name) {
36
+ return types.callExpression(template.expression("globalThis.__TYPEGPU_AUTONAME__ ?? (a => a)", { placeholderPattern: false })(), [node, types.stringLiteral(name)]);
37
+ }
38
+ function functionVisitor(ctx) {
39
+ let inUseGpuScope = false;
40
+ return {
41
+ VariableDeclarator(path) {
42
+ performExpressionNaming(ctx, path.node, (node, name) => {
43
+ path.get("init").replaceWith(wrapInAutoName(node, name));
44
+ });
45
+ },
46
+ AssignmentExpression(path) {
47
+ if (inUseGpuScope) {
48
+ const runtimeFn = operators[path.node.operator];
49
+ if (runtimeFn) path.replaceWith(types.assignmentExpression("=", path.node.left, types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right])));
50
+ }
51
+ performExpressionNaming(ctx, path.node, (node, name) => {
52
+ path.get("right").replaceWith(wrapInAutoName(node, name));
53
+ });
54
+ },
55
+ ObjectProperty(path) {
56
+ performExpressionNaming(ctx, path.node, (node, name) => {
57
+ path.get("value").replaceWith(wrapInAutoName(node, name));
58
+ });
59
+ },
60
+ ClassProperty(path) {
61
+ performExpressionNaming(ctx, path.node, (node, name) => {
62
+ path.get("value").replaceWith(wrapInAutoName(node, name));
63
+ });
64
+ },
65
+ ImportDeclaration(path) {
66
+ gatherTgpuAliases(path.node, ctx);
67
+ },
68
+ BinaryExpression: { exit(path) {
69
+ if (!inUseGpuScope) return;
70
+ const runtimeFn = operators[path.node.operator];
71
+ if (runtimeFn) path.replaceWith(types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right]));
72
+ } },
73
+ ArrowFunctionExpression: {
74
+ enter(path) {
75
+ if (containsUseGpuDirective(path.node)) {
76
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
77
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
78
+ inUseGpuScope = true;
79
+ }
80
+ },
81
+ exit(path) {
82
+ const node = path.node;
83
+ if (containsUseGpuDirective(node)) {
84
+ inUseGpuScope = false;
85
+ const parent = path.parentPath.node;
86
+ path.replaceWith(functionToTranspiled(node, parent));
87
+ path.skip();
88
+ }
89
+ }
90
+ },
91
+ FunctionExpression: {
92
+ enter(path) {
93
+ if (containsUseGpuDirective(path.node)) {
94
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
95
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
96
+ inUseGpuScope = true;
97
+ }
98
+ },
99
+ exit(path) {
100
+ const node = path.node;
101
+ if (containsUseGpuDirective(node)) {
102
+ inUseGpuScope = false;
103
+ const parent = path.parentPath.node;
104
+ path.replaceWith(functionToTranspiled(node, parent));
105
+ path.skip();
106
+ }
107
+ }
108
+ },
109
+ FunctionDeclaration: {
110
+ enter(path) {
111
+ if (containsUseGpuDirective(path.node)) {
112
+ fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
113
+ if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
114
+ inUseGpuScope = true;
115
+ }
116
+ },
117
+ exit(path) {
118
+ var _fnNodeToOriginalMap$2;
119
+ const node = (_fnNodeToOriginalMap$2 = fnNodeToOriginalMap.get(path.node)) !== null && _fnNodeToOriginalMap$2 !== void 0 ? _fnNodeToOriginalMap$2 : path.node;
120
+ if (containsUseGpuDirective(node)) {
121
+ inUseGpuScope = false;
122
+ if (!node.id) return;
123
+ const parent = path.parentPath.node;
124
+ const expression = types.functionExpression(node.id, node.params, node.body);
125
+ path.replaceWith(types.variableDeclaration("const", [types.variableDeclarator(node.id, functionToTranspiled(expression, parent))]));
126
+ path.skip();
127
+ }
128
+ }
129
+ },
130
+ CallExpression: { exit(path) {
131
+ const node = path.node;
132
+ if (isShellImplementationCall(node, ctx)) {
133
+ const implementation = node.arguments[0];
134
+ if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
135
+ const transpiled = functionToTranspiled(implementation, null);
136
+ path.replaceWith(types.callExpression(node.callee, [transpiled]));
137
+ path.skip();
138
+ }
139
+ }
140
+ } }
141
+ };
142
+ }
143
+ function babel_default() {
144
+ return { visitor: { Program(path, state) {
145
+ const options = defu(state.opts, defaultOptions);
146
+ const id = state.filename;
147
+ const filter = createFilterForId(options);
148
+ if (id && filter && !(filter === null || filter === void 0 ? void 0 : filter(id))) return;
149
+ const ctx = {
150
+ tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
151
+ fileId: id,
152
+ autoNamingEnabled: options.autoNamingEnabled
153
+ };
154
+ path.traverse(functionVisitor(ctx));
155
+ } } };
156
+ }
157
+
158
+ //#endregion
159
+ export { babel_default as default };
package/bun.cjs CHANGED
@@ -1,2 +1,40 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkNQODVO3Lcjs = require('./chunk-NQODVO3L.cjs');require('./chunk-DEVVEJF4.cjs');exports.default = _chunkNQODVO3Lcjs.a;
2
- //# sourceMappingURL=bun.cjs.map
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
+ const require_common = require('./common.cjs');
4
+ const require_babel = require('./babel.cjs');
5
+ let defu = require("defu");
6
+ defu = require_runtime.__toESM(defu);
7
+ let _babel_standalone = require("@babel/standalone");
8
+ _babel_standalone = require_runtime.__toESM(_babel_standalone);
9
+
10
+ //#region src/bun.ts
11
+ var bun_default = (rawOptions) => {
12
+ const options = (0, defu.default)(rawOptions, require_common.defaultOptions);
13
+ const include = options.include;
14
+ if (!(include instanceof RegExp)) throw new Error(`Unsupported 'include' options in Bun plugin. Please provide a single regular expression`);
15
+ if (options.exclude) throw new Error(`Unsupported 'exclude' option in Bun plugin`);
16
+ return {
17
+ name: "unplugin-typegpu",
18
+ setup(build) {
19
+ build.onLoad({ filter: include }, async (args) => {
20
+ const text = await Bun.file(args.path).text();
21
+ if (require_common.earlyPruneRegex.every((pattern) => !pattern.test(text))) return {
22
+ contents: text,
23
+ loader: args.loader
24
+ };
25
+ const result = _babel_standalone.transform(text, {
26
+ presets: [["typescript", { allowDeclareFields: true }]],
27
+ filename: args.path,
28
+ plugins: [require_babel.default]
29
+ }).code;
30
+ return {
31
+ contents: result !== null && result !== void 0 ? result : text,
32
+ loader: args.loader
33
+ };
34
+ });
35
+ }
36
+ };
37
+ };
38
+
39
+ //#endregion
40
+ exports.default = bun_default;
package/bun.d.cts CHANGED
@@ -1,3 +1,5 @@
1
- import 'bun';
2
- export { _ as default } from './bun-Dq3TUCfa.cjs';
3
- import 'unplugin';
1
+ import { Options } from "./common.cjs";
2
+
3
+ //#region src/bun.d.ts
4
+ declare const _default: (rawOptions: Options) => BunPlugin;
5
+ export = _default;
package/bun.d.mts ADDED
@@ -0,0 +1,6 @@
1
+ import { Options } from "./common.mjs";
2
+
3
+ //#region src/bun.d.ts
4
+ declare const _default: (rawOptions: Options) => BunPlugin;
5
+ //#endregion
6
+ export { _default as default };
package/bun.mjs ADDED
@@ -0,0 +1,36 @@
1
+ import { defaultOptions, earlyPruneRegex } from "./common.mjs";
2
+ import babel_default from "./babel.mjs";
3
+ import defu from "defu";
4
+ import * as Babel from "@babel/standalone";
5
+
6
+ //#region src/bun.ts
7
+ var bun_default = (rawOptions) => {
8
+ const options = defu(rawOptions, defaultOptions);
9
+ const include = options.include;
10
+ if (!(include instanceof RegExp)) throw new Error(`Unsupported 'include' options in Bun plugin. Please provide a single regular expression`);
11
+ if (options.exclude) throw new Error(`Unsupported 'exclude' option in Bun plugin`);
12
+ return {
13
+ name: "unplugin-typegpu",
14
+ setup(build) {
15
+ build.onLoad({ filter: include }, async (args) => {
16
+ const text = await Bun.file(args.path).text();
17
+ if (earlyPruneRegex.every((pattern) => !pattern.test(text))) return {
18
+ contents: text,
19
+ loader: args.loader
20
+ };
21
+ const result = Babel.transform(text, {
22
+ presets: [["typescript", { allowDeclareFields: true }]],
23
+ filename: args.path,
24
+ plugins: [babel_default]
25
+ }).code;
26
+ return {
27
+ contents: result !== null && result !== void 0 ? result : text,
28
+ loader: args.loader
29
+ };
30
+ });
31
+ }
32
+ };
33
+ };
34
+
35
+ //#endregion
36
+ export { bun_default as default };
package/common.cjs ADDED
@@ -0,0 +1,183 @@
1
+
2
+ //#region src/common.ts
3
+ const defaultOptions = {
4
+ include: /\.m?[jt]sx?(?:\?.*)?$/,
5
+ autoNamingEnabled: true,
6
+ earlyPruning: true
7
+ };
8
+ function embedJSON(jsValue) {
9
+ return JSON.stringify(jsValue).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
10
+ }
11
+ /**
12
+ * Checks if `node` is an alias for the 'tgpu' object, traditionally
13
+ * available via `import tgpu from 'typegpu'`.
14
+ */
15
+ function isTgpu(ctx, node) {
16
+ let path = "";
17
+ let tail = node;
18
+ while (true) if (tail.type === "MemberExpression") {
19
+ if ((tail.property.type === "Literal" || tail.property.type === "StringLiteral") && tail.property.value === "~unstable") {
20
+ tail = tail.object;
21
+ continue;
22
+ }
23
+ if (tail.property.type !== "Identifier") break;
24
+ path = path ? `${tail.property.name}.${path}` : tail.property.name;
25
+ tail = tail.object;
26
+ } else if (tail.type === "Identifier") {
27
+ path = path ? `${tail.name}.${path}` : tail.name;
28
+ break;
29
+ } else break;
30
+ return ctx.tgpuAliases.has(path);
31
+ }
32
+ function gatherTgpuAliases(node, ctx) {
33
+ if (node.source.value === "typegpu") {
34
+ for (const spec of node.specifiers) if (spec.type === "ImportDefaultSpecifier" || spec.type === "ImportSpecifier" && spec.imported.type === "Identifier" && spec.imported.name === "tgpu") ctx.tgpuAliases.add(spec.local.name);
35
+ else if (spec.type === "ImportNamespaceSpecifier") ctx.tgpuAliases.add(`${spec.local.name}.tgpu`);
36
+ }
37
+ }
38
+ const fnShellFunctionNames = [
39
+ "fn",
40
+ "vertexFn",
41
+ "fragmentFn",
42
+ "computeFn"
43
+ ];
44
+ function isShellImplementationCall(node, ctx) {
45
+ return node.callee.type === "CallExpression" && node.callee.callee.type === "MemberExpression" && node.callee.callee.property.type === "Identifier" && fnShellFunctionNames.includes(node.callee.callee.property.name) && node.arguments.length === 1 && isTgpu(ctx, node.callee.callee.object);
46
+ }
47
+ /**
48
+ * Extracts a name and expression from nodes that contain a label and an expression,
49
+ * such as VariableDeclarator or PropertyDefinition.
50
+ * Returns a tuple of [name, expression] if found, otherwise undefined.
51
+ *
52
+ * @example
53
+ * extractLabelledExpression(node`let name = tgpu.bindGroupLayout({});`)
54
+ * // ["name", node`tgpu.bindGroupLayout({})`]
55
+ */
56
+ function extractLabelledExpression(node) {
57
+ if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init) return [node.id.name, node.init];
58
+ else if (node.type === "AssignmentExpression") {
59
+ const maybeName = tryFindIdentifier(node.left);
60
+ if (maybeName) return [maybeName, node.right];
61
+ } else if ((node.type === "Property" || node.type === "ObjectProperty") && node.key.type === "Identifier") return [node.key.name, node.value];
62
+ else if ((node.type === "ClassProperty" || node.type === "PropertyDefinition") && node.value && node.key.type === "Identifier") return [node.key.name, node.value];
63
+ }
64
+ function getFunctionName(node, parent) {
65
+ var _extractLabelledExpre, _node$id;
66
+ const maybeName = parent ? (_extractLabelledExpre = extractLabelledExpression(parent)) === null || _extractLabelledExpre === void 0 ? void 0 : _extractLabelledExpre[0] : void 0;
67
+ return maybeName !== null && maybeName !== void 0 ? maybeName : node.type === "FunctionDeclaration" || node.type === "FunctionExpression" ? (_node$id = node.id) === null || _node$id === void 0 ? void 0 : _node$id.name : void 0;
68
+ }
69
+ const resourceConstructors = [
70
+ "bindGroupLayout",
71
+ "vertexLayout",
72
+ "privateVar",
73
+ "workgroupVar",
74
+ "const",
75
+ "slot",
76
+ "accessor",
77
+ "comptime",
78
+ ...fnShellFunctionNames,
79
+ "createBuffer",
80
+ "createMutable",
81
+ "createReadonly",
82
+ "createUniform",
83
+ "createQuerySet",
84
+ "createPipeline",
85
+ "createComputePipeline",
86
+ "createGuardedComputePipeline",
87
+ "createRenderPipeline",
88
+ "createTexture",
89
+ "createSampler",
90
+ "createComparisonSampler",
91
+ "struct",
92
+ "unstruct",
93
+ "createView"
94
+ ];
95
+ /**
96
+ * Checks if `node` should be wrapped in an autoname function.
97
+ * Since it is mostly for debugging and clean WGSL generation,
98
+ * some false positives and false negatives are admissible.
99
+ */
100
+ function containsResourceConstructorCall(node, ctx) {
101
+ if (node.type === "CallExpression") {
102
+ if (isShellImplementationCall(node, ctx)) return true;
103
+ if (node.callee.type === "Identifier" && resourceConstructors.includes(node.callee.name)) return true;
104
+ if (node.callee.type === "MemberExpression") {
105
+ if (node.callee.property.type === "Identifier") {
106
+ if (resourceConstructors.includes(node.callee.property.name)) return true;
107
+ if (node.callee.property.name === "$name") return false;
108
+ }
109
+ return containsResourceConstructorCall(node.callee.object, ctx);
110
+ }
111
+ }
112
+ if (node.type === "TaggedTemplateExpression") return containsResourceConstructorCall(node.tag, ctx);
113
+ return false;
114
+ }
115
+ /**
116
+ * Tries to find an identifier in a node.
117
+ *
118
+ * @example
119
+ * // syntax is simplified, imagine the arguments are appropriate nodes instead
120
+ * tryFindIdentifier('myBuffer'); // 'myBuffer'
121
+ * tryFindIdentifier('buffers.myBuffer'); // 'myBuffer'
122
+ * tryFindIdentifier('this.myBuffer'); // 'myBuffer'
123
+ * tryFindIdentifier('[a, b]'); // undefined
124
+ */
125
+ function tryFindIdentifier(node) {
126
+ if (node.type === "Identifier") return node.name;
127
+ if (node.type === "PrivateName") return tryFindIdentifier(node.id);
128
+ if (node.type === "PrivateIdentifier") return node.name;
129
+ if (node.type === "MemberExpression") return tryFindIdentifier(node.property);
130
+ }
131
+ /**
132
+ * Checks if `node` contains a label and a tgpu expression that could be named.
133
+ * If so, it calls the provided callback. Nodes selected for naming include:
134
+ *
135
+ * `let name = tgpu.bindGroupLayout({});` (VariableDeclarator)
136
+ *
137
+ * `name = tgpu.bindGroupLayout({});` (AssignmentExpression)
138
+ *
139
+ * `property: tgpu.bindGroupLayout({})` (Property/ObjectProperty)
140
+ *
141
+ * Since it is mostly for debugging and clean WGSL generation,
142
+ * some false positives and false negatives are admissible.
143
+ *
144
+ * This function is NOT used for auto-naming shell-less functions.
145
+ * Those are handled separately.
146
+ *
147
+ * @privateRemarks
148
+ * When adding new checks, you need to call this method in the corresponding node in Babel.
149
+ */
150
+ function performExpressionNaming(ctx, node, namingCallback) {
151
+ if (!ctx.autoNamingEnabled) return;
152
+ const labelledExpression = extractLabelledExpression(node);
153
+ if (labelledExpression) {
154
+ const [label, expression] = labelledExpression;
155
+ if (containsResourceConstructorCall(expression, ctx)) namingCallback(expression, label);
156
+ }
157
+ }
158
+ const useGpuDirective = "use gpu";
159
+ /** Regular expressions used for early pruning (to avoid unnecessary parsing, which is expensive) */
160
+ const earlyPruneRegex = [/["']use gpu["']/, /t(ype)?gpu/];
161
+ const operators = {
162
+ "+": "__tsover_add",
163
+ "-": "__tsover_sub",
164
+ "*": "__tsover_mul",
165
+ "/": "__tsover_div",
166
+ "%": "__tsover_mod",
167
+ "+=": "__tsover_add",
168
+ "-=": "__tsover_sub",
169
+ "*=": "__tsover_mul",
170
+ "/=": "__tsover_div",
171
+ "%=": "__tsover_mod"
172
+ };
173
+
174
+ //#endregion
175
+ exports.defaultOptions = defaultOptions;
176
+ exports.earlyPruneRegex = earlyPruneRegex;
177
+ exports.embedJSON = embedJSON;
178
+ exports.gatherTgpuAliases = gatherTgpuAliases;
179
+ exports.getFunctionName = getFunctionName;
180
+ exports.isShellImplementationCall = isShellImplementationCall;
181
+ exports.operators = operators;
182
+ exports.performExpressionNaming = performExpressionNaming;
183
+ exports.useGpuDirective = useGpuDirective;