@sapphire/docusaurus-plugin-ts2esm2cjs
Version:
Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code
1 lines • 12.3 kB
Source Map (JSON)
{"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,MAAc,YAC9B,GAAG,gBAAgB,MAAM,EAAE,mBAAmB,OAAO,iBAAiB,sBAAsB,QAAQ,yBAAyB,EAAE,CAAC,GADjH;AAST,SAAS,OAAO,OAAe,SAAwB;AAC7D,QAAM,SAAS,eAAe,KAA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AAM;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"]}