UNPKG

unplugin-typegpu

Version:

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

210 lines (203 loc) 8.62 kB
const require_common = require('./common-ddv56_tk.cjs'); let defu = require("defu"); defu = require_common.__toESM(defu); let tinyest_for_wgsl = require("tinyest-for-wgsl"); tinyest_for_wgsl = require_common.__toESM(tinyest_for_wgsl); let __babel_types = require("@babel/types"); __babel_types = require_common.__toESM(__babel_types); let magic_string = require("magic-string"); magic_string = require_common.__toESM(magic_string); let ast_kit = require("ast-kit"); ast_kit = require_common.__toESM(ast_kit); let __babel_traverse = require("@babel/traverse"); __babel_traverse = require_common.__toESM(__babel_traverse); let __babel_parser = require("@babel/parser"); __babel_parser = require_common.__toESM(__babel_parser); //#region \0@oxc-project+runtime@0.95.0/helpers/typeof.js function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) { return typeof o$1; } : function(o$1) { return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1; }, _typeof(o); } //#endregion //#region \0@oxc-project+runtime@0.95.0/helpers/toPrimitive.js function toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } //#endregion //#region \0@oxc-project+runtime@0.95.0/helpers/toPropertyKey.js function toPropertyKey(t) { var i = toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } //#endregion //#region \0@oxc-project+runtime@0.95.0/helpers/defineProperty.js function _defineProperty(e, r, t) { return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } //#endregion //#region \0@oxc-project+runtime@0.95.0/helpers/objectSpread2.js function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function(r$1) { return Object.getOwnPropertyDescriptor(e, r$1).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) { _defineProperty(e, r$1, t[r$1]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) { Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1)); }); } return e; } //#endregion //#region src/core/factory.ts let traverse = __babel_traverse.default; if (typeof traverse.default === "function") traverse = traverse.default; const fnWrapperTemplate = (fnCode, metadata) => `(/*#__PURE__*/($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (${fnCode}), ${metadata}) && $.f)({}))`; function embedJSON(jsValue) { return JSON.stringify(jsValue).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029"); } function assignMetadata(path, name, ast) { var _path$node$start, _path$node$start2; const metadata = `{ v: ${require_common.METADATA_FORMAT_VERSION}, name: ${name ? `"${name}"` : "undefined"}, ast: ${embedJSON(ast)}, externals: () => ({${ast.externalNames.map((e) => e === "this" ? "\"this\": this" : e).join(", ")}}), }`; const visibility = __babel_types.isFunctionDeclaration(path.node) ? require_common.getBlockScope(path) : void 0; const fnCode = this.slice(path.node); let nodeToOverride = path.node; let code = fnWrapperTemplate(fnCode, metadata); let insertPos = (_path$node$start = path.node.start) !== null && _path$node$start !== void 0 ? _path$node$start : 0; if (__babel_types.isFunctionDeclaration(path.node) && path.node.id) code = `const ${path.node.id.name} = ${code};\n\n`; if (visibility) { var _visibility$node$body, _visibility$node$body2; insertPos = (_visibility$node$body = (_visibility$node$body2 = visibility.node.body[0]) === null || _visibility$node$body2 === void 0 ? void 0 : _visibility$node$body2.start) !== null && _visibility$node$body !== void 0 ? _visibility$node$body : insertPos; if (__babel_types.isExportNamedDeclaration(path.parent)) { nodeToOverride = path.parent; code = `export ${code}`; } else if (__babel_types.isExportDefaultDeclaration(path.parent)) { nodeToOverride = path.parent; if (__babel_types.isFunctionDeclaration(path.node) && path.node.id) code = `${code}export default ${path.node.id.name};`; else code = `export default ${code};`; } } if (insertPos < ((_path$node$start2 = path.node.start) !== null && _path$node$start2 !== void 0 ? _path$node$start2 : 0)) { var _nodeToOverride$leadi, _nodeToOverride$leadi2; for (const comment of (_nodeToOverride$leadi = (_nodeToOverride$leadi2 = nodeToOverride.leadingComments) === null || _nodeToOverride$leadi2 === void 0 ? void 0 : _nodeToOverride$leadi2.toReversed()) !== null && _nodeToOverride$leadi !== void 0 ? _nodeToOverride$leadi : []) if (comment) { code = `${this.slice(comment)}\n${code}`; this.remove(comment); } this.remove(nodeToOverride); this.magicString.prependLeft(insertPos, code); } else this.overwrite(nodeToOverride, code); } function wrapInAutoName(path, name) { var _path$node$start3, _path$node$end; this.magicString.appendLeft((_path$node$start3 = path.node.start) !== null && _path$node$start3 !== void 0 ? _path$node$start3 : 0, "(/*#__PURE__*/(globalThis.__TYPEGPU_AUTONAME__ ?? (a => a))(").prependRight((_path$node$end = path.node.end) !== null && _path$node$end !== void 0 ? _path$node$end : 0, `, "${name}"))`); } function replaceWithAssignmentOverload(path, runtimeFn) { const lhs = this.slice(path.node.left); const rhs = this.slice(path.node.right); this.overwrite(path.node, `${lhs} = ${runtimeFn}(${lhs}, ${rhs})`); } function replaceWithBinaryOverload(path, runtimeFn) { const lhs = this.slice(path.node.left); const rhs = this.slice(path.node.right); this.overwrite(path.node, `${runtimeFn}(${lhs}, ${rhs})`); } const NodeUtils = { slice(node) { var _node$start, _node$end; return this.magicString.slice((_node$start = node.start) !== null && _node$start !== void 0 ? _node$start : 0, (_node$end = node.end) !== null && _node$end !== void 0 ? _node$end : 0); }, remove(node) { var _node$start2, _node$end2; this.magicString.remove((_node$start2 = node.start) !== null && _node$start2 !== void 0 ? _node$start2 : 0, (_node$end2 = node.end) !== null && _node$end2 !== void 0 ? _node$end2 : 0); }, overwrite(node, content) { var _node$start3, _node$end3; this.magicString.overwrite((_node$start3 = node.start) !== null && _node$start3 !== void 0 ? _node$start3 : 0, (_node$end3 = node.end) !== null && _node$end3 !== void 0 ? _node$end3 : 0, content); } }; const unpluginFactory = ((rawOptions, _meta) => { const options = (0, defu.default)(rawOptions, require_common.defaultOptions); return { name: "unplugin-typegpu", enforce: options.enforce, transform: { filter: options.earlyPruning ? { id: options, code: require_common.earlyPruneRegex } : { id: options }, handler(code, id) { let ast; try { ast = __babel_parser.parse(code, (0, ast_kit.getBabelParserOptions)((0, ast_kit.getLang)(id), { sourceType: "module", allowReturnOutsideFunction: true })); } catch (cause) { console.warn(`[unplugin-typegpu] Failed to parse ${id}. Cause: ${typeof cause === "object" && cause && "message" in cause ? cause.message : cause}`); return; } const magicString = new magic_string.default(code); const state = _objectSpread2({ filename: id, magicString, opts: options }, NodeUtils); require_common.initPluginState(state, { warn: (message) => this.warn(message), assignMetadata, wrapInAutoName, replaceWithAssignmentOverload, replaceWithBinaryOverload }); traverse(ast, require_common.functionVisitor, void 0, state); if (magicString.hasChanged()) return { code: magicString.toString(), get map() { return magicString.generateMap({ source: id, includeContent: true, hires: "boundary" }); } }; } } }; }); //#endregion Object.defineProperty(exports, 'unpluginFactory', { enumerable: true, get: function () { return unpluginFactory; } });