unplugin-typegpu 0.10.1 → 0.11.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.
Files changed (72) hide show
  1. package/README.md +14 -4
  2. package/babel-Bnfeonoo.mjs +126 -0
  3. package/babel-C0RyQRMk.d.mts +14 -0
  4. package/babel-C1TaJd38.d.cts +14 -0
  5. package/babel-izRcz0Uk.cjs +134 -0
  6. package/babel.cjs +6 -163
  7. package/babel.d.cts +2 -10
  8. package/babel.d.mts +2 -11
  9. package/babel.mjs +2 -159
  10. package/bun-9JrHWE4u.d.mts +6 -0
  11. package/bun-CUGitP44.cjs +147 -0
  12. package/bun-ChNeklBe.d.cts +6 -0
  13. package/bun-DFr_PiLh.mjs +132 -0
  14. package/bun.cjs +6 -40
  15. package/bun.d.cts +1 -4
  16. package/bun.d.mts +1 -5
  17. package/bun.mjs +2 -36
  18. package/common-BKqBivuE.d.cts +84 -0
  19. package/common-BvAFT5IV.d.mts +85 -0
  20. package/common-Ccx_dEqH.cjs +329 -0
  21. package/common-e08X6w0a.mjs +272 -0
  22. package/esbuild.cjs +6 -6
  23. package/esbuild.d.cts +1 -1
  24. package/esbuild.d.mts +1 -1
  25. package/esbuild.mjs +2 -4
  26. package/farm.cjs +6 -6
  27. package/farm.d.cts +1 -1
  28. package/farm.d.mts +1 -1
  29. package/farm.mjs +2 -4
  30. package/index-BuKVXaph.d.mts +48 -0
  31. package/index-HrLja1mV.d.cts +48 -0
  32. package/index.cjs +19 -30
  33. package/index.d.cts +5 -18
  34. package/index.d.mts +5 -19
  35. package/index.mjs +4 -18
  36. package/package.json +13 -10
  37. package/rolldown.cjs +6 -6
  38. package/rolldown.d.cts +1 -1
  39. package/rolldown.d.mts +1 -1
  40. package/rolldown.mjs +2 -4
  41. package/rollup.cjs +6 -6
  42. package/rollup.d.cts +1 -1
  43. package/rollup.d.mts +1 -1
  44. package/rollup.mjs +2 -4
  45. package/rspack.cjs +6 -6
  46. package/rspack.d.cts +1 -1
  47. package/rspack.d.mts +1 -1
  48. package/rspack.mjs +2 -4
  49. package/src-2vvPp2dY.cjs +77 -0
  50. package/src-BV9Uh6WO.mjs +17 -0
  51. package/vite.cjs +6 -6
  52. package/vite.d.cts +1 -1
  53. package/vite.d.mts +1 -1
  54. package/vite.mjs +2 -4
  55. package/webpack.cjs +6 -6
  56. package/webpack.d.cts +1 -1
  57. package/webpack.d.mts +1 -1
  58. package/webpack.mjs +2 -4
  59. package/_virtual/_rolldown/runtime.cjs +0 -29
  60. package/common.cjs +0 -183
  61. package/common.d.cts +0 -25
  62. package/common.d.mts +0 -25
  63. package/common.mjs +0 -174
  64. package/filter.cjs +0 -63
  65. package/filter.mjs +0 -61
  66. package/rolldown-browser.cjs +0 -7
  67. package/rolldown-browser.d.mts +0 -1
  68. package/rolldown-browser.mjs +0 -7
  69. package/rollup-impl.cjs +0 -127
  70. package/rollup-impl.d.cts +0 -38
  71. package/rollup-impl.d.mts +0 -39
  72. package/rollup-impl.mjs +0 -125
package/babel.mjs CHANGED
@@ -1,159 +1,2 @@
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 };
1
+ import { t as TypeGPUPlugin } from "./babel-Bnfeonoo.mjs";
2
+ export { TypeGPUPlugin as default };
@@ -0,0 +1,6 @@
1
+ import { t as Options } from "./common-BvAFT5IV.mjs";
2
+
3
+ //#region src/bun.d.ts
4
+ declare const _default: (rawOptions?: Options) => Bun.BunPlugin;
5
+ //#endregion
6
+ export { _default as t };
@@ -0,0 +1,147 @@
1
+ const require_common = require("./common-Ccx_dEqH.cjs");
2
+ let defu = require("defu");
3
+ defu = require_common.__toESM(defu);
4
+ let magic_string_ast = require("magic-string-ast");
5
+ let ast_kit = require("ast-kit");
6
+ let _babel_traverse = require("@babel/traverse");
7
+ _babel_traverse = require_common.__toESM(_babel_traverse);
8
+ let tinyest = require("tinyest");
9
+ require("tinyest-for-wgsl");
10
+ let _babel_parser = require("@babel/parser");
11
+ _babel_parser = require_common.__toESM(_babel_parser);
12
+ let _babel_types = require("@babel/types");
13
+ _babel_types = require_common.__toESM(_babel_types);
14
+ //#region src/core/factory.ts
15
+ let traverse = _babel_traverse.default;
16
+ if (typeof traverse.default === "function") traverse = traverse.default;
17
+ const fnWrapperTemplate = (fnCode, metadata) => `(/*#__PURE__*/($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (${fnCode}), ${metadata}) && $.f)({}))`;
18
+ function embedJSON(jsValue) {
19
+ return JSON.stringify(jsValue).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
20
+ }
21
+ function assignMetadata(path, name, ast) {
22
+ var _path$node$start, _path$node$start2;
23
+ const metadata = `{
24
+ v: ${tinyest.FORMAT_VERSION},
25
+ name: ${name ? `"${name}"` : "undefined"},
26
+ ast: ${embedJSON(ast)},
27
+ externals: () => ({${ast.externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
28
+ }`;
29
+ const visibility = _babel_types.isFunctionDeclaration(path.node) ? require_common.getBlockScope(path) : void 0;
30
+ const fnCode = this.magicString.sliceNode(path.node);
31
+ let nodeToOverride = path.node;
32
+ let code = fnWrapperTemplate(fnCode, metadata);
33
+ let insertPos = (_path$node$start = path.node.start) !== null && _path$node$start !== void 0 ? _path$node$start : 0;
34
+ if (_babel_types.isFunctionDeclaration(path.node) && path.node.id) code = `const ${path.node.id.name} = ${code};\n\n`;
35
+ if (visibility) {
36
+ var _visibility$node$body, _visibility$node$body2;
37
+ 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;
38
+ if (_babel_types.isExportNamedDeclaration(path.parent)) {
39
+ nodeToOverride = path.parent;
40
+ code = `export ${code}`;
41
+ }
42
+ }
43
+ if (insertPos < ((_path$node$start2 = path.node.start) !== null && _path$node$start2 !== void 0 ? _path$node$start2 : 0)) {
44
+ var _nodeToOverride$leadi, _nodeToOverride$leadi2;
45
+ 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 : []) {
46
+ var _comment$start, _comment$end;
47
+ 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}`;
48
+ this.magicString.removeNode(comment);
49
+ }
50
+ this.magicString.removeNode(nodeToOverride);
51
+ this.magicString.prependLeft(insertPos, code);
52
+ } else this.magicString.overwriteNode(nodeToOverride, code);
53
+ }
54
+ function wrapInAutoName(path, name) {
55
+ var _path$node$start3, _path$node$end;
56
+ 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}"))`);
57
+ }
58
+ function replaceWithAssignmentOverload(path, runtimeFn) {
59
+ const lhs = this.magicString.sliceNode(path.node.left);
60
+ const rhs = this.magicString.sliceNode(path.node.right);
61
+ this.magicString.overwriteNode(path.node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
62
+ }
63
+ function replaceWithBinaryOverload(path, runtimeFn) {
64
+ const lhs = this.magicString.sliceNode(path.node.left);
65
+ const rhs = this.magicString.sliceNode(path.node.right);
66
+ this.magicString.overwriteNode(path.node, `${runtimeFn}(${lhs}, ${rhs})`);
67
+ }
68
+ const unpluginFactory = ((rawOptions, _meta) => {
69
+ const options = (0, defu.default)(rawOptions, require_common.defaultOptions);
70
+ return {
71
+ name: "unplugin-typegpu",
72
+ enforce: options.enforce,
73
+ transform: {
74
+ filter: options.earlyPruning ? {
75
+ id: options,
76
+ code: require_common.earlyPruneRegex
77
+ } : { id: options },
78
+ handler(code, id) {
79
+ let ast;
80
+ try {
81
+ ast = _babel_parser.parse(code, (0, ast_kit.getBabelParserOptions)((0, ast_kit.getLang)(id), {
82
+ sourceType: "module",
83
+ allowReturnOutsideFunction: true
84
+ }));
85
+ } catch (cause) {
86
+ console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
87
+ return;
88
+ }
89
+ const magicString = new magic_string_ast.MagicStringAST(code);
90
+ const state = {
91
+ filename: id,
92
+ magicString,
93
+ opts: options
94
+ };
95
+ require_common.initPluginState(state, {
96
+ warn: (message) => this.warn(message),
97
+ assignMetadata,
98
+ wrapInAutoName,
99
+ replaceWithAssignmentOverload,
100
+ replaceWithBinaryOverload
101
+ });
102
+ traverse(ast, require_common.functionVisitor, void 0, state);
103
+ return (0, magic_string_ast.generateTransform)(magicString, id);
104
+ }
105
+ }
106
+ };
107
+ });
108
+ //#endregion
109
+ //#region src/bun.ts
110
+ var bun_default = (rawOptions) => {
111
+ const options = (0, defu.default)(rawOptions, require_common.defaultOptions);
112
+ const include = options.include;
113
+ if (!(include instanceof RegExp)) throw new Error(`Unsupported 'include' options in Bun plugin. Please provide a single regular expression`);
114
+ if (options.exclude) throw new Error(`Unsupported 'exclude' option in Bun plugin`);
115
+ const rawPlugin = unpluginFactory(rawOptions, { framework: "bun" });
116
+ return {
117
+ name: "unplugin-typegpu",
118
+ setup(build) {
119
+ build.onLoad({ filter: include }, async (args) => {
120
+ var _result$code;
121
+ const codeIn = await Bun.file(args.path).text();
122
+ if (require_common.earlyPruneRegex.every((pattern) => !pattern.test(codeIn))) return {
123
+ contents: codeIn,
124
+ loader: args.loader
125
+ };
126
+ const result = rawPlugin.transform.handler.apply({}, [codeIn, args.path]);
127
+ return {
128
+ contents: (_result$code = result === null || result === void 0 ? void 0 : result.code) !== null && _result$code !== void 0 ? _result$code : codeIn,
129
+ loader: args.loader
130
+ };
131
+ });
132
+ }
133
+ };
134
+ };
135
+ //#endregion
136
+ Object.defineProperty(exports, "bun_default", {
137
+ enumerable: true,
138
+ get: function() {
139
+ return bun_default;
140
+ }
141
+ });
142
+ Object.defineProperty(exports, "unpluginFactory", {
143
+ enumerable: true,
144
+ get: function() {
145
+ return unpluginFactory;
146
+ }
147
+ });
@@ -0,0 +1,6 @@
1
+ import { t as Options } from "./common-BKqBivuE.cjs";
2
+
3
+ //#region src/bun.d.ts
4
+ declare const _default: (rawOptions?: Options) => Bun.BunPlugin;
5
+ //#endregion
6
+ export { _default as t };
@@ -0,0 +1,132 @@
1
+ import { a as initPluginState, i as getBlockScope, n as earlyPruneRegex, r as functionVisitor, t as defaultOptions } from "./common-e08X6w0a.mjs";
2
+ import defu from "defu";
3
+ import { MagicStringAST, generateTransform } from "magic-string-ast";
4
+ import { getBabelParserOptions, getLang } from "ast-kit";
5
+ import _traverse from "@babel/traverse";
6
+ import { FORMAT_VERSION } from "tinyest";
7
+ import "tinyest-for-wgsl";
8
+ import * as parser from "@babel/parser";
9
+ import * as t from "@babel/types";
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
+ //#endregion
105
+ //#region src/bun.ts
106
+ var bun_default = (rawOptions) => {
107
+ const options = defu(rawOptions, defaultOptions);
108
+ const include = options.include;
109
+ if (!(include instanceof RegExp)) throw new Error(`Unsupported 'include' options in Bun plugin. Please provide a single regular expression`);
110
+ if (options.exclude) throw new Error(`Unsupported 'exclude' option in Bun plugin`);
111
+ const rawPlugin = unpluginFactory(rawOptions, { framework: "bun" });
112
+ return {
113
+ name: "unplugin-typegpu",
114
+ setup(build) {
115
+ build.onLoad({ filter: include }, async (args) => {
116
+ var _result$code;
117
+ const codeIn = await Bun.file(args.path).text();
118
+ if (earlyPruneRegex.every((pattern) => !pattern.test(codeIn))) return {
119
+ contents: codeIn,
120
+ loader: args.loader
121
+ };
122
+ const result = rawPlugin.transform.handler.apply({}, [codeIn, args.path]);
123
+ return {
124
+ contents: (_result$code = result === null || result === void 0 ? void 0 : result.code) !== null && _result$code !== void 0 ? _result$code : codeIn,
125
+ loader: args.loader
126
+ };
127
+ });
128
+ }
129
+ };
130
+ };
131
+ //#endregion
132
+ export { unpluginFactory as n, bun_default as t };
package/bun.cjs CHANGED
@@ -1,40 +1,6 @@
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;
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ const require_bun = require("./bun-CUGitP44.cjs");
6
+ exports.default = require_bun.bun_default;
package/bun.d.cts CHANGED
@@ -1,5 +1,2 @@
1
- import { Options } from "./common.cjs";
2
-
3
- //#region src/bun.d.ts
4
- declare const _default: (rawOptions: Options) => BunPlugin;
1
+ import { t as _default } from "./bun-ChNeklBe.cjs";
5
2
  export = _default;
package/bun.d.mts CHANGED
@@ -1,6 +1,2 @@
1
- import { Options } from "./common.mjs";
2
-
3
- //#region src/bun.d.ts
4
- declare const _default: (rawOptions: Options) => BunPlugin;
5
- //#endregion
1
+ import { t as _default } from "./bun-9JrHWE4u.mjs";
6
2
  export { _default as default };
package/bun.mjs CHANGED
@@ -1,36 +1,2 @@
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 };
1
+ import { t as bun_default } from "./bun-DFr_PiLh.mjs";
2
+ export { bun_default as default };
@@ -0,0 +1,84 @@
1
+ import * as t from "@babel/types";
2
+ import { NodePath } from "@babel/traverse";
3
+ import { FilterPattern } from "unplugin";
4
+ import { transpileFn } from "tinyest-for-wgsl";
5
+
6
+ //#region src/core/common.d.ts
7
+ interface Options {
8
+ /** @default [/\.m?[jt]sx?$/] */
9
+ include?: FilterPattern;
10
+ /** @default undefined */
11
+ exclude?: FilterPattern;
12
+ /** @default undefined */
13
+ enforce?: 'post' | 'pre' | undefined;
14
+ /** @default undefined */
15
+ forceTgpuAlias?: string | undefined;
16
+ /** @default true */
17
+ autoNamingEnabled?: boolean | undefined;
18
+ /**
19
+ * Skipping files that don't contain "typegpu", "tgpu" or "use gpu".
20
+ * In case this early pruning hinders transformation, you
21
+ * can disable it.
22
+ *
23
+ * @default true
24
+ */
25
+ earlyPruning?: boolean | undefined;
26
+ }
27
+ type MetadatableFunction = t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression;
28
+ interface TransformMethods {
29
+ warn(message: string): void;
30
+ /**
31
+ * For function statements to have metadata assigned, they first have to be manually hoisted
32
+ * to the top of their scope, then replaced with a variable declaration.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const value = mod(13, 10);
37
+ * function mod(a: number, b: number): number {
38
+ * 'use gpu';
39
+ * return a % b;
40
+ * }
41
+ * ```
42
+ *
43
+ * Should be transformed to:
44
+ *
45
+ * ```ts
46
+ * const mod = (a: number, b: number): number => {
47
+ * 'use gpu';
48
+ * return a % b;
49
+ * };
50
+ * const value = mod(13, 10);
51
+ * ```
52
+ *
53
+ * Note that named function expressions aren't hoisted, so they don't pose a problem.
54
+ * ```ts
55
+ * double(2); // Uncaught ReferenceError: double is not defined
56
+ * console.log(function double(a) { return a * 2; });
57
+ * ```
58
+ */
59
+ assignMetadata(this: PluginState, path: NodePath<MetadatableFunction>, name: string | undefined, ast: ReturnType<typeof transpileFn>): void;
60
+ wrapInAutoName(this: PluginState, path: NodePath<t.Expression>, name: string): void;
61
+ replaceWithAssignmentOverload(path: NodePath<t.AssignmentExpression>, runtimeFn: string): void;
62
+ replaceWithBinaryOverload(path: NodePath<t.BinaryExpression>, runtimeFn: string): void;
63
+ }
64
+ interface PluginState extends TransformMethods {
65
+ /**
66
+ * How the `tgpu` object is used in code. Since it can be aliased, we
67
+ * need to catch that and act accordingly.
68
+ */
69
+ tgpuAliases: Set<string>;
70
+ autoNamingEnabled: boolean;
71
+ /**
72
+ * Populated by Babel
73
+ */
74
+ filename?: string | undefined;
75
+ /**
76
+ * In Babel, options are assigned to the property `opts` on the plugin state.
77
+ * We use this pattern everywhere for consistency.
78
+ */
79
+ opts: Options;
80
+ inUseGpuScope: boolean;
81
+ alreadyTransformed: WeakSet<t.Node>;
82
+ }
83
+ //#endregion
84
+ export { PluginState as n, Options as t };