UNPKG

unplugin-typegpu

Version:

Build plugins for TypeGPU, enabling seamless JavaScript -> WGSL transpilation and improved debugging.

7 lines 2.7 kB
import{a as F,b as h,c as w,d as N,e as A,f as E,g,h as P}from"./chunk-V4Q4PLLO.js";import{createUnplugin as M}from"unplugin";import $ from"defu";import{generateTransform as T,MagicStringAST as U}from"magic-string-ast";import{walk as _}from"estree-walker";import{transpileFn as k}from"tinyest-for-wgsl";import{FORMAT_VERSION as v}from"tinyest";function C(o){if(o.body.type==="BlockStatement"){for(let e of o.body.body)if(e.type==="ExpressionStatement"&&e.directive===g)return!0}return!1}function S(o){let e=structuredClone(o);return e.body.type==="BlockStatement"&&(e.body.body=e.body.body.filter(r=>!(r.type==="ExpressionStatement"&&r.directive===g))),e}function D(o,e,r){o.prependLeft(e.start,"(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (").appendRight(e.end,`), ${r}) && $.f)({}))`)}function O(o,e,r){o.prependLeft(e.start,"((globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").appendRight(e.end,`, "${r}"))`)}var f=o=>{let e=$(o,F);return{name:"unplugin-typegpu",enforce:e.enforce,transform:{filter:e.earlyPruning?{id:e,code:P}:{id:e},handler(r,p){let m={tgpuAliases:new Set(e.forceTgpuAlias?[e.forceTgpuAlias]:[]),fileId:p,autoNamingEnabled:e.autoNamingEnabled},x;try{x=this.parse(r,{lang:"ts",allowReturnOutsideFunction:!0})}catch(n){console.warn(`[unplugin-typegpu] Failed to parse ${p}. Cause: ${typeof n=="object"&&n&&"message"in n?n.message:n}`);return}let d=[],l=new U(r);_(x,{enter(n,i,y,b){let t=n,u=i;if(E(m,t,(s,c)=>{O(l,s,c)}),t.type==="ImportDeclaration"&&w(t,m),t.type==="CallExpression"&&N(t,m)){let s=t.arguments[0];s&&(s.type==="FunctionExpression"||s.type==="ArrowFunctionExpression")&&(d.push({def:S(s)}),this.skip())}(t.type==="ArrowFunctionExpression"||t.type==="FunctionExpression"||t.type==="FunctionDeclaration")&&C(t)&&(d.push({def:S(t),name:A(t,u)}),this.skip())}});for(let{def:n,name:i}of d){let{params:y,body:b,externalNames:t}=k(n),u=n.type==="FunctionDeclaration";u&&i&&r.slice(0,n.start).search(new RegExp(`(?<![\\w_.])${i}(?![\\w_])`))!==-1&&console.warn(`File ${p}: function "${i}" might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`);let s=`{ v: ${v}, name: ${i?`"${i}"`:"undefined"}, ast: ${h({params:y,body:b,externalNames:t})}, externals: () => ({${t.map(c=>c==="this"?'"this": this':c).join(", ")}}), }`;D(l,n,s),u&&i&&l.prependLeft(n.start,`const ${i} = `)}return T(l,p)}}}};var I=f;var a=M(f),Q=a,X=a.vite,Z=a.rollup,ee=a.rolldown,te=a.webpack,ne=a.rspack,oe=a.esbuild,re=a.farm;export{I as a,Q as b,X as c,Z as d,ee as e,te as f,ne as g,oe as h,re as i}; //# sourceMappingURL=chunk-RIZD7OMC.js.map