@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.4-next.f301c2b.0 → 1.1.4-next.f603749.0

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <div align="center">
2
2
 
3
- ![Sapphire Logo](https://cdn.skyra.pw/gh-assets/sapphire-banner.png)
3
+ ![Sapphire Logo](https://raw.githubusercontent.com/sapphiredev/assets/main/banners/SapphireCommunity.png)
4
4
 
5
5
  # @sapphire/docusaurus-plugin-ts2esm2cjs
6
6
 
@@ -0,0 +1,22 @@
1
+ import { Plugin } from 'unified';
2
+ import { Options } from 'prettier';
3
+ import { CompilerOptions } from 'typescript';
4
+
5
+ interface PluginOptions {
6
+ sync?: boolean;
7
+ prettierOptions?: Options;
8
+ typescriptCompilerOptions?: CompilerOptions;
9
+ }
10
+
11
+ /**
12
+ * Transforms input TypeScript code to ESM code.
13
+ * @param input The TypeScript code to transform
14
+ * @param options The plugin options
15
+ * @returns The transformed code (ESM)
16
+ */
17
+ declare function ts2esm(input: string, options: PluginOptions): string;
18
+ declare function esm2cjs(input: string, options: PluginOptions): string;
19
+
20
+ declare const ts2esm2cjs: Plugin<[PluginOptions?]>;
21
+
22
+ export { PluginOptions, esm2cjs, ts2esm, ts2esm2cjs };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/ts2esm2cjs.ts"],"sourcesContent":["import type { Code, Content, Literal } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport { PluginOptions } from './types';\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 = options.sync ? ' groupId=\"ts2esm2cjs\"' : '';\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn [\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: `<Tabs${groupIdProp}\n\t\t\t\t\t\tdefaultValue=\"typescript\"\n\t\t\t\t\t\tvalues={[\n\t\t\t\t\t\t\t{ label: \"JavaScript\", value: \"javascript\" },\n\t\t\t\t\t\t\t{ label: \"ESM\", value: \"esm\" },\n\t\t\t\t\t\t\t{ label: \"TypeScript\", value: \"typescript\" },\n\t\t\t\t\t\t]}\n\t\t\t>\\n<TabItem value=\"javascript\">`\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: cjsCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"esm\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: esmCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"typescript\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${tsHighlight} showLineNumbers`,\n\t\t\tvalue: node.value\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n</Tabs>'\n\t\t}\n\t] as Content[];\n};\n\nconst isImport = (node: Node): node is Literal => node.type === 'import';\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');\nconst nodeForImport: Literal = {\n\ttype: 'import',\n\tvalue: \"import Tabs from '@theme/Tabs';\\nimport TabItem from '@theme/TabItem';\"\n};\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> = (\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n) => {\n\treturn (root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isImport(node) && node.value.includes('@theme/Tabs')) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\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\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n};\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport prettier, { Options } from 'prettier';\nimport ts, { 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\t...sapphirePrettierConfig,\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\tprettier.format(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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAAkB;;;ACHlB,6BAAmC;AACnC,wBAA6B;AAC7B,sBAAkC;AAClC,wBAAoC;AAGpC,IAAM,wBAAwB,CAAC,qBAAwD;AAAA,EACtF,SAAS,kBAAAA,QAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,EACR,GAAG;AAAA,EACH,QAAQ,kBAAAA,QAAG,WAAW;AAAA,EACtB,kBAAkB,kBAAAA,QAAG,qBAAqB;AAAA,EAC1C,QAAQ,kBAAAA,QAAG,aAAa;AACzB;AAEA,IAAM,8BAAuC;AAAA,EAC5C,GAAG,uBAAAC;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,CAAC,aAAyB,gCAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC;AAO9I,IAAM,iBAAiB,CAAC,SAAiB,KAAK,QAAQ,SAAS,mBAAmB;AAOlF,IAAM,kBAAkB,CAAC,SAAyB,KAAK,QAAQ,wBAAwB,IAAI;AAQ3F,IAAM,qBAAqB,CAAC,MAAc,mBACzC,gBAAAC,QAAS,OAAO,MAAM,EAAE,GAAG,6BAA6B,GAAG,eAAe,CAAC,EAAE,MAAM,GAAG,EAAE;AAOzF,IAAM,UAAU,CAAC,MAAc,YAC9B,kBAAAF,QAAG,gBAAgB,MAAM,EAAE,mBAAmB,OAAO,iBAAiB,sBAAsB,QAAQ,yBAAyB,EAAE,CAAC;AAQ1H,SAAS,OAAO,OAAe,SAAwB;AAC7D,Q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CAAC,SAAgC,KAAK,SAAS;AAChE,IAAM,WAAW,CAAC,SAA+B,MAAM,QAAS,KAAgB,QAAQ;AACxF,IAAM,YAAY,CAAC,SAClB,KAAK,SAAS,UAAU,OAAQ,KAAc,SAAS,aAAc,KAAc,QAAQ,IAAI,WAAW,YAAY;AACvH,IAAM,gBAAyB;AAAA,EAC9B,MAAM;AAAA,EACN,OAAO;AACR;AAEO,IAAM,aAAuC,CACnD,EAAE,OAAO,MAAM,kBAAkB,CAAC,GAAG,4BAA4B,CAAC,EAAE,IAAI,EAAE,MAAM,MAAM,iBAAiB,CAAC,GAAG,2BAA2B,CAAC,EAAE,MACrI;AACJ,SAAO,CAAC,SAAS;AAChB,QAAI,cAAc;AAClB,QAAI,kBAAkB;AACtB,gCAAAG,SAAM,MAAM,CAAC,SAAe;AAC3B,UAAI,SAAS,IAAI,KAAK,KAAK,MAAM,SAAS,aAAa,GAAG;AACzD,0BAAkB;AAAA,MACnB;AACA,UAAI,SAAS,IAAI,GAAG;AACnB,YAAI,QAAQ;AACZ,eAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,gBAAM,QAAQ,KAAK,SAAS,KAAK;AACjC,cAAI,UAAU,KAAK,GAAG;AACrB,kBAAM,SAAS,cAAc,OAAO,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AACxF,iBAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,qBAAS,OAAO;AAChB,0BAAc;AAAA,UACf,OAAO;AACN,qBAAS;AAAA,UACV;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAC;AACD,QAAI,eAAe,CAAC,iBAAiB;AACpC,MAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,IAChD;AAAA,EACD;AACD;","names":["ts","sapphirePrettierConfig","prettier","visit"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/ts2esm2cjs.ts"],"sourcesContent":["import type { Code, Content, Literal } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\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 = options.sync ? ' groupId=\"ts2esm2cjs\"' : '';\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn [\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: `<Tabs${groupIdProp}\n\t\t\t\t\t\tdefaultValue=\"typescript\"\n\t\t\t\t\t\tvalues={[\n\t\t\t\t\t\t\t{ label: \"JavaScript\", value: \"javascript\" },\n\t\t\t\t\t\t\t{ label: \"ESM\", value: \"esm\" },\n\t\t\t\t\t\t\t{ label: \"TypeScript\", value: \"typescript\" },\n\t\t\t\t\t\t]}\n\t\t\t>\\n<TabItem value=\"javascript\">`\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: cjsCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"esm\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: esmCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"typescript\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${tsHighlight} showLineNumbers`,\n\t\t\tvalue: node.value\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n</Tabs>'\n\t\t}\n\t] as Content[];\n};\n\nconst isImport = (node: Node): node is Literal => node.type === 'import';\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');\nconst nodeForImport: Literal = {\n\ttype: 'import',\n\tvalue: \"import Tabs from '@theme/Tabs';\\nimport TabItem from '@theme/TabItem';\"\n};\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> = (\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n) => {\n\treturn (root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isImport(node) && node.value.includes('@theme/Tabs')) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\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\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n};\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport prettier, { 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\t...sapphirePrettierConfig,\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\tprettier.format(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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAAkB;;;ACHlB,6BAAmC;AACnC,wBAA6B;AAC7B,sBAAuC;AACvC,wBAAyC;AAGzC,IAAM,wBAAwB,CAAC,qBAAwD;AAAA,EACtF,SAAS,kBAAAA,QAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,EACR,GAAG;AAAA,EACH,QAAQ,kBAAAA,QAAG,WAAW;AAAA,EACtB,kBAAkB,kBAAAA,QAAG,qBAAqB;AAAA,EAC1C,QAAQ,kBAAAA,QAAG,aAAa;AACzB;AAEA,IAAM,8BAAuC;AAAA,EAC5C,GAAG,uBAAAC;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,CAAC,aAAyB,gCAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC;AAO9I,IAAM,iBAAiB,CAAC,SAAiB,KAAK,QAAQ,SAAS,mBAAmB;AAOlF,IAAM,kBAAkB,CAAC,SAAyB,KAAK,QAAQ,wBAAwB,IAAI;AAQ3F,IAAM,qBAAqB,CAAC,MAAc,mBACzC,gBAAAC,QAAS,OAAO,MAAM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ACD,CAAC;AACD,QAAI,eAAe,CAAC,iBAAiB;AACpC,MAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,IAChD;AAAA,EACD;AACD;","names":["ts","sapphirePrettierConfig","prettier","visit"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/ts2esm2cjs.ts"],"sourcesContent":["import type { Code, Content, Literal } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport { PluginOptions } from './types';\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 = options.sync ? ' groupId=\"ts2esm2cjs\"' : '';\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn [\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: `<Tabs${groupIdProp}\n\t\t\t\t\t\tdefaultValue=\"typescript\"\n\t\t\t\t\t\tvalues={[\n\t\t\t\t\t\t\t{ label: \"JavaScript\", value: \"javascript\" },\n\t\t\t\t\t\t\t{ label: \"ESM\", value: \"esm\" },\n\t\t\t\t\t\t\t{ label: \"TypeScript\", value: \"typescript\" },\n\t\t\t\t\t\t]}\n\t\t\t>\\n<TabItem value=\"javascript\">`\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: cjsCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"esm\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: esmCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"typescript\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${tsHighlight} showLineNumbers`,\n\t\t\tvalue: node.value\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n</Tabs>'\n\t\t}\n\t] as Content[];\n};\n\nconst isImport = (node: Node): node is Literal => node.type === 'import';\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');\nconst nodeForImport: Literal = {\n\ttype: 'import',\n\tvalue: \"import Tabs from '@theme/Tabs';\\nimport TabItem from '@theme/TabItem';\"\n};\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> = (\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n) => {\n\treturn (root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isImport(node) && node.value.includes('@theme/Tabs')) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\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\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n};\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport prettier, { Options } from 'prettier';\nimport ts, { 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\t...sapphirePrettierConfig,\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\tprettier.format(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"],"mappings":";AAGA,OAAO,WAAW;;;ACHlB,OAAO,4BAA4B;AACnC,SAAS,oBAAoB;AAC7B,OAAO,cAA2B;AAClC,OAAO,QAA6B;AAGpC,IAAM,wBAAwB,CAAC,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;AAEA,IAAM,8BAAuC;AAAA,EAC5C,GAAG;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,CAAC,SAAyB,aAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC;AAO9I,IAA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O,OAAO,GAAG,GAAG,MAAM;AACxC,qBAAS,OAAO;AAChB,0BAAc;AAAA,UACf,OAAO;AACN,qBAAS;AAAA,UACV;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAC;AACD,QAAI,eAAe,CAAC,iBAAiB;AACpC,MAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,IAChD;AAAA,EACD;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/ts2esm2cjs.ts"],"sourcesContent":["import type { Code, Content, Literal } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\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 = options.sync ? ' groupId=\"ts2esm2cjs\"' : '';\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn [\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: `<Tabs${groupIdProp}\n\t\t\t\t\t\tdefaultValue=\"typescript\"\n\t\t\t\t\t\tvalues={[\n\t\t\t\t\t\t\t{ label: \"JavaScript\", value: \"javascript\" },\n\t\t\t\t\t\t\t{ label: \"ESM\", value: \"esm\" },\n\t\t\t\t\t\t\t{ label: \"TypeScript\", value: \"typescript\" },\n\t\t\t\t\t\t]}\n\t\t\t>\\n<TabItem value=\"javascript\">`\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: cjsCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"esm\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${jsHighlight} showLineNumbers`,\n\t\t\tvalue: esmCode\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n<TabItem value=\"typescript\">'\n\t\t},\n\t\t{\n\t\t\ttype: node.type,\n\t\t\tlang: node.lang,\n\t\t\tmeta: `${tsHighlight} showLineNumbers`,\n\t\t\tvalue: node.value\n\t\t},\n\t\t{\n\t\t\ttype: 'jsx',\n\t\t\tvalue: '</TabItem>\\n</Tabs>'\n\t\t}\n\t] as Content[];\n};\n\nconst isImport = (node: Node): node is Literal => node.type === 'import';\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');\nconst nodeForImport: Literal = {\n\ttype: 'import',\n\tvalue: \"import Tabs from '@theme/Tabs';\\nimport TabItem from '@theme/TabItem';\"\n};\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> = (\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n) => {\n\treturn (root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isImport(node) && node.value.includes('@theme/Tabs')) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\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\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n};\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport prettier, { 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\t...sapphirePrettierConfig,\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\tprettier.format(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"],"mappings":";AAGA,OAAO,WAAW;;;ACHlB,OAAO,4BAA4B;AACnC,SAAS,oBAAoB;AAC7B,OAAO,cAAgC;AACvC,OAAO,QAAkC;AAGzC,IAAM,wBAAwB,CAAC,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;AAEA,IAAM,8BAAuC;AAAA,EAC5C,GAAG;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAOA,IAAM,WAAW,CAAC,S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;AACjC,cAAI,UAAU,KAAK,GAAG;AACrB,kBAAM,SAAS,cAAc,OAAO,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AACxF,iBAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,qBAAS,OAAO;AAChB,0BAAc;AAAA,UACf,OAAO;AACN,qBAAS;AAAA,UACV;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAC;AACD,QAAI,eAAe,CAAC,iBAAiB;AACpC,MAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,IAChD;AAAA,EACD;AACD;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
3
- "version": "1.1.4-next.f301c2b.0",
3
+ "version": "1.1.4-next.f603749.0",
4
4
  "description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -25,8 +25,8 @@
25
25
  "dependencies": {
26
26
  "@sapphire/prettier-config": "^1.4.5",
27
27
  "esm-to-cjs": "^1.2.1",
28
- "prettier": "^2.8.7",
29
- "typescript": "^4.9.5",
28
+ "prettier": "^2.8.8",
29
+ "typescript": "^5.1.6",
30
30
  "unist-util-visit": "^2.0.3"
31
31
  },
32
32
  "repository": {
@@ -63,9 +63,9 @@
63
63
  "access": "public"
64
64
  },
65
65
  "devDependencies": {
66
- "@favware/cliff-jumper": "^2.0.0",
67
- "tsup": "^6.7.0",
68
- "typescript": "^4.9.5",
69
- "vitest": "^0.29.8"
66
+ "@favware/cliff-jumper": "^2.1.1",
67
+ "@sapphire/framework": "^4.5.1",
68
+ "tsup": "^7.1.0",
69
+ "vitest": "^0.33.0"
70
70
  }
71
71
  }