UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

62 lines (59 loc) 2.12 kB
import { __name } from './chunk-IRPJW6HH.js'; import { transform } from 'esbuild'; var TS_EXTS = /* @__PURE__ */ new Set([ ".ts", ".mts", ".cts" ]); var getUnbuildLoader = /* @__PURE__ */ __name((context) => { return async (input, { options }) => { if (!/\.(?:c|m)?[jt]sx?$/.test(input.path) || /\.d\.[cm]?ts$/.test(input.path)) { return; } const output = []; let contents = await input.getContents(); if (options.declaration && !input.srcPath?.match(/\.d\.[cm]?ts$/)) { const cm = input.srcPath?.match(/(?<=\.)(?:c|m)(?=[jt]s$)/)?.[0] || ""; const extension2 = `.d.${cm}ts`; output.push({ contents, srcPath: input.srcPath, path: input.path, extension: extension2, declaration: true }); } if (TS_EXTS.has(input.extension)) { contents = await transform(await context.compiler.compile(context, input.path, contents), { ...Object.fromEntries(Object.entries(options.esbuild ?? {}).filter(([key]) => key !== "absPaths")), loader: "ts" }).then((r) => r.code); } else if ([ ".tsx", ".jsx" ].includes(input.extension)) { contents = await transform(await context.compiler.compile(context, input.path, contents), { loader: input.extension === ".tsx" ? "tsx" : "jsx", ...Object.fromEntries(Object.entries(options.esbuild ?? {}).filter(([key]) => key !== "absPaths")) }).then((r) => r.code); } const isCjs = options.format === "cjs"; if (isCjs) { contents = context.resolver.transform({ source: contents, retainLines: false }).replace(/^exports.default = /gm, "module.exports = ").replace(/^var _default = exports.default = /gm, "module.exports = ").replace("module.exports = void 0;", ""); } let extension = isCjs ? ".js" : ".mjs"; if (options.ext) { extension = options.ext.startsWith(".") ? options.ext : `.${options.ext}`; } output.push({ contents, path: input.path, extension }); return output; }; }, "getUnbuildLoader"); export { getUnbuildLoader };