@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.7-next.fb672d2.0 → 2.0.0-next.61f0a04.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 +121 -73
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -75
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
package/dist/index.js
CHANGED
|
@@ -35,7 +35,6 @@ __export(src_exports, {
|
|
|
35
35
|
ts2esm2cjs: () => ts2esm2cjs
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(src_exports);
|
|
38
|
-
var import_unist_util_visit = __toESM(require("unist-util-visit"));
|
|
39
38
|
|
|
40
39
|
// src/ts2esm2cjs.ts
|
|
41
40
|
var import_sync = require("@prettier/sync");
|
|
@@ -78,93 +77,142 @@ function esm2cjs(input, options) {
|
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
// src/index.ts
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
function createAttribute(attributeName, attributeValue) {
|
|
81
|
+
return {
|
|
82
|
+
type: "mdxJsxAttribute",
|
|
83
|
+
name: attributeName,
|
|
84
|
+
value: attributeValue
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function createTabItem({ code, node, value, label }) {
|
|
85
88
|
let [, jsHighlight, tsHighlight] = (node.meta ?? "").split("|");
|
|
86
89
|
if (!tsHighlight && jsHighlight) {
|
|
87
90
|
tsHighlight = jsHighlight;
|
|
88
91
|
}
|
|
92
|
+
return {
|
|
93
|
+
type: "mdxJsxFlowElement",
|
|
94
|
+
name: "TabItem",
|
|
95
|
+
attributes: [createAttribute("value", value), createAttribute("label", label)],
|
|
96
|
+
children: [
|
|
97
|
+
{
|
|
98
|
+
type: node.type,
|
|
99
|
+
lang: node.lang,
|
|
100
|
+
value: code,
|
|
101
|
+
meta: value === "typescript" ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
var transformNode = (node, options) => {
|
|
107
|
+
const groupIdProp = {
|
|
108
|
+
type: "mdxJsxAttribute",
|
|
109
|
+
name: "groupId",
|
|
110
|
+
value: "ts2esm2cjs"
|
|
111
|
+
};
|
|
112
|
+
const esmCode = ts2esm(node.value, options);
|
|
113
|
+
const cjsCode = esm2cjs(esmCode, options);
|
|
89
114
|
return [
|
|
90
115
|
{
|
|
91
|
-
type: "
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
value: esmCode
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
type: "jsx",
|
|
120
|
-
value: '</TabItem>\n<TabItem value="typescript">'
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
type: node.type,
|
|
124
|
-
lang: node.lang,
|
|
125
|
-
meta: `${tsHighlight} showLineNumbers`,
|
|
126
|
-
value: node.value
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
type: "jsx",
|
|
130
|
-
value: "</TabItem>\n</Tabs>"
|
|
116
|
+
type: "mdxJsxFlowElement",
|
|
117
|
+
name: "Tabs",
|
|
118
|
+
...options.sync && {
|
|
119
|
+
attributes: [groupIdProp]
|
|
120
|
+
},
|
|
121
|
+
children: [
|
|
122
|
+
createTabItem({
|
|
123
|
+
code: cjsCode,
|
|
124
|
+
node,
|
|
125
|
+
value: "cjs",
|
|
126
|
+
label: "CommonJS"
|
|
127
|
+
}),
|
|
128
|
+
createTabItem({
|
|
129
|
+
code: esmCode,
|
|
130
|
+
node,
|
|
131
|
+
value: "esm",
|
|
132
|
+
label: "ESM"
|
|
133
|
+
}),
|
|
134
|
+
createTabItem({
|
|
135
|
+
code: node.value,
|
|
136
|
+
node,
|
|
137
|
+
value: "typescript",
|
|
138
|
+
label: "TypeScript"
|
|
139
|
+
})
|
|
140
|
+
]
|
|
131
141
|
}
|
|
132
142
|
];
|
|
133
143
|
};
|
|
134
|
-
var
|
|
144
|
+
var isMdxEsmLiteral = (node) => node.type === "mdxjsEsm";
|
|
145
|
+
var isTabsImport = (node) => isMdxEsmLiteral(node) && node.value.includes("@theme/Tabs");
|
|
135
146
|
var isParent = (node) => Array.isArray(node.children);
|
|
136
147
|
var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && (node.meta ?? "").startsWith("ts2esm2cjs");
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
148
|
+
function createImportNode() {
|
|
149
|
+
return {
|
|
150
|
+
type: "mdxjsEsm",
|
|
151
|
+
value: "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
|
|
152
|
+
data: {
|
|
153
|
+
estree: {
|
|
154
|
+
type: "Program",
|
|
155
|
+
body: [
|
|
156
|
+
{
|
|
157
|
+
type: "ImportDeclaration",
|
|
158
|
+
specifiers: [
|
|
159
|
+
{
|
|
160
|
+
type: "ImportDefaultSpecifier",
|
|
161
|
+
local: { type: "Identifier", name: "Tabs" }
|
|
162
|
+
}
|
|
163
|
+
],
|
|
164
|
+
source: {
|
|
165
|
+
type: "Literal",
|
|
166
|
+
value: "@theme/Tabs",
|
|
167
|
+
raw: "'@theme/Tabs'"
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
type: "ImportDeclaration",
|
|
172
|
+
specifiers: [
|
|
173
|
+
{
|
|
174
|
+
type: "ImportDefaultSpecifier",
|
|
175
|
+
local: { type: "Identifier", name: "TabItem" }
|
|
176
|
+
}
|
|
177
|
+
],
|
|
178
|
+
source: {
|
|
179
|
+
type: "Literal",
|
|
180
|
+
value: "@theme/TabItem",
|
|
181
|
+
raw: "'@theme/TabItem'"
|
|
182
|
+
}
|
|
160
183
|
}
|
|
161
|
-
|
|
184
|
+
],
|
|
185
|
+
sourceType: "module"
|
|
162
186
|
}
|
|
163
|
-
});
|
|
164
|
-
if (transformed && !alreadyImported) {
|
|
165
|
-
root.children.unshift(nodeForImport);
|
|
166
187
|
}
|
|
167
188
|
};
|
|
189
|
+
}
|
|
190
|
+
var ts2esm2cjs = ({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }) => async (root) => {
|
|
191
|
+
const { visit } = await import("unist-util-visit");
|
|
192
|
+
let transformed = false;
|
|
193
|
+
let alreadyImported = false;
|
|
194
|
+
visit(root, (node) => {
|
|
195
|
+
if (isTabsImport(node)) {
|
|
196
|
+
alreadyImported = true;
|
|
197
|
+
}
|
|
198
|
+
if (isParent(node)) {
|
|
199
|
+
let index = 0;
|
|
200
|
+
while (index < node.children.length) {
|
|
201
|
+
const child = node.children[index];
|
|
202
|
+
if (matchNode(child)) {
|
|
203
|
+
const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });
|
|
204
|
+
node.children.splice(index, 1, ...result);
|
|
205
|
+
index += result.length;
|
|
206
|
+
transformed = true;
|
|
207
|
+
} else {
|
|
208
|
+
index += 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
if (transformed && !alreadyImported) {
|
|
214
|
+
root.children.unshift(createImportNode());
|
|
215
|
+
}
|
|
168
216
|
};
|
|
169
217
|
// Annotate the CommonJS export names for ESM import in node:
|
|
170
218
|
0 && (module.exports = {
|
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 { 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,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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","visit"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ts2esm2cjs.ts"],"sourcesContent":["import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\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 = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\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');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\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(createImportNode());\n\t\t}\n\t};\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;;;ACAA,kB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CAAC,EAAE,MAE1I,OAAO,SAAS;AACf,QAAM,EAAE,MAAM,IAAI,MAAM,OAAO,kBAAkB;AAEjD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,MAAM,CAAC,SAAe;AAC3B,QAAI,aAAa,IAAI,GAAG;AACvB,wBAAkB;AAAA,IACnB;AAEA,QAAI,SAAS,IAAI,GAAG;AACnB,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,QAAQ,KAAK,SAAS,KAAK;AACjC,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,iBAAiB,CAAC;AAAA,EACrD;AACD;","names":["ts"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import visit from "unist-util-visit";
|
|
3
|
-
|
|
4
1
|
// src/ts2esm2cjs.ts
|
|
5
2
|
import { format } from "@prettier/sync";
|
|
6
3
|
import { runTransform } from "esm-to-cjs";
|
|
@@ -42,93 +39,142 @@ function esm2cjs(input, options) {
|
|
|
42
39
|
}
|
|
43
40
|
|
|
44
41
|
// src/index.ts
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
function createAttribute(attributeName, attributeValue) {
|
|
43
|
+
return {
|
|
44
|
+
type: "mdxJsxAttribute",
|
|
45
|
+
name: attributeName,
|
|
46
|
+
value: attributeValue
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function createTabItem({ code, node, value, label }) {
|
|
49
50
|
let [, jsHighlight, tsHighlight] = (node.meta ?? "").split("|");
|
|
50
51
|
if (!tsHighlight && jsHighlight) {
|
|
51
52
|
tsHighlight = jsHighlight;
|
|
52
53
|
}
|
|
54
|
+
return {
|
|
55
|
+
type: "mdxJsxFlowElement",
|
|
56
|
+
name: "TabItem",
|
|
57
|
+
attributes: [createAttribute("value", value), createAttribute("label", label)],
|
|
58
|
+
children: [
|
|
59
|
+
{
|
|
60
|
+
type: node.type,
|
|
61
|
+
lang: node.lang,
|
|
62
|
+
value: code,
|
|
63
|
+
meta: value === "typescript" ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
var transformNode = (node, options) => {
|
|
69
|
+
const groupIdProp = {
|
|
70
|
+
type: "mdxJsxAttribute",
|
|
71
|
+
name: "groupId",
|
|
72
|
+
value: "ts2esm2cjs"
|
|
73
|
+
};
|
|
74
|
+
const esmCode = ts2esm(node.value, options);
|
|
75
|
+
const cjsCode = esm2cjs(esmCode, options);
|
|
53
76
|
return [
|
|
54
77
|
{
|
|
55
|
-
type: "
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
value: esmCode
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
type: "jsx",
|
|
84
|
-
value: '</TabItem>\n<TabItem value="typescript">'
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
type: node.type,
|
|
88
|
-
lang: node.lang,
|
|
89
|
-
meta: `${tsHighlight} showLineNumbers`,
|
|
90
|
-
value: node.value
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
type: "jsx",
|
|
94
|
-
value: "</TabItem>\n</Tabs>"
|
|
78
|
+
type: "mdxJsxFlowElement",
|
|
79
|
+
name: "Tabs",
|
|
80
|
+
...options.sync && {
|
|
81
|
+
attributes: [groupIdProp]
|
|
82
|
+
},
|
|
83
|
+
children: [
|
|
84
|
+
createTabItem({
|
|
85
|
+
code: cjsCode,
|
|
86
|
+
node,
|
|
87
|
+
value: "cjs",
|
|
88
|
+
label: "CommonJS"
|
|
89
|
+
}),
|
|
90
|
+
createTabItem({
|
|
91
|
+
code: esmCode,
|
|
92
|
+
node,
|
|
93
|
+
value: "esm",
|
|
94
|
+
label: "ESM"
|
|
95
|
+
}),
|
|
96
|
+
createTabItem({
|
|
97
|
+
code: node.value,
|
|
98
|
+
node,
|
|
99
|
+
value: "typescript",
|
|
100
|
+
label: "TypeScript"
|
|
101
|
+
})
|
|
102
|
+
]
|
|
95
103
|
}
|
|
96
104
|
];
|
|
97
105
|
};
|
|
98
|
-
var
|
|
106
|
+
var isMdxEsmLiteral = (node) => node.type === "mdxjsEsm";
|
|
107
|
+
var isTabsImport = (node) => isMdxEsmLiteral(node) && node.value.includes("@theme/Tabs");
|
|
99
108
|
var isParent = (node) => Array.isArray(node.children);
|
|
100
109
|
var matchNode = (node) => node.type === "code" && typeof node.meta === "string" && (node.meta ?? "").startsWith("ts2esm2cjs");
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
110
|
+
function createImportNode() {
|
|
111
|
+
return {
|
|
112
|
+
type: "mdxjsEsm",
|
|
113
|
+
value: "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
|
|
114
|
+
data: {
|
|
115
|
+
estree: {
|
|
116
|
+
type: "Program",
|
|
117
|
+
body: [
|
|
118
|
+
{
|
|
119
|
+
type: "ImportDeclaration",
|
|
120
|
+
specifiers: [
|
|
121
|
+
{
|
|
122
|
+
type: "ImportDefaultSpecifier",
|
|
123
|
+
local: { type: "Identifier", name: "Tabs" }
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
source: {
|
|
127
|
+
type: "Literal",
|
|
128
|
+
value: "@theme/Tabs",
|
|
129
|
+
raw: "'@theme/Tabs'"
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
type: "ImportDeclaration",
|
|
134
|
+
specifiers: [
|
|
135
|
+
{
|
|
136
|
+
type: "ImportDefaultSpecifier",
|
|
137
|
+
local: { type: "Identifier", name: "TabItem" }
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
source: {
|
|
141
|
+
type: "Literal",
|
|
142
|
+
value: "@theme/TabItem",
|
|
143
|
+
raw: "'@theme/TabItem'"
|
|
144
|
+
}
|
|
124
145
|
}
|
|
125
|
-
|
|
146
|
+
],
|
|
147
|
+
sourceType: "module"
|
|
126
148
|
}
|
|
127
|
-
});
|
|
128
|
-
if (transformed && !alreadyImported) {
|
|
129
|
-
root.children.unshift(nodeForImport);
|
|
130
149
|
}
|
|
131
150
|
};
|
|
151
|
+
}
|
|
152
|
+
var ts2esm2cjs = ({ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }) => async (root) => {
|
|
153
|
+
const { visit } = await import("unist-util-visit");
|
|
154
|
+
let transformed = false;
|
|
155
|
+
let alreadyImported = false;
|
|
156
|
+
visit(root, (node) => {
|
|
157
|
+
if (isTabsImport(node)) {
|
|
158
|
+
alreadyImported = true;
|
|
159
|
+
}
|
|
160
|
+
if (isParent(node)) {
|
|
161
|
+
let index = 0;
|
|
162
|
+
while (index < node.children.length) {
|
|
163
|
+
const child = node.children[index];
|
|
164
|
+
if (matchNode(child)) {
|
|
165
|
+
const result = transformNode(child, { sync, prettierOptions, typescriptCompilerOptions });
|
|
166
|
+
node.children.splice(index, 1, ...result);
|
|
167
|
+
index += result.length;
|
|
168
|
+
transformed = true;
|
|
169
|
+
} else {
|
|
170
|
+
index += 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
if (transformed && !alreadyImported) {
|
|
176
|
+
root.children.unshift(createImportNode());
|
|
177
|
+
}
|
|
132
178
|
};
|
|
133
179
|
export {
|
|
134
180
|
esm2cjs,
|
package/dist/index.mjs.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 { 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":";AAGA,OAAO,WAAW;;;ACHlB,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAE7B,OAAO,QAAkC;AAGzC,IAAM,wBAAwB,CAAC,qBAAwD;AAAA,EACtF,SAAS,GAAG,YAAY;AAAA,EACxB,gBAAgB;AAA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,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":[]}
|
|
1
|
+
{"version":3,"sources":["../src/ts2esm2cjs.ts","../src/index.ts"],"sourcesContent":["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","import type { Code, Literal, RootContent } from 'mdast';\nimport type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';\nimport type { Plugin, Transformer } from 'unified';\nimport type { Node, Parent } from 'unist';\nimport { esm2cjs, ts2esm } from './ts2esm2cjs';\nimport type { PluginOptions } from './types';\n\nexport { esm2cjs, ts2esm } from './ts2esm2cjs';\nexport type { PluginOptions } from './types';\n\nfunction createAttribute(attributeName: string, attributeValue: MdxJsxAttribute['value']): MdxJsxAttribute {\n\treturn {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: attributeName,\n\t\tvalue: attributeValue\n\t};\n}\n\ninterface CreateTabItemOptions {\n\tcode: string;\n\tnode: Code;\n\tvalue: 'cjs' | 'esm' | 'typescript';\n\tlabel: string;\n}\n\nfunction createTabItem({ code, node, value, label }: CreateTabItemOptions): MdxJsxFlowElement {\n\tlet [, jsHighlight, tsHighlight] = (node.meta ?? '').split('|');\n\n\tif (!tsHighlight && jsHighlight) {\n\t\ttsHighlight = jsHighlight;\n\t}\n\n\treturn {\n\t\ttype: 'mdxJsxFlowElement',\n\t\tname: 'TabItem',\n\t\tattributes: [createAttribute('value', value), createAttribute('label', label)],\n\t\tchildren: [\n\t\t\t{\n\t\t\t\ttype: node.type,\n\t\t\t\tlang: node.lang,\n\t\t\t\tvalue: code,\n\t\t\t\tmeta: value === 'typescript' ? `${tsHighlight} showLineNumbers` : `${jsHighlight} showLineNumbers`\n\t\t\t}\n\t\t]\n\t};\n}\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 = {\n\t\ttype: 'mdxJsxAttribute',\n\t\tname: 'groupId',\n\t\tvalue: 'ts2esm2cjs'\n\t};\n\n\tconst esmCode = ts2esm(node.value, options);\n\tconst cjsCode = esm2cjs(esmCode, options);\n\n\treturn [\n\t\t{\n\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\tname: 'Tabs',\n\t\t\t...(options.sync && {\n\t\t\t\tattributes: [groupIdProp]\n\t\t\t}),\n\t\t\tchildren: [\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: cjsCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'cjs',\n\t\t\t\t\tlabel: 'CommonJS'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: esmCode,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'esm',\n\t\t\t\t\tlabel: 'ESM'\n\t\t\t\t}),\n\t\t\t\tcreateTabItem({\n\t\t\t\t\tcode: node.value,\n\t\t\t\t\tnode,\n\t\t\t\t\tvalue: 'typescript',\n\t\t\t\t\tlabel: 'TypeScript'\n\t\t\t\t})\n\t\t\t]\n\t\t}\n\t] as RootContent[];\n};\n\nconst isMdxEsmLiteral = (node: Node): node is Literal => node.type === 'mdxjsEsm';\nconst isTabsImport = (node: Node): boolean => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');\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');\n\nfunction createImportNode() {\n\treturn {\n\t\ttype: 'mdxjsEsm',\n\t\tvalue: \"import Tabs from '@theme/Tabs'\\nimport TabItem from '@theme/TabItem'\",\n\t\tdata: {\n\t\t\testree: {\n\t\t\t\ttype: 'Program',\n\t\t\t\tbody: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'Tabs' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/Tabs',\n\t\t\t\t\t\t\traw: \"'@theme/Tabs'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'ImportDeclaration',\n\t\t\t\t\t\tspecifiers: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'ImportDefaultSpecifier',\n\t\t\t\t\t\t\t\tlocal: { type: 'Identifier', name: 'TabItem' }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t],\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\tvalue: '@theme/TabItem',\n\t\t\t\t\t\t\traw: \"'@theme/TabItem'\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tsourceType: 'module'\n\t\t\t}\n\t\t}\n\t};\n}\n\nexport const ts2esm2cjs: Plugin<[PluginOptions?]> =\n\t(\n\t\t{ sync = true, prettierOptions = {}, typescriptCompilerOptions = {} } = { sync: true, prettierOptions: {}, typescriptCompilerOptions: {} }\n\t): Transformer =>\n\tasync (root) => {\n\t\tconst { visit } = await import('unist-util-visit');\n\n\t\tlet transformed = false;\n\t\tlet alreadyImported = false;\n\n\t\tvisit(root, (node: Node) => {\n\t\t\tif (isTabsImport(node)) {\n\t\t\t\talreadyImported = true;\n\t\t\t}\n\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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AAEO,IAAM,aACZ,CACC,EAAE,OAAO,MAAM,kBAAkB,CAAC,GAAG,4BAA4B,CAAC,EAAE,IAAI,EAAE,MAAM,MAAM,iBAAiB,CAAC,GAAG,2BAA2B,CAAC,EAAE,MAE1I,OAAO,SAAS;AACf,QAAM,EAAE,MAAM,IAAI,MAAM,OAAO,kBAAkB;AAEjD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,MAAM,CAAC,SAAe;AAC3B,QAAI,aAAa,IAAI,GAAG;AACvB,wBAAkB;AAAA,IACnB;AAEA,QAAI,SAAS,IAAI,GAAG;AACnB,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,QAAQ,KAAK,SAAS,KAAK;AACjC,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,iBAAiB,CAAC;AAAA,EACrD;AACD;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-next.61f0a04.0",
|
|
4
4
|
"description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,8 +30,10 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@prettier/sync": "^0.3.0",
|
|
32
32
|
"esm-to-cjs": "^1.2.1",
|
|
33
|
+
"mdast-util-mdx": "^3.0.0",
|
|
33
34
|
"typescript": "^5.2.2",
|
|
34
|
-
"
|
|
35
|
+
"unified": "^11.0.3",
|
|
36
|
+
"unist-util-visit": "^5.0.0"
|
|
35
37
|
},
|
|
36
38
|
"repository": {
|
|
37
39
|
"type": "git",
|
|
@@ -43,8 +45,7 @@
|
|
|
43
45
|
"!dist/.tsbuildinfo"
|
|
44
46
|
],
|
|
45
47
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
47
|
-
"npm": ">=7.0.0"
|
|
48
|
+
"node": ">=18.0"
|
|
48
49
|
},
|
|
49
50
|
"keywords": [
|
|
50
51
|
"docusaurus",
|
|
@@ -66,9 +67,9 @@
|
|
|
66
67
|
"access": "public"
|
|
67
68
|
},
|
|
68
69
|
"devDependencies": {
|
|
69
|
-
"@favware/cliff-jumper": "^2.
|
|
70
|
-
"@sapphire/framework": "^4.
|
|
70
|
+
"@favware/cliff-jumper": "^2.2.0",
|
|
71
|
+
"@sapphire/framework": "^4.7.2",
|
|
71
72
|
"tsup": "^7.2.0",
|
|
72
|
-
"vitest": "^0.34.
|
|
73
|
+
"vitest": "^0.34.6"
|
|
73
74
|
}
|
|
74
75
|
}
|