unplugin-typegpu
Version:
Build plugins for TypeGPU, enabling seamless JavaScript -> WGSL transpilation and improved debugging.
6 lines • 6.14 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _standalone = require('@babel/standalone'); var b = _interopRequireWildcard(_standalone);var _tinyest = require('tinyest');var _tinyestforwgsl = require('tinyest-for-wgsl');var L={include:[/\.m?[jt]sx?$/],autoNamingEnabled:!0};function F(r){return JSON.stringify(r).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029")}function k(r,e){let t="",n=e;for(;;)if(n.type==="MemberExpression"){if((n.property.type==="Literal"||n.property.type==="StringLiteral")&&n.property.value==="~unstable"){n=n.object;continue}if(n.property.type!=="Identifier")break;t=t?`${n.property.name}.${t}`:n.property.name,n=n.object}else if(n.type==="Identifier"){t=t?`${n.name}.${t}`:n.name;break}else break;return r.tgpuAliases.has(t)}function T(r,e){if(r.source.value==="typegpu")for(let t of r.specifiers)t.type==="ImportDefaultSpecifier"||t.type==="ImportSpecifier"&&t.imported.type==="Identifier"&&t.imported.name==="tgpu"?e.tgpuAliases.add(t.local.name):t.type==="ImportNamespaceSpecifier"&&e.tgpuAliases.add(`${t.local.name}.tgpu`)}var A=["fn","vertexFn","fragmentFn","computeFn"];function m(r,e){return r.callee.type==="CallExpression"&&r.callee.callee.type==="MemberExpression"&&r.callee.callee.property.type==="Identifier"&&A.includes(r.callee.callee.property.name)&&r.arguments.length===1&&k(e,r.callee.callee.object)}var E=["bindGroupLayout","vertexLayout","slot","accessor","privateVar","workgroupVar","const",...A,"struct","unstruct","createBuffer","createMutable","createReadonly","createUniform","createQuerySet","createPipeline","createTexture","sampler","comparisonSampler"];function l(r,e){if(r.type==="CallExpression"){if(m(r,e)||r.callee.type==="Identifier"&&E.includes(r.callee.name))return!0;if(r.callee.type==="MemberExpression"){if(r.callee.property.type==="Identifier"){if(E.includes(r.callee.property.name))return!0;if(r.callee.property.name==="$name")return!1}return l(r.callee.object,e)}}return r.type==="TaggedTemplateExpression"?l(r.tag,e):!1}function p(r,e,t){r.autoNamingEnabled&&(e.type==="VariableDeclarator"&&e.id.type==="Identifier"&&e.init&&l(e.init,r)?t(e.init,e.id.name):e.type==="AssignmentExpression"&&e.left.type==="Identifier"&&l(e.right,r)?t(e.right,e.left.name):(e.type==="Property"||e.type==="ObjectProperty")&&e.key.type==="Identifier"&&l(e.value,r)&&t(e.value,e.key.name))}var S="kernel";var _path = require('path');var _picomatch = require('picomatch'); var _picomatch2 = _interopRequireDefault(_picomatch);function I(r){let e=r||[];return Array.isArray(e)?e:[e]}var w=/\\/g;function u(r){return r.replace(w,"/")}var R=/^(?:\/|(?:[A-Z]:)?[/\\|])/i;function _(r){return R.test(r)}function $(r,e){if(r.startsWith("**")||_(r))return u(r);let t=_path.resolve.call(void 0, e,r);return u(t)}function N(r){if(r instanceof RegExp)return o=>{let a=u(o),f=r.test(a);return r.lastIndex=0,f};let e=process.cwd(),t=$(r,e),n=_picomatch2.default.call(void 0, t,{dot:!0});return o=>{let a=u(o);return n(a)}}function h(r,e){if(!(!r&&!e))return t=>r!=null&&r.some(n=>n(t))?!1:e!=null&&e.some(n=>n(t))?!0:!(e&&e.length>0)}function D(r){return typeof r=="string"||r instanceof RegExp?{include:[r]}:Array.isArray(r)?{include:r}:{exclude:r.exclude?I(r.exclude):void 0,include:r.include?I(r.include):void 0}}function j(r){if(!r)return;let{exclude:e,include:t}=D(r),n=e==null?void 0:e.map(N),o=t==null?void 0:t.map(N);return h(n,o)}function v(r){let e=j(r);return e?t=>!!e(t):void 0}var P=b.packages.template,i=b.packages.types;function d(r){var e,t;return("directives"in r.body?(t=(e=r.body)==null?void 0:e.directives)!=null?t:[]:[]).map(n=>n.value.value).includes(S)}function s(r){return i.identifier(r)}function c(r){let{params:e,body:t,externalNames:n}=_tinyestforwgsl.transpileFn.call(void 0, r),o=`{
v: ${_tinyest.FORMAT_VERSION},
ast: ${F({params:e,body:t,externalNames:n})},
externals: {${n.join(", ")}},
}`;return i.callExpression(i.arrowFunctionExpression([s("$")],i.logicalExpression("&&",i.callExpression(i.memberExpression(i.assignmentExpression("??=",i.memberExpression(s("globalThis"),s("__TYPEGPU_META__")),i.newExpression(s("WeakMap"),[])),s("set")),[i.assignmentExpression("=",i.memberExpression(s("$"),s("f")),r),P.expression`${o}`()]),i.memberExpression(s("$"),s("f")))),[i.objectExpression([])])}function g(r,e){return i.callExpression(P.expression("globalThis.__TYPEGPU_AUTONAME__ ?? (a => a)",{placeholderPattern:!1})(),[r,i.stringLiteral(e)])}function G(r){return{VariableDeclarator(e){p(r,e.node,(t,n)=>{e.get("init").replaceWith(g(t,n))})},AssignmentExpression(e){p(r,e.node,(t,n)=>{e.get("right").replaceWith(g(t,n))})},ObjectProperty(e){p(r,e.node,(t,n)=>{e.get("value").replaceWith(g(t,n))})},ImportDeclaration(e){T(e.node,r)},ArrowFunctionExpression(e){d(e.node)&&(e.replaceWith(c(e.node)),e.skip())},FunctionExpression(e){d(e.node)&&(e.replaceWith(c(e.node)),e.skip())},FunctionDeclaration(e){let t=e.node,n=i.functionExpression(t.id,t.params,t.body);if(d(e.node)&&t.id){let o=c(n);e.replaceWith(i.variableDeclaration("const",[i.variableDeclarator(t.id,o)])),e.skip()}},CallExpression(e){let t=e.node;if(m(t,r)){let n=t.arguments[0];if(n&&(n.type==="FunctionExpression"||n.type==="ArrowFunctionExpression")){let o=c(n);e.replaceWith(i.callExpression(t.callee,[o])),e.skip()}}}}}function Y(){return{visitor:{Program(r,e){var y,x;let t=(y=e.file)==null?void 0:y.code,n=e.opts,o=e.filename,a=v(n);if(o&&a&&!(a!=null&&a(o)))return;let f={tgpuAliases:new Set(n!=null&&n.forceTgpuAlias?[n.forceTgpuAlias]:[]),fileId:o,autoNamingEnabled:(x=n==null?void 0:n.autoNamingEnabled)!=null?x:!0};r.traverse(G(f))}}}}exports.a = L; exports.b = F; exports.c = T; exports.d = m; exports.e = p; exports.f = S; exports.g = Y;
//# sourceMappingURL=chunk-4DTTWQTW.cjs.map