@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.0-next.410e846.0 → 1.1.0-next.46a1f02.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/CHANGELOG.md ADDED
@@ -0,0 +1,45 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.0.3](https://github.com/sapphiredev/documentation-plugins/compare/@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.2...@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.3) (2022-01-13)
6
+
7
+ ### Bug Fixes
8
+
9
+ - **ts2esm2cjs:** switch back to published package of esm-to-cjs ([0ca3884](https://github.com/sapphiredev/documentation-plugins/commit/0ca388495cf91a7c4ab19c24bd48e0d992e0f821))
10
+
11
+ ### Reverts
12
+
13
+ - Revert "fix(ts2esm2cjs): fixed imports that have `as` in their name" ([684b297](https://github.com/sapphiredev/documentation-plugins/commit/684b2975f08df8a00ed2a6f5576770e6c7168383))
14
+
15
+ ## [1.0.2](https://github.com/sapphiredev/documentation-plugins/compare/@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.1...@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.2) (2022-01-12)
16
+
17
+ ### Bug Fixes
18
+
19
+ - **ts2esm2cjs:** fixed imports that have `as` in their name ([147c85d](https://github.com/sapphiredev/documentation-plugins/commit/147c85db866ac0544b4bcc62aa5e54a9f3009903))
20
+
21
+ ### Reverts
22
+
23
+ - Revert "ci: try adding extra dev deps for ci" ([de49260](https://github.com/sapphiredev/documentation-plugins/commit/de49260d4d8c4be4bb27d53f13472946e4cd3700))
24
+
25
+ ## [1.0.1](https://github.com/sapphiredev/documentation-plugins/compare/@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.0...@sapphire/docusaurus-plugin-ts2esm2cjs@1.0.1) (2021-11-30)
26
+
27
+ ### Bug Fixes
28
+
29
+ - **ts2esm2cjs:** allow longer lengths for esm to cjs ([280746c](https://github.com/sapphiredev/documentation-plugins/commit/280746cbf7120c9baddf8deac436ea0a9ba1835e))
30
+
31
+ # 1.0.0 (2021-11-26)
32
+
33
+ ### Bug Fixes
34
+
35
+ - **ts2esm2cjs:** removed esm exports to ensure cjs works fine with require syntax ([73b6898](https://github.com/sapphiredev/documentation-plugins/commit/73b6898881374259f9a38b74fe741cf46a468e06))
36
+ - **ts2esm2cjs:** removed exports field from package.json ([7dfac94](https://github.com/sapphiredev/documentation-plugins/commit/7dfac9464b28caa8e2d0dc03040a1b38f6b02d68))
37
+
38
+ ### Features
39
+
40
+ - add ts2esm2cjs package ([35ec87d](https://github.com/sapphiredev/documentation-plugins/commit/35ec87dd8743aecc57c344b1d2d4ae88038fcd7b))
41
+ - **ts2esm2cjs:** add option to customize tsc options ([af19a1d](https://github.com/sapphiredev/documentation-plugins/commit/af19a1d8473b8a3723933f87f8a0c6f79e98ebc7))
42
+
43
+ ### Reverts
44
+
45
+ - Revert "fix(typedoc-djs-links): removed esm exports to ensure cjs works fine with require syntax" ([7b4c6ab](https://github.com/sapphiredev/documentation-plugins/commit/7b4c6ab434265903213d7d965b0e927912b1aba1))
package/dist/index.d.ts CHANGED
@@ -1,19 +1,10 @@
1
1
  import { Options } from 'prettier';
2
2
  import { CompilerOptions } from 'typescript';
3
+ import type { Plugin } from 'unified';
3
4
  export interface PluginOptions {
4
5
  sync?: boolean;
5
6
  prettierOptions?: Options;
6
7
  typescriptCompilerOptions?: CompilerOptions;
7
8
  }
8
- export declare function ts2esm2cjs({ sync, prettierOptions, typescriptCompilerOptions }?: PluginOptions): (node: any) => ({
9
- type: string;
10
- value: string;
11
- lang?: undefined;
12
- meta?: undefined;
13
- } | {
14
- type: any;
15
- lang: any;
16
- meta: any;
17
- value: any;
18
- })[] | null;
9
+ export declare const ts2esm2cjs: Plugin<[PluginOptions?]>;
19
10
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@ var import_prettier_config = __toESM(require("@sapphire/prettier-config"));
29
29
  var import_esm_to_cjs = require("esm-to-cjs");
30
30
  var import_prettier = __toESM(require("prettier"));
31
31
  var import_typescript = __toESM(require("typescript"));
32
+ var import_unist_util_visit = __toESM(require("unist-util-visit"));
32
33
  var documentationPrettierConfig = {
33
34
  ...import_prettier_config.default,
34
35
  tabWidth: 2,
@@ -71,7 +72,7 @@ var transformNode = (node, options) => {
71
72
  {
72
73
  type: node.type,
73
74
  lang: node.lang,
74
- meta: jsHighlight,
75
+ meta: `${jsHighlight} showLineNumbers`,
75
76
  value: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)
76
77
  },
77
78
  {
@@ -81,7 +82,7 @@ var transformNode = (node, options) => {
81
82
  {
82
83
  type: node.type,
83
84
  lang: node.lang,
84
- meta: jsHighlight,
85
+ meta: `${jsHighlight} showLineNumbers`,
85
86
  value: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)
86
87
  },
87
88
  {
@@ -91,7 +92,7 @@ var transformNode = (node, options) => {
91
92
  {
92
93
  type: node.type,
93
94
  lang: node.lang,
94
- meta: tsHighlight,
95
+ meta: `${tsHighlight} showLineNumbers`,
95
96
  value: node.value
96
97
  },
97
98
  {
@@ -100,45 +101,43 @@ var transformNode = (node, options) => {
100
101
  }
101
102
  ];
102
103
  };
104
+ var isImport = (node) => node.type === "import";
105
+ var isParent = (node) => Array.isArray(node.children);
103
106
  var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && node.meta.startsWith("ts2esm2cjs");
104
107
  var nodeForImport = {
105
108
  type: "import",
106
109
  value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';"
107
110
  };
108
- function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
111
+ var ts2esm2cjs = ({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
109
112
  sync: true,
110
113
  prettierOptions: {},
111
114
  typescriptCompilerOptions: {}
112
- }) {
115
+ }) => (root) => {
113
116
  let transformed = false;
114
117
  let alreadyImported = false;
115
- const transformer = (node) => {
116
- if (node.type === "import" && node.value.includes("@theme/Tabs")) {
118
+ (0, import_unist_util_visit.default)(root, (node) => {
119
+ if (isImport(node) && node.value.includes("@theme/Tabs")) {
117
120
  alreadyImported = true;
118
121
  }
119
- if (matchNode(node)) {
120
- transformed = true;
121
- return transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });
122
- }
123
- if (Array.isArray(node.children)) {
122
+ if (isParent(node)) {
124
123
  let index = 0;
125
124
  while (index < node.children.length) {
126
- const result = transformer(node.children[index]);
127
- if (result) {
125
+ const child = node.children[index];
126
+ if (matchNode(child)) {
127
+ const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });
128
128
  node.children.splice(index, 1, ...result);
129
129
  index += result.length;
130
+ transformed = true;
130
131
  } else {
131
132
  index += 1;
132
133
  }
133
134
  }
134
135
  }
135
- if (node.type === "root" && transformed && !alreadyImported) {
136
- node.children.unshift(nodeForImport);
137
- }
138
- return null;
139
- };
140
- return transformer;
141
- }
136
+ });
137
+ if (transformed && !alreadyImported) {
138
+ root.children.unshift(nodeForImport);
139
+ }
140
+ };
142
141
  // Annotate the CommonJS export names for ESM import in node:
143
142
  0 && (module.exports = {
144
143
  ts2esm2cjs
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 prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\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 isSync Whether the transform should synchronize between all entries of this type\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: any, 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:\n\t\t\t\t`<Tabs defaultValue=\"typescript\" ${groupIdProp}` +\n\t\t\t\t`values={[\n { label: 'JavaScript', value: 'javascript', },\n { label: 'ESM', value: 'esm', },\n { label: 'TypeScript', value: 'typescript', },\n ]}\n>\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,\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,\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,\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];\n};\n\nconst matchNode = (node: any) => node.type === 'code' && typeof node.meta === 'string' && node.meta.startsWith('ts2esm2cjs');\nconst nodeForImport = {\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 function ts2esm2cjs(\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} }: PluginOptions = {\n\t\tsync: true,\n\t\tprettierOptions: {},\n\t\ttypescriptCompilerOptions: {}\n\t}\n) {\n\tlet transformed = false;\n\tlet alreadyImported = false;\n\n\tconst transformer = (node: any) => {\n\t\tif (node.type === 'import' && node.value.includes('@theme/Tabs')) {\n\t\t\talreadyImported = true;\n\t\t}\n\n\t\tif (matchNode(node)) {\n\t\t\ttransformed = true;\n\t\t\treturn transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t}\n\n\t\tif (Array.isArray(node.children)) {\n\t\t\tlet index = 0;\n\t\t\twhile (index < node.children.length) {\n\t\t\t\tconst result = transformer(node.children[index]);\n\t\t\t\tif (result) {\n\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\tindex += result.length;\n\t\t\t\t} else {\n\t\t\t\t\tindex += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (node.type === 'root' && transformed && !alreadyImported) {\n\t\t\tnode.children.unshift(nodeForImport);\n\t\t}\n\n\t\treturn null;\n\t};\n\n\treturn transformer;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAmC;AACnC,wBAA6B;AAC7B,sBAAkC;AAClC,wBAAoC;AAEpC,IAAM,8BAAuC;AAAA,KACzC;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAEA,IAAM,wBAAwB,CAAC,oBAAwD;AAAA,EACtF,SAAS,0BAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,KACL;AAAA,EACH,QAAQ,0BAAG,WAAW;AAAA,EACtB,kBAAkB,0BAAG,qBAAqB;AAAA,EAC1C,QAAQ,0BAAG,aAAa;AACzB;AAOA,IAAM,UAAU,CAAC,MAAc,YAC9B,0BAAG,gBAAgB,MAAM,EAAE,mBAAmB,OAAO,iBAAiB,sBAAsB,QAAQ,yBAAyB,EAAE,CAAC;AAOjI,IAAM,WA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IACb;AAAA,IACA;AAAA,MACC,MAAM;AAAA,MACN,OAAO;AAAA,IACR;AAAA,EACD;AACD;AAEA,IAAM,YAAY,CAAC,SAAc,KAAK,SAAS,UAAU,OAAO,KAAK,SAAS,YAAY,KAAK,KAAK,WAAW,YAAY;AAC3H,IAAM,gBAAgB;AAAA,EACrB,MAAM;AAAA,EACN,OAAO;AACR;AAQO,oBACN,EAAE,OAAO,MAAM,kBAAkB,CAAC,GAAG,4BAA4B,CAAC,MAAqB;AAAA,EACtF,MAAM;AAAA,EACN,iBAAiB,CAAC;AAAA,EAClB,2BAA2B,CAAC;AAC7B,GACC;AACD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,cAAc,CAAC,SAAc;AAClC,QAAI,KAAK,SAAS,YAAY,KAAK,MAAM,SAAS,aAAa,GAAG;AACjE,wBAAkB;AAAA,IACnB;AAEA,QAAI,UAAU,IAAI,GAAG;AACpB,oBAAc;AACd,aAAO,cAAc,MAAM,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AAAA,IAChF;AAEA,QAAI,MAAM,QAAQ,KAAK,QAAQ,GAAG;AACjC,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,SAAS,YAAY,KAAK,SAAS,MAAM;AAC/C,YAAI,QAAQ;AACX,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,mBAAS,OAAO;AAAA,QACjB,OAAO;AACN,mBAAS;AAAA,QACV;AAAA,MACD;AAAA,IACD;AAEA,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ,aAAa;AAAA,IACpC;AAEA,WAAO;AAAA,EACR;AAEA,SAAO;AACR;","names":[]}
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: any, 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:\n\t\t\t\t`<Tabs defaultValue=\"typescript\" ${groupIdProp}` +\n\t\t\t\t`values={[\n { label: 'JavaScript', value: 'javascript', },\n { label: 'ESM', value: 'esm', },\n { label: 'TypeScript', value: 'typescript', },\n ]}\n>\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 Node =>\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(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} }: PluginOptions = {\n\t\t\tsync: true,\n\t\t\tprettierOptions: {},\n\t\t\ttypescriptCompilerOptions: {}\n\t\t}\n\t) =>\n\t(root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\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\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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AkB;AAAA,IACnB;AACA,QAAI,SAAS,IAAI,GAAG;AACnB,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,QAAQ,KAAK,SAAS;AAC5B,YAAI,UAAU,KAAK,GAAG;AACrB,gBAAM,SAAS,cAAc,OAAO,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AACxF,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,mBAAS,OAAO;AAChB,wBAAc;AAAA,QACf,OAAO;AACN,mBAAS;AAAA,QACV;AAAA,MACD;AAAA,IACD;AAAA,EACD,CAAC;AAED,MAAI,eAAe,CAAC,iBAAiB;AACpC,IAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,EAChD;AACD;","names":[]}
package/dist/index.mjs CHANGED
@@ -3,6 +3,7 @@ import sapphirePrettierConfig from "@sapphire/prettier-config";
3
3
  import { runTransform } from "esm-to-cjs";
4
4
  import prettier from "prettier";
5
5
  import ts from "typescript";
6
+ import visit from "unist-util-visit";
6
7
  var documentationPrettierConfig = {
7
8
  ...sapphirePrettierConfig,
8
9
  tabWidth: 2,
@@ -45,7 +46,7 @@ var transformNode = (node, options) => {
45
46
  {
46
47
  type: node.type,
47
48
  lang: node.lang,
48
- meta: jsHighlight,
49
+ meta: `${jsHighlight} showLineNumbers`,
49
50
  value: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)
50
51
  },
51
52
  {
@@ -55,7 +56,7 @@ var transformNode = (node, options) => {
55
56
  {
56
57
  type: node.type,
57
58
  lang: node.lang,
58
- meta: jsHighlight,
59
+ meta: `${jsHighlight} showLineNumbers`,
59
60
  value: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)
60
61
  },
61
62
  {
@@ -65,7 +66,7 @@ var transformNode = (node, options) => {
65
66
  {
66
67
  type: node.type,
67
68
  lang: node.lang,
68
- meta: tsHighlight,
69
+ meta: `${tsHighlight} showLineNumbers`,
69
70
  value: node.value
70
71
  },
71
72
  {
@@ -74,45 +75,43 @@ var transformNode = (node, options) => {
74
75
  }
75
76
  ];
76
77
  };
78
+ var isImport = (node) => node.type === "import";
79
+ var isParent = (node) => Array.isArray(node.children);
77
80
  var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && node.meta.startsWith("ts2esm2cjs");
78
81
  var nodeForImport = {
79
82
  type: "import",
80
83
  value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';"
81
84
  };
82
- function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
85
+ var ts2esm2cjs = ({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
83
86
  sync: true,
84
87
  prettierOptions: {},
85
88
  typescriptCompilerOptions: {}
86
- }) {
89
+ }) => (root) => {
87
90
  let transformed = false;
88
91
  let alreadyImported = false;
89
- const transformer = (node) => {
90
- if (node.type === "import" && node.value.includes("@theme/Tabs")) {
92
+ visit(root, (node) => {
93
+ if (isImport(node) && node.value.includes("@theme/Tabs")) {
91
94
  alreadyImported = true;
92
95
  }
93
- if (matchNode(node)) {
94
- transformed = true;
95
- return transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });
96
- }
97
- if (Array.isArray(node.children)) {
96
+ if (isParent(node)) {
98
97
  let index = 0;
99
98
  while (index < node.children.length) {
100
- const result = transformer(node.children[index]);
101
- if (result) {
99
+ const child = node.children[index];
100
+ if (matchNode(child)) {
101
+ const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });
102
102
  node.children.splice(index, 1, ...result);
103
103
  index += result.length;
104
+ transformed = true;
104
105
  } else {
105
106
  index += 1;
106
107
  }
107
108
  }
108
109
  }
109
- if (node.type === "root" && transformed && !alreadyImported) {
110
- node.children.unshift(nodeForImport);
111
- }
112
- return null;
113
- };
114
- return transformer;
115
- }
110
+ });
111
+ if (transformed && !alreadyImported) {
112
+ root.children.unshift(nodeForImport);
113
+ }
114
+ };
116
115
  export {
117
116
  ts2esm2cjs
118
117
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\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 isSync Whether the transform should synchronize between all entries of this type\n * @returns The transformed node in the form of Tabs.\n */\nconst transformNode = (node: any, 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:\n\t\t\t\t`<Tabs defaultValue=\"typescript\" ${groupIdProp}` +\n\t\t\t\t`values={[\n { label: 'JavaScript', value: 'javascript', },\n { label: 'ESM', value: 'esm', },\n { label: 'TypeScript', value: 'typescript', },\n ]}\n>\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,\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,\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,\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];\n};\n\nconst matchNode = (node: any) => node.type === 'code' && typeof node.meta === 'string' && node.meta.startsWith('ts2esm2cjs');\nconst nodeForImport = {\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 function ts2esm2cjs(\n\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} }: PluginOptions = {\n\t\tsync: true,\n\t\tprettierOptions: {},\n\t\ttypescriptCompilerOptions: {}\n\t}\n) {\n\tlet transformed = false;\n\tlet alreadyImported = false;\n\n\tconst transformer = (node: any) => {\n\t\tif (node.type === 'import' && node.value.includes('@theme/Tabs')) {\n\t\t\talreadyImported = true;\n\t\t}\n\n\t\tif (matchNode(node)) {\n\t\t\ttransformed = true;\n\t\t\treturn transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });\n\t\t}\n\n\t\tif (Array.isArray(node.children)) {\n\t\t\tlet index = 0;\n\t\t\twhile (index < node.children.length) {\n\t\t\t\tconst result = transformer(node.children[index]);\n\t\t\t\tif (result) {\n\t\t\t\t\tnode.children.splice(index, 1, ...result);\n\t\t\t\t\tindex += result.length;\n\t\t\t\t} else {\n\t\t\t\t\tindex += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (node.type === 'root' && transformed && !alreadyImported) {\n\t\t\tnode.children.unshift(nodeForImport);\n\t\t}\n\n\t\treturn null;\n\t};\n\n\treturn transformer;\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AAEA,IAAM,8BAAuC;AAAA,KACzC;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAEA,IAAM,wBAAwB,CAAC,oBAAwD;AAAA,EACtF,SAAS,GAAG,Y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AAAA,QACV;AAAA,MACD;AAAA,IACD;AAEA,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ,aAAa;AAAA,IACpC;AAEA,WAAO;AAAA,EACR;AAEA,SAAO;AACR;","names":[]}
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: any, 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:\n\t\t\t\t`<Tabs defaultValue=\"typescript\" ${groupIdProp}` +\n\t\t\t\t`values={[\n { label: 'JavaScript', value: 'javascript', },\n { label: 'ESM', value: 'esm', },\n { label: 'TypeScript', value: 'typescript', },\n ]}\n>\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 Node =>\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(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} }: PluginOptions = {\n\t\t\tsync: true,\n\t\t\tprettierOptions: {},\n\t\t\ttypescriptCompilerOptions: {}\n\t\t}\n\t) =>\n\t(root) => {\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\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\n\t\tif (transformed && !alreadyImported) {\n\t\t\t(root as Parent).children.unshift(nodeForImport);\n\t\t}\n\t};\n"],"mappings":";AAAA;AACA;AAEA;AACA;AAGA;AAEA,IAAM,8BAAuC;AAAA,KACzC;AAAA,EACH,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,QAAQ;AACT;AAEA,IAAM,wBAAwB,CAAC,oBAAwD;AAAA,EACtF,SAAS,GAAG,YAAY;AAAA,EACxB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ;AAAA,KACL;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,SAAiB,KAAK,QAAQ,SAAS,mBAAmB;AAOlF,IAAM,kBAAkB,CAAC,SAAyB,KAAK,QAAQ,wBAAwB,IAAI;AAQ3F,IAAM,qBAAqB,CAAC,MAAc,mBACzC,SAAS,OAAO,MAAM,KAAK,gCAAgC,eAAe,CAAC,EAAE,MAAM,GAAG,EAAE;AAQzF,IAAM,gBAAgB,CAAC,MAAW,YAA2B;AAC5D,QAAM,cAAc,QAAQ,OAAO,0BAA0B;AAE7D,QAAM,SAAS,eAAe,KAAK,KAAK;AACxC,QAAM,UAAU,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BAAiB,CAAC;AAAA,EAClB,2BAA2B,CAAC;AAC7B,MAED,CAAC,SAAS;AACT,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,MAAM,CAAC,SAAe;AAC3B,QAAI,SAAS,IAAI,KAAK,KAAK,MAAM,SAAS,aAAa,GAAG;AACzD,wBAAkB;AAAA,IACnB;AACA,QAAI,SAAS,IAAI,GAAG;AACnB,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,QAAQ,KAAK,SAAS;AAC5B,YAAI,UAAU,KAAK,GAAG;AACrB,gBAAM,SAAS,cAAc,OAAO,EAAE,MAAM,iBAAiB,0BAA0B,CAAC;AACxF,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG,MAAM;AACxC,mBAAS,OAAO;AAChB,wBAAc;AAAA,QACf,OAAO;AACN,mBAAS;AAAA,QACV;AAAA,MACD;AAAA,IACD;AAAA,EACD,CAAC;AAED,MAAI,eAAe,CAAC,iBAAiB;AACpC,IAAC,KAAgB,SAAS,QAAQ,aAAa;AAAA,EAChD;AACD;","names":[]}
package/package.json CHANGED
@@ -1,60 +1,67 @@
1
1
  {
2
- "name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
3
- "version": "1.1.0-next.410e846.0",
4
- "description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
5
- "author": "@sapphire",
6
- "license": "MIT",
7
- "main": "dist/index.js",
8
- "module": "dist/index.mjs",
9
- "types": "dist/index.d.ts",
10
- "exports": {
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js",
13
- "types": "./dist/index.d.ts"
14
- },
15
- "scripts": {
16
- "lint": "eslint src --ext ts --fix",
17
- "build": "tsup && tsc -b src",
18
- "typecheck": "tsc -p tsconfig.eslint.json",
19
- "prepublishOnly": "yarn build"
20
- },
21
- "dependencies": {
22
- "@sapphire/prettier-config": "^1.4.3",
23
- "esm-to-cjs": "^1.2.1",
24
- "prettier": "^2.6.2",
25
- "typescript": "^4.6.4"
26
- },
27
- "repository": {
28
- "type": "git",
29
- "url": "git+https://github.com/sapphiredev/documentation-plugins.git",
30
- "directory": "packages/ts2esm2cjs"
31
- },
32
- "files": [
33
- "dist",
34
- "!dist/*.tsbuildinfo"
35
- ],
36
- "engines": {
37
- "node": ">=v16.6.0",
38
- "npm": ">=7.0.0"
39
- },
40
- "keywords": [
41
- "docusaurus",
42
- "docusaurus-plugin",
43
- "@sapphire",
44
- "typescript",
45
- "ts",
46
- "esm",
47
- "cjs",
48
- "discord",
49
- "sapphire",
50
- "discordjs"
51
- ],
52
- "bugs": {
53
- "url": "https://github.com/sapphiredev/documentation-plugins/issues"
54
- },
55
- "homepage": "https://www.sapphirejs.dev",
56
- "publishConfig": {
57
- "access": "public"
58
- },
59
- "gitHead": "410e846100b5de83dc3a0d2ff64440c5b978f804"
60
- }
2
+ "name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
3
+ "version": "1.1.0-next.46a1f02.0",
4
+ "description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
5
+ "author": "@sapphire",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.mjs",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ },
15
+ "scripts": {
16
+ "lint": "eslint src --ext ts --fix",
17
+ "build": "tsup && tsc -b src",
18
+ "typecheck": "tsc -p tsconfig.eslint.json",
19
+ "prepack": "yarn build",
20
+ "bump": "cliff-jumper",
21
+ "check-update": "cliff-jumper --dry-run"
22
+ },
23
+ "dependencies": {
24
+ "@sapphire/prettier-config": "^1.4.3",
25
+ "esm-to-cjs": "^1.2.1",
26
+ "prettier": "^2.6.2",
27
+ "typescript": "^4.6.4",
28
+ "unist-util-visit": "^2.0.3"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/sapphiredev/documentation-plugins.git",
33
+ "directory": "packages/ts2esm2cjs"
34
+ },
35
+ "files": [
36
+ "dist/**/*.js*",
37
+ "dist/**/*.mjs*",
38
+ "dist/**/*.d*"
39
+ ],
40
+ "engines": {
41
+ "node": ">=v16.6.0",
42
+ "npm": ">=7.0.0"
43
+ },
44
+ "keywords": [
45
+ "docusaurus",
46
+ "docusaurus-plugin",
47
+ "@sapphire",
48
+ "typescript",
49
+ "ts",
50
+ "esm",
51
+ "cjs",
52
+ "discord",
53
+ "sapphire",
54
+ "discordjs"
55
+ ],
56
+ "bugs": {
57
+ "url": "https://github.com/sapphiredev/documentation-plugins/issues"
58
+ },
59
+ "homepage": "https://www.sapphirejs.dev",
60
+ "publishConfig": {
61
+ "access": "public"
62
+ },
63
+ "devDependencies": {
64
+ "tsup": "^5.12.7",
65
+ "typescript": "^4.6.4"
66
+ }
67
+ }
package/LICENSE.md DELETED
@@ -1,24 +0,0 @@
1
- # The MIT License (MIT)
2
-
3
- Copyright © `2021` `The Sapphire Community and its contributors`
4
-
5
- Permission is hereby granted, free of charge, to any person
6
- obtaining a copy of this software and associated documentation
7
- files (the “Software”), to deal in the Software without
8
- restriction, including without limitation the rights to use,
9
- copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the
11
- Software is furnished to do so, subject to the following
12
- conditions:
13
-
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
- OTHER DEALINGS IN THE SOFTWARE.
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAiB,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA8HjD,MAAM,WAAW,aAAa;IAC7B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,yBAAyB,CAAC,EAAE,eAAe,CAAC;CAC5C;AAED,wBAAgB,UAAU,CACzB,EAAE,IAAW,EAAE,eAAoB,EAAE,yBAA8B,EAAE,GAAE,aAItE,UAK0B,GAAG;;;;;;;;;;YA+B9B"}