UNPKG

@sapphire/docusaurus-plugin-ts2esm2cjs

Version:

Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code

189 lines (186 loc) 6.39 kB
import { format } from '@prettier/sync'; import { runTransform } from 'esm-to-cjs'; import ts from 'typescript'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var makeTsCompilerOptions = /* @__PURE__ */ __name((overrideOptions) => ({ newLine: ts.NewLineKind.LineFeed, removeComments: false, esModuleInterop: true, pretty: true, ...overrideOptions, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.NodeJs, target: ts.ScriptTarget.ESNext }), "makeTsCompilerOptions"); var documentationPrettierConfig = { endOfLine: "lf", quoteProps: "as-needed", semi: true, singleQuote: true, trailingComma: "none", tabWidth: 2, useTabs: false, printWidth: 120, parser: "babel" }; var esmToCjs = /* @__PURE__ */ __name((code) => runTransform(code, { quote: "single", lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 }), "esmToCjs"); var escapeNewLines = /* @__PURE__ */ __name((code) => code.replace(/\n\n/g, "\n/* :newline: */"), "escapeNewLines"); var restoreNewLines = /* @__PURE__ */ __name((code) => code.replace(/\/\* :newline: \*\//g, "\n"), "restoreNewLines"); var prettierFormatCode = /* @__PURE__ */ __name((code, prettierConfig) => format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1), "prettierFormatCode"); var tsToEsm = /* @__PURE__ */ __name((code, options) => ts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) }), "tsToEsm"); function ts2esm(input, options) { const tsCode = escapeNewLines(input); const esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText; return prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions); } __name(ts2esm, "ts2esm"); function esm2cjs(input, options) { const cjsCode = esmToCjs(input); return prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions); } __name(esm2cjs, "esm2cjs"); // src/index.ts function createAttribute(attributeName, attributeValue) { return { type: "mdxJsxAttribute", name: attributeName, value: attributeValue }; } __name(createAttribute, "createAttribute"); function createTabItem({ code, node, value, label }) { let [, jsHighlight, tsHighlight] = (node.meta ?? "").split("|"); if (!tsHighlight && jsHighlight) { tsHighlight = jsHighlight; } return { type: "mdxJsxFlowElement", name: "TabItem", attributes: [createAttribute("value", value), createAttribute("label", label)], children: [ { type: node.type, lang: node.lang, value: code, meta: value === "typescript" ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers` } ] }; } __name(createTabItem, "createTabItem"); var transformNode = /* @__PURE__ */ __name((node, options) => { const groupIdProp = { type: "mdxJsxAttribute", name: "groupId", value: "ts2esm2cjs" }; const esmCode = ts2esm(node.value, options); const cjsCode = esm2cjs(esmCode, options); return [ { type: "mdxJsxFlowElement", name: "Tabs", ...options.sync && { attributes: [groupIdProp] }, children: [ createTabItem({ code: cjsCode, node, value: "cjs", label: "CommonJS" }), createTabItem({ code: esmCode, node, value: "esm", label: "ESM" }), createTabItem({ code: node.value, node, value: "typescript", label: "TypeScript" }) ] } ]; }, "transformNode"); var isMdxEsmLiteral = /* @__PURE__ */ __name((node) => node.type === "mdxjsEsm", "isMdxEsmLiteral"); var isTabsImport = /* @__PURE__ */ __name((node) => isMdxEsmLiteral(node) && node.value.includes("@theme/Tabs"), "isTabsImport"); var isParent = /* @__PURE__ */ __name((node) => Array.isArray(node.children), "isParent"); var matchNode = /* @__PURE__ */ __name((node) => node.type === "code" && typeof node.meta === "string" && (node.meta ?? "").startsWith("ts2esm2cjs"), "matchNode"); function createImportNode() { return { type: "mdxjsEsm", value: "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'", data: { estree: { type: "Program", body: [ { type: "ImportDeclaration", specifiers: [ { type: "ImportDefaultSpecifier", local: { type: "Identifier", name: "Tabs" } } ], source: { type: "Literal", value: "@theme/Tabs", raw: "'@theme/Tabs'" } }, { type: "ImportDeclaration", specifiers: [ { type: "ImportDefaultSpecifier", local: { type: "Identifier", name: "TabItem" } } ], source: { type: "Literal", value: "@theme/TabItem", raw: "'@theme/TabItem'" } } ], sourceType: "module" } } }; } __name(createImportNode, "createImportNode"); var ts2esm2cjs = /* @__PURE__ */ __name(({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }) => async (root) => { const { visit } = await import('unist-util-visit'); let transformed = false; let alreadyImported = false; visit(root, (node) => { if (isTabsImport(node)) { alreadyImported = true; } if (isParent(node)) { let index = 0; while (index < node.children.length) { const child = node.children[index]; if (matchNode(child)) { const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions }); node.children.splice(index, 1, ...result); index += result.length; transformed = true; } else { index += 1; } } } }); if (transformed && !alreadyImported) { root.children.unshift(createImportNode()); } }, "ts2esm2cjs"); export { esm2cjs, ts2esm, ts2esm2cjs }; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.mjs.map