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