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.
- package/_virtual/_rolldown/runtime.cjs +29 -0
- package/babel.cjs +163 -2
- package/babel.d.cts +7 -7
- package/babel.d.mts +11 -0
- package/babel.mjs +159 -0
- package/bun.cjs +40 -2
- package/bun.d.cts +5 -3
- package/bun.d.mts +6 -0
- package/bun.mjs +36 -0
- package/common.cjs +183 -0
- package/common.d.cts +25 -0
- package/common.d.mts +25 -0
- package/common.mjs +174 -0
- package/esbuild.cjs +8 -2
- package/esbuild.d.cts +2 -14
- package/esbuild.d.mts +2 -0
- package/esbuild.mjs +7 -0
- package/farm.cjs +8 -2
- package/farm.d.cts +2 -14
- package/farm.d.mts +2 -0
- package/farm.mjs +7 -0
- package/filter.cjs +63 -0
- package/filter.mjs +61 -0
- package/index.cjs +30 -2
- package/index.d.cts +12 -48
- package/index.d.mts +19 -0
- package/index.mjs +18 -0
- package/package.json +110 -66
- package/rolldown-browser.cjs +7 -0
- package/rolldown-browser.d.mts +1 -0
- package/rolldown-browser.mjs +7 -0
- package/rolldown.cjs +8 -2
- package/rolldown.d.cts +2 -14
- package/rolldown.d.mts +2 -0
- package/rolldown.mjs +7 -0
- package/rollup-impl.cjs +127 -0
- package/rollup-impl.d.cts +38 -0
- package/rollup-impl.d.mts +39 -0
- package/rollup-impl.mjs +125 -0
- package/rollup.cjs +8 -2
- package/rollup.d.cts +2 -14
- package/rollup.d.mts +2 -0
- package/rollup.mjs +7 -0
- package/rspack.cjs +8 -2
- package/rspack.d.cts +2 -14
- package/rspack.d.mts +2 -0
- package/rspack.mjs +7 -0
- package/vite.cjs +8 -2
- package/vite.d.cts +2 -14
- package/vite.d.mts +2 -0
- package/vite.mjs +7 -0
- package/webpack.cjs +8 -2
- package/webpack.d.cts +2 -14
- package/webpack.d.mts +2 -0
- package/webpack.mjs +7 -0
- package/babel.cjs.map +0 -1
- package/babel.d.ts +0 -10
- package/babel.js +0 -2
- package/babel.js.map +0 -1
- package/bun-Dq3TUCfa.d.cts +0 -27
- package/bun-Dq3TUCfa.d.ts +0 -27
- package/bun.cjs.map +0 -1
- package/bun.d.ts +0 -3
- package/bun.js +0 -2
- package/bun.js.map +0 -1
- package/chunk-DEVVEJF4.cjs +0 -2
- package/chunk-DEVVEJF4.cjs.map +0 -1
- package/chunk-EESBB3Y4.js +0 -2
- package/chunk-EESBB3Y4.js.map +0 -1
- package/chunk-LJXNN2RM.cjs +0 -7
- package/chunk-LJXNN2RM.cjs.map +0 -1
- package/chunk-NQODVO3L.cjs +0 -2
- package/chunk-NQODVO3L.cjs.map +0 -1
- package/chunk-RIZD7OMC.js +0 -7
- package/chunk-RIZD7OMC.js.map +0 -1
- package/chunk-V4Q4PLLO.js +0 -2
- package/chunk-V4Q4PLLO.js.map +0 -1
- package/esbuild.cjs.map +0 -1
- package/esbuild.d.ts +0 -14
- package/esbuild.js +0 -2
- package/esbuild.js.map +0 -1
- package/farm.cjs.map +0 -1
- package/farm.d.ts +0 -14
- package/farm.js +0 -2
- package/farm.js.map +0 -1
- package/index.cjs.map +0 -1
- package/index.d.ts +0 -54
- package/index.js +0 -2
- package/index.js.map +0 -1
- package/rolldown.cjs.map +0 -1
- package/rolldown.d.ts +0 -14
- package/rolldown.js +0 -2
- package/rolldown.js.map +0 -1
- package/rollup.cjs.map +0 -1
- package/rollup.d.ts +0 -14
- package/rollup.js +0 -2
- package/rollup.js.map +0 -1
- package/rspack.cjs.map +0 -1
- package/rspack.d.ts +0 -14
- package/rspack.js +0 -2
- package/rspack.js.map +0 -1
- package/vite.cjs.map +0 -1
- package/vite.d.ts +0 -14
- package/vite.js +0 -2
- package/vite.js.map +0 -1
- package/webpack.cjs.map +0 -1
- package/webpack.d.ts +0 -14
- package/webpack.js +0 -2
- package/webpack.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,116 +1,160 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-typegpu",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Build plugins for TypeGPU, enabling seamless JavaScript -> WGSL transpilation and improved debugging.",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"babel-plugin",
|
|
7
|
+
"gpgpu",
|
|
6
8
|
"rollup-plugin",
|
|
9
|
+
"shaders",
|
|
10
|
+
"typegpu",
|
|
11
|
+
"typescript",
|
|
7
12
|
"unplugin",
|
|
8
|
-
"babel-plugin",
|
|
9
13
|
"vite-plugin",
|
|
10
|
-
"typegpu",
|
|
11
|
-
"gpgpu",
|
|
12
14
|
"webgpu",
|
|
13
15
|
"wgpu",
|
|
14
|
-
"wgsl"
|
|
15
|
-
"typescript",
|
|
16
|
-
"shaders"
|
|
16
|
+
"wgsl"
|
|
17
17
|
],
|
|
18
|
+
"homepage": "https://typegpu.com",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/software-mansion/TypeGPU/issues"
|
|
21
|
+
},
|
|
18
22
|
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/software-mansion/TypeGPU.git"
|
|
26
|
+
},
|
|
19
27
|
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
20
29
|
"exports": {
|
|
21
30
|
"./package.json": "./package.json",
|
|
22
31
|
".": {
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./index.d.mts",
|
|
34
|
+
"default": "./index.mjs"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./index.d.cts",
|
|
38
|
+
"default": "./index.cjs"
|
|
39
|
+
}
|
|
27
40
|
},
|
|
28
41
|
"./rollup": {
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./rollup.d.mts",
|
|
44
|
+
"default": "./rollup.mjs"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./rollup.d.cts",
|
|
48
|
+
"default": "./rollup.cjs"
|
|
49
|
+
}
|
|
33
50
|
},
|
|
34
51
|
"./babel": {
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
"import": {
|
|
53
|
+
"types": "./babel.d.mts",
|
|
54
|
+
"default": "./babel.mjs"
|
|
55
|
+
},
|
|
56
|
+
"require": {
|
|
57
|
+
"types": "./babel.d.cts",
|
|
58
|
+
"default": "./babel.cjs"
|
|
59
|
+
}
|
|
39
60
|
},
|
|
40
61
|
"./bun": {
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./bun.d.mts",
|
|
64
|
+
"default": "./bun.mjs"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./bun.d.cts",
|
|
68
|
+
"default": "./bun.cjs"
|
|
69
|
+
}
|
|
45
70
|
},
|
|
46
71
|
"./esbuild": {
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
72
|
+
"import": {
|
|
73
|
+
"types": "./esbuild.d.mts",
|
|
74
|
+
"default": "./esbuild.mjs"
|
|
75
|
+
},
|
|
76
|
+
"require": {
|
|
77
|
+
"types": "./esbuild.d.cts",
|
|
78
|
+
"default": "./esbuild.cjs"
|
|
79
|
+
}
|
|
51
80
|
},
|
|
52
81
|
"./farm": {
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
82
|
+
"import": {
|
|
83
|
+
"types": "./farm.d.mts",
|
|
84
|
+
"default": "./farm.mjs"
|
|
85
|
+
},
|
|
86
|
+
"require": {
|
|
87
|
+
"types": "./farm.d.cts",
|
|
88
|
+
"default": "./farm.cjs"
|
|
89
|
+
}
|
|
57
90
|
},
|
|
58
91
|
"./rolldown": {
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
92
|
+
"import": {
|
|
93
|
+
"types": "./rolldown.d.mts",
|
|
94
|
+
"default": "./rolldown.mjs"
|
|
95
|
+
},
|
|
96
|
+
"require": {
|
|
97
|
+
"types": "./rolldown.d.cts",
|
|
98
|
+
"default": "./rolldown.cjs"
|
|
99
|
+
}
|
|
63
100
|
},
|
|
64
101
|
"./rolldown-browser": {
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
102
|
+
"import": {
|
|
103
|
+
"types": "./rolldown-browser.d.mts",
|
|
104
|
+
"default": "./rolldown-browser.mjs"
|
|
105
|
+
},
|
|
106
|
+
"require": {
|
|
107
|
+
"types": "./rolldown-browser.d.cts",
|
|
108
|
+
"default": "./rolldown-browser.cjs"
|
|
109
|
+
}
|
|
69
110
|
},
|
|
70
111
|
"./rspack": {
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
112
|
+
"import": {
|
|
113
|
+
"types": "./rspack.d.mts",
|
|
114
|
+
"default": "./rspack.mjs"
|
|
115
|
+
},
|
|
116
|
+
"require": {
|
|
117
|
+
"types": "./rspack.d.cts",
|
|
118
|
+
"default": "./rspack.cjs"
|
|
119
|
+
}
|
|
75
120
|
},
|
|
76
121
|
"./vite": {
|
|
77
|
-
"
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
122
|
+
"import": {
|
|
123
|
+
"types": "./vite.d.mts",
|
|
124
|
+
"default": "./vite.mjs"
|
|
125
|
+
},
|
|
126
|
+
"require": {
|
|
127
|
+
"types": "./vite.d.cts",
|
|
128
|
+
"default": "./vite.cjs"
|
|
129
|
+
}
|
|
81
130
|
},
|
|
82
131
|
"./webpack": {
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
132
|
+
"import": {
|
|
133
|
+
"types": "./webpack.d.mts",
|
|
134
|
+
"default": "./webpack.mjs"
|
|
135
|
+
},
|
|
136
|
+
"require": {
|
|
137
|
+
"types": "./webpack.d.cts",
|
|
138
|
+
"default": "./webpack.cjs"
|
|
139
|
+
}
|
|
87
140
|
}
|
|
88
141
|
},
|
|
89
|
-
"sideEffects": false,
|
|
90
|
-
"repository": {
|
|
91
|
-
"type": "git",
|
|
92
|
-
"url": "git+https://github.com/software-mansion/TypeGPU.git"
|
|
93
|
-
},
|
|
94
|
-
"bugs": {
|
|
95
|
-
"url": "https://github.com/software-mansion/TypeGPU/issues"
|
|
96
|
-
},
|
|
97
|
-
"homepage": "https://typegpu.com",
|
|
98
142
|
"dependencies": {
|
|
99
|
-
"@babel/standalone": "^7.
|
|
143
|
+
"@babel/standalone": "^7.28.6",
|
|
100
144
|
"defu": "^6.1.4",
|
|
101
145
|
"estree-walker": "^3.0.3",
|
|
102
146
|
"magic-string-ast": "^1.0.0",
|
|
103
147
|
"pathe": "^2.0.3",
|
|
104
148
|
"picomatch": "^4.0.3",
|
|
105
|
-
"tinyest": "~0.
|
|
106
|
-
"tinyest-for-wgsl": "~0.
|
|
149
|
+
"tinyest": "~0.3.0",
|
|
150
|
+
"tinyest-for-wgsl": "~0.3.1",
|
|
107
151
|
"unplugin": "^2.3.5"
|
|
108
152
|
},
|
|
109
153
|
"peerDependencies": {
|
|
110
|
-
"typegpu": "^0.
|
|
154
|
+
"typegpu": "^0.10.2"
|
|
111
155
|
},
|
|
112
|
-
"main": "./index.
|
|
113
|
-
"types": "./index.d.
|
|
156
|
+
"main": "./index.mjs",
|
|
157
|
+
"types": "./index.d.mts",
|
|
114
158
|
"private": false,
|
|
115
159
|
"scripts": {}
|
|
116
160
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { rollUpImpl } from "./rollup-impl.mjs";
|
package/rolldown.cjs
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
+
const require_index = require('./index.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/rolldown.ts
|
|
5
|
+
var rolldown_default = require_index.rolldownPlugin;
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.default = rolldown_default;
|
package/rolldown.d.cts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
import { rolldownPlugin } from
|
|
2
|
-
|
|
3
|
-
import 'vite';
|
|
4
|
-
import 'unplugin';
|
|
5
|
-
import './bun-Dq3TUCfa.cjs';
|
|
6
|
-
import 'bun';
|
|
7
|
-
import './babel.cjs';
|
|
8
|
-
import '@babel/traverse';
|
|
9
|
-
import '@babel/types';
|
|
10
|
-
import 'magic-string-ast';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export { rolldownPlugin as default };
|
|
1
|
+
import { rolldownPlugin } from "./index.cjs";
|
|
2
|
+
export = rolldownPlugin;
|
package/rolldown.d.mts
ADDED
package/rolldown.mjs
ADDED
package/rollup-impl.cjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_common = require('./common.cjs');
|
|
3
|
+
let defu = require("defu");
|
|
4
|
+
defu = require_runtime.__toESM(defu);
|
|
5
|
+
let magic_string_ast = require("magic-string-ast");
|
|
6
|
+
let estree_walker = require("estree-walker");
|
|
7
|
+
let tinyest_for_wgsl = require("tinyest-for-wgsl");
|
|
8
|
+
let tinyest = require("tinyest");
|
|
9
|
+
|
|
10
|
+
//#region src/rollup-impl.ts
|
|
11
|
+
function containsUseGpuDirective(node) {
|
|
12
|
+
if (node.body.type === "BlockStatement") {
|
|
13
|
+
for (const statement of node.body.body) if (statement.type === "ExpressionStatement" && statement.directive === require_common.useGpuDirective) return true;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
function removeUseGpuDirective(node) {
|
|
18
|
+
const cloned = structuredClone(node);
|
|
19
|
+
if (cloned.body.type === "BlockStatement") cloned.body.body = cloned.body.body.filter((statement) => !(statement.type === "ExpressionStatement" && statement.directive === require_common.useGpuDirective));
|
|
20
|
+
return cloned;
|
|
21
|
+
}
|
|
22
|
+
function assignMetadata(magicString, node, metadata) {
|
|
23
|
+
magicString.prependLeft(node.start, "(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (").appendRight(node.end, `), ${metadata}) && $.f)({}))`);
|
|
24
|
+
}
|
|
25
|
+
function wrapInAutoName(magicString, node, name) {
|
|
26
|
+
magicString.prependLeft(node.start, "((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").appendRight(node.end, `, "${name}"))`);
|
|
27
|
+
}
|
|
28
|
+
const rollUpImpl = (rawOptions) => {
|
|
29
|
+
const options = (0, defu.default)(rawOptions, require_common.defaultOptions);
|
|
30
|
+
return {
|
|
31
|
+
name: "unplugin-typegpu",
|
|
32
|
+
enforce: options.enforce,
|
|
33
|
+
transform: {
|
|
34
|
+
filter: options.earlyPruning ? {
|
|
35
|
+
id: options,
|
|
36
|
+
code: require_common.earlyPruneRegex
|
|
37
|
+
} : { id: options },
|
|
38
|
+
handler(code, id) {
|
|
39
|
+
const ctx = {
|
|
40
|
+
tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
|
|
41
|
+
fileId: id,
|
|
42
|
+
autoNamingEnabled: options.autoNamingEnabled
|
|
43
|
+
};
|
|
44
|
+
let ast;
|
|
45
|
+
try {
|
|
46
|
+
ast = this.parse(code, {
|
|
47
|
+
lang: "ts",
|
|
48
|
+
allowReturnOutsideFunction: true
|
|
49
|
+
});
|
|
50
|
+
} catch (cause) {
|
|
51
|
+
console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const tgslFunctionDefs = [];
|
|
55
|
+
const magicString = new magic_string_ast.MagicStringAST(code);
|
|
56
|
+
(0, estree_walker.walk)(ast, { enter(_node, _parent) {
|
|
57
|
+
const node = _node;
|
|
58
|
+
const parent = _parent;
|
|
59
|
+
require_common.performExpressionNaming(ctx, node, (node, name) => {
|
|
60
|
+
wrapInAutoName(magicString, node, name);
|
|
61
|
+
});
|
|
62
|
+
if (node.type === "ImportDeclaration") require_common.gatherTgpuAliases(node, ctx);
|
|
63
|
+
if (node.type === "CallExpression") {
|
|
64
|
+
if (require_common.isShellImplementationCall(node, ctx)) {
|
|
65
|
+
const implementation = node.arguments[0];
|
|
66
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
|
|
67
|
+
tgslFunctionDefs.push({ def: removeUseGpuDirective(implementation) });
|
|
68
|
+
this.skip();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
|
|
73
|
+
if (containsUseGpuDirective(node)) {
|
|
74
|
+
tgslFunctionDefs.push({
|
|
75
|
+
def: removeUseGpuDirective(node),
|
|
76
|
+
name: require_common.getFunctionName(node, parent)
|
|
77
|
+
});
|
|
78
|
+
this.skip();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} });
|
|
82
|
+
for (const { def, name } of tgslFunctionDefs) {
|
|
83
|
+
const { params, body, externalNames } = (0, tinyest_for_wgsl.transpileFn)(def);
|
|
84
|
+
const isFunctionStatement = def.type === "FunctionDeclaration";
|
|
85
|
+
(0, estree_walker.walk)(def, { leave(_node) {
|
|
86
|
+
const node = _node;
|
|
87
|
+
if (node.type === "AssignmentExpression") {
|
|
88
|
+
const runtimeFn = require_common.operators[node.operator];
|
|
89
|
+
if (runtimeFn) {
|
|
90
|
+
const left = node.left;
|
|
91
|
+
const right = node.right;
|
|
92
|
+
const lhs = magicString.sliceNode(left);
|
|
93
|
+
const rhs = magicString.sliceNode(right);
|
|
94
|
+
magicString.overwriteNode(node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
|
|
95
|
+
}
|
|
96
|
+
} else if (node.type === "BinaryExpression") {
|
|
97
|
+
const runtimeFn = require_common.operators[node.operator];
|
|
98
|
+
if (runtimeFn) {
|
|
99
|
+
const left = node.left;
|
|
100
|
+
const right = node.right;
|
|
101
|
+
const lhs = magicString.sliceNode(left);
|
|
102
|
+
const rhs = magicString.sliceNode(right);
|
|
103
|
+
magicString.overwriteNode(node, `${runtimeFn}(${lhs}, ${rhs})`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} });
|
|
107
|
+
if (isFunctionStatement && name && code.slice(0, def.start).search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1) console.warn(`File ${id}: function "${name}" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);
|
|
108
|
+
assignMetadata(magicString, def, `{
|
|
109
|
+
v: ${tinyest.FORMAT_VERSION},
|
|
110
|
+
name: ${name ? `"${name}"` : "undefined"},
|
|
111
|
+
ast: ${require_common.embedJSON({
|
|
112
|
+
params,
|
|
113
|
+
body,
|
|
114
|
+
externalNames
|
|
115
|
+
})},
|
|
116
|
+
externals: () => ({${externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
|
|
117
|
+
}`);
|
|
118
|
+
if (isFunctionStatement && name) magicString.prependLeft(def.start, `const ${name} = `);
|
|
119
|
+
}
|
|
120
|
+
return (0, magic_string_ast.generateTransform)(magicString, id);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
exports.rollUpImpl = rollUpImpl;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Options } from "./common.cjs";
|
|
2
|
+
import * as unplugin from "unplugin";
|
|
3
|
+
import { UnpluginBuildContext, UnpluginContext } from "unplugin";
|
|
4
|
+
import * as magic_string_ast0 from "magic-string-ast";
|
|
5
|
+
|
|
6
|
+
//#region src/rollup-impl.d.ts
|
|
7
|
+
declare const rollUpImpl: (rawOptions: Options) => {
|
|
8
|
+
name: "unplugin-typegpu";
|
|
9
|
+
enforce: "post" | "pre" | undefined;
|
|
10
|
+
transform: {
|
|
11
|
+
filter: {
|
|
12
|
+
id: Omit<Options, "include" | "autoNamingEnabled" | "earlyPruning"> & Omit<{
|
|
13
|
+
include: RegExp;
|
|
14
|
+
autoNamingEnabled: boolean;
|
|
15
|
+
earlyPruning: boolean;
|
|
16
|
+
}, "include" | "autoNamingEnabled" | "earlyPruning"> & {
|
|
17
|
+
include: string | RegExp | unplugin.StringOrRegExp[];
|
|
18
|
+
autoNamingEnabled: boolean;
|
|
19
|
+
earlyPruning: boolean;
|
|
20
|
+
};
|
|
21
|
+
code: RegExp[];
|
|
22
|
+
} | {
|
|
23
|
+
id: Omit<Options, "include" | "autoNamingEnabled" | "earlyPruning"> & Omit<{
|
|
24
|
+
include: RegExp;
|
|
25
|
+
autoNamingEnabled: boolean;
|
|
26
|
+
earlyPruning: boolean;
|
|
27
|
+
}, "include" | "autoNamingEnabled" | "earlyPruning"> & {
|
|
28
|
+
include: string | RegExp | unplugin.StringOrRegExp[];
|
|
29
|
+
autoNamingEnabled: boolean;
|
|
30
|
+
earlyPruning: boolean;
|
|
31
|
+
};
|
|
32
|
+
code?: never;
|
|
33
|
+
};
|
|
34
|
+
handler(this: UnpluginBuildContext & UnpluginContext, code: string, id: string): magic_string_ast0.CodeTransform | undefined;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
//#endregion
|
|
38
|
+
export { rollUpImpl };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Options } from "./common.mjs";
|
|
2
|
+
import * as unplugin from "unplugin";
|
|
3
|
+
import { UnpluginBuildContext, UnpluginContext } from "unplugin";
|
|
4
|
+
import * as magic_string_ast0 from "magic-string-ast";
|
|
5
|
+
import { MagicStringAST } from "magic-string-ast";
|
|
6
|
+
|
|
7
|
+
//#region src/rollup-impl.d.ts
|
|
8
|
+
declare const rollUpImpl: (rawOptions: Options) => {
|
|
9
|
+
name: "unplugin-typegpu";
|
|
10
|
+
enforce: "post" | "pre" | undefined;
|
|
11
|
+
transform: {
|
|
12
|
+
filter: {
|
|
13
|
+
id: Omit<Options, "include" | "autoNamingEnabled" | "earlyPruning"> & Omit<{
|
|
14
|
+
include: RegExp;
|
|
15
|
+
autoNamingEnabled: boolean;
|
|
16
|
+
earlyPruning: boolean;
|
|
17
|
+
}, "include" | "autoNamingEnabled" | "earlyPruning"> & {
|
|
18
|
+
include: string | RegExp | unplugin.StringOrRegExp[];
|
|
19
|
+
autoNamingEnabled: boolean;
|
|
20
|
+
earlyPruning: boolean;
|
|
21
|
+
};
|
|
22
|
+
code: RegExp[];
|
|
23
|
+
} | {
|
|
24
|
+
id: Omit<Options, "include" | "autoNamingEnabled" | "earlyPruning"> & Omit<{
|
|
25
|
+
include: RegExp;
|
|
26
|
+
autoNamingEnabled: boolean;
|
|
27
|
+
earlyPruning: boolean;
|
|
28
|
+
}, "include" | "autoNamingEnabled" | "earlyPruning"> & {
|
|
29
|
+
include: string | RegExp | unplugin.StringOrRegExp[];
|
|
30
|
+
autoNamingEnabled: boolean;
|
|
31
|
+
earlyPruning: boolean;
|
|
32
|
+
};
|
|
33
|
+
code?: never;
|
|
34
|
+
};
|
|
35
|
+
handler(this: UnpluginBuildContext & UnpluginContext, code: string, id: string): magic_string_ast0.CodeTransform | undefined;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { rollUpImpl };
|
package/rollup-impl.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { defaultOptions, earlyPruneRegex, embedJSON, gatherTgpuAliases, getFunctionName, isShellImplementationCall, operators, performExpressionNaming, useGpuDirective } from "./common.mjs";
|
|
2
|
+
import defu from "defu";
|
|
3
|
+
import { MagicStringAST, generateTransform } from "magic-string-ast";
|
|
4
|
+
import { walk } from "estree-walker";
|
|
5
|
+
import { transpileFn } from "tinyest-for-wgsl";
|
|
6
|
+
import { FORMAT_VERSION } from "tinyest";
|
|
7
|
+
|
|
8
|
+
//#region src/rollup-impl.ts
|
|
9
|
+
function containsUseGpuDirective(node) {
|
|
10
|
+
if (node.body.type === "BlockStatement") {
|
|
11
|
+
for (const statement of node.body.body) if (statement.type === "ExpressionStatement" && statement.directive === useGpuDirective) return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
function removeUseGpuDirective(node) {
|
|
16
|
+
const cloned = structuredClone(node);
|
|
17
|
+
if (cloned.body.type === "BlockStatement") cloned.body.body = cloned.body.body.filter((statement) => !(statement.type === "ExpressionStatement" && statement.directive === useGpuDirective));
|
|
18
|
+
return cloned;
|
|
19
|
+
}
|
|
20
|
+
function assignMetadata(magicString, node, metadata) {
|
|
21
|
+
magicString.prependLeft(node.start, "(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (").appendRight(node.end, `), ${metadata}) && $.f)({}))`);
|
|
22
|
+
}
|
|
23
|
+
function wrapInAutoName(magicString, node, name) {
|
|
24
|
+
magicString.prependLeft(node.start, "((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").appendRight(node.end, `, "${name}"))`);
|
|
25
|
+
}
|
|
26
|
+
const rollUpImpl = (rawOptions) => {
|
|
27
|
+
const options = defu(rawOptions, defaultOptions);
|
|
28
|
+
return {
|
|
29
|
+
name: "unplugin-typegpu",
|
|
30
|
+
enforce: options.enforce,
|
|
31
|
+
transform: {
|
|
32
|
+
filter: options.earlyPruning ? {
|
|
33
|
+
id: options,
|
|
34
|
+
code: earlyPruneRegex
|
|
35
|
+
} : { id: options },
|
|
36
|
+
handler(code, id) {
|
|
37
|
+
const ctx = {
|
|
38
|
+
tgpuAliases: new Set(options.forceTgpuAlias ? [options.forceTgpuAlias] : []),
|
|
39
|
+
fileId: id,
|
|
40
|
+
autoNamingEnabled: options.autoNamingEnabled
|
|
41
|
+
};
|
|
42
|
+
let ast;
|
|
43
|
+
try {
|
|
44
|
+
ast = this.parse(code, {
|
|
45
|
+
lang: "ts",
|
|
46
|
+
allowReturnOutsideFunction: true
|
|
47
|
+
});
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const tgslFunctionDefs = [];
|
|
53
|
+
const magicString = new MagicStringAST(code);
|
|
54
|
+
walk(ast, { enter(_node, _parent) {
|
|
55
|
+
const node = _node;
|
|
56
|
+
const parent = _parent;
|
|
57
|
+
performExpressionNaming(ctx, node, (node, name) => {
|
|
58
|
+
wrapInAutoName(magicString, node, name);
|
|
59
|
+
});
|
|
60
|
+
if (node.type === "ImportDeclaration") gatherTgpuAliases(node, ctx);
|
|
61
|
+
if (node.type === "CallExpression") {
|
|
62
|
+
if (isShellImplementationCall(node, ctx)) {
|
|
63
|
+
const implementation = node.arguments[0];
|
|
64
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression")) {
|
|
65
|
+
tgslFunctionDefs.push({ def: removeUseGpuDirective(implementation) });
|
|
66
|
+
this.skip();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
|
|
71
|
+
if (containsUseGpuDirective(node)) {
|
|
72
|
+
tgslFunctionDefs.push({
|
|
73
|
+
def: removeUseGpuDirective(node),
|
|
74
|
+
name: getFunctionName(node, parent)
|
|
75
|
+
});
|
|
76
|
+
this.skip();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} });
|
|
80
|
+
for (const { def, name } of tgslFunctionDefs) {
|
|
81
|
+
const { params, body, externalNames } = transpileFn(def);
|
|
82
|
+
const isFunctionStatement = def.type === "FunctionDeclaration";
|
|
83
|
+
walk(def, { leave(_node) {
|
|
84
|
+
const node = _node;
|
|
85
|
+
if (node.type === "AssignmentExpression") {
|
|
86
|
+
const runtimeFn = operators[node.operator];
|
|
87
|
+
if (runtimeFn) {
|
|
88
|
+
const left = node.left;
|
|
89
|
+
const right = node.right;
|
|
90
|
+
const lhs = magicString.sliceNode(left);
|
|
91
|
+
const rhs = magicString.sliceNode(right);
|
|
92
|
+
magicString.overwriteNode(node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`);
|
|
93
|
+
}
|
|
94
|
+
} else if (node.type === "BinaryExpression") {
|
|
95
|
+
const runtimeFn = operators[node.operator];
|
|
96
|
+
if (runtimeFn) {
|
|
97
|
+
const left = node.left;
|
|
98
|
+
const right = node.right;
|
|
99
|
+
const lhs = magicString.sliceNode(left);
|
|
100
|
+
const rhs = magicString.sliceNode(right);
|
|
101
|
+
magicString.overwriteNode(node, `${runtimeFn}(${lhs}, ${rhs})`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} });
|
|
105
|
+
if (isFunctionStatement && name && code.slice(0, def.start).search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1) console.warn(`File ${id}: function "${name}" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);
|
|
106
|
+
assignMetadata(magicString, def, `{
|
|
107
|
+
v: ${FORMAT_VERSION},
|
|
108
|
+
name: ${name ? `"${name}"` : "undefined"},
|
|
109
|
+
ast: ${embedJSON({
|
|
110
|
+
params,
|
|
111
|
+
body,
|
|
112
|
+
externalNames
|
|
113
|
+
})},
|
|
114
|
+
externals: () => ({${externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}),
|
|
115
|
+
}`);
|
|
116
|
+
if (isFunctionStatement && name) magicString.prependLeft(def.start, `const ${name} = `);
|
|
117
|
+
}
|
|
118
|
+
return generateTransform(magicString, id);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
export { rollUpImpl };
|
package/rollup.cjs
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
+
const require_index = require('./index.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/rollup.ts
|
|
5
|
+
var rollup_default = require_index.rollupPlugin;
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.default = rollup_default;
|
package/rollup.d.cts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
import { rollupPlugin } from
|
|
2
|
-
|
|
3
|
-
import 'vite';
|
|
4
|
-
import 'unplugin';
|
|
5
|
-
import './bun-Dq3TUCfa.cjs';
|
|
6
|
-
import 'bun';
|
|
7
|
-
import './babel.cjs';
|
|
8
|
-
import '@babel/traverse';
|
|
9
|
-
import '@babel/types';
|
|
10
|
-
import 'magic-string-ast';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export { rollupPlugin as default };
|
|
1
|
+
import { rollupPlugin } from "./index.cjs";
|
|
2
|
+
export = rollupPlugin;
|
package/rollup.d.mts
ADDED