unplugin-typegpu 0.10.2 → 0.11.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 (88) hide show
  1. package/README.md +14 -4
  2. package/babel-C8UzthFd.d.cts +13 -0
  3. package/babel-CbManzjz.js +128 -0
  4. package/babel-CmCnq-43.d.ts +13 -0
  5. package/babel-Dis-Pwfu.cjs +140 -0
  6. package/babel.cjs +4 -160
  7. package/babel.d.cts +3 -10
  8. package/babel.d.ts +3 -0
  9. package/babel.js +4 -0
  10. package/bun-CDECNeIU.js +134 -0
  11. package/bun-DCACcaN2.cjs +154 -0
  12. package/bun-cwXs2EDP.d.cts +6 -0
  13. package/bun-ovScJ4Es.d.ts +6 -0
  14. package/bun.cjs +4 -38
  15. package/bun.d.cts +2 -4
  16. package/bun.d.ts +3 -0
  17. package/bun.js +4 -0
  18. package/common-BlvLUwEz.d.ts +85 -0
  19. package/common-BoeQmwpt.cjs +340 -0
  20. package/common-CElmMmuq.js +279 -0
  21. package/common-DZ2cLtbe.d.cts +84 -0
  22. package/esbuild.cjs +6 -3
  23. package/esbuild.d.cts +4 -1
  24. package/esbuild.d.ts +5 -0
  25. package/esbuild.js +10 -0
  26. package/farm.cjs +6 -3
  27. package/farm.d.cts +4 -1
  28. package/farm.d.ts +5 -0
  29. package/farm.js +10 -0
  30. package/index-COIv1oYx.d.cts +48 -0
  31. package/index-PPreQbn5.d.ts +48 -0
  32. package/index.cjs +17 -28
  33. package/index.d.cts +5 -17
  34. package/index.d.ts +5 -0
  35. package/index.js +6 -0
  36. package/package.json +11 -9
  37. package/rolldown.cjs +6 -3
  38. package/rolldown.d.cts +4 -1
  39. package/rolldown.d.ts +5 -0
  40. package/rolldown.js +10 -0
  41. package/rollup.cjs +6 -3
  42. package/rollup.d.cts +4 -1
  43. package/rollup.d.ts +5 -0
  44. package/rollup.js +10 -0
  45. package/rspack.cjs +6 -3
  46. package/rspack.d.cts +4 -1
  47. package/rspack.d.ts +5 -0
  48. package/rspack.js +10 -0
  49. package/src-D84rmPxv.js +21 -0
  50. package/src-bIj7OBnd.cjs +82 -0
  51. package/vite.cjs +6 -3
  52. package/vite.d.cts +4 -1
  53. package/vite.d.ts +5 -0
  54. package/vite.js +10 -0
  55. package/webpack.cjs +6 -3
  56. package/webpack.d.cts +4 -1
  57. package/webpack.d.ts +5 -0
  58. package/webpack.js +10 -0
  59. package/babel.d.mts +0 -11
  60. package/babel.mjs +0 -158
  61. package/bun.d.mts +0 -6
  62. package/bun.mjs +0 -36
  63. package/common-CRPQ2KE6.d.mts +0 -25
  64. package/common-Dpl8jppn.d.cts +0 -25
  65. package/esbuild.d.mts +0 -2
  66. package/esbuild.mjs +0 -7
  67. package/farm.d.mts +0 -2
  68. package/farm.mjs +0 -7
  69. package/filter-6T-5w4kG.mjs +0 -234
  70. package/filter-CKmD4HRk.cjs +0 -327
  71. package/index-legacy-DRI9LQiG.d.mts +0 -1488
  72. package/index-legacy-xKi5C2ga.d.cts +0 -1488
  73. package/index.d.mts +0 -17
  74. package/index.mjs +0 -17
  75. package/rolldown-browser-DKeiFM_e.mjs +0 -129
  76. package/rolldown-browser-DUanNY-R.cjs +0 -141
  77. package/rolldown-browser-JYqfNLQ8.d.mts +0 -1247
  78. package/rolldown.d.mts +0 -2
  79. package/rolldown.mjs +0 -7
  80. package/rollup-impl-DqC_d-_W.d.cts +0 -1246
  81. package/rollup.d.mts +0 -2
  82. package/rollup.mjs +0 -7
  83. package/rspack.d.mts +0 -2
  84. package/rspack.mjs +0 -7
  85. package/vite.d.mts +0 -2
  86. package/vite.mjs +0 -7
  87. package/webpack.d.mts +0 -2
  88. package/webpack.mjs +0 -7
package/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  # unplugin-typegpu
4
4
 
5
- 🚧 **Under Construction** 🚧 -
6
5
  [GitHub](https://github.com/software-mansion/TypeGPU/tree/main/packages/unplugin-typegpu)
7
6
 
8
7
  Read more about the plugin in the
@@ -10,10 +9,10 @@ Read more about the plugin in the
10
9
 
11
10
  </div>
12
11
 
13
- Build plugins for [TypeGPU](https://typegpu.com) that enable:
12
+ A set of bundler plugins that enhance [TypeGPU](https://typegpu.com) with:
14
13
 
15
- - Seamless JavaScript -> WGSL transpilation
16
- - [🚧 TODO] Improved debugging with automatic naming of resources
14
+ - JavaScript/TypeScript shader support ('use gpu' directive)
15
+ - Improved debugging with automatic naming of resources
17
16
 
18
17
  ## Getting Started
19
18
 
@@ -48,6 +47,17 @@ export default defineConfig({
48
47
  });
49
48
  ```
50
49
 
50
+ - bun
51
+
52
+ ```ts
53
+ // preload.ts
54
+
55
+ import { plugin } from 'bun';
56
+ import typegpu from 'unplugin-typegpu/bun';
57
+
58
+ void plugin(typegpu());
59
+ ```
60
+
51
61
  ## TypeGPU is created by Software Mansion
52
62
 
53
63
  [![swm](https://logo.swmansion.com/logo?color=white&variant=desktop&width=150&tag=typegpu-github 'Software Mansion')](https://swmansion.com)
@@ -0,0 +1,13 @@
1
+ import { n as PluginState, r as t } from "./common-DZ2cLtbe.cjs";
2
+ import { NodePath } from "@babel/traverse";
3
+
4
+ //#region src/babel.d.ts
5
+ declare function TypeGPUPlugin(): {
6
+ name: string;
7
+ pre(this: PluginState): void;
8
+ visitor: {
9
+ Program(this: PluginState, path: NodePath<t.Program>, state: PluginState): void;
10
+ };
11
+ };
12
+ //#endregion
13
+ export { TypeGPUPlugin as t };
@@ -0,0 +1,128 @@
1
+ import { a as initPluginState, i as getBlockScope, o as t, r as functionVisitor, t as defaultOptions } from "./common-CElmMmuq.js";
2
+ import defu from "defu";
3
+ import { transpileFn } from "tinyest-for-wgsl";
4
+ import { FORMAT_VERSION } from "tinyest";
5
+ import { resolve } from "pathe";
6
+ import picomatch from "picomatch";
7
+
8
+ //#region src/core/filter.ts
9
+ function toArray(array) {
10
+ const result = array || [];
11
+ if (Array.isArray(result)) return result;
12
+ return [result];
13
+ }
14
+ const BACKSLASH_REGEX = /\\/g;
15
+ function normalize(path) {
16
+ return path.replace(BACKSLASH_REGEX, "/");
17
+ }
18
+ const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Z]:)?[/\\|])/i;
19
+ function isAbsolute(path) {
20
+ return ABSOLUTE_PATH_REGEX.test(path);
21
+ }
22
+ function getMatcherString(glob, cwd) {
23
+ if (glob.startsWith("**") || isAbsolute(glob)) return normalize(glob);
24
+ return normalize(resolve(cwd, glob));
25
+ }
26
+ function patternToIdFilter(pattern) {
27
+ if (pattern instanceof RegExp) return (id) => {
28
+ const normalizedId = normalize(id);
29
+ const result = pattern.test(normalizedId);
30
+ pattern.lastIndex = 0;
31
+ return result;
32
+ };
33
+ const matcher = picomatch(getMatcherString(pattern, process.cwd()), { dot: true });
34
+ return (id) => {
35
+ return matcher(normalize(id));
36
+ };
37
+ }
38
+ function createFilter(exclude, include) {
39
+ if (!exclude && !include) return;
40
+ return (input) => {
41
+ if (exclude === null || exclude === void 0 ? void 0 : exclude.some((filter) => filter(input))) return false;
42
+ if (include === null || include === void 0 ? void 0 : include.some((filter) => filter(input))) return true;
43
+ return !(include && include.length > 0);
44
+ };
45
+ }
46
+ function normalizeFilter(filter) {
47
+ if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] };
48
+ if (Array.isArray(filter)) return { include: filter };
49
+ return {
50
+ exclude: filter.exclude ? toArray(filter.exclude) : void 0,
51
+ include: filter.include ? toArray(filter.include) : void 0
52
+ };
53
+ }
54
+ function createIdFilter(filter) {
55
+ if (!filter) return;
56
+ const { exclude, include } = normalizeFilter(filter);
57
+ return createFilter(exclude === null || exclude === void 0 ? void 0 : exclude.map(patternToIdFilter), include === null || include === void 0 ? void 0 : include.map(patternToIdFilter));
58
+ }
59
+ function createFilterForId(filter) {
60
+ const filterFunction = createIdFilter(filter);
61
+ return filterFunction ? (id) => !!filterFunction(id) : void 0;
62
+ }
63
+
64
+ //#endregion
65
+ //#region src/babel.ts
66
+ function i(identifier) {
67
+ return t.identifier(identifier);
68
+ }
69
+ function assignMetadata(path, name, ast) {
70
+ const metadata = t.objectExpression([
71
+ t.objectProperty(i("v"), t.numericLiteral(FORMAT_VERSION)),
72
+ t.objectProperty(i("name"), t.valueToNode(name)),
73
+ t.objectProperty(i("ast"), t.valueToNode(ast)),
74
+ t.objectProperty(i("externals"), t.arrowFunctionExpression([], t.blockStatement([t.returnStatement(t.objectExpression(ast.externalNames.map((name$1) => name$1 === "this" ? t.objectProperty(i("this"), t.thisExpression()) : t.objectProperty(i(name$1), i(name$1), false, name$1 !== "this"))))])))
75
+ ]);
76
+ let expression;
77
+ const visibility = t.isFunctionDeclaration(path.node) ? getBlockScope(path) : void 0;
78
+ if (t.isFunctionDeclaration(path.node)) expression = t.functionExpression(path.node.id, path.node.params, path.node.body);
79
+ else expression = path.node;
80
+ const callExpr = t.callExpression(t.arrowFunctionExpression([i("$")], t.logicalExpression("&&", t.callExpression(t.memberExpression(t.assignmentExpression("??=", t.memberExpression(i("globalThis"), i("__TYPEGPU_META__")), t.newExpression(i("WeakMap"), [])), i("set")), [t.assignmentExpression("=", t.memberExpression(i("$"), i("f")), expression), metadata]), t.memberExpression(i("$"), i("f")))), [t.objectExpression([])]);
81
+ t.addComment(callExpr, "leading", "#__PURE__");
82
+ let replacement = callExpr;
83
+ if (t.isFunctionDeclaration(path.node) && path.node.id) {
84
+ const declaration = t.variableDeclaration("const", [t.variableDeclarator(path.node.id, callExpr)]);
85
+ t.inheritLeadingComments(declaration, path.node);
86
+ replacement = declaration;
87
+ }
88
+ if (visibility) {
89
+ visibility.unshiftContainer("body", replacement);
90
+ this.alreadyTransformed.add(expression);
91
+ path.remove();
92
+ } else path.replaceWith(replacement);
93
+ path.skip();
94
+ }
95
+ function wrapInAutoName(path, name) {
96
+ const callExpr = t.callExpression(t.logicalExpression("??", t.memberExpression(i("globalThis"), i("__TYPEGPU_AUTONAME__")), t.arrowFunctionExpression([i("a")], i("a"))), [path.node, t.stringLiteral(name)]);
97
+ t.addComment(callExpr, "leading", "#__PURE__");
98
+ path.replaceWith(callExpr);
99
+ }
100
+ function replaceWithAssignmentOverload(path, runtimeFn) {
101
+ path.replaceWith(t.assignmentExpression("=", path.node.left, t.callExpression(i(runtimeFn), [path.node.left, path.node.right])));
102
+ }
103
+ function replaceWithBinaryOverload(path, runtimeFn) {
104
+ path.replaceWith(t.callExpression(i(runtimeFn), [path.node.left, path.node.right]));
105
+ }
106
+ function TypeGPUPlugin() {
107
+ return {
108
+ name: "typegpu",
109
+ pre() {
110
+ this.opts = defu(this.opts, defaultOptions);
111
+ initPluginState(this, {
112
+ warn: (message) => console.warn(message),
113
+ assignMetadata,
114
+ wrapInAutoName,
115
+ replaceWithAssignmentOverload,
116
+ replaceWithBinaryOverload
117
+ });
118
+ },
119
+ visitor: { Program(path, state) {
120
+ const filter = createFilterForId(state.opts);
121
+ if (state.filename && filter && !(filter === null || filter === void 0 ? void 0 : filter(state.filename))) return;
122
+ path.traverse(functionVisitor, state);
123
+ } }
124
+ };
125
+ }
126
+
127
+ //#endregion
128
+ export { TypeGPUPlugin as t };
@@ -0,0 +1,13 @@
1
+ import { n as PluginState, r as t } from "./common-BlvLUwEz.js";
2
+ import { NodePath } from "@babel/traverse";
3
+
4
+ //#region src/babel.d.ts
5
+ declare function TypeGPUPlugin(): {
6
+ name: string;
7
+ pre(this: PluginState): void;
8
+ visitor: {
9
+ Program(this: PluginState, path: NodePath<t.Program>, state: PluginState): void;
10
+ };
11
+ };
12
+ //#endregion
13
+ export { TypeGPUPlugin as t };
@@ -0,0 +1,140 @@
1
+ const require_common = require('./common-BoeQmwpt.cjs');
2
+ let defu = require("defu");
3
+ defu = require_common.__toESM(defu);
4
+ let tinyest_for_wgsl = require("tinyest-for-wgsl");
5
+ tinyest_for_wgsl = require_common.__toESM(tinyest_for_wgsl);
6
+ let tinyest = require("tinyest");
7
+ tinyest = require_common.__toESM(tinyest);
8
+ let pathe = require("pathe");
9
+ pathe = require_common.__toESM(pathe);
10
+ let picomatch = require("picomatch");
11
+ picomatch = require_common.__toESM(picomatch);
12
+ let __babel_types = require("@babel/types");
13
+ __babel_types = require_common.__toESM(__babel_types);
14
+
15
+ //#region src/core/filter.ts
16
+ function toArray(array) {
17
+ const result = array || [];
18
+ if (Array.isArray(result)) return result;
19
+ return [result];
20
+ }
21
+ const BACKSLASH_REGEX = /\\/g;
22
+ function normalize(path) {
23
+ return path.replace(BACKSLASH_REGEX, "/");
24
+ }
25
+ const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Z]:)?[/\\|])/i;
26
+ function isAbsolute(path) {
27
+ return ABSOLUTE_PATH_REGEX.test(path);
28
+ }
29
+ function getMatcherString(glob, cwd) {
30
+ if (glob.startsWith("**") || isAbsolute(glob)) return normalize(glob);
31
+ return normalize((0, pathe.resolve)(cwd, glob));
32
+ }
33
+ function patternToIdFilter(pattern) {
34
+ if (pattern instanceof RegExp) return (id) => {
35
+ const normalizedId = normalize(id);
36
+ const result = pattern.test(normalizedId);
37
+ pattern.lastIndex = 0;
38
+ return result;
39
+ };
40
+ const matcher = (0, picomatch.default)(getMatcherString(pattern, process.cwd()), { dot: true });
41
+ return (id) => {
42
+ return matcher(normalize(id));
43
+ };
44
+ }
45
+ function createFilter(exclude, include) {
46
+ if (!exclude && !include) return;
47
+ return (input) => {
48
+ if (exclude === null || exclude === void 0 ? void 0 : exclude.some((filter) => filter(input))) return false;
49
+ if (include === null || include === void 0 ? void 0 : include.some((filter) => filter(input))) return true;
50
+ return !(include && include.length > 0);
51
+ };
52
+ }
53
+ function normalizeFilter(filter) {
54
+ if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] };
55
+ if (Array.isArray(filter)) return { include: filter };
56
+ return {
57
+ exclude: filter.exclude ? toArray(filter.exclude) : void 0,
58
+ include: filter.include ? toArray(filter.include) : void 0
59
+ };
60
+ }
61
+ function createIdFilter(filter) {
62
+ if (!filter) return;
63
+ const { exclude, include } = normalizeFilter(filter);
64
+ return createFilter(exclude === null || exclude === void 0 ? void 0 : exclude.map(patternToIdFilter), include === null || include === void 0 ? void 0 : include.map(patternToIdFilter));
65
+ }
66
+ function createFilterForId(filter) {
67
+ const filterFunction = createIdFilter(filter);
68
+ return filterFunction ? (id) => !!filterFunction(id) : void 0;
69
+ }
70
+
71
+ //#endregion
72
+ //#region src/babel.ts
73
+ function i(identifier) {
74
+ return __babel_types.identifier(identifier);
75
+ }
76
+ function assignMetadata(path, name, ast) {
77
+ const metadata = __babel_types.objectExpression([
78
+ __babel_types.objectProperty(i("v"), __babel_types.numericLiteral(tinyest.FORMAT_VERSION)),
79
+ __babel_types.objectProperty(i("name"), __babel_types.valueToNode(name)),
80
+ __babel_types.objectProperty(i("ast"), __babel_types.valueToNode(ast)),
81
+ __babel_types.objectProperty(i("externals"), __babel_types.arrowFunctionExpression([], __babel_types.blockStatement([__babel_types.returnStatement(__babel_types.objectExpression(ast.externalNames.map((name$1) => name$1 === "this" ? __babel_types.objectProperty(i("this"), __babel_types.thisExpression()) : __babel_types.objectProperty(i(name$1), i(name$1), false, name$1 !== "this"))))])))
82
+ ]);
83
+ let expression;
84
+ const visibility = __babel_types.isFunctionDeclaration(path.node) ? require_common.getBlockScope(path) : void 0;
85
+ if (__babel_types.isFunctionDeclaration(path.node)) expression = __babel_types.functionExpression(path.node.id, path.node.params, path.node.body);
86
+ else expression = path.node;
87
+ const callExpr = __babel_types.callExpression(__babel_types.arrowFunctionExpression([i("$")], __babel_types.logicalExpression("&&", __babel_types.callExpression(__babel_types.memberExpression(__babel_types.assignmentExpression("??=", __babel_types.memberExpression(i("globalThis"), i("__TYPEGPU_META__")), __babel_types.newExpression(i("WeakMap"), [])), i("set")), [__babel_types.assignmentExpression("=", __babel_types.memberExpression(i("$"), i("f")), expression), metadata]), __babel_types.memberExpression(i("$"), i("f")))), [__babel_types.objectExpression([])]);
88
+ __babel_types.addComment(callExpr, "leading", "#__PURE__");
89
+ let replacement = callExpr;
90
+ if (__babel_types.isFunctionDeclaration(path.node) && path.node.id) {
91
+ const declaration = __babel_types.variableDeclaration("const", [__babel_types.variableDeclarator(path.node.id, callExpr)]);
92
+ __babel_types.inheritLeadingComments(declaration, path.node);
93
+ replacement = declaration;
94
+ }
95
+ if (visibility) {
96
+ visibility.unshiftContainer("body", replacement);
97
+ this.alreadyTransformed.add(expression);
98
+ path.remove();
99
+ } else path.replaceWith(replacement);
100
+ path.skip();
101
+ }
102
+ function wrapInAutoName(path, name) {
103
+ const callExpr = __babel_types.callExpression(__babel_types.logicalExpression("??", __babel_types.memberExpression(i("globalThis"), i("__TYPEGPU_AUTONAME__")), __babel_types.arrowFunctionExpression([i("a")], i("a"))), [path.node, __babel_types.stringLiteral(name)]);
104
+ __babel_types.addComment(callExpr, "leading", "#__PURE__");
105
+ path.replaceWith(callExpr);
106
+ }
107
+ function replaceWithAssignmentOverload(path, runtimeFn) {
108
+ path.replaceWith(__babel_types.assignmentExpression("=", path.node.left, __babel_types.callExpression(i(runtimeFn), [path.node.left, path.node.right])));
109
+ }
110
+ function replaceWithBinaryOverload(path, runtimeFn) {
111
+ path.replaceWith(__babel_types.callExpression(i(runtimeFn), [path.node.left, path.node.right]));
112
+ }
113
+ function TypeGPUPlugin() {
114
+ return {
115
+ name: "typegpu",
116
+ pre() {
117
+ this.opts = (0, defu.default)(this.opts, require_common.defaultOptions);
118
+ require_common.initPluginState(this, {
119
+ warn: (message) => console.warn(message),
120
+ assignMetadata,
121
+ wrapInAutoName,
122
+ replaceWithAssignmentOverload,
123
+ replaceWithBinaryOverload
124
+ });
125
+ },
126
+ visitor: { Program(path, state) {
127
+ const filter = createFilterForId(state.opts);
128
+ if (state.filename && filter && !(filter === null || filter === void 0 ? void 0 : filter(state.filename))) return;
129
+ path.traverse(require_common.functionVisitor, state);
130
+ } }
131
+ };
132
+ }
133
+
134
+ //#endregion
135
+ Object.defineProperty(exports, 'TypeGPUPlugin', {
136
+ enumerable: true,
137
+ get: function () {
138
+ return TypeGPUPlugin;
139
+ }
140
+ });
package/babel.cjs CHANGED
@@ -1,161 +1,5 @@
1
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
- const require_filter = require('./filter-CKmD4HRk.cjs');
3
- let defu = require("defu");
4
- defu = require_filter.__toESM(defu);
5
- let tinyest_for_wgsl = require("tinyest-for-wgsl");
6
- let tinyest = require("tinyest");
7
- let _babel_standalone = require("@babel/standalone");
8
- _babel_standalone = require_filter.__toESM(_babel_standalone);
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ require('./common-BoeQmwpt.cjs');
3
+ const require_babel = require('./babel-Dis-Pwfu.cjs');
9
4
 
10
- //#region src/babel.ts
11
- const template = _babel_standalone.packages.template;
12
- const types = _babel_standalone.packages.types;
13
- function containsUseGpuDirective(node) {
14
- var _node$body$directives, _node$body;
15
- 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_filter.useGpuDirective);
16
- }
17
- function i(identifier) {
18
- return types.identifier(identifier);
19
- }
20
- const fnNodeToOriginalMap = /* @__PURE__ */ new WeakMap();
21
- function functionToTranspiled(node, parent) {
22
- var _fnNodeToOriginalMap$;
23
- const { params, body, externalNames } = (0, tinyest_for_wgsl.transpileFn)((_fnNodeToOriginalMap$ = fnNodeToOriginalMap.get(node)) !== null && _fnNodeToOriginalMap$ !== void 0 ? _fnNodeToOriginalMap$ : node);
24
- const maybeName = require_filter.getFunctionName(node, parent);
25
- const metadata = types.objectExpression([
26
- types.objectProperty(i("v"), types.numericLiteral(tinyest.FORMAT_VERSION)),
27
- types.objectProperty(i("name"), maybeName ? types.stringLiteral(maybeName) : types.buildUndefinedNode()),
28
- types.objectProperty(i("ast"), template.expression`${require_filter.embedJSON({
29
- params,
30
- body,
31
- externalNames
32
- })}`()),
33
- types.objectProperty(i("externals"), types.arrowFunctionExpression([], types.blockStatement([types.returnStatement(types.objectExpression(externalNames.map((name) => types.objectProperty(i(name), i(name), false, name !== "this"))))])))
34
- ]);
35
- 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([])]);
36
- }
37
- function wrapInAutoName(node, name) {
38
- return types.callExpression(template.expression("globalThis.__TYPEGPU_AUTONAME__ ?? (a => a)", { placeholderPattern: false })(), [node, types.stringLiteral(name)]);
39
- }
40
- function functionVisitor(ctx) {
41
- let inUseGpuScope = false;
42
- return {
43
- VariableDeclarator(path) {
44
- require_filter.performExpressionNaming(ctx, path.node, (node, name) => {
45
- path.get("init").replaceWith(wrapInAutoName(node, name));
46
- });
47
- },
48
- AssignmentExpression(path) {
49
- if (inUseGpuScope) {
50
- const runtimeFn = require_filter.operators[path.node.operator];
51
- if (runtimeFn) path.replaceWith(types.assignmentExpression("=", path.node.left, types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right])));
52
- }
53
- require_filter.performExpressionNaming(ctx, path.node, (node, name) => {
54
- path.get("right").replaceWith(wrapInAutoName(node, name));
55
- });
56
- },
57
- ObjectProperty(path) {
58
- require_filter.performExpressionNaming(ctx, path.node, (node, name) => {
59
- path.get("value").replaceWith(wrapInAutoName(node, name));
60
- });
61
- },
62
- ClassProperty(path) {
63
- require_filter.performExpressionNaming(ctx, path.node, (node, name) => {
64
- path.get("value").replaceWith(wrapInAutoName(node, name));
65
- });
66
- },
67
- ImportDeclaration(path) {
68
- require_filter.gatherTgpuAliases(path.node, ctx);
69
- },
70
- BinaryExpression: { exit(path) {
71
- if (!inUseGpuScope) return;
72
- const runtimeFn = require_filter.operators[path.node.operator];
73
- if (runtimeFn) path.replaceWith(types.callExpression(types.identifier(runtimeFn), [path.node.left, path.node.right]));
74
- } },
75
- ArrowFunctionExpression: {
76
- enter(path) {
77
- if (containsUseGpuDirective(path.node)) {
78
- fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
79
- if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
80
- inUseGpuScope = true;
81
- }
82
- },
83
- exit(path) {
84
- const node = path.node;
85
- if (containsUseGpuDirective(node)) {
86
- inUseGpuScope = false;
87
- const parent = path.parentPath.node;
88
- path.replaceWith(functionToTranspiled(node, parent));
89
- path.skip();
90
- }
91
- }
92
- },
93
- FunctionExpression: {
94
- enter(path) {
95
- if (containsUseGpuDirective(path.node)) {
96
- fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
97
- if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
98
- inUseGpuScope = true;
99
- }
100
- },
101
- exit(path) {
102
- const node = path.node;
103
- if (containsUseGpuDirective(node)) {
104
- inUseGpuScope = false;
105
- const parent = path.parentPath.node;
106
- path.replaceWith(functionToTranspiled(node, parent));
107
- path.skip();
108
- }
109
- }
110
- },
111
- FunctionDeclaration: {
112
- enter(path) {
113
- if (containsUseGpuDirective(path.node)) {
114
- fnNodeToOriginalMap.set(path.node, types.cloneNode(path.node, true));
115
- if (inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
116
- inUseGpuScope = true;
117
- }
118
- },
119
- exit(path) {
120
- var _fnNodeToOriginalMap$2;
121
- const node = (_fnNodeToOriginalMap$2 = fnNodeToOriginalMap.get(path.node)) !== null && _fnNodeToOriginalMap$2 !== void 0 ? _fnNodeToOriginalMap$2 : path.node;
122
- if (containsUseGpuDirective(node)) {
123
- inUseGpuScope = false;
124
- if (!node.id) return;
125
- const parent = path.parentPath.node;
126
- const expression = types.functionExpression(node.id, node.params, node.body);
127
- path.replaceWith(types.variableDeclaration("const", [types.variableDeclarator(node.id, functionToTranspiled(expression, parent))]));
128
- path.skip();
129
- }
130
- }
131
- },
132
- CallExpression: { exit(path) {
133
- const node = path.node;
134
- if (require_filter.isShellImplementationCall(node, ctx)) {
135
- const implementation = node.arguments[0];
136
- if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
137
- const transpiled = functionToTranspiled(implementation, null);
138
- path.replaceWith(types.callExpression(node.callee, [transpiled]));
139
- path.skip();
140
- }
141
- }
142
- } }
143
- };
144
- }
145
- function babel_default() {
146
- return { visitor: { Program(path, state) {
147
- const options = (0, defu.default)(state.opts, require_filter.defaultOptions);
148
- const id = state.filename;
149
- const filter = require_filter.createFilterForId(options);
150
- if (id && filter && !(filter === null || filter === void 0 ? void 0 : filter(id))) return;
151
- const ctx = {
152
- tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
153
- fileId: id,
154
- autoNamingEnabled: options.autoNamingEnabled
155
- };
156
- path.traverse(functionVisitor(ctx));
157
- } } };
158
- }
159
-
160
- //#endregion
161
- exports.default = babel_default;
5
+ exports.default = require_babel.TypeGPUPlugin;
package/babel.d.cts CHANGED
@@ -1,10 +1,3 @@
1
- import { n as Program, t as Node } from "./index-legacy-xKi5C2ga.cjs";
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
- export = export_default;
1
+ import "./common-DZ2cLtbe.cjs";
2
+ import { t as TypeGPUPlugin } from "./babel-C8UzthFd.cjs";
3
+ export = TypeGPUPlugin;
package/babel.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import "./common-BlvLUwEz.js";
2
+ import { t as TypeGPUPlugin } from "./babel-CmCnq-43.js";
3
+ export { TypeGPUPlugin as default };
package/babel.js ADDED
@@ -0,0 +1,4 @@
1
+ import "./common-CElmMmuq.js";
2
+ import { t as TypeGPUPlugin } from "./babel-CbManzjz.js";
3
+
4
+ export { TypeGPUPlugin as default };
@@ -0,0 +1,134 @@
1
+ import { a as initPluginState, i as getBlockScope, n as earlyPruneRegex, o as t, r as functionVisitor, t as defaultOptions } from "./common-CElmMmuq.js";
2
+ import defu from "defu";
3
+ import { transpileFn } from "tinyest-for-wgsl";
4
+ import { FORMAT_VERSION } from "tinyest";
5
+ import { MagicStringAST, generateTransform } from "magic-string-ast";
6
+ import { getBabelParserOptions, getLang } from "ast-kit";
7
+ import _traverse from "@babel/traverse";
8
+ import * as parser from "@babel/parser";
9
+
10
+ //#region src/core/factory.ts
11
+ let traverse = _traverse;
12
+ if (typeof traverse.default === "function") traverse = traverse.default;
13
+ const fnWrapperTemplate = (fnCode, metadata) => `(/*#__PURE__*/($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (${fnCode}), ${metadata}) && $.f)({}))`;
14
+ function embedJSON(jsValue) {
15
+ return JSON.stringify(jsValue).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
16
+ }
17
+ function assignMetadata(path, name, ast) {
18
+ var _path$node$start, _path$node$start2;
19
+ const metadata = `{
20
+ v: ${FORMAT_VERSION},
21
+ name: ${name ? `"${name}"` : "undefined"},
22
+ ast: ${embedJSON(ast)},
23
+ externals: () => ({${ast.externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
24
+ }`;
25
+ const visibility = t.isFunctionDeclaration(path.node) ? getBlockScope(path) : void 0;
26
+ const fnCode = this.magicString.sliceNode(path.node);
27
+ let nodeToOverride = path.node;
28
+ let code = fnWrapperTemplate(fnCode, metadata);
29
+ let insertPos = (_path$node$start = path.node.start) !== null && _path$node$start !== void 0 ? _path$node$start : 0;
30
+ if (t.isFunctionDeclaration(path.node) && path.node.id) code = `const ${path.node.id.name} = ${code};\n\n`;
31
+ if (visibility) {
32
+ var _visibility$node$body, _visibility$node$body2;
33
+ insertPos = (_visibility$node$body = (_visibility$node$body2 = visibility.node.body[0]) === null || _visibility$node$body2 === void 0 ? void 0 : _visibility$node$body2.start) !== null && _visibility$node$body !== void 0 ? _visibility$node$body : insertPos;
34
+ if (t.isExportNamedDeclaration(path.parent)) {
35
+ nodeToOverride = path.parent;
36
+ code = `export ${code}`;
37
+ }
38
+ }
39
+ if (insertPos < ((_path$node$start2 = path.node.start) !== null && _path$node$start2 !== void 0 ? _path$node$start2 : 0)) {
40
+ var _nodeToOverride$leadi, _nodeToOverride$leadi2;
41
+ for (const comment of (_nodeToOverride$leadi = (_nodeToOverride$leadi2 = nodeToOverride.leadingComments) === null || _nodeToOverride$leadi2 === void 0 ? void 0 : _nodeToOverride$leadi2.toReversed()) !== null && _nodeToOverride$leadi !== void 0 ? _nodeToOverride$leadi : []) {
42
+ var _comment$start, _comment$end;
43
+ code = `${this.magicString.slice((_comment$start = comment.start) !== null && _comment$start !== void 0 ? _comment$start : 0, (_comment$end = comment.end) !== null && _comment$end !== void 0 ? _comment$end : 0)}\n${code}`;
44
+ this.magicString.removeNode(comment);
45
+ }
46
+ this.magicString.removeNode(nodeToOverride);
47
+ this.magicString.prependLeft(insertPos, code);
48
+ } else this.magicString.overwriteNode(nodeToOverride, code);
49
+ }
50
+ function wrapInAutoName(path, name) {
51
+ var _path$node$start3, _path$node$end;
52
+ this.magicString.appendLeft((_path$node$start3 = path.node.start) !== null && _path$node$start3 !== void 0 ? _path$node$start3 : 0, "(/*#__PURE__*/(globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").prependRight((_path$node$end = path.node.end) !== null && _path$node$end !== void 0 ? _path$node$end : 0, `, "${name}"))`);
53
+ }
54
+ function replaceWithAssignmentOverload(path, runtimeFn) {
55
+ const lhs = this.magicString.sliceNode(path.node.left);
56
+ const rhs = this.magicString.sliceNode(path.node.right);
57
+ this.magicString.overwriteNode(path.node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
58
+ }
59
+ function replaceWithBinaryOverload(path, runtimeFn) {
60
+ const lhs = this.magicString.sliceNode(path.node.left);
61
+ const rhs = this.magicString.sliceNode(path.node.right);
62
+ this.magicString.overwriteNode(path.node, `${runtimeFn}(${lhs}, ${rhs})`);
63
+ }
64
+ const unpluginFactory = ((rawOptions, _meta) => {
65
+ const options = defu(rawOptions, defaultOptions);
66
+ return {
67
+ name: "unplugin-typegpu",
68
+ enforce: options.enforce,
69
+ transform: {
70
+ filter: options.earlyPruning ? {
71
+ id: options,
72
+ code: earlyPruneRegex
73
+ } : { id: options },
74
+ handler(code, id) {
75
+ let ast;
76
+ try {
77
+ ast = parser.parse(code, getBabelParserOptions(getLang(id), {
78
+ sourceType: "module",
79
+ allowReturnOutsideFunction: true
80
+ }));
81
+ } catch (cause) {
82
+ console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
83
+ return;
84
+ }
85
+ const magicString = new MagicStringAST(code);
86
+ const state = {
87
+ filename: id,
88
+ magicString,
89
+ opts: options
90
+ };
91
+ initPluginState(state, {
92
+ warn: (message) => this.warn(message),
93
+ assignMetadata,
94
+ wrapInAutoName,
95
+ replaceWithAssignmentOverload,
96
+ replaceWithBinaryOverload
97
+ });
98
+ traverse(ast, functionVisitor, void 0, state);
99
+ return generateTransform(magicString, id);
100
+ }
101
+ }
102
+ };
103
+ });
104
+
105
+ //#endregion
106
+ //#region src/bun.ts
107
+ var bun_default = (rawOptions) => {
108
+ const options = defu(rawOptions, defaultOptions);
109
+ const include = options.include;
110
+ if (!(include instanceof RegExp)) throw new Error(`Unsupported 'include' options in Bun plugin. Please provide a single regular expression`);
111
+ if (options.exclude) throw new Error(`Unsupported 'exclude' option in Bun plugin`);
112
+ const rawPlugin = unpluginFactory(rawOptions, { framework: "bun" });
113
+ return {
114
+ name: "unplugin-typegpu",
115
+ setup(build) {
116
+ build.onLoad({ filter: include }, async (args) => {
117
+ var _result$code;
118
+ const codeIn = await Bun.file(args.path).text();
119
+ if (earlyPruneRegex.every((pattern) => !pattern.test(codeIn))) return {
120
+ contents: codeIn,
121
+ loader: args.loader
122
+ };
123
+ const result = rawPlugin.transform.handler.apply({}, [codeIn, args.path]);
124
+ return {
125
+ contents: (_result$code = result === null || result === void 0 ? void 0 : result.code) !== null && _result$code !== void 0 ? _result$code : codeIn,
126
+ loader: args.loader
127
+ };
128
+ });
129
+ }
130
+ };
131
+ };
132
+
133
+ //#endregion
134
+ export { unpluginFactory as n, bun_default as t };