@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.0-next.410e846.0 → 1.1.0-next.7411ff7.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
+ # Changelog
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,11 @@
1
1
  import { Options } from 'prettier';
2
2
  import { CompilerOptions } from 'typescript';
3
- export interface PluginOptions {
3
+ import type { Plugin } from 'unified';
4
+ 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
+ declare const ts2esm2cjs: Plugin<[PluginOptions?]>;
10
+ export = ts2esm2cjs;
19
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAiB,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAmItC,UAAU,aAAa;IACtB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,yBAAyB,CAAC,EAAE,eAAe,CAAC;CAC5C;AAED,QAAA,MAAM,UAAU,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,CAmCvC,CAAC;AAEH,SAAS,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -1,146 +1,151 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ "use strict";
2
+ const tslib_1 = require("tslib");
3
+ const prettier_config_1 = tslib_1.__importDefault(require("@sapphire/prettier-config"));
4
+ const esm_to_cjs_1 = require("esm-to-cjs");
5
+ const prettier_1 = tslib_1.__importDefault(require("prettier"));
6
+ const typescript_1 = tslib_1.__importDefault(require("typescript"));
7
+ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
8
+ const documentationPrettierConfig = {
9
+ ...prettier_config_1.default,
10
+ tabWidth: 2,
11
+ useTabs: false,
12
+ printWidth: 120,
13
+ parser: 'babel'
10
14
  };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
-
22
- // src/index.ts
23
- var src_exports = {};
24
- __export(src_exports, {
25
- ts2esm2cjs: () => ts2esm2cjs
26
- });
27
- module.exports = __toCommonJS(src_exports);
28
- var import_prettier_config = __toESM(require("@sapphire/prettier-config"));
29
- var import_esm_to_cjs = require("esm-to-cjs");
30
- var import_prettier = __toESM(require("prettier"));
31
- var import_typescript = __toESM(require("typescript"));
32
- var documentationPrettierConfig = {
33
- ...import_prettier_config.default,
34
- tabWidth: 2,
35
- useTabs: false,
36
- printWidth: 120,
37
- parser: "babel"
38
- };
39
- var makeTsCompilerOptions = (overrideOptions) => ({
40
- newLine: import_typescript.default.NewLineKind.LineFeed,
41
- removeComments: false,
42
- esModuleInterop: true,
43
- pretty: true,
44
- ...overrideOptions,
45
- module: import_typescript.default.ModuleKind.ESNext,
46
- moduleResolution: import_typescript.default.ModuleResolutionKind.NodeJs,
47
- target: import_typescript.default.ScriptTarget.ESNext
15
+ const makeTsCompilerOptions = (overrideOptions) => ({
16
+ newLine: typescript_1.default.NewLineKind.LineFeed,
17
+ removeComments: false,
18
+ esModuleInterop: true,
19
+ pretty: true,
20
+ ...overrideOptions,
21
+ module: typescript_1.default.ModuleKind.ESNext,
22
+ moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs,
23
+ target: typescript_1.default.ScriptTarget.ESNext
48
24
  });
49
- var tsToEsm = (code, options) => import_typescript.default.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });
50
- var esmToCjs = (code) => (0, import_esm_to_cjs.runTransform)(code, { quote: "single", lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });
51
- var escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
52
- var restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
53
- var prettierFormatCode = (code, prettierConfig) => import_prettier.default.format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);
54
- var transformNode = (node, options) => {
55
- const groupIdProp = options.sync ? 'groupId="ts2esm2cjs" ' : "";
56
- const tsCode = escapeNewLines(node.value);
57
- const esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;
58
- const cjsCode = esmToCjs(esmCode);
59
- const [, jsHighlight, tsHighlight] = node.meta.split("|");
60
- return [
61
- {
62
- type: "jsx",
63
- value: `<Tabs defaultValue="typescript" ${groupIdProp}values={[
25
+ /**
26
+ * Transpiles input TypeScript code to ESM code.
27
+ * @param code The code to transpile
28
+ * @returns Input code transpiled to ESM
29
+ */
30
+ const tsToEsm = (code, options) => typescript_1.default.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });
31
+ /**
32
+ * Transforms input ESM code to CJS code.
33
+ * @param code The code to transform
34
+ * @returns Input code transformed to CommonJS
35
+ */
36
+ const esmToCjs = (code) => (0, esm_to_cjs_1.runTransform)(code, { quote: 'single', lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });
37
+ /**
38
+ * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}
39
+ * @param code The code to escape new lines in
40
+ * @returns The same code but with new lines escaped using block comments
41
+ */
42
+ const escapeNewLines = (code) => code.replace(/\n\n/g, '\n/* :newline: */');
43
+ /**
44
+ * Reverses {@link escapeNewLines} and restores new lines
45
+ * @param code The code with escaped new lines
46
+ * @returns The same code with new lines restored
47
+ */
48
+ const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, '\n');
49
+ /**
50
+ * Formats the code using Prettier
51
+ * @param code The code to prettier format
52
+ * @param prettierConfig Additional prettier options to use for formatting
53
+ * @returns Prettier formatted code
54
+ */
55
+ const prettierFormatCode = (code, prettierConfig) => prettier_1.default.format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);
56
+ /**
57
+ * Transforms a Docusaurus node from TypeScript to ESM and CJS
58
+ * @param node The Docusaurus node to transform
59
+ * @param options The plugin options to pass to the transformer
60
+ * @returns The transformed node in the form of Tabs.
61
+ */
62
+ const transformNode = (node, options) => {
63
+ const groupIdProp = options.sync ? 'groupId="ts2esm2cjs" ' : '';
64
+ const tsCode = escapeNewLines(node.value);
65
+ const esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;
66
+ const cjsCode = esmToCjs(esmCode);
67
+ const [, jsHighlight, tsHighlight] = node.meta.split('|');
68
+ return [
69
+ {
70
+ type: 'jsx',
71
+ value: `<Tabs defaultValue="typescript" ${groupIdProp}` +
72
+ `values={[
64
73
  { label: 'JavaScript', value: 'javascript', },
65
74
  { label: 'ESM', value: 'esm', },
66
75
  { label: 'TypeScript', value: 'typescript', },
67
76
  ]}
68
77
  >
69
78
  <TabItem value="javascript">`
70
- },
71
- {
72
- type: node.type,
73
- lang: node.lang,
74
- meta: jsHighlight,
75
- value: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)
76
- },
77
- {
78
- type: "jsx",
79
- value: '</TabItem>\n<TabItem value="esm">'
80
- },
81
- {
82
- type: node.type,
83
- lang: node.lang,
84
- meta: jsHighlight,
85
- value: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)
86
- },
87
- {
88
- type: "jsx",
89
- value: '</TabItem>\n<TabItem value="typescript">'
90
- },
91
- {
92
- type: node.type,
93
- lang: node.lang,
94
- meta: tsHighlight,
95
- value: node.value
96
- },
97
- {
98
- type: "jsx",
99
- value: "</TabItem>\n</Tabs>"
100
- }
101
- ];
79
+ },
80
+ {
81
+ type: node.type,
82
+ lang: node.lang,
83
+ meta: `${jsHighlight} showLineNumbers`,
84
+ value: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)
85
+ },
86
+ {
87
+ type: 'jsx',
88
+ value: '</TabItem>\n<TabItem value="esm">'
89
+ },
90
+ {
91
+ type: node.type,
92
+ lang: node.lang,
93
+ meta: `${jsHighlight} showLineNumbers`,
94
+ value: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)
95
+ },
96
+ {
97
+ type: 'jsx',
98
+ value: '</TabItem>\n<TabItem value="typescript">'
99
+ },
100
+ {
101
+ type: node.type,
102
+ lang: node.lang,
103
+ meta: `${tsHighlight} showLineNumbers`,
104
+ value: node.value
105
+ },
106
+ {
107
+ type: 'jsx',
108
+ value: '</TabItem>\n</Tabs>'
109
+ }
110
+ ];
102
111
  };
103
- var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && node.meta.startsWith("ts2esm2cjs");
104
- var nodeForImport = {
105
- type: "import",
106
- value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';"
112
+ const isImport = (node) => node.type === 'import';
113
+ const isParent = (node) => Array.isArray(node.children);
114
+ const matchNode = (node) => node.type === 'code' && typeof node.meta === 'string' && node.meta.startsWith('ts2esm2cjs');
115
+ const nodeForImport = {
116
+ type: 'import',
117
+ value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';"
107
118
  };
108
- function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
109
- sync: true,
110
- prettierOptions: {},
111
- typescriptCompilerOptions: {}
112
- }) {
113
- let transformed = false;
114
- let alreadyImported = false;
115
- const transformer = (node) => {
116
- if (node.type === "import" && node.value.includes("@theme/Tabs")) {
117
- alreadyImported = true;
118
- }
119
- if (matchNode(node)) {
120
- transformed = true;
121
- return transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });
122
- }
123
- if (Array.isArray(node.children)) {
124
- let index = 0;
125
- while (index < node.children.length) {
126
- const result = transformer(node.children[index]);
127
- if (result) {
128
- node.children.splice(index, 1, ...result);
129
- index += result.length;
130
- } else {
131
- index += 1;
119
+ const ts2esm2cjs = ({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
120
+ sync: true,
121
+ prettierOptions: {},
122
+ typescriptCompilerOptions: {}
123
+ }) => (root) => {
124
+ let transformed = false;
125
+ let alreadyImported = false;
126
+ (0, unist_util_visit_1.default)(root, (node) => {
127
+ if (isImport(node) && node.value.includes('@theme/Tabs')) {
128
+ alreadyImported = true;
132
129
  }
133
- }
134
- }
135
- if (node.type === "root" && transformed && !alreadyImported) {
136
- node.children.unshift(nodeForImport);
130
+ if (isParent(node)) {
131
+ let index = 0;
132
+ while (index < node.children.length) {
133
+ const child = node.children[index];
134
+ if (matchNode(child)) {
135
+ const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });
136
+ node.children.splice(index, 1, ...result);
137
+ index += result.length;
138
+ transformed = true;
139
+ }
140
+ else {
141
+ index += 1;
142
+ }
143
+ }
144
+ }
145
+ });
146
+ if (transformed && !alreadyImported) {
147
+ root.children.unshift(nodeForImport);
137
148
  }
138
- return null;
139
- };
140
- return transformer;
141
- }
142
- // Annotate the CommonJS export names for ESM import in node:
143
- 0 && (module.exports = {
144
- ts2esm2cjs
145
- });
149
+ };
150
+ module.exports = ts2esm2cjs;
146
151
  //# sourceMappingURL=index.js.map
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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AI,WAAW,IAAI,CAAC,eAAe,EAAE;QACnC,IAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;KACjD;AACF,CAAC,CAAC;AAEH,iBAAS,UAAU,CAAC"}
package/package.json CHANGED
@@ -1,60 +1,59 @@
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.7411ff7.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
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "lint": "eslint src --ext ts --fix",
11
+ "build": "tsc -b src",
12
+ "prepack": "yarn build",
13
+ "bump": "cliff-jumper",
14
+ "check-update": "cliff-jumper --dry-run"
15
+ },
16
+ "dependencies": {
17
+ "@sapphire/prettier-config": "^1.4.3",
18
+ "esm-to-cjs": "^1.2.1",
19
+ "prettier": "^2.6.2",
20
+ "tslib": "^2.4.0",
21
+ "typescript": "^4.6.4",
22
+ "unist-util-visit": "^2.0.3"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/sapphiredev/documentation-plugins.git",
27
+ "directory": "packages/ts2esm2cjs"
28
+ },
29
+ "files": [
30
+ "dist/**/*.js*",
31
+ "dist/**/*.d*"
32
+ ],
33
+ "engines": {
34
+ "node": ">=v16.6.0",
35
+ "npm": ">=7.0.0"
36
+ },
37
+ "keywords": [
38
+ "docusaurus",
39
+ "docusaurus-plugin",
40
+ "@sapphire",
41
+ "typescript",
42
+ "ts",
43
+ "esm",
44
+ "cjs",
45
+ "discord",
46
+ "sapphire",
47
+ "discordjs"
48
+ ],
49
+ "bugs": {
50
+ "url": "https://github.com/sapphiredev/documentation-plugins/issues"
51
+ },
52
+ "homepage": "https://www.sapphirejs.dev",
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "devDependencies": {
57
+ "typescript": "^4.6.4"
58
+ }
59
+ }
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.
package/dist/index.mjs DELETED
@@ -1,119 +0,0 @@
1
- // src/index.ts
2
- import sapphirePrettierConfig from "@sapphire/prettier-config";
3
- import { runTransform } from "esm-to-cjs";
4
- import prettier from "prettier";
5
- import ts from "typescript";
6
- var documentationPrettierConfig = {
7
- ...sapphirePrettierConfig,
8
- tabWidth: 2,
9
- useTabs: false,
10
- printWidth: 120,
11
- parser: "babel"
12
- };
13
- var makeTsCompilerOptions = (overrideOptions) => ({
14
- newLine: ts.NewLineKind.LineFeed,
15
- removeComments: false,
16
- esModuleInterop: true,
17
- pretty: true,
18
- ...overrideOptions,
19
- module: ts.ModuleKind.ESNext,
20
- moduleResolution: ts.ModuleResolutionKind.NodeJs,
21
- target: ts.ScriptTarget.ESNext
22
- });
23
- var tsToEsm = (code, options) => ts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });
24
- var esmToCjs = (code) => runTransform(code, { quote: "single", lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });
25
- var escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
26
- var restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
27
- var prettierFormatCode = (code, prettierConfig) => prettier.format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);
28
- var transformNode = (node, options) => {
29
- const groupIdProp = options.sync ? 'groupId="ts2esm2cjs" ' : "";
30
- const tsCode = escapeNewLines(node.value);
31
- const esmCode = tsToEsm(tsCode, { typescriptCompilerOptions: options.typescriptCompilerOptions }).outputText;
32
- const cjsCode = esmToCjs(esmCode);
33
- const [, jsHighlight, tsHighlight] = node.meta.split("|");
34
- return [
35
- {
36
- type: "jsx",
37
- value: `<Tabs defaultValue="typescript" ${groupIdProp}values={[
38
- { label: 'JavaScript', value: 'javascript', },
39
- { label: 'ESM', value: 'esm', },
40
- { label: 'TypeScript', value: 'typescript', },
41
- ]}
42
- >
43
- <TabItem value="javascript">`
44
- },
45
- {
46
- type: node.type,
47
- lang: node.lang,
48
- meta: jsHighlight,
49
- value: prettierFormatCode(restoreNewLines(cjsCode), options.prettierOptions)
50
- },
51
- {
52
- type: "jsx",
53
- value: '</TabItem>\n<TabItem value="esm">'
54
- },
55
- {
56
- type: node.type,
57
- lang: node.lang,
58
- meta: jsHighlight,
59
- value: prettierFormatCode(restoreNewLines(esmCode), options.prettierOptions)
60
- },
61
- {
62
- type: "jsx",
63
- value: '</TabItem>\n<TabItem value="typescript">'
64
- },
65
- {
66
- type: node.type,
67
- lang: node.lang,
68
- meta: tsHighlight,
69
- value: node.value
70
- },
71
- {
72
- type: "jsx",
73
- value: "</TabItem>\n</Tabs>"
74
- }
75
- ];
76
- };
77
- var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && node.meta.startsWith("ts2esm2cjs");
78
- var nodeForImport = {
79
- type: "import",
80
- value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';"
81
- };
82
- function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = {
83
- sync: true,
84
- prettierOptions: {},
85
- typescriptCompilerOptions: {}
86
- }) {
87
- let transformed = false;
88
- let alreadyImported = false;
89
- const transformer = (node) => {
90
- if (node.type === "import" && node.value.includes("@theme/Tabs")) {
91
- alreadyImported = true;
92
- }
93
- if (matchNode(node)) {
94
- transformed = true;
95
- return transformNode(node, { sync, prettierOptions, typescriptCompilerOptions });
96
- }
97
- if (Array.isArray(node.children)) {
98
- let index = 0;
99
- while (index < node.children.length) {
100
- const result = transformer(node.children[index]);
101
- if (result) {
102
- node.children.splice(index, 1, ...result);
103
- index += result.length;
104
- } else {
105
- index += 1;
106
- }
107
- }
108
- }
109
- if (node.type === "root" && transformed && !alreadyImported) {
110
- node.children.unshift(nodeForImport);
111
- }
112
- return null;
113
- };
114
- return transformer;
115
- }
116
- export {
117
- ts2esm2cjs
118
- };
119
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
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,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,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