unplugin-typegpu
Version:
Build plugins for TypeGPU, enabling seamless JavaScript -> WGSL transpilation and improved debugging.
10 lines • 2.69 kB
JavaScript
import{a as x,b,c as v,d as F,e as d,f as w,g as h}from"./chunk-DO6SOWHC.js";import $ from"defu";import{walk as A}from"estree-walker";import{generateTransform as S,MagicStringAST as I}from"magic-string-ast";import{FORMAT_VERSION as N}from"tinyest";import{transpileFn as P}from"tinyest-for-wgsl";import{createUnplugin as T}from"unplugin";function D(c){if(c.body.type==="BlockStatement"){for(let n of c.body.body)if(n.type==="ExpressionStatement"&&d.includes(n.directive))return n.directive}}function E(c){let n=structuredClone(c);return n.body.type==="BlockStatement"&&(n.body.body=n.body.body.filter(s=>!(s.type==="ExpressionStatement"&&s.directive&&d.includes(s.directive)))),n}var r=T(c=>{let n=$(c,x);return{name:"unplugin-typegpu",enforce:n.enforce,transform:{filter:{id:n},handler(s,p){let g={tgpuAliases:new Set(n.forceTgpuAlias?[n.forceTgpuAlias]:[]),fileId:p},k=this.parse(s,{allowReturnOutsideFunction:!0}),l=[],u=new I(s);A(k,{enter(i,t,m,y){var a;let e=i;if(e.type==="ImportDeclaration"&&v(e,g),e.type==="CallExpression"&&F(e,g)){let o=e.arguments[0];if(o&&(o.type==="FunctionExpression"||o.type==="ArrowFunctionExpression")){let f=D(o);l.push({def:E(o),removeJsImplementation:f!=="kernel & js"}),this.skip()}}if(e.type==="ArrowFunctionExpression"||e.type==="FunctionExpression"||e.type==="FunctionDeclaration"){let o=D(e);o&&(l.push({def:E(e),name:e.type==="FunctionDeclaration"||e.type==="FunctionExpression"?(a=e.id)==null?void 0:a.name:(t==null?void 0:t.type)==="VariableDeclarator"&&t.id.type==="Identifier"?t.id.name:void 0,removeJsImplementation:o!=="kernel & js"}),this.skip())}}});for(let{def:i,name:t,removeJsImplementation:m}of l){let{params:y,body:e,externalNames:a}=P(i),o=i.type==="FunctionDeclaration";o&&t&&s.slice(0,i.start).search(new RegExp(`(?<![\\w_.])${t}(?![\\w_])`))!==-1&&console.warn(`File ${p}: function "${t}", containing ${m?"kernel":"kernel & js"} directive, might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);let f=`{
v: ${N},
ast: ${b({params:y,body:e,externalNames:a})},
externals: {${a.join(", ")}},
}`;u.appendLeft(i.start,`${o&&t?`const ${t} = `:""}
(($) => ((globalThis.__TYPEGPU_META__ ??= new WeakMap()).set(
$.f = (`).appendRight(i.end,`) , ${f}) && $.f))({})`),m&&u.overwriteNode(i,`() => {
throw new Error(\`${w(t)}\`);
}`)}return S(u,p)}}}}),B=r,V=r.vite,_=r.rollup,G=r.rolldown,L=r.webpack,W=r.rspack,Y=r.esbuild,q=r.farm,z=h;export{B as a,V as b,_ as c,G as d,L as e,W as f,Y as g,q as h,z as i};
//# sourceMappingURL=chunk-LML6XL7I.js.map