@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.5-next.cd31a04.0 → 1.1.5-next.ef09164.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/dist/index.js CHANGED
@@ -39,7 +39,6 @@ var import_unist_util_visit = __toESM(require("unist-util-visit"));
39
39
 
40
40
  // src/ts2esm2cjs.ts
41
41
  var import_sync = require("@prettier/sync");
42
- var import_prettier_config = __toESM(require("@sapphire/prettier-config"));
43
42
  var import_esm_to_cjs = require("esm-to-cjs");
44
43
  var import_typescript = __toESM(require("typescript"));
45
44
  var makeTsCompilerOptions = (overrideOptions) => ({
@@ -53,7 +52,11 @@ var makeTsCompilerOptions = (overrideOptions) => ({
53
52
  target: import_typescript.default.ScriptTarget.ESNext
54
53
  });
55
54
  var documentationPrettierConfig = {
56
- ...import_prettier_config.default,
55
+ endOfLine: "lf",
56
+ quoteProps: "as-needed",
57
+ semi: true,
58
+ singleQuote: true,
59
+ trailingComma: "none",
57
60
  tabWidth: 2,
58
61
  useTabs: false,
59
62
  printWidth: 120,
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 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 { format } from '@prettier/sync';\nimport sapphirePrettierConfig from '@sapphire/prettier-config';\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\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\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"],"mapp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,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,gCAAAE,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","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 { 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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAAkB;;;ACHlB,kBAAuB;AACvB,wBAA6B;AAE7B,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,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,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,uBACzC,oBAAO,MAAM,EAAE,GAAG,6BAA6B,GAAG,eAAe,CAAC,EAAE,MAA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AC,KAAgB,SAAS,QAAQ,aAAa;AAAA,IAChD;AAAA,EACD;AACD;","names":["ts","visit"]}
package/dist/index.mjs CHANGED
@@ -3,7 +3,6 @@ import visit from "unist-util-visit";
3
3
 
4
4
  // src/ts2esm2cjs.ts
5
5
  import { format } from "@prettier/sync";
6
- import sapphirePrettierConfig from "@sapphire/prettier-config";
7
6
  import { runTransform } from "esm-to-cjs";
8
7
  import ts from "typescript";
9
8
  var makeTsCompilerOptions = (overrideOptions) => ({
@@ -17,7 +16,11 @@ var makeTsCompilerOptions = (overrideOptions) => ({
17
16
  target: ts.ScriptTarget.ESNext
18
17
  });
19
18
  var documentationPrettierConfig = {
20
- ...sapphirePrettierConfig,
19
+ endOfLine: "lf",
20
+ quoteProps: "as-needed",
21
+ semi: true,
22
+ singleQuote: true,
23
+ trailingComma: "none",
21
24
  tabWidth: 2,
22
25
  useTabs: false,
23
26
  printWidth: 120,
@@ -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 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 { format } from '@prettier/sync';\nimport sapphirePrettierConfig from '@sapphire/prettier-config';\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\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\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"],"mappings":";AAGA,OAAO,WAAW;;;ACHlB,SAAS,cAAc;AACvB,OAAO,4BAA4B;AACnC,SAAS,oBAAoB;AAE7B,OAAO,QAAkC;AAGzC,IAAM,wBAAwB,CAAC,qBAAwD;A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CAAC,GAAG,2BAA2B,CAAC,EAAE,MACrI;AACJ,SAAO,CAAC,SAAS;AAChB,QAAI,cAAc;AAClB,QAAI,kBAAkB;AACtB,UAAM,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":[]}
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 { 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);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AAQ,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,UAAM,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":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
3
- "version": "1.1.5-next.cd31a04.0",
3
+ "version": "1.1.5-next.ef09164.0",
4
4
  "description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -8,9 +8,9 @@
8
8
  "module": "dist/index.mjs",
9
9
  "types": "dist/index.d.ts",
10
10
  "exports": {
11
+ "types": "./dist/index.d.ts",
11
12
  "import": "./dist/index.mjs",
12
- "require": "./dist/index.js",
13
- "types": "./dist/index.d.ts"
13
+ "require": "./dist/index.js"
14
14
  },
15
15
  "sideEffects": false,
16
16
  "scripts": {
@@ -24,7 +24,6 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@prettier/sync": "^0.3.0",
27
- "@sapphire/prettier-config": "^2.0.0",
28
27
  "esm-to-cjs": "^1.2.1",
29
28
  "typescript": "^5.1.6",
30
29
  "unist-util-visit": "^2.0.3"
@@ -35,9 +34,8 @@
35
34
  "directory": "packages/ts2esm2cjs"
36
35
  },
37
36
  "files": [
38
- "dist/**/*.js*",
39
- "dist/**/*.mjs*",
40
- "dist/**/*.d*"
37
+ "dist/",
38
+ "!dist/.tsbuildinfo"
41
39
  ],
42
40
  "engines": {
43
41
  "node": ">=v16.6.0",
@@ -66,6 +64,6 @@
66
64
  "@favware/cliff-jumper": "^2.1.1",
67
65
  "@sapphire/framework": "^4.5.1",
68
66
  "tsup": "^7.2.0",
69
- "vitest": "^0.34.1"
67
+ "vitest": "^0.34.2"
70
68
  }
71
69
  }