@sapphire/docusaurus-plugin-ts2esm2cjs 1.1.0-next.8221bc2.0 → 1.1.0-next.9495628.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 +9 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -4,33 +4,27 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
8
7
|
var __export = (target, all) => {
|
|
9
8
|
for (var name in all)
|
|
10
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
10
|
};
|
|
12
|
-
var
|
|
13
|
-
if (
|
|
14
|
-
for (let key of __getOwnPropNames(
|
|
15
|
-
if (!__hasOwnProp.call(
|
|
16
|
-
__defProp(
|
|
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 });
|
|
17
16
|
}
|
|
18
|
-
return
|
|
17
|
+
return to;
|
|
19
18
|
};
|
|
20
|
-
var __toESM = (
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
var __toCommonJS = /* @__PURE__ */ ((cache) => {
|
|
24
|
-
return (module2, temp) => {
|
|
25
|
-
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
|
|
26
|
-
};
|
|
27
|
-
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
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);
|
|
28
21
|
|
|
29
22
|
// src/index.ts
|
|
30
23
|
var src_exports = {};
|
|
31
24
|
__export(src_exports, {
|
|
32
25
|
ts2esm2cjs: () => ts2esm2cjs
|
|
33
26
|
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
28
|
var import_prettier_config = __toESM(require("@sapphire/prettier-config"));
|
|
35
29
|
var import_esm_to_cjs = require("esm-to-cjs");
|
|
36
30
|
var import_prettier = __toESM(require("prettier"));
|
|
@@ -145,7 +139,6 @@ function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptio
|
|
|
145
139
|
};
|
|
146
140
|
return transformer;
|
|
147
141
|
}
|
|
148
|
-
module.exports = __toCommonJS(src_exports);
|
|
149
142
|
// Annotate the CommonJS export names for ESM import in node:
|
|
150
143
|
0 && (module.exports = {
|
|
151
144
|
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":"
|
|
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,WAAW,CAAC,SAAyB,oCAAa,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,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AA,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":[]}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from 'esm-to-cjs';\nimport 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;
|
|
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,UAA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O;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":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
|
|
3
|
-
"version": "1.1.0-next.
|
|
3
|
+
"version": "1.1.0-next.9495628.0",
|
|
4
4
|
"description": "Docusaurus Remark plugin for converting TypeScript code to ESM and CJS code",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"prepublishOnly": "yarn build"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@sapphire/prettier-config": "^1.3
|
|
22
|
+
"@sapphire/prettier-config": "^1.4.3",
|
|
23
23
|
"esm-to-cjs": "^1.2.1",
|
|
24
|
-
"prettier": "^2.
|
|
25
|
-
"typescript": "^4.
|
|
24
|
+
"prettier": "^2.6.2",
|
|
25
|
+
"typescript": "^4.6.3"
|
|
26
26
|
},
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"publishConfig": {
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "949562836cafbc113e9afecb8d97e3dd66e5b580"
|
|
60
60
|
}
|