@sapphire/docusaurus-plugin-ts2esm2cjs 2.0.3-next.ecc3f23.0 → 2.0.3-next.fd6fb41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -193,5 +193,5 @@ var ts2esm2cjs = /* @__PURE__ */ __name(({ sync = true, prettierOptions = {}, ty
193
193
  exports.esm2cjs = esm2cjs;
194
194
  exports.ts2esm = ts2esm;
195
195
  exports.ts2esm2cjs = ts2esm2cjs;
196
- //# sourceMappingURL=out.js.map
196
+ //# sourceMappingURL=index.cjs.map
197
197
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/ts2esm2cjs.ts","../../src/index.ts"],"names":[],"mappings":";;;;AAAA,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAE7B,OAAO,QAAkC;AAGzC,IAAM,wBAAwB,wBAAC,qBAAwD;AAAA,EACtF,SAAS,GAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,EACR,GAAG;AAAA,EACH,QAAQ,GAAG,WAAW;AAAA,EACtB,kBAAkB,GAAG,qBAAqB;AAAA,EAC1C,QAAQ,GAAG,aAAa;AACzB,IAT8B;AAW9B,IAAM,8BAAuC;AAAA,EAC5C,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,aAAa;AAAA,EACb,eAAe;AAAA,EACf,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,wBAAC,SAAyB,aAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC,GAA7H;AAOjB,IAAM,iBAAiB,wBAAC,SAAiB,KAAK,QAAQ,SAAS,mBAAmB,GAA3D;AAOvB,IAAM,kBAAkB,wBAAC,SAAyB,KAAK,QAAQ,wBAAwB,IAAI,GAAnE;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AQ,iBAAiB,CAAC;AAAA,EACrD;AACD,GAjCA","sourcesContent":["import { format } from '@prettier/sync';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Options } from 'prettier';\nimport ts, { type CompilerOptions } from 'typescript';\nimport type { PluginOptions } from './types';\n\nconst makeTsCompilerOptions = (overrideOptions?: CompilerOptions): CompilerOptions => ({\n\tnewLine: ts.NewLineKind.LineFeed,\n\tremoveComments: false,\n\tesModuleInterop: true,\n\tpretty: true,\n\t...overrideOptions,\n\tmodule: ts.ModuleKind.ESNext,\n\tmoduleResolution: ts.ModuleResolutionKind.NodeJs,\n\ttarget: ts.ScriptTarget.ESNext\n});\n\nconst documentationPrettierConfig: Options = {\n\tendOfLine: 'lf',\n\tquoteProps: 'as-needed',\n\tsemi: true,\n\tsingleQuote: true,\n\ttrailingComma: 'none',\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\n/**\n * Transforms input ESM code to CJS code.\n * @param code The code to transform\n * @returns Input code transformed to CommonJS\n */\nconst esmToCjs = (code: string): string => runTransform(code, { quote: 'single', lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n * @param code The code to escape new lines in\n * @returns The same code but with new lines escaped using block comments\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */');\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n * @param code The code with escaped new lines\n * @returns The same code with new lines restored\n */\nconst restoreNewLines = (code: string): string => code.replace(/\\/\\* :newline: \\*\\//g, '\\n');\n\n/**\n * Formats the code using Prettier\n * @param code The code to prettier format\n * @param prettierConfig Additional prettier options to use for formatting\n * @returns Prettier formatted code\n */\nconst prettierFormatCode = (code: string, prettierConfig?: Options) =>\n\tformat(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);\n\n/**\n * Transpiles input TypeScript code to ESM code.\n * @param code The code to transpile\n * @returns Input code transpiled to ESM\n */\nconst tsToEsm = (code: string, options: Pick<PluginOptions, 'typescriptCompilerOptions'>): ts.TranspileOutput =>\n\tts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });\n\n/**\n * Transforms input TypeScript code to ESM code.\n * @param input The TypeScript code to transform\n * @param options The plugin options\n * @returns The transformed code (ESM)\n */\nexport function ts2esm(input: string, options: PluginOptions) {\n\tconst tsCode = escapeNewLines(input);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\n\treturn prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions);\n}\n\nexport function esm2cjs(input: string, options: PluginOptions) {\n\tconst cjsCode = esmToCjs(input);\n\n\treturn prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions);\n}\n","import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\n\n/**\n * Transforms a Docusaurus node from TypeScript to ESM and CJS\n * @param node The Docusaurus node to transform\n * @param options The plugin options to pass to the transformer\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: Code, options: PluginOptions) => {\n\tconst groupIdProp = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\nconst isParent = (node: Node): node is Parent => Array.isArray((node as Parent).children);\nconst matchNode = (node: Node): node is Code =>\n\tnode.type === 'code' && typeof (node as Code).meta === 'string' && ((node as Code).meta ?? '').startsWith('ts2esm2cjs');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\n\t\t\tif (isParent(node)) {\n\t\t\t\tlet index = 0;\n\t\t\t\twhile (index < node.children.length) {\n\t\t\t\t\tconst child = node.children[index]!;\n\t\t\t\t\tif (matchNode(child)) {\n\t\t\t\t\t\tconst result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\t\tindex += result.length;\n\t\t\t\t\t\ttransformed = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tindex += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(createImportNode());\n\t\t}\n\t};\n"]}
1
+ {"version":3,"sources":["../../src/ts2esm2cjs.ts","../../src/index.ts"],"names":["ts","runTransform","format"],"mappings":";;;;;;;;;;;;AAMA,IAAM,qBAAA,2BAAyB,eAAwD,MAAA;AAAA,EACtF,OAAA,EAASA,oBAAG,WAAY,CAAA,QAAA;AAAA,EACxB,cAAgB,EAAA,KAAA;AAAA,EAChB,eAAiB,EAAA,IAAA;AAAA,EACjB,MAAQ,EAAA,IAAA;AAAA,EACR,GAAG,eAAA;AAAA,EACH,MAAA,EAAQA,oBAAG,UAAW,CAAA,MAAA;AAAA,EACtB,gBAAA,EAAkBA,oBAAG,oBAAqB,CAAA,MAAA;AAAA,EAC1C,MAAA,EAAQA,oBAAG,YAAa,CAAA,MAAA;AACzB,CAT8B,CAAA,EAAA,uBAAA,CAAA,CAAA;AAW9B,IAAM,2BAAuC,GAAA;AAAA,EAC5C,SAAW,EAAA,IAAA;AAAA,EACX,UAAY,EAAA,WAAA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Q,IAAK,CAAA,QAAA,CAAS,KAAK,CAAA,CAAA;AACjC,QAAI,IAAA,SAAA,CAAU,KAAK,CAAG,EAAA;AACrB,UAAA,MAAM,SAAS,aAAc,CAAA,KAAA,EAAO,EAAE,IAAM,EAAA,eAAA,EAAiB,2BAA2B,CAAA,CAAA;AACxF,UAAA,IAAA,CAAK,QAAS,CAAA,MAAA,CAAO,KAAO,EAAA,CAAA,EAAG,GAAG,MAAM,CAAA,CAAA;AACxC,UAAA,KAAA,IAAS,MAAO,CAAA,MAAA,CAAA;AAChB,UAAc,WAAA,GAAA,IAAA,CAAA;AAAA,SACR,MAAA;AACN,UAAS,KAAA,IAAA,CAAA,CAAA;AAAA,SACV;AAAA,OACD;AAAA,KACD;AAAA,GACA,CAAA,CAAA;AAED,EAAI,IAAA,WAAA,IAAe,CAAC,eAAiB,EAAA;AACpC,IAAC,IAAgB,CAAA,QAAA,CAAS,OAAQ,CAAA,gBAAA,EAAkB,CAAA,CAAA;AAAA,GACrD;AACD,CAjCA,EAAA,YAAA","file":"index.cjs","sourcesContent":["import { format } from '@prettier/sync';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Options } from 'prettier';\nimport ts, { type CompilerOptions } from 'typescript';\nimport type { PluginOptions } from './types';\n\nconst makeTsCompilerOptions = (overrideOptions?: CompilerOptions): CompilerOptions => ({\n\tnewLine: ts.NewLineKind.LineFeed,\n\tremoveComments: false,\n\tesModuleInterop: true,\n\tpretty: true,\n\t...overrideOptions,\n\tmodule: ts.ModuleKind.ESNext,\n\tmoduleResolution: ts.ModuleResolutionKind.NodeJs,\n\ttarget: ts.ScriptTarget.ESNext\n});\n\nconst documentationPrettierConfig: Options = {\n\tendOfLine: 'lf',\n\tquoteProps: 'as-needed',\n\tsemi: true,\n\tsingleQuote: true,\n\ttrailingComma: 'none',\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\n/**\n * Transforms input ESM code to CJS code.\n * @param code The code to transform\n * @returns Input code transformed to CommonJS\n */\nconst esmToCjs = (code: string): string => runTransform(code, { quote: 'single', lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n * @param code The code to escape new lines in\n * @returns The same code but with new lines escaped using block comments\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */');\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n * @param code The code with escaped new lines\n * @returns The same code with new lines restored\n */\nconst restoreNewLines = (code: string): string => code.replace(/\\/\\* :newline: \\*\\//g, '\\n');\n\n/**\n * Formats the code using Prettier\n * @param code The code to prettier format\n * @param prettierConfig Additional prettier options to use for formatting\n * @returns Prettier formatted code\n */\nconst prettierFormatCode = (code: string, prettierConfig?: Options) =>\n\tformat(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);\n\n/**\n * Transpiles input TypeScript code to ESM code.\n * @param code The code to transpile\n * @returns Input code transpiled to ESM\n */\nconst tsToEsm = (code: string, options: Pick<PluginOptions, 'typescriptCompilerOptions'>): ts.TranspileOutput =>\n\tts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });\n\n/**\n * Transforms input TypeScript code to ESM code.\n * @param input The TypeScript code to transform\n * @param options The plugin options\n * @returns The transformed code (ESM)\n */\nexport function ts2esm(input: string, options: PluginOptions) {\n\tconst tsCode = escapeNewLines(input);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\n\treturn prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions);\n}\n\nexport function esm2cjs(input: string, options: PluginOptions) {\n\tconst cjsCode = esmToCjs(input);\n\n\treturn prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions);\n}\n","import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\n\n/**\n * Transforms a Docusaurus node from TypeScript to ESM and CJS\n * @param node The Docusaurus node to transform\n * @param options The plugin options to pass to the transformer\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: Code, options: PluginOptions) => {\n\tconst groupIdProp = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\nconst isParent = (node: Node): node is Parent => Array.isArray((node as Parent).children);\nconst matchNode = (node: Node): node is Code =>\n\tnode.type === 'code' && typeof (node as Code).meta === 'string' && ((node as Code).meta ?? '').startsWith('ts2esm2cjs');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\n\t\t\tif (isParent(node)) {\n\t\t\t\tlet index = 0;\n\t\t\t\twhile (index < node.children.length) {\n\t\t\t\t\tconst child = node.children[index]!;\n\t\t\t\t\tif (matchNode(child)) {\n\t\t\t\t\t\tconst result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\t\tindex += result.length;\n\t\t\t\t\t\ttransformed = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tindex += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(createImportNode());\n\t\t}\n\t};\n"]}
@@ -185,5 +185,5 @@ var ts2esm2cjs = /* @__PURE__ */ __name(({ sync = true, prettierOptions = {}, ty
185
185
  }, "ts2esm2cjs");
186
186
 
187
187
  export { esm2cjs, ts2esm, ts2esm2cjs };
188
- //# sourceMappingURL=out.js.map
188
+ //# sourceMappingURL=index.mjs.map
189
189
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/ts2esm2cjs.ts","../../src/index.ts"],"names":[],"mappings":";;;;AAAA,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAE7B,OAAO,QAAkC;AAGzC,IAAM,wBAAwB,wBAAC,qBAAwD;AAAA,EACtF,SAAS,GAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,EACR,GAAG;AAAA,EACH,QAAQ,GAAG,WAAW;AAAA,EACtB,kBAAkB,GAAG,qBAAqB;AAAA,EAC1C,QAAQ,GAAG,aAAa;AACzB,IAT8B;AAW9B,IAAM,8BAAuC;AAAA,EAC5C,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,aAAa;AAAA,EACb,eAAe;AAAA,EACf,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,wBAAC,SAAyB,aAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC,GAA7H;AAOjB,IAAM,iBAAiB,wBAAC,SAAiB,KAAK,QAAQ,SAAS,mBAAmB,GAA3D;AAOvB,IAAM,kBAAkB,wBAAC,SAAyB,KAAK,QAAQ,wBAAwB,IAAI,GAAnE;AAQxB,IAAM,qBAAqB,wBAAC,MAAc,mBACzC,OAAO,MAAM,EAAE,GAAG,6BAA6B,GAAG,eAAe,CAAC,EAAE,MAAM,GAAG,EAAE,GADrD;AAQ3B,IAAM,UAAU,wBAAC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AAAA,YACC,MAAM;AAAA,YACN,YAAY;AAAA,cACX;AAAA,gBACC,MAAM;AAAA,gBACN,OAAO,EAAE,MAAM,cAAc,MAAM,UAAU;AAAA,cAC9C;AAAA,YACD;AAAA,YACA,QAAQ;AAAA,cACP,MAAM;AAAA,cACN,OAAO;AAAA,cACP,KAAK;AAAA,YACN;AAAA,UACD;AAAA,QACD;AAAA,QACA,YAAY;AAAA,MACb;AAAA,IACD;AAAA,EACD;AACD;AAzCS;AA2CF,IAAM,aACZ,wBACC,EAAE,OAAO,MAAM,kBAAkB,CAAC,GAAG,4BAA4B,CAAC,EAAE,IAAI,EAAE,MAAM,MAAM,iBAAiB,CAAC,GAAG,2BAA2B,CAAC,EAAE,MAE1I,OAAO,SAAS;AACf,QAAM,EAAE,MAAM,IAAI,MAAM,OAAO,kBAAkB;AAEjD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,MAAM,CAAC,SAAe;AAC3B,QAAI,aAAa,IAAI,GAAG;AACvB,wBAAkB;AAAA,IACnB;AAEA,QAAI,SAAS,IAAI,GAAG;AACnB,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,QAAQ,KAAK,SAAS,KAAK;AACjC,YAAI,UAAU,KAAK,GAAG;AACrB,gBAAM,SAAS,cAAc,OAAO,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AACxF,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,mBAAS,OAAO;AAChB,wBAAc;AAAA,QACf,OAAO;AACN,mBAAS;AAAA,QACV;AAAA,MACD;AAAA,IACD;AAAA,EACD,CAAC;AAED,MAAI,eAAe,CAAC,iBAAiB;AACpC,IAAC,KAAgB,SAAS,QAAQ,iBAAiB,CAAC;AAAA,EACrD;AACD,GAjCA","sourcesContent":["import { format } from '@prettier/sync';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Options } from 'prettier';\nimport ts, { type CompilerOptions } from 'typescript';\nimport type { PluginOptions } from './types';\n\nconst makeTsCompilerOptions = (overrideOptions?: CompilerOptions): CompilerOptions => ({\n\tnewLine: ts.NewLineKind.LineFeed,\n\tremoveComments: false,\n\tesModuleInterop: true,\n\tpretty: true,\n\t...overrideOptions,\n\tmodule: ts.ModuleKind.ESNext,\n\tmoduleResolution: ts.ModuleResolutionKind.NodeJs,\n\ttarget: ts.ScriptTarget.ESNext\n});\n\nconst documentationPrettierConfig: Options = {\n\tendOfLine: 'lf',\n\tquoteProps: 'as-needed',\n\tsemi: true,\n\tsingleQuote: true,\n\ttrailingComma: 'none',\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\n/**\n * Transforms input ESM code to CJS code.\n * @param code The code to transform\n * @returns Input code transformed to CommonJS\n */\nconst esmToCjs = (code: string): string => runTransform(code, { quote: 'single', lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n * @param code The code to escape new lines in\n * @returns The same code but with new lines escaped using block comments\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */');\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n * @param code The code with escaped new lines\n * @returns The same code with new lines restored\n */\nconst restoreNewLines = (code: string): string => code.replace(/\\/\\* :newline: \\*\\//g, '\\n');\n\n/**\n * Formats the code using Prettier\n * @param code The code to prettier format\n * @param prettierConfig Additional prettier options to use for formatting\n * @returns Prettier formatted code\n */\nconst prettierFormatCode = (code: string, prettierConfig?: Options) =>\n\tformat(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);\n\n/**\n * Transpiles input TypeScript code to ESM code.\n * @param code The code to transpile\n * @returns Input code transpiled to ESM\n */\nconst tsToEsm = (code: string, options: Pick<PluginOptions, 'typescriptCompilerOptions'>): ts.TranspileOutput =>\n\tts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });\n\n/**\n * Transforms input TypeScript code to ESM code.\n * @param input The TypeScript code to transform\n * @param options The plugin options\n * @returns The transformed code (ESM)\n */\nexport function ts2esm(input: string, options: PluginOptions) {\n\tconst tsCode = escapeNewLines(input);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\n\treturn prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions);\n}\n\nexport function esm2cjs(input: string, options: PluginOptions) {\n\tconst cjsCode = esmToCjs(input);\n\n\treturn prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions);\n}\n","import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\n\n/**\n * Transforms a Docusaurus node from TypeScript to ESM and CJS\n * @param node The Docusaurus node to transform\n * @param options The plugin options to pass to the transformer\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: Code, options: PluginOptions) => {\n\tconst groupIdProp = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\nconst isParent = (node: Node): node is Parent => Array.isArray((node as Parent).children);\nconst matchNode = (node: Node): node is Code =>\n\tnode.type === 'code' && typeof (node as Code).meta === 'string' && ((node as Code).meta ?? '').startsWith('ts2esm2cjs');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\n\t\t\tif (isParent(node)) {\n\t\t\t\tlet index = 0;\n\t\t\t\twhile (index < node.children.length) {\n\t\t\t\t\tconst child = node.children[index]!;\n\t\t\t\t\tif (matchNode(child)) {\n\t\t\t\t\t\tconst result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\t\tindex += result.length;\n\t\t\t\t\t\ttransformed = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tindex += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(createImportNode());\n\t\t}\n\t};\n"]}
1
+ {"version":3,"sources":["../../src/ts2esm2cjs.ts","../../src/index.ts"],"names":[],"mappings":";;;;;;AAMA,IAAM,qBAAA,2BAAyB,eAAwD,MAAA;AAAA,EACtF,OAAA,EAAS,GAAG,WAAY,CAAA,QAAA;AAAA,EACxB,cAAgB,EAAA,KAAA;AAAA,EAChB,eAAiB,EAAA,IAAA;AAAA,EACjB,MAAQ,EAAA,IAAA;AAAA,EACR,GAAG,eAAA;AAAA,EACH,MAAA,EAAQ,GAAG,UAAW,CAAA,MAAA;AAAA,EACtB,gBAAA,EAAkB,GAAG,oBAAqB,CAAA,MAAA;AAAA,EAC1C,MAAA,EAAQ,GAAG,YAAa,CAAA,MAAA;AACzB,CAT8B,CAAA,EAAA,uBAAA,CAAA,CAAA;AAW9B,IAAM,2BAAuC,GAAA;AAAA,EAC5C,SAAW,EAAA,IAAA;AAAA,EACX,UAAY,EAAA,WAAA;AAAA,EACZ,IAAM,EAAA,IAAA;AAAA,EACN,WAAa,EAAA,IAAA;AAAA,EACb,aAAe,EAAA,MAAA;AAAA,EACf,QAAU,EAAA,CAAA;AAAA,EACV,OAAS,EAAA,KAAA;AAAA,EACT,UAAY,EAAA,GAAA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,CAAA,CAAA;AACxF,UAAA,IAAA,CAAK,QAAS,CAAA,MAAA,CAAO,KAAO,EAAA,CAAA,EAAG,GAAG,MAAM,CAAA,CAAA;AACxC,UAAA,KAAA,IAAS,MAAO,CAAA,MAAA,CAAA;AAChB,UAAc,WAAA,GAAA,IAAA,CAAA;AAAA,SACR,MAAA;AACN,UAAS,KAAA,IAAA,CAAA,CAAA;AAAA,SACV;AAAA,OACD;AAAA,KACD;AAAA,GACA,CAAA,CAAA;AAED,EAAI,IAAA,WAAA,IAAe,CAAC,eAAiB,EAAA;AACpC,IAAC,IAAgB,CAAA,QAAA,CAAS,OAAQ,CAAA,gBAAA,EAAkB,CAAA,CAAA;AAAA,GACrD;AACD,CAjCA,EAAA,YAAA","file":"index.mjs","sourcesContent":["import { format } from '@prettier/sync';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Options } from 'prettier';\nimport ts, { type CompilerOptions } from 'typescript';\nimport type { PluginOptions } from './types';\n\nconst makeTsCompilerOptions = (overrideOptions?: CompilerOptions): CompilerOptions => ({\n\tnewLine: ts.NewLineKind.LineFeed,\n\tremoveComments: false,\n\tesModuleInterop: true,\n\tpretty: true,\n\t...overrideOptions,\n\tmodule: ts.ModuleKind.ESNext,\n\tmoduleResolution: ts.ModuleResolutionKind.NodeJs,\n\ttarget: ts.ScriptTarget.ESNext\n});\n\nconst documentationPrettierConfig: Options = {\n\tendOfLine: 'lf',\n\tquoteProps: 'as-needed',\n\tsemi: true,\n\tsingleQuote: true,\n\ttrailingComma: 'none',\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\n/**\n * Transforms input ESM code to CJS code.\n * @param code The code to transform\n * @returns Input code transformed to CommonJS\n */\nconst esmToCjs = (code: string): string => runTransform(code, { quote: 'single', lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n * @param code The code to escape new lines in\n * @returns The same code but with new lines escaped using block comments\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */');\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n * @param code The code with escaped new lines\n * @returns The same code with new lines restored\n */\nconst restoreNewLines = (code: string): string => code.replace(/\\/\\* :newline: \\*\\//g, '\\n');\n\n/**\n * Formats the code using Prettier\n * @param code The code to prettier format\n * @param prettierConfig Additional prettier options to use for formatting\n * @returns Prettier formatted code\n */\nconst prettierFormatCode = (code: string, prettierConfig?: Options) =>\n\tformat(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);\n\n/**\n * Transpiles input TypeScript code to ESM code.\n * @param code The code to transpile\n * @returns Input code transpiled to ESM\n */\nconst tsToEsm = (code: string, options: Pick<PluginOptions, 'typescriptCompilerOptions'>): ts.TranspileOutput =>\n\tts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });\n\n/**\n * Transforms input TypeScript code to ESM code.\n * @param input The TypeScript code to transform\n * @param options The plugin options\n * @returns The transformed code (ESM)\n */\nexport function ts2esm(input: string, options: PluginOptions) {\n\tconst tsCode = escapeNewLines(input);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\n\treturn prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions);\n}\n\nexport function esm2cjs(input: string, options: PluginOptions) {\n\tconst cjsCode = esmToCjs(input);\n\n\treturn prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions);\n}\n","import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\n\n/**\n * Transforms a Docusaurus node from TypeScript to ESM and CJS\n * @param node The Docusaurus node to transform\n * @param options The plugin options to pass to the transformer\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: Code, options: PluginOptions) => {\n\tconst groupIdProp = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\nconst isParent = (node: Node): node is Parent => Array.isArray((node as Parent).children);\nconst matchNode = (node: Node): node is Code =>\n\tnode.type === 'code' && typeof (node as Code).meta === 'string' && ((node as Code).meta ?? '').startsWith('ts2esm2cjs');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\n\t\t\tif (isParent(node)) {\n\t\t\t\tlet index = 0;\n\t\t\t\twhile (index < node.children.length) {\n\t\t\t\t\tconst child = node.children[index]!;\n\t\t\t\t\tif (matchNode(child)) {\n\t\t\t\t\t\tconst result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\t\tindex += result.length;\n\t\t\t\t\t\ttransformed = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tindex += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(createImportNode());\n\t\t}\n\t};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
3
- "version": "2.0.3-next.ecc3f23.0",
3
+ "version": "2.0.3-next.fd6fb41",
4
4
  "description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -29,11 +29,11 @@
29
29
  "test": "vitest run"
30
30
  },
31
31
  "dependencies": {
32
- "@prettier/sync": "^0.5.0",
32
+ "@prettier/sync": "^0.5.2",
33
33
  "esm-to-cjs": "^1.2.1",
34
34
  "mdast-util-mdx": "^3.0.0",
35
- "typescript": "^5.3.3",
36
- "unified": "^11.0.4",
35
+ "typescript": "~5.4.5",
36
+ "unified": "^11.0.5",
37
37
  "unist-util-visit": "^5.0.0"
38
38
  },
39
39
  "repository": {
@@ -68,9 +68,9 @@
68
68
  "access": "public"
69
69
  },
70
70
  "devDependencies": {
71
- "@favware/cliff-jumper": "^2.2.3",
72
- "@sapphire/framework": "^5.0.7",
73
- "tsup": "^8.0.1",
74
- "vitest": "^1.2.2"
71
+ "@favware/cliff-jumper": "^4.0.3",
72
+ "@sapphire/framework": "^5.2.1",
73
+ "tsup": "^8.2.4",
74
+ "vitest": "^2.0.5"
75
75
  }
76
76
  }