@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.4-next.b8e3053.0 → 1.1.4-next.cd26f50.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 +1 -0
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -170,6 +170,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
170
170
|
<td align="center"><a href="https://favware.tech/"><img src="https://avatars3.githubusercontent.com/u/4019718?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jeroen Claassens</b></sub></a><br /><a href="https://github.com/sapphiredev/documentation-plugins/commits?author=favna" title="Code">💻</a> <a href="#infra-favna" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#projectManagement-favna" title="Project Management">📆</a></td>
|
|
171
171
|
<td align="center"><a href="https://github.com/apps/renovate"><img src="https://avatars.githubusercontent.com/in/2740?v=4?s=100" width="100px;" alt=""/><br /><sub><b>renovate[bot]</b></sub></a><br /><a href="#maintenance-renovate[bot]" title="Maintenance">🚧</a></td>
|
|
172
172
|
<td align="center"><a href="https://renovate.whitesourcesoftware.com/"><img src="https://avatars.githubusercontent.com/u/25180681?v=4?s=100" width="100px;" alt=""/><br /><sub><b>WhiteSource Renovate</b></sub></a><br /><a href="#maintenance-renovate-bot" title="Maintenance">🚧</a></td>
|
|
173
|
+
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/documentation-plugins/commits?author=vladfrangu" title="Code">💻</a></td>
|
|
173
174
|
</tr>
|
|
174
175
|
</table>
|
|
175
176
|
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
21
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
25
|
|
|
23
26
|
// src/index.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Code, Content, Literal } from 'mdast';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\n\nconst documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\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\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 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 * 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 tsCode = escapeNewLines(node.value);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\tconst cjsCode = esmToCjs(esmCode);\n\n\tconst [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\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: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)\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: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)\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 interface PluginOptions {\n\tsync?: boolean;\n\tprettierOptions?: Options;\n\ttypescriptCompilerOptions?: CompilerOptions;\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"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Code, Content, Literal } from 'mdast';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\n\nconst documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\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\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 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 * 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 tsCode = escapeNewLines(node.value);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\tconst cjsCode = esmToCjs(esmCode);\n\n\tconst [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\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: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)\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: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)\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 interface PluginOptions {\n\tsync?: boolean;\n\tprettierOptions?: Options;\n\ttypescriptCompilerOptions?: CompilerOptions;\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAmC;AACnC,wBAA6B;AAE7B,sBAAkC;AAClC,wBAAoC;AAGpC,8BAAkB;AAElB,IAAM,8BAAuC;AAAA,EAC5C,GAAG,uBAAAA;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAEA,IAAM,wBAAwB,CAAC,qBAAwD;AAAA,EACtF,SAAS,kBAAAC,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,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AAS;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":["sapphirePrettierConfig","ts","prettier","visit"]}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Code, Content, Literal } from 'mdast';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\n\nconst documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\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\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 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 * 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 tsCode = escapeNewLines(node.value);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\tconst cjsCode = esmToCjs(esmCode);\n\n\tconst [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\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: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)\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: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)\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 interface PluginOptions {\n\tsync?: boolean;\n\tprettierOptions?: Options;\n\ttypescriptCompilerOptions?: CompilerOptions;\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"],"mappings":";AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport type { Code, Content, Literal } from 'mdast';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\nimport type { Plugin } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport visit from 'unist-util-visit';\n\nconst documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\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\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 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 * 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 tsCode = escapeNewLines(node.value);\n\tconst esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;\n\tconst cjsCode = esmToCjs(esmCode);\n\n\tconst [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\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: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)\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: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)\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 interface PluginOptions {\n\tsync?: boolean;\n\tprettierOptions?: Options;\n\ttypescriptCompilerOptions?: CompilerOptions;\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"],"mappings":";AAAA,OAAO,4BAA4B;AACnC,SAAS,oBAAoB;AAE7B,OAAO,cAA2B;AAClC,OAAO,QAA6B;AAGpC,OAAO,WAAW;AAElB,IAAM,8BAAuC;AAAA,EAC5C,GAAG;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAEA,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;AAOA,IAAM,UAAU,CAAC,MAAc,YAC9B,GAAG,gBAAgB,MAAM,EAAE,mBAAmB,OAAO,iBAAiB,sBAAsB,QAAQ,yBAAyB,EAAE,CAAC;AAOjI,IAAM,WAAW,CAAC,SAAyB,aAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe,IAAI,CAAC;AAO9I,IAAM,iBAAiB,CAAC,SAAi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
+
"version": "1.1.4-next.cd26f50.0",
|
|
4
4
|
"description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@favware/cliff-jumper": "^1.8.6",
|
|
66
|
-
"tsup": "^6.2.
|
|
66
|
+
"tsup": "^6.2.2",
|
|
67
67
|
"typescript": "^4.7.4"
|
|
68
68
|
}
|
|
69
69
|
}
|