UNPKG

unplugin-typegpu

Version:

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

145 lines (142 loc) 7.09 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 pathe = require("pathe"); pathe = require_common.__toESM(pathe); let picomatch = require("picomatch"); picomatch = require_common.__toESM(picomatch); //#region src/core/filter.ts function toArray(array) { const result = array || []; if (Array.isArray(result)) return result; return [result]; } const BACKSLASH_REGEX = /\\/g; function normalize(path) { return path.replace(BACKSLASH_REGEX, "/"); } const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Z]:)?[/\\|])/i; function isAbsolute(path) { return ABSOLUTE_PATH_REGEX.test(path); } function getMatcherString(glob, cwd) { if (glob.startsWith("**") || isAbsolute(glob)) return normalize(glob); return normalize((0, pathe.resolve)(cwd, glob)); } function patternToIdFilter(pattern) { if (pattern instanceof RegExp) return (id) => { const normalizedId = normalize(id); const result = pattern.test(normalizedId); pattern.lastIndex = 0; return result; }; const matcher = (0, picomatch.default)(getMatcherString(pattern, process.cwd()), { dot: true }); return (id) => { return matcher(normalize(id)); }; } function createFilter(exclude, include) { if (!exclude && !include) return; return (input) => { if (exclude === null || exclude === void 0 ? void 0 : exclude.some((filter) => filter(input))) return false; if (include === null || include === void 0 ? void 0 : include.some((filter) => filter(input))) return true; return !(include && include.length > 0); }; } function normalizeFilter(filter) { if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] }; if (Array.isArray(filter)) return { include: filter }; return { exclude: filter.exclude ? toArray(filter.exclude) : void 0, include: filter.include ? toArray(filter.include) : void 0 }; } function createIdFilter(filter) { if (!filter) return; const { exclude, include } = normalizeFilter(filter); return createFilter(exclude === null || exclude === void 0 ? void 0 : exclude.map(patternToIdFilter), include === null || include === void 0 ? void 0 : include.map(patternToIdFilter)); } function createFilterForId(filter) { const filterFunction = createIdFilter(filter); return filterFunction ? (id) => !!filterFunction(id) : void 0; } //#endregion //#region src/babel.ts function i(identifier) { return __babel_types.identifier(identifier); } function assignMetadata(path, name, ast) { const metadata = __babel_types.objectExpression([ __babel_types.objectProperty(i("v"), __babel_types.numericLiteral(require_common.METADATA_FORMAT_VERSION)), __babel_types.objectProperty(i("name"), __babel_types.valueToNode(name)), __babel_types.objectProperty(i("ast"), __babel_types.valueToNode(ast)), __babel_types.objectProperty(i("externals"), __babel_types.arrowFunctionExpression([], __babel_types.blockStatement([__babel_types.returnStatement(__babel_types.objectExpression(ast.externalNames.map((name$1) => name$1 === "this" ? __babel_types.objectProperty(i("this"), __babel_types.thisExpression()) : __babel_types.objectProperty(i(name$1), i(name$1), false, name$1 !== "this"))))]))) ]); let expression; const visibility = __babel_types.isFunctionDeclaration(path.node) ? require_common.getBlockScope(path) : void 0; if (__babel_types.isFunctionDeclaration(path.node)) expression = __babel_types.functionExpression(path.node.id, path.node.params, path.node.body); else expression = path.node; const callExpr = __babel_types.callExpression(__babel_types.arrowFunctionExpression([i("$")], __babel_types.logicalExpression("&&", __babel_types.callExpression(__babel_types.memberExpression(__babel_types.assignmentExpression("??=", __babel_types.memberExpression(i("globalThis"), i("__TYPEGPU_META__")), __babel_types.newExpression(i("WeakMap"), [])), i("set")), [__babel_types.assignmentExpression("=", __babel_types.memberExpression(i("$"), i("f")), expression), metadata]), __babel_types.memberExpression(i("$"), i("f")))), [__babel_types.objectExpression([])]); __babel_types.addComment(callExpr, "leading", "#__PURE__"); let replacement = callExpr; if (__babel_types.isFunctionDeclaration(path.node) && path.node.id) { const declaration = __babel_types.variableDeclaration("const", [__babel_types.variableDeclarator(path.node.id, callExpr)]); __babel_types.inheritLeadingComments(declaration, path.node); if (path.parentPath && (path.parentPath.isExportNamedDeclaration() || path.parentPath.isExportDefaultDeclaration())) { __babel_types.inheritLeadingComments(declaration, path.parentPath.node); path.parentPath.node.leadingComments = null; } replacement = declaration; } if (visibility) { visibility.unshiftContainer("body", replacement); this.alreadyTransformed.add(expression); const id = __babel_types.isFunctionDeclaration(path.node) ? path.node.id : void 0; if (id && path.parentPath.isExportNamedDeclaration()) path.parentPath.replaceWith(__babel_types.exportNamedDeclaration(null, [__babel_types.exportSpecifier(__babel_types.cloneNode(id), __babel_types.cloneNode(id))])); else if (id && path.parentPath.isExportDefaultDeclaration()) path.parentPath.replaceWith(__babel_types.exportDefaultDeclaration(__babel_types.cloneNode(id))); else path.remove(); } else path.replaceWith(replacement); path.skip(); } function wrapInAutoName(path, name) { const callExpr = __babel_types.callExpression(__babel_types.logicalExpression("??", __babel_types.memberExpression(i("globalThis"), i("__TYPEGPU_AUTONAME__")), __babel_types.arrowFunctionExpression([i("a")], i("a"))), [path.node, __babel_types.stringLiteral(name)]); __babel_types.addComment(callExpr, "leading", "#__PURE__"); path.replaceWith(callExpr); } function replaceWithAssignmentOverload(path, runtimeFn) { path.replaceWith(__babel_types.assignmentExpression("=", path.node.left, __babel_types.callExpression(i(runtimeFn), [path.node.left, path.node.right]))); } function replaceWithBinaryOverload(path, runtimeFn) { path.replaceWith(__babel_types.callExpression(i(runtimeFn), [path.node.left, path.node.right])); } function TypeGPUPlugin() { return { name: "typegpu", pre() { this.opts = (0, defu.default)(this.opts, require_common.defaultOptions); require_common.initPluginState(this, { warn: (message) => console.warn(message), assignMetadata, wrapInAutoName, replaceWithAssignmentOverload, replaceWithBinaryOverload }); }, visitor: { Program(path, state) { const filter = createFilterForId(state.opts); if (state.filename && filter && !(filter === null || filter === void 0 ? void 0 : filter(state.filename))) return; path.traverse(require_common.functionVisitor, state); } } }; } //#endregion Object.defineProperty(exports, 'TypeGPUPlugin', { enumerable: true, get: function () { return TypeGPUPlugin; } });