unplugin-typegpu
Version:
Build plugins for TypeGPU, enabling seamless JavaScript -> WGSL transpilation and improved debugging.
6 lines • 5.53 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 f = _interopRequireWildcard(_standalone);var _tinyest = require('tinyest');var _tinyestforwgsl = require('tinyest-for-wgsl');var M={include:[/\.m?[jt]sx?$/]};function g(e){return JSON.stringify(e).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029")}function m(e,n){let r="",t=n;for(;;)if(t.type==="MemberExpression"){if(t.property.type!=="Identifier")break;r=r?`${t.property.name}.${r}`:t.property.name,t=t.object}else if(t.type==="Identifier"){r=r?`${t.name}.${r}`:t.name;break}else break;return e.tgpuAliases.has(r)}function b(e,n){if(e.source.value==="typegpu")for(let r of e.specifiers)r.type==="ImportDefaultSpecifier"||r.type==="ImportSpecifier"&&r.imported.type==="Identifier"&&r.imported.name==="tgpu"?n.tgpuAliases.add(r.local.name):r.type==="ImportNamespaceSpecifier"&&n.tgpuAliases.add(`${r.local.name}.tgpu`)}var k=["fn","vertexFn","fragmentFn","computeFn"];function y(e,n){return e.callee.type==="CallExpression"&&e.callee.callee.type==="MemberExpression"&&e.callee.callee.property.type==="Identifier"&&k.includes(e.callee.callee.property.name)&&e.arguments.length===1&&(e.callee.callee.object.type==="MemberExpression"?m(n,e.callee.callee.object.object):m(n,e.callee.callee.object))||e.callee.type==="MemberExpression"&&e.arguments.length===1&&e.callee.property.type==="Identifier"&&e.callee.property.name==="does"}var x=["kernel","kernel & js"];function F(e){return`The function "${e!=null?e:"<unnamed>"}" is invokable only on the GPU. If you want to use it on the CPU, mark it with the "kernel & js" directive.`}var _path = require('path');var _picomatch = require('picomatch'); var _picomatch2 = _interopRequireDefault(_picomatch);function E(e){let n=e||[];return Array.isArray(n)?n:[n]}var T=/\\/g;function c(e){return e.replace(T,"/")}var P=/^(?:\/|(?:[A-Z]:)?[/\\|])/i;function C(e){return P.test(e)}function w(e,n){if(e.startsWith("**")||C(e))return c(e);let r=_path.resolve.call(void 0, n,e);return c(r)}function S(e){if(e instanceof RegExp)return i=>{let s=c(i),l=e.test(s);return e.lastIndex=0,l};let n=process.cwd(),r=w(e,n),t=_picomatch2.default.call(void 0, r,{dot:!0});return i=>{let s=c(i);return t(s)}}function D(e,n){if(!(!e&&!n))return r=>e!=null&&e.some(t=>t(r))?!1:n!=null&&n.some(t=>t(r))?!0:!(n&&n.length>0)}function O(e){return typeof e=="string"||e instanceof RegExp?{include:[e]}:Array.isArray(e)?{include:e}:{exclude:e.exclude?E(e.exclude):void 0,include:e.include?E(e.include):void 0}}function $(e){if(!e)return;let{exclude:n,include:r}=O(e),t=n==null?void 0:n.map(S),i=r==null?void 0:r.map(S);return D(t,i)}function A(e){let n=$(e);return n?r=>!!n(r):void 0}var j=f.packages.template,o=f.packages.types;function u(e){var r,t;let n=("directives"in e.body?(t=(r=e.body)==null?void 0:r.directives)!=null?t:[]:[]).map(i=>i.value.value);for(let i of x)if(n.includes(i))return i}function a(e){return o.identifier(e)}function d(e,n,r){if(!n)return null;let{params:t,body:i,externalNames:s}=_tinyestforwgsl.transpileFn.call(void 0, e),l=`{
v: ${_tinyest.FORMAT_VERSION},
ast: ${g({params:t,body:i,externalNames:s})},
externals: {${s.join(", ")}},
}`,p=n==="kernel & js"?e:o.arrowFunctionExpression([],o.blockStatement([o.throwStatement(o.newExpression(a("Error"),[o.stringLiteral(F(r))]))]));return o.callExpression(o.arrowFunctionExpression([a("$")],o.logicalExpression("&&",o.callExpression(o.memberExpression(o.assignmentExpression("??=",o.memberExpression(a("globalThis"),a("__TYPEGPU_META__")),o.newExpression(a("WeakMap"),[])),a("set")),[o.assignmentExpression("=",o.memberExpression(a("$"),a("f")),p),j.expression`${l}`()]),o.memberExpression(a("$"),a("f")))),[o.objectExpression([])])}function R(e){return{ImportDeclaration(n){b(n.node,e)},ArrowFunctionExpression(n){let r=d(n.node,u(n.node),n.parentPath.node.type==="VariableDeclarator"&&n.parentPath.node.id.type==="Identifier"?n.parentPath.node.id.name:void 0);r&&(n.replaceWith(r),n.skip())},FunctionExpression(n){var t;let r=d(n.node,u(n.node),(t=n.node.id)!=null&&t.name?n.node.id.name:n.parentPath.node.type==="VariableDeclarator"&&n.parentPath.node.id.type==="Identifier"?n.parentPath.node.id.name:void 0);r&&(n.replaceWith(r),n.skip())},FunctionDeclaration(n){var s;let r=n.node,t=o.functionExpression(r.id,r.params,r.body),i=d(t,u(n.node),(s=r.id)==null?void 0:s.name);i&&r.id&&(n.replaceWith(o.variableDeclaration("const",[o.variableDeclarator(r.id,i)])),n.skip())},CallExpression(n){var t;let r=n.node;if(y(r,e)){let i=r.arguments[0];if(i&&(i.type==="FunctionExpression"||i.type==="ArrowFunctionExpression")){let s=d(i,(t=u(i))!=null?t:"kernel");n.replaceWith(o.callExpression(r.callee,[s])),n.skip()}}}}}function V(){return{visitor:{Program(e,n){var p;let r=(p=n.file)==null?void 0:p.code,t=n.opts,i=n.filename,s=A(t);if(i&&s&&!(s!=null&&s(i)))return;let l={tgpuAliases:new Set(t!=null&&t.forceTgpuAlias?[t.forceTgpuAlias]:[]),fileId:i};e.traverse(R(l))}}}}exports.a = M; exports.b = g; exports.c = b; exports.d = y; exports.e = x; exports.f = F; exports.g = V;
//# sourceMappingURL=chunk-G6NIIU55.cjs.map