@sapphire/docusaurus-plugin-ts2esm2cjs 1.0.2 → 1.0.3-next.2528066.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.d.ts +2 -35
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -337
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -350
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
export declare const documentationPrettierConfig: Options;
|
|
4
|
-
export declare const makeTsCompilerOptions: (overrideOptions?: ts.CompilerOptions | undefined) => CompilerOptions;
|
|
5
|
-
/**
|
|
6
|
-
* Transpiles input TypeScript code to ESM code.
|
|
7
|
-
* @param code The code to transpile
|
|
8
|
-
* @returns Input code transpiled to ESM
|
|
9
|
-
*/
|
|
10
|
-
export declare const tsToEsm: (code: string, options: Pick<PluginOptions, 'typescriptCompilerOptions'>) => ts.TranspileOutput;
|
|
11
|
-
/**
|
|
12
|
-
* Transforms input ESM code to CJS code.
|
|
13
|
-
* @param code The code to transform
|
|
14
|
-
* @returns Input code transformed to CommonJS
|
|
15
|
-
*/
|
|
16
|
-
export declare const esmToCjs: (code: string) => string;
|
|
17
|
-
/**
|
|
18
|
-
* Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}
|
|
19
|
-
* @param code The code to escape new lines in
|
|
20
|
-
* @returns The same code but with new lines escaped using block comments
|
|
21
|
-
*/
|
|
22
|
-
export declare const escapeNewLines: (code: string) => string;
|
|
23
|
-
/**
|
|
24
|
-
* Reverses {@link escapeNewLines} and restores new lines
|
|
25
|
-
* @param code The code with escaped new lines
|
|
26
|
-
* @returns The same code with new lines restored
|
|
27
|
-
*/
|
|
28
|
-
export declare const restoreNewLines: (code: string) => string;
|
|
29
|
-
/**
|
|
30
|
-
* Formats the code using Prettier
|
|
31
|
-
* @param code The code to prettier format
|
|
32
|
-
* @param prettierConfig Additional prettier options to use for formatting
|
|
33
|
-
* @returns Prettier formatted code
|
|
34
|
-
*/
|
|
35
|
-
export declare const prettierFormatCode: (code: string, prettierConfig?: prettier.Options | undefined) => string;
|
|
1
|
+
import { Options } from 'prettier';
|
|
2
|
+
import { CompilerOptions } from 'typescript';
|
|
36
3
|
export interface PluginOptions {
|
|
37
4
|
sync?: boolean;
|
|
38
5
|
prettierOptions?: Options;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,
|
|
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"}
|
package/dist/index.js
CHANGED
|
@@ -5,12 +5,6 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
7
|
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
8
|
-
var __esm = (fn, res) => function __init() {
|
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
-
};
|
|
11
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
12
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
|
-
};
|
|
14
8
|
var __export = (target, all) => {
|
|
15
9
|
for (var name in all)
|
|
16
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -32,326 +26,13 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
|
|
|
32
26
|
};
|
|
33
27
|
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
34
28
|
|
|
35
|
-
// ../../node_modules/tsup/assets/cjs_shims.js
|
|
36
|
-
var init_cjs_shims = __esm({
|
|
37
|
-
"../../node_modules/tsup/assets/cjs_shims.js"() {
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// src/esmToCjs.js
|
|
42
|
-
var require_esmToCjs = __commonJS({
|
|
43
|
-
"src/esmToCjs.js"(exports, module2) {
|
|
44
|
-
init_cjs_shims();
|
|
45
|
-
module2.exports.runTransform = runTransform2;
|
|
46
|
-
var LOOKING_FOR;
|
|
47
|
-
var DISTANCE = 6;
|
|
48
|
-
var defaultOptions = {
|
|
49
|
-
quote: "double",
|
|
50
|
-
lenDestructure: 60,
|
|
51
|
-
lenModuleName: 20,
|
|
52
|
-
lenIdentifier: 20,
|
|
53
|
-
indent: 2
|
|
54
|
-
};
|
|
55
|
-
function runTransform2(str, options = {}) {
|
|
56
|
-
options = { ...defaultOptions, ...options };
|
|
57
|
-
options.quote = options.quote === "single" ? "'" : '"';
|
|
58
|
-
const buffer = [];
|
|
59
|
-
const exportBuffer = {
|
|
60
|
-
items: [],
|
|
61
|
-
requires: []
|
|
62
|
-
};
|
|
63
|
-
let pos = 0;
|
|
64
|
-
for (const token of tokenize(str, options)) {
|
|
65
|
-
buffer.push(str.slice(pos, token.start));
|
|
66
|
-
buffer.push(transform(token, str, exportBuffer, options));
|
|
67
|
-
pos = token.end + 1;
|
|
68
|
-
}
|
|
69
|
-
pos = skipNewLines(str, pos);
|
|
70
|
-
buffer.push(str.slice(pos, str.length));
|
|
71
|
-
if (exportBuffer.items.length) {
|
|
72
|
-
const indent = " ".repeat(options.indent);
|
|
73
|
-
for (const item of exportBuffer.requires) {
|
|
74
|
-
buffer.push(item);
|
|
75
|
-
}
|
|
76
|
-
buffer.push("\nmodule.exports = {\n");
|
|
77
|
-
const exportNames = exportBuffer.items.map((item) => `${indent}${item[0]}${item[1] ? `: ${item[1]}` : ""}`);
|
|
78
|
-
buffer.push(exportNames.join(",\n"));
|
|
79
|
-
buffer.push("\n}");
|
|
80
|
-
}
|
|
81
|
-
buffer.push("\n");
|
|
82
|
-
return buffer.join("");
|
|
83
|
-
}
|
|
84
|
-
function transform(token, str, exportBuffer, { indent }) {
|
|
85
|
-
indent = " ".repeat(indent);
|
|
86
|
-
const { type } = token;
|
|
87
|
-
switch (type) {
|
|
88
|
-
case "import": {
|
|
89
|
-
const identifiers = token.modules.map((s) => s.join(": ")).join(", ");
|
|
90
|
-
return `const { ${identifiers} } = require(${token.moduleName})`;
|
|
91
|
-
}
|
|
92
|
-
case "import*":
|
|
93
|
-
case "importDefault": {
|
|
94
|
-
const { identifier, moduleName } = token;
|
|
95
|
-
return `const ${identifier} = require(${moduleName})${token.isDefaultImport ? ".default" : ""}`;
|
|
96
|
-
}
|
|
97
|
-
case "awaitImport": {
|
|
98
|
-
return `require(${token.moduleName})`;
|
|
99
|
-
}
|
|
100
|
-
case "export": {
|
|
101
|
-
exportBuffer.items.push(token.modules);
|
|
102
|
-
return "";
|
|
103
|
-
}
|
|
104
|
-
case "reExport": {
|
|
105
|
-
const { moduleName } = token;
|
|
106
|
-
if (token.modules.length === 1) {
|
|
107
|
-
const [original, alias] = token.modules[0];
|
|
108
|
-
exportBuffer.items.push([alias ? alias : original, `require(${moduleName}).${original}`]);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
exportBuffer.requires.push("const {\n");
|
|
112
|
-
const names = token.modules.map(([original]) => `${indent}${original}: __${original}__`).join(",\n");
|
|
113
|
-
exportBuffer.requires.push(names);
|
|
114
|
-
exportBuffer.requires.push(`
|
|
115
|
-
} = require(${moduleName});`);
|
|
116
|
-
for (const [original, alias] of token.modules) {
|
|
117
|
-
exportBuffer.items.push([alias ? alias : original, `__${original}__`]);
|
|
118
|
-
}
|
|
119
|
-
return "";
|
|
120
|
-
}
|
|
121
|
-
case "reExportImported": {
|
|
122
|
-
exportBuffer.items.push(...token.modules);
|
|
123
|
-
return "";
|
|
124
|
-
}
|
|
125
|
-
default:
|
|
126
|
-
throw new Error("should not reach here");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
String.prototype.indexWithin = indexWithin;
|
|
130
|
-
function* tokenize(str, options) {
|
|
131
|
-
const { quote, lenDestructure, lenModuleName, lenIdentifier } = options;
|
|
132
|
-
let start = 0;
|
|
133
|
-
let pos;
|
|
134
|
-
const types = /* @__PURE__ */ new Map([
|
|
135
|
-
["import", "import "],
|
|
136
|
-
["export", "export "],
|
|
137
|
-
["awaitImport", "await import("]
|
|
138
|
-
]);
|
|
139
|
-
while (types.size !== 0) {
|
|
140
|
-
pos = Number.POSITIVE_INFINITY;
|
|
141
|
-
let type;
|
|
142
|
-
for (const t of types.keys()) {
|
|
143
|
-
const idx = str.indexOf(types.get(t), start);
|
|
144
|
-
if (idx === -1) {
|
|
145
|
-
types.delete(t);
|
|
146
|
-
} else if (idx < pos) {
|
|
147
|
-
pos = idx;
|
|
148
|
-
type = t;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
switch (type) {
|
|
152
|
-
case "import":
|
|
153
|
-
yield handleImport();
|
|
154
|
-
break;
|
|
155
|
-
case "export":
|
|
156
|
-
yield handleExport();
|
|
157
|
-
break;
|
|
158
|
-
case "awaitImport":
|
|
159
|
-
yield handleAwaitImport();
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
function handleImport() {
|
|
164
|
-
LOOKING_FOR = "import names";
|
|
165
|
-
const braceStart = str.indexWithin("{", pos + 7, DISTANCE, false);
|
|
166
|
-
if (braceStart === -1) {
|
|
167
|
-
return handleImportStar();
|
|
168
|
-
}
|
|
169
|
-
const braceEnd = str.indexWithin("}", braceStart + 1, lenDestructure);
|
|
170
|
-
LOOKING_FOR = "name of imported module";
|
|
171
|
-
let moduleStart = str.indexWithin("from ", braceEnd + 1, DISTANCE);
|
|
172
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1, 5);
|
|
173
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
174
|
-
start = moduleEnd + 1;
|
|
175
|
-
return {
|
|
176
|
-
type: "import",
|
|
177
|
-
start: pos,
|
|
178
|
-
end: moduleEnd,
|
|
179
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1)),
|
|
180
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
function handleAwaitImport() {
|
|
184
|
-
LOOKING_FOR = "name of imported module for await import()";
|
|
185
|
-
const moduleStart = str.indexWithin("(", pos + 12, 10) + 1;
|
|
186
|
-
const moduleEnd = str.indexWithin(")", moduleStart + 1, lenIdentifier) - 1;
|
|
187
|
-
start = moduleEnd + 2;
|
|
188
|
-
return {
|
|
189
|
-
type: "awaitImport",
|
|
190
|
-
start: pos,
|
|
191
|
-
end: moduleEnd + 1,
|
|
192
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
function handleExport() {
|
|
196
|
-
LOOKING_FOR = "export pattern";
|
|
197
|
-
let skipStart = pos + "export ".length;
|
|
198
|
-
if (str.indexWithin("{", skipStart, 5, false) !== -1) {
|
|
199
|
-
return handleReExport();
|
|
200
|
-
} else if (str.indexWithin("*", skipStart, 5, false) !== -1) {
|
|
201
|
-
return handleExportStar();
|
|
202
|
-
}
|
|
203
|
-
LOOKING_FOR = "identifier type (function|class|const) for export";
|
|
204
|
-
if (str.indexWithin("async ", skipStart, DISTANCE, false) !== -1) {
|
|
205
|
-
skipStart += 6;
|
|
206
|
-
}
|
|
207
|
-
let isDefaultExport = false;
|
|
208
|
-
if (str.indexWithin("default ", skipStart, DISTANCE, false) !== -1) {
|
|
209
|
-
skipStart += 8;
|
|
210
|
-
isDefaultExport = true;
|
|
211
|
-
}
|
|
212
|
-
const typeEnd = str.indexWithin(" ", skipStart, 9);
|
|
213
|
-
const exportType = str.slice(skipStart, typeEnd);
|
|
214
|
-
LOOKING_FOR = "export identifiers";
|
|
215
|
-
const identifierStart = str.indexWithin(" ", skipStart + exportType.length, 5) + 1;
|
|
216
|
-
const identifierEnd = str.indexWithin(exportType === "function" ? "(" : " ", identifierStart, lenIdentifier) - 1;
|
|
217
|
-
const end = pos + 6 + (isDefaultExport ? 8 : 0);
|
|
218
|
-
const modules = isDefaultExport ? ["default", str.slice(identifierStart, identifierEnd + 1)] : [str.slice(identifierStart, identifierEnd + 1)];
|
|
219
|
-
start = end + 1;
|
|
220
|
-
return {
|
|
221
|
-
type: "export",
|
|
222
|
-
start: pos,
|
|
223
|
-
end,
|
|
224
|
-
modules
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function handleImportStar() {
|
|
228
|
-
LOOKING_FOR = "import name for import*";
|
|
229
|
-
let identifierStart = str.indexWithin("* as ", pos + 7, DISTANCE, false);
|
|
230
|
-
if (identifierStart === -1) {
|
|
231
|
-
return handleDefaultImport();
|
|
232
|
-
}
|
|
233
|
-
identifierStart += 5;
|
|
234
|
-
const identifierEnd = str.indexWithin(" ", identifierStart + 1, lenIdentifier);
|
|
235
|
-
LOOKING_FOR = "name of imported module for import*";
|
|
236
|
-
let moduleStart = str.indexWithin("from ", identifierEnd + 1) + "from".length;
|
|
237
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1);
|
|
238
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
239
|
-
start = moduleEnd + 1;
|
|
240
|
-
return {
|
|
241
|
-
type: "import*",
|
|
242
|
-
start: pos,
|
|
243
|
-
end: moduleEnd,
|
|
244
|
-
identifier: str.slice(identifierStart, identifierEnd),
|
|
245
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
function handleDefaultImport() {
|
|
249
|
-
LOOKING_FOR = "import name for default import";
|
|
250
|
-
const identifierStart = pos + 7;
|
|
251
|
-
const identifierEnd = str.indexWithin(" ", identifierStart, DISTANCE);
|
|
252
|
-
LOOKING_FOR = "name of imported module for import*";
|
|
253
|
-
let moduleStart = str.indexWithin("from ", identifierEnd + 1) + "from".length;
|
|
254
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1);
|
|
255
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
256
|
-
start = moduleEnd + 1;
|
|
257
|
-
return {
|
|
258
|
-
type: "importDefault",
|
|
259
|
-
start: pos,
|
|
260
|
-
end: moduleEnd,
|
|
261
|
-
isDefaultImport: true,
|
|
262
|
-
identifier: str.slice(identifierStart, identifierEnd),
|
|
263
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
function handleReExport() {
|
|
267
|
-
LOOKING_FOR = "export pattern for re-export";
|
|
268
|
-
const braceStart = str.indexWithin("{", pos + "export ".length, 5);
|
|
269
|
-
const braceEnd = str.indexWithin("}", braceStart + 1, lenDestructure);
|
|
270
|
-
LOOKING_FOR = "name of re-exported module";
|
|
271
|
-
let moduleStart = str.indexWithin("from ", braceEnd + 1, 10, false);
|
|
272
|
-
if (moduleStart === -1) {
|
|
273
|
-
const end2 = skipNewLines(str, braceEnd);
|
|
274
|
-
start = end2 + 1;
|
|
275
|
-
return {
|
|
276
|
-
type: "reExportImported",
|
|
277
|
-
start: pos,
|
|
278
|
-
end: end2,
|
|
279
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1))
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
moduleStart = str.indexWithin(quote, moduleStart, "from ".length + 4);
|
|
283
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
284
|
-
const end = skipNewLines(str, moduleEnd);
|
|
285
|
-
start = end + 1;
|
|
286
|
-
return {
|
|
287
|
-
type: "reExport",
|
|
288
|
-
start: pos,
|
|
289
|
-
end,
|
|
290
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1)),
|
|
291
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
function handleExportStar() {
|
|
295
|
-
throw new Error("not implemented");
|
|
296
|
-
}
|
|
297
|
-
function destructureModules(objLiteral) {
|
|
298
|
-
return objLiteral.trim().slice(1, -1).split(/,\s*/).map((i) => i.trim()).filter((i) => i).map((i) => i.split(/\s*\bas\b\s*/));
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
function indexWithin(needle, from, within = 99, throws = true) {
|
|
302
|
-
for (let i = from, L = from + within, j = 0; i < L; ++i) {
|
|
303
|
-
if (this.charCodeAt(i) === needle.charCodeAt(j)) {
|
|
304
|
-
while (j < needle.length) {
|
|
305
|
-
if (this.charCodeAt(i + j) === needle.charCodeAt(j)) {
|
|
306
|
-
++j;
|
|
307
|
-
} else {
|
|
308
|
-
j = 0;
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
if (j === needle.length) {
|
|
313
|
-
return i;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (throws) {
|
|
318
|
-
throw new Error(`ParseError: Failed to find \`${needle}\` within ${within} characters from position ${from}${LOOKING_FOR ? ` while looking for ${LOOKING_FOR}` : ""}
|
|
319
|
-
|
|
320
|
-
INPUT STRING:
|
|
321
|
-
${"*".repeat(20)}
|
|
322
|
-
${this}
|
|
323
|
-
${"*".repeat(20)}
|
|
324
|
-
`);
|
|
325
|
-
} else {
|
|
326
|
-
return -1;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
function skipNewLines(str, i) {
|
|
330
|
-
if (str.charAt(i + 1) === ";")
|
|
331
|
-
++i;
|
|
332
|
-
while (i < str.length && /\s/.test(str.charAt(i))) {
|
|
333
|
-
++i;
|
|
334
|
-
}
|
|
335
|
-
return i;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
|
|
340
29
|
// src/index.ts
|
|
341
30
|
var src_exports = {};
|
|
342
31
|
__export(src_exports, {
|
|
343
|
-
|
|
344
|
-
escapeNewLines: () => escapeNewLines,
|
|
345
|
-
esmToCjs: () => esmToCjs,
|
|
346
|
-
makeTsCompilerOptions: () => makeTsCompilerOptions,
|
|
347
|
-
prettierFormatCode: () => prettierFormatCode,
|
|
348
|
-
restoreNewLines: () => restoreNewLines,
|
|
349
|
-
ts2esm2cjs: () => ts2esm2cjs,
|
|
350
|
-
tsToEsm: () => tsToEsm
|
|
32
|
+
ts2esm2cjs: () => ts2esm2cjs
|
|
351
33
|
});
|
|
352
|
-
init_cjs_shims();
|
|
353
34
|
var import_prettier_config = __toESM(require("@sapphire/prettier-config"));
|
|
354
|
-
var
|
|
35
|
+
var import_esm_to_cjs = require("esm-to-cjs");
|
|
355
36
|
var import_prettier = __toESM(require("prettier"));
|
|
356
37
|
var import_typescript = __toESM(require("typescript"));
|
|
357
38
|
var documentationPrettierConfig = {
|
|
@@ -372,7 +53,7 @@ var makeTsCompilerOptions = (overrideOptions) => ({
|
|
|
372
53
|
target: import_typescript.default.ScriptTarget.ESNext
|
|
373
54
|
});
|
|
374
55
|
var tsToEsm = (code, options) => import_typescript.default.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });
|
|
375
|
-
var esmToCjs = (code) => (0,
|
|
56
|
+
var esmToCjs = (code) => (0, import_esm_to_cjs.runTransform)(code, { quote: "single", lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });
|
|
376
57
|
var escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
|
|
377
58
|
var restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
|
|
378
59
|
var prettierFormatCode = (code, prettierConfig) => import_prettier.default.format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);
|
|
@@ -467,20 +148,6 @@ function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptio
|
|
|
467
148
|
module.exports = __toCommonJS(src_exports);
|
|
468
149
|
// Annotate the CommonJS export names for ESM import in node:
|
|
469
150
|
0 && (module.exports = {
|
|
470
|
-
|
|
471
|
-
escapeNewLines,
|
|
472
|
-
esmToCjs,
|
|
473
|
-
makeTsCompilerOptions,
|
|
474
|
-
prettierFormatCode,
|
|
475
|
-
restoreNewLines,
|
|
476
|
-
ts2esm2cjs,
|
|
477
|
-
tsToEsm
|
|
151
|
+
ts2esm2cjs
|
|
478
152
|
});
|
|
479
|
-
/**
|
|
480
|
-
* @license MIT
|
|
481
|
-
* @copyright 2018 Sid Vishnoi <sidvishnoi8@gmail.com>
|
|
482
|
-
* @see https://github.com/sidvishnoi/esm-to-cjs/blob/d032518da994b6d28cff5fd38ac2d0b45a13f0d9/src/core.js
|
|
483
|
-
*
|
|
484
|
-
* Modified code is line 340
|
|
485
|
-
*/
|
|
486
153
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../node_modules/tsup/assets/cjs_shims.js","../src/esmToCjs.js","../src/index.ts"],"sourcesContent":["// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL('file:' + __filename).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","/* eslint-disable @typescript-eslint/no-invalid-this, @typescript-eslint/switch-exhaustiveness-check, no-extend-native, no-useless-concat */\n\n/**\n * @license MIT\n * @copyright 2018 Sid Vishnoi <sidvishnoi8@gmail.com>\n * @see https://github.com/sidvishnoi/esm-to-cjs/blob/d032518da994b6d28cff5fd38ac2d0b45a13f0d9/src/core.js\n *\n * Modified code is line 340\n */\n\nmodule.exports.runTransform = runTransform;\n\n// gives parsing errors better description\nlet LOOKING_FOR;\n// length/distance defaults during parsing\nconst DISTANCE = 6;\n\nconst defaultOptions = {\n\tquote: 'double',\n\tlenDestructure: 60,\n\tlenModuleName: 20,\n\tlenIdentifier: 20,\n\tindent: 2\n};\n\nfunction runTransform(str, options = {}) {\n\toptions = { ...defaultOptions, ...options };\n\toptions.quote = options.quote === 'single' ? \"'\" : '\"';\n\tconst buffer = [];\n\tconst exportBuffer = {\n\t\titems: [],\n\t\trequires: []\n\t};\n\tlet pos = 0;\n\n\tfor (const token of tokenize(str, options)) {\n\t\tbuffer.push(str.slice(pos, token.start));\n\t\tbuffer.push(transform(token, str, exportBuffer, options));\n\t\tpos = token.end + 1;\n\t}\n\n\t// add rest of input\n\tpos = skipNewLines(str, pos);\n\tbuffer.push(str.slice(pos, str.length));\n\n\tif (exportBuffer.items.length) {\n\t\tconst indent = ' '.repeat(options.indent);\n\t\t// add module.exports\n\t\tfor (const item of exportBuffer.requires) {\n\t\t\tbuffer.push(item);\n\t\t}\n\t\tbuffer.push('\\nmodule.exports = {\\n');\n\t\tconst exportNames = exportBuffer.items.map((item) => `${indent}${item[0]}${item[1] ? `: ${item[1]}` : ''}`);\n\t\tbuffer.push(exportNames.join(',\\n'));\n\t\tbuffer.push('\\n}');\n\t}\n\n\tbuffer.push('\\n'); // end file\n\treturn buffer.join('');\n}\n\nfunction transform(token, str, exportBuffer, { indent }) {\n\tindent = ' '.repeat(indent);\n\n\tconst { type } = token;\n\tswitch (type) {\n\t\tcase 'import': {\n\t\t\tconst identifiers = token.modules.map((s) => s.join(': ')).join(', ');\n\t\t\treturn `const { ${identifiers} } = require(${token.moduleName})`;\n\t\t}\n\t\tcase 'import*':\n\t\tcase 'importDefault': {\n\t\t\tconst { identifier, moduleName } = token;\n\t\t\treturn `const ${identifier} = require(${moduleName})${token.isDefaultImport ? '.default' : ''}`;\n\t\t}\n\t\tcase 'awaitImport': {\n\t\t\treturn `require(${token.moduleName})`;\n\t\t}\n\t\tcase 'export': {\n\t\t\texportBuffer.items.push(token.modules);\n\t\t\treturn '';\n\t\t}\n\t\tcase 'reExport': {\n\t\t\tconst { moduleName } = token;\n\n\t\t\tif (token.modules.length === 1) {\n\t\t\t\tconst [original, alias] = token.modules[0];\n\t\t\t\texportBuffer.items.push([alias ? alias : original, `require(${moduleName}).${original}`]);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\texportBuffer.requires.push('const {\\n');\n\t\t\tconst names = token.modules.map(([original]) => `${indent}${original}: __${original}__`).join(',\\n');\n\t\t\texportBuffer.requires.push(names);\n\t\t\texportBuffer.requires.push(`\\n} = require(${moduleName});`);\n\n\t\t\tfor (const [original, alias] of token.modules) {\n\t\t\t\texportBuffer.items.push([alias ? alias : original, `__${original}__`]);\n\t\t\t}\n\n\t\t\treturn '';\n\t\t}\n\t\tcase 'reExportImported': {\n\t\t\texportBuffer.items.push(...token.modules);\n\t\t\treturn '';\n\t\t}\n\t\tdefault:\n\t\t\tthrow new Error('should not reach here');\n\t}\n}\n\nString.prototype.indexWithin = indexWithin;\n\nfunction* tokenize(str, options) {\n\tconst { quote, lenDestructure, lenModuleName, lenIdentifier } = options;\n\n\tlet start = 0;\n\tlet pos;\n\n\tconst types = new Map([\n\t\t['import', 'import '],\n\t\t['export', 'export '],\n\t\t['awaitImport', 'await import(']\n\t]);\n\n\twhile (types.size !== 0) {\n\t\t// look for first matching pattern\n\t\tpos = Number.POSITIVE_INFINITY;\n\t\tlet type;\n\t\tfor (const t of types.keys()) {\n\t\t\tconst idx = str.indexOf(types.get(t), start);\n\t\t\tif (idx === -1) {\n\t\t\t\ttypes.delete(t);\n\t\t\t} else if (idx < pos) {\n\t\t\t\tpos = idx;\n\t\t\t\ttype = t;\n\t\t\t}\n\t\t}\n\n\t\tswitch (type) {\n\t\t\tcase 'import':\n\t\t\t\tyield handleImport();\n\t\t\t\tbreak;\n\t\t\tcase 'export':\n\t\t\t\tyield handleExport();\n\t\t\t\tbreak;\n\t\t\tcase 'awaitImport':\n\t\t\t\tyield handleAwaitImport();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t// import { ... } from \"MODULE\"\n\t// import * as IDENTIFIER from \"MODULE\"\n\tfunction handleImport() {\n\t\tLOOKING_FOR = 'import names';\n\t\tconst braceStart = str.indexWithin('{', pos + 7, DISTANCE, false);\n\t\t// 7 === \"import \".length\n\t\tif (braceStart === -1) {\n\t\t\t// try to see if it's `import *`\n\t\t\treturn handleImportStar();\n\t\t}\n\n\t\tconst braceEnd = str.indexWithin('}', braceStart + 1, lenDestructure);\n\n\t\tLOOKING_FOR = 'name of imported module';\n\t\tlet moduleStart = str.indexWithin('from ', braceEnd + 1, DISTANCE);\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1, 5);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'import',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1)),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// await import(...)\n\tfunction handleAwaitImport() {\n\t\tLOOKING_FOR = 'name of imported module for await import()';\n\t\tconst moduleStart = str.indexWithin('(', pos + 12, 10) + 1;\n\t\t// 12 === \"await import\".length\n\t\tconst moduleEnd = str.indexWithin(')', moduleStart + 1, lenIdentifier) - 1;\n\n\t\tstart = moduleEnd + 2;\n\t\treturn {\n\t\t\ttype: 'awaitImport',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd + 1,\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// export [default] const IDENTIFIER = ...\n\t// export [default] function IDENTIFIER(...) ...\n\t// export [default] class IDENTIFIER ...\n\t// export { ... } >> handleReExport()\n\t// export { ... } from \"MODULE\" >> handleReExport()\n\tfunction handleExport() {\n\t\tLOOKING_FOR = 'export pattern';\n\t\tlet skipStart = pos + 'export '.length;\n\n\t\tif (str.indexWithin('{', skipStart, 5, false) !== -1) {\n\t\t\treturn handleReExport();\n\t\t} else if (str.indexWithin('*', skipStart, 5, false) !== -1) {\n\t\t\treturn handleExportStar();\n\t\t}\n\n\t\tLOOKING_FOR = 'identifier type (function|class|const) for export';\n\n\t\tif (str.indexWithin('async ', skipStart, DISTANCE, false) !== -1) {\n\t\t\tskipStart += 6; // 6 === \"async \".length;\n\t\t}\n\n\t\tlet isDefaultExport = false;\n\t\tif (str.indexWithin('default ', skipStart, DISTANCE, false) !== -1) {\n\t\t\tskipStart += 8; // 8 === \"default \".length;\n\t\t\tisDefaultExport = true;\n\t\t}\n\n\t\tconst typeEnd = str.indexWithin(' ', skipStart, 9);\n\t\t// 9 === \"function\".length + 1\n\t\tconst exportType = str.slice(skipStart, typeEnd);\n\n\t\tLOOKING_FOR = 'export identifiers';\n\t\tconst identifierStart = str.indexWithin(' ', skipStart + exportType.length, 5) + 1;\n\t\tconst identifierEnd = str.indexWithin(exportType === 'function' ? '(' : ' ', identifierStart, lenIdentifier) - 1;\n\n\t\tconst end = pos + 6 + (isDefaultExport ? 8 : 0);\n\t\t// 6 == \"export\".length, 8 === \"default \".length;\n\n\t\tconst modules = isDefaultExport\n\t\t\t? ['default', str.slice(identifierStart, identifierEnd + 1)]\n\t\t\t: [str.slice(identifierStart, identifierEnd + 1)];\n\n\t\tstart = end + 1;\n\t\treturn {\n\t\t\ttype: 'export',\n\t\t\tstart: pos,\n\t\t\tend,\n\t\t\tmodules\n\t\t};\n\t}\n\n\t// import * as IDENTIFIER from \"MODULE\"\n\tfunction handleImportStar() {\n\t\tLOOKING_FOR = 'import name for import*';\n\t\tlet identifierStart = str.indexWithin('* as ', pos + 7, DISTANCE, false);\n\t\tif (identifierStart === -1) {\n\t\t\treturn handleDefaultImport();\n\t\t}\n\t\tidentifierStart += 5;\n\t\t// 7 === \"import \".length, 5 === \"* as \".length\n\t\tconst identifierEnd = str.indexWithin(' ', identifierStart + 1, lenIdentifier);\n\n\t\tLOOKING_FOR = 'name of imported module for import*';\n\t\tlet moduleStart = str.indexWithin('from ', identifierEnd + 1) + 'from'.length;\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'import*',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tidentifier: str.slice(identifierStart, identifierEnd),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// import IDENTIFIER from \"MODULE\"\n\tfunction handleDefaultImport() {\n\t\tLOOKING_FOR = 'import name for default import';\n\t\tconst identifierStart = pos + 7; // 7 === \"import \".length\n\t\tconst identifierEnd = str.indexWithin(' ', identifierStart, DISTANCE);\n\n\t\tLOOKING_FOR = 'name of imported module for import*';\n\t\tlet moduleStart = str.indexWithin('from ', identifierEnd + 1) + 'from'.length;\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'importDefault',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tisDefaultImport: true,\n\t\t\tidentifier: str.slice(identifierStart, identifierEnd),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// export { ... } from \"...\" | export { ... }\n\tfunction handleReExport() {\n\t\tLOOKING_FOR = 'export pattern for re-export';\n\t\tconst braceStart = str.indexWithin('{', pos + 'export '.length, 5);\n\t\tconst braceEnd = str.indexWithin('}', braceStart + 1, lenDestructure);\n\n\t\tLOOKING_FOR = 'name of re-exported module';\n\t\tlet moduleStart = str.indexWithin('from ', braceEnd + 1, 10, false);\n\n\t\tif (moduleStart === -1) {\n\t\t\t// export { ... }\n\t\t\tconst end = skipNewLines(str, braceEnd);\n\n\t\t\tstart = end + 1;\n\t\t\treturn {\n\t\t\t\ttype: 'reExportImported',\n\t\t\t\tstart: pos,\n\t\t\t\tend,\n\t\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1))\n\t\t\t};\n\t\t}\n\n\t\tmoduleStart = str.indexWithin(quote, moduleStart, 'from '.length + 4);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tconst end = skipNewLines(str, moduleEnd);\n\n\t\tstart = end + 1;\n\t\treturn {\n\t\t\ttype: 'reExport',\n\t\t\tstart: pos,\n\t\t\tend,\n\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1)),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\tfunction handleExportStar() {\n\t\tthrow new Error('not implemented');\n\t}\n\n\tfunction destructureModules(objLiteral) {\n\t\treturn objLiteral\n\t\t\t.trim()\n\t\t\t.slice(1, -1)\n\t\t\t.split(/,\\s*/)\n\t\t\t.map((i) => i.trim())\n\t\t\t.filter((i) => i)\n\t\t\t.map((i) => i.split(/\\s*\\bas\\b\\s*/)); // Modified line to match https://github.com/sidvishnoi/esm-to-cjs/pull/7\n\t}\n}\n\n// this is same as indexOf, but can stop searching earlier\nfunction indexWithin(needle, from, within = 99, throws = true) {\n\tfor (let i = from, L = from + within, j = 0; i < L; ++i) {\n\t\tif (this.charCodeAt(i) === needle.charCodeAt(j)) {\n\t\t\twhile (j < needle.length) {\n\t\t\t\tif (this.charCodeAt(i + j) === needle.charCodeAt(j)) {\n\t\t\t\t\t++j;\n\t\t\t\t} else {\n\t\t\t\t\tj = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (j === needle.length) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t}\n\tif (throws) {\n\t\tthrow new Error(\n\t\t\t`ParseError: Failed to find \\`${needle}\\` within ${within} characters from position ${from}${\n\t\t\t\tLOOKING_FOR ? ` while looking for ${LOOKING_FOR}` : ''\n\t\t\t}\\n\\nINPUT STRING:` + `\\n${'*'.repeat(20)}\\n${this}\\n${'*'.repeat(20)}\\n`\n\t\t);\n\t} else {\n\t\treturn -1;\n\t}\n}\n\nfunction skipNewLines(str, i) {\n\tif (str.charAt(i + 1) === ';') ++i;\n\twhile (i < str.length && /\\s/.test(str.charAt(i))) {\n\t\t++i;\n\t}\n\treturn i;\n}\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from './esmToCjs';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\n\nexport const documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\nexport const 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 */\nexport const 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 */\nexport const esmToCjs = (code: string): string =>\n\trunTransform(code, { quote: 'single', lenDestructure: 512, lenModuleName: 512, lenIdentifier: 512 });\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 */\nexport const 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 */\nexport const 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 */\nexport const 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;;;ACAA;AAAA;AAAA;AAEA,AAQA,YAAO,QAAQ,eAAe;AAG9B,QAAI;AAEJ,QAAM,WAAW;AAEjB,QAAM,iBAAiB;AAAA,MACtB,OAAO;AAAA,MACP,gBAAgB;AAAA,MAChB,eAAe;AAAA,MACf,eAAe;AAAA,MACf,QAAQ;AAAA;AAGT,2BAAsB,KAAK,UAAU,IAAI;AACxC,gBAAU,KAAK,mBAAmB;AAClC,cAAQ,QAAQ,QAAQ,UAAU,WAAW,MAAM;AACnD,YAAM,SAAS;AACf,YAAM,eAAe;AAAA,QACpB,OAAO;AAAA,QACP,UAAU;AAAA;AAEX,UAAI,MAAM;AAEV,iBAAW,SAAS,SAAS,KAAK,UAAU;AAC3C,eAAO,KAAK,IAAI,MAAM,KAAK,MAAM;AACjC,eAAO,KAAK,UAAU,OAAO,KAAK,cAAc;AAChD,cAAM,MAAM,MAAM;AAAA;AAInB,YAAM,aAAa,KAAK;AACxB,aAAO,KAAK,IAAI,MAAM,KAAK,IAAI;AAE/B,UAAI,aAAa,MAAM,QAAQ;AAC9B,cAAM,SAAS,IAAI,OAAO,QAAQ;AAElC,mBAAW,QAAQ,aAAa,UAAU;AACzC,iBAAO,KAAK;AAAA;AAEb,eAAO,KAAK;AACZ,cAAM,cAAc,aAAa,MAAM,IAAI,CAAC,SAAS,G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SAAS,QAAQ;AACpC,cAAM,SAAS,YAAY,KAAK,SAAS;AACzC,YAAI,QAAQ;AACX,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG;AAClC,mBAAS,OAAO;AAAA,eACV;AACN,mBAAS;AAAA;AAAA;AAAA;AAKZ,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ;AAAA;AAGvB,WAAO;AAAA;AAGR,SAAO;AAAA;","names":[]}
|
|
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 === 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,KAAK,SAAS,QAAQ;AACpC,cAAM,SAAS,YAAY,KAAK,SAAS;AACzC,YAAI,QAAQ;AACX,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG;AAClC,mBAAS,OAAO;AAAA,eACV;AACN,mBAAS;AAAA;AAAA;AAAA;AAKZ,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ;AAAA;AAGvB,WAAO;AAAA;AAGR,SAAO;AAAA;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,339 +1,6 @@
|
|
|
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 __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
8
|
-
var __esm = (fn, res) => function __init() {
|
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
-
};
|
|
11
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
12
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, module, copyDefault, desc) => {
|
|
15
|
-
if (module && typeof module === "object" || typeof module === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(module))
|
|
17
|
-
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
|
|
18
|
-
__defProp(target, key, { get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return target;
|
|
21
|
-
};
|
|
22
|
-
var __toESM = (module, isNodeMode) => {
|
|
23
|
-
return __reExport(__markAsModule(__defProp(module != null ? __create(__getProtoOf(module)) : {}, "default", !isNodeMode && module && module.__esModule ? { get: () => module.default, enumerable: true } : { value: module, enumerable: true })), module);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// ../../node_modules/tsup/assets/esm_shims.js
|
|
27
|
-
import { fileURLToPath } from "url";
|
|
28
|
-
import path from "path";
|
|
29
|
-
var init_esm_shims = __esm({
|
|
30
|
-
"../../node_modules/tsup/assets/esm_shims.js"() {
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// src/esmToCjs.js
|
|
35
|
-
var require_esmToCjs = __commonJS({
|
|
36
|
-
"src/esmToCjs.js"(exports, module) {
|
|
37
|
-
init_esm_shims();
|
|
38
|
-
module.exports.runTransform = runTransform2;
|
|
39
|
-
var LOOKING_FOR;
|
|
40
|
-
var DISTANCE = 6;
|
|
41
|
-
var defaultOptions = {
|
|
42
|
-
quote: "double",
|
|
43
|
-
lenDestructure: 60,
|
|
44
|
-
lenModuleName: 20,
|
|
45
|
-
lenIdentifier: 20,
|
|
46
|
-
indent: 2
|
|
47
|
-
};
|
|
48
|
-
function runTransform2(str, options = {}) {
|
|
49
|
-
options = { ...defaultOptions, ...options };
|
|
50
|
-
options.quote = options.quote === "single" ? "'" : '"';
|
|
51
|
-
const buffer = [];
|
|
52
|
-
const exportBuffer = {
|
|
53
|
-
items: [],
|
|
54
|
-
requires: []
|
|
55
|
-
};
|
|
56
|
-
let pos = 0;
|
|
57
|
-
for (const token of tokenize(str, options)) {
|
|
58
|
-
buffer.push(str.slice(pos, token.start));
|
|
59
|
-
buffer.push(transform(token, str, exportBuffer, options));
|
|
60
|
-
pos = token.end + 1;
|
|
61
|
-
}
|
|
62
|
-
pos = skipNewLines(str, pos);
|
|
63
|
-
buffer.push(str.slice(pos, str.length));
|
|
64
|
-
if (exportBuffer.items.length) {
|
|
65
|
-
const indent = " ".repeat(options.indent);
|
|
66
|
-
for (const item of exportBuffer.requires) {
|
|
67
|
-
buffer.push(item);
|
|
68
|
-
}
|
|
69
|
-
buffer.push("\nmodule.exports = {\n");
|
|
70
|
-
const exportNames = exportBuffer.items.map((item) => `${indent}${item[0]}${item[1] ? `: ${item[1]}` : ""}`);
|
|
71
|
-
buffer.push(exportNames.join(",\n"));
|
|
72
|
-
buffer.push("\n}");
|
|
73
|
-
}
|
|
74
|
-
buffer.push("\n");
|
|
75
|
-
return buffer.join("");
|
|
76
|
-
}
|
|
77
|
-
function transform(token, str, exportBuffer, { indent }) {
|
|
78
|
-
indent = " ".repeat(indent);
|
|
79
|
-
const { type } = token;
|
|
80
|
-
switch (type) {
|
|
81
|
-
case "import": {
|
|
82
|
-
const identifiers = token.modules.map((s) => s.join(": ")).join(", ");
|
|
83
|
-
return `const { ${identifiers} } = require(${token.moduleName})`;
|
|
84
|
-
}
|
|
85
|
-
case "import*":
|
|
86
|
-
case "importDefault": {
|
|
87
|
-
const { identifier, moduleName } = token;
|
|
88
|
-
return `const ${identifier} = require(${moduleName})${token.isDefaultImport ? ".default" : ""}`;
|
|
89
|
-
}
|
|
90
|
-
case "awaitImport": {
|
|
91
|
-
return `require(${token.moduleName})`;
|
|
92
|
-
}
|
|
93
|
-
case "export": {
|
|
94
|
-
exportBuffer.items.push(token.modules);
|
|
95
|
-
return "";
|
|
96
|
-
}
|
|
97
|
-
case "reExport": {
|
|
98
|
-
const { moduleName } = token;
|
|
99
|
-
if (token.modules.length === 1) {
|
|
100
|
-
const [original, alias] = token.modules[0];
|
|
101
|
-
exportBuffer.items.push([alias ? alias : original, `require(${moduleName}).${original}`]);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
exportBuffer.requires.push("const {\n");
|
|
105
|
-
const names = token.modules.map(([original]) => `${indent}${original}: __${original}__`).join(",\n");
|
|
106
|
-
exportBuffer.requires.push(names);
|
|
107
|
-
exportBuffer.requires.push(`
|
|
108
|
-
} = require(${moduleName});`);
|
|
109
|
-
for (const [original, alias] of token.modules) {
|
|
110
|
-
exportBuffer.items.push([alias ? alias : original, `__${original}__`]);
|
|
111
|
-
}
|
|
112
|
-
return "";
|
|
113
|
-
}
|
|
114
|
-
case "reExportImported": {
|
|
115
|
-
exportBuffer.items.push(...token.modules);
|
|
116
|
-
return "";
|
|
117
|
-
}
|
|
118
|
-
default:
|
|
119
|
-
throw new Error("should not reach here");
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
String.prototype.indexWithin = indexWithin;
|
|
123
|
-
function* tokenize(str, options) {
|
|
124
|
-
const { quote, lenDestructure, lenModuleName, lenIdentifier } = options;
|
|
125
|
-
let start = 0;
|
|
126
|
-
let pos;
|
|
127
|
-
const types = /* @__PURE__ */ new Map([
|
|
128
|
-
["import", "import "],
|
|
129
|
-
["export", "export "],
|
|
130
|
-
["awaitImport", "await import("]
|
|
131
|
-
]);
|
|
132
|
-
while (types.size !== 0) {
|
|
133
|
-
pos = Number.POSITIVE_INFINITY;
|
|
134
|
-
let type;
|
|
135
|
-
for (const t of types.keys()) {
|
|
136
|
-
const idx = str.indexOf(types.get(t), start);
|
|
137
|
-
if (idx === -1) {
|
|
138
|
-
types.delete(t);
|
|
139
|
-
} else if (idx < pos) {
|
|
140
|
-
pos = idx;
|
|
141
|
-
type = t;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
switch (type) {
|
|
145
|
-
case "import":
|
|
146
|
-
yield handleImport();
|
|
147
|
-
break;
|
|
148
|
-
case "export":
|
|
149
|
-
yield handleExport();
|
|
150
|
-
break;
|
|
151
|
-
case "awaitImport":
|
|
152
|
-
yield handleAwaitImport();
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
function handleImport() {
|
|
157
|
-
LOOKING_FOR = "import names";
|
|
158
|
-
const braceStart = str.indexWithin("{", pos + 7, DISTANCE, false);
|
|
159
|
-
if (braceStart === -1) {
|
|
160
|
-
return handleImportStar();
|
|
161
|
-
}
|
|
162
|
-
const braceEnd = str.indexWithin("}", braceStart + 1, lenDestructure);
|
|
163
|
-
LOOKING_FOR = "name of imported module";
|
|
164
|
-
let moduleStart = str.indexWithin("from ", braceEnd + 1, DISTANCE);
|
|
165
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1, 5);
|
|
166
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
167
|
-
start = moduleEnd + 1;
|
|
168
|
-
return {
|
|
169
|
-
type: "import",
|
|
170
|
-
start: pos,
|
|
171
|
-
end: moduleEnd,
|
|
172
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1)),
|
|
173
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
function handleAwaitImport() {
|
|
177
|
-
LOOKING_FOR = "name of imported module for await import()";
|
|
178
|
-
const moduleStart = str.indexWithin("(", pos + 12, 10) + 1;
|
|
179
|
-
const moduleEnd = str.indexWithin(")", moduleStart + 1, lenIdentifier) - 1;
|
|
180
|
-
start = moduleEnd + 2;
|
|
181
|
-
return {
|
|
182
|
-
type: "awaitImport",
|
|
183
|
-
start: pos,
|
|
184
|
-
end: moduleEnd + 1,
|
|
185
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
function handleExport() {
|
|
189
|
-
LOOKING_FOR = "export pattern";
|
|
190
|
-
let skipStart = pos + "export ".length;
|
|
191
|
-
if (str.indexWithin("{", skipStart, 5, false) !== -1) {
|
|
192
|
-
return handleReExport();
|
|
193
|
-
} else if (str.indexWithin("*", skipStart, 5, false) !== -1) {
|
|
194
|
-
return handleExportStar();
|
|
195
|
-
}
|
|
196
|
-
LOOKING_FOR = "identifier type (function|class|const) for export";
|
|
197
|
-
if (str.indexWithin("async ", skipStart, DISTANCE, false) !== -1) {
|
|
198
|
-
skipStart += 6;
|
|
199
|
-
}
|
|
200
|
-
let isDefaultExport = false;
|
|
201
|
-
if (str.indexWithin("default ", skipStart, DISTANCE, false) !== -1) {
|
|
202
|
-
skipStart += 8;
|
|
203
|
-
isDefaultExport = true;
|
|
204
|
-
}
|
|
205
|
-
const typeEnd = str.indexWithin(" ", skipStart, 9);
|
|
206
|
-
const exportType = str.slice(skipStart, typeEnd);
|
|
207
|
-
LOOKING_FOR = "export identifiers";
|
|
208
|
-
const identifierStart = str.indexWithin(" ", skipStart + exportType.length, 5) + 1;
|
|
209
|
-
const identifierEnd = str.indexWithin(exportType === "function" ? "(" : " ", identifierStart, lenIdentifier) - 1;
|
|
210
|
-
const end = pos + 6 + (isDefaultExport ? 8 : 0);
|
|
211
|
-
const modules = isDefaultExport ? ["default", str.slice(identifierStart, identifierEnd + 1)] : [str.slice(identifierStart, identifierEnd + 1)];
|
|
212
|
-
start = end + 1;
|
|
213
|
-
return {
|
|
214
|
-
type: "export",
|
|
215
|
-
start: pos,
|
|
216
|
-
end,
|
|
217
|
-
modules
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
function handleImportStar() {
|
|
221
|
-
LOOKING_FOR = "import name for import*";
|
|
222
|
-
let identifierStart = str.indexWithin("* as ", pos + 7, DISTANCE, false);
|
|
223
|
-
if (identifierStart === -1) {
|
|
224
|
-
return handleDefaultImport();
|
|
225
|
-
}
|
|
226
|
-
identifierStart += 5;
|
|
227
|
-
const identifierEnd = str.indexWithin(" ", identifierStart + 1, lenIdentifier);
|
|
228
|
-
LOOKING_FOR = "name of imported module for import*";
|
|
229
|
-
let moduleStart = str.indexWithin("from ", identifierEnd + 1) + "from".length;
|
|
230
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1);
|
|
231
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
232
|
-
start = moduleEnd + 1;
|
|
233
|
-
return {
|
|
234
|
-
type: "import*",
|
|
235
|
-
start: pos,
|
|
236
|
-
end: moduleEnd,
|
|
237
|
-
identifier: str.slice(identifierStart, identifierEnd),
|
|
238
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function handleDefaultImport() {
|
|
242
|
-
LOOKING_FOR = "import name for default import";
|
|
243
|
-
const identifierStart = pos + 7;
|
|
244
|
-
const identifierEnd = str.indexWithin(" ", identifierStart, DISTANCE);
|
|
245
|
-
LOOKING_FOR = "name of imported module for import*";
|
|
246
|
-
let moduleStart = str.indexWithin("from ", identifierEnd + 1) + "from".length;
|
|
247
|
-
moduleStart = str.indexWithin(quote, moduleStart + 1);
|
|
248
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
249
|
-
start = moduleEnd + 1;
|
|
250
|
-
return {
|
|
251
|
-
type: "importDefault",
|
|
252
|
-
start: pos,
|
|
253
|
-
end: moduleEnd,
|
|
254
|
-
isDefaultImport: true,
|
|
255
|
-
identifier: str.slice(identifierStart, identifierEnd),
|
|
256
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
function handleReExport() {
|
|
260
|
-
LOOKING_FOR = "export pattern for re-export";
|
|
261
|
-
const braceStart = str.indexWithin("{", pos + "export ".length, 5);
|
|
262
|
-
const braceEnd = str.indexWithin("}", braceStart + 1, lenDestructure);
|
|
263
|
-
LOOKING_FOR = "name of re-exported module";
|
|
264
|
-
let moduleStart = str.indexWithin("from ", braceEnd + 1, 10, false);
|
|
265
|
-
if (moduleStart === -1) {
|
|
266
|
-
const end2 = skipNewLines(str, braceEnd);
|
|
267
|
-
start = end2 + 1;
|
|
268
|
-
return {
|
|
269
|
-
type: "reExportImported",
|
|
270
|
-
start: pos,
|
|
271
|
-
end: end2,
|
|
272
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1))
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
moduleStart = str.indexWithin(quote, moduleStart, "from ".length + 4);
|
|
276
|
-
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);
|
|
277
|
-
const end = skipNewLines(str, moduleEnd);
|
|
278
|
-
start = end + 1;
|
|
279
|
-
return {
|
|
280
|
-
type: "reExport",
|
|
281
|
-
start: pos,
|
|
282
|
-
end,
|
|
283
|
-
modules: destructureModules(str.slice(braceStart, braceEnd + 1)),
|
|
284
|
-
moduleName: str.slice(moduleStart, moduleEnd + 1)
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
function handleExportStar() {
|
|
288
|
-
throw new Error("not implemented");
|
|
289
|
-
}
|
|
290
|
-
function destructureModules(objLiteral) {
|
|
291
|
-
return objLiteral.trim().slice(1, -1).split(/,\s*/).map((i) => i.trim()).filter((i) => i).map((i) => i.split(/\s*\bas\b\s*/));
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
function indexWithin(needle, from, within = 99, throws = true) {
|
|
295
|
-
for (let i = from, L = from + within, j = 0; i < L; ++i) {
|
|
296
|
-
if (this.charCodeAt(i) === needle.charCodeAt(j)) {
|
|
297
|
-
while (j < needle.length) {
|
|
298
|
-
if (this.charCodeAt(i + j) === needle.charCodeAt(j)) {
|
|
299
|
-
++j;
|
|
300
|
-
} else {
|
|
301
|
-
j = 0;
|
|
302
|
-
break;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (j === needle.length) {
|
|
306
|
-
return i;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if (throws) {
|
|
311
|
-
throw new Error(`ParseError: Failed to find \`${needle}\` within ${within} characters from position ${from}${LOOKING_FOR ? ` while looking for ${LOOKING_FOR}` : ""}
|
|
312
|
-
|
|
313
|
-
INPUT STRING:
|
|
314
|
-
${"*".repeat(20)}
|
|
315
|
-
${this}
|
|
316
|
-
${"*".repeat(20)}
|
|
317
|
-
`);
|
|
318
|
-
} else {
|
|
319
|
-
return -1;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
function skipNewLines(str, i) {
|
|
323
|
-
if (str.charAt(i + 1) === ";")
|
|
324
|
-
++i;
|
|
325
|
-
while (i < str.length && /\s/.test(str.charAt(i))) {
|
|
326
|
-
++i;
|
|
327
|
-
}
|
|
328
|
-
return i;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
|
|
333
1
|
// src/index.ts
|
|
334
|
-
init_esm_shims();
|
|
335
|
-
var import_esmToCjs = __toESM(require_esmToCjs());
|
|
336
2
|
import sapphirePrettierConfig from "@sapphire/prettier-config";
|
|
3
|
+
import { runTransform } from "esm-to-cjs";
|
|
337
4
|
import prettier from "prettier";
|
|
338
5
|
import ts from "typescript";
|
|
339
6
|
var documentationPrettierConfig = {
|
|
@@ -354,7 +21,7 @@ var makeTsCompilerOptions = (overrideOptions) => ({
|
|
|
354
21
|
target: ts.ScriptTarget.ESNext
|
|
355
22
|
});
|
|
356
23
|
var tsToEsm = (code, options) => ts.transpileModule(code, { reportDiagnostics: false, compilerOptions: makeTsCompilerOptions(options.typescriptCompilerOptions) });
|
|
357
|
-
var esmToCjs = (code) =>
|
|
24
|
+
var esmToCjs = (code) => runTransform(code, { quote: "single", lenDestructure: 128, lenModuleName: 128, lenIdentifier: 128 });
|
|
358
25
|
var escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
|
|
359
26
|
var restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
|
|
360
27
|
var prettierFormatCode = (code, prettierConfig) => prettier.format(code, { ...documentationPrettierConfig, ...prettierConfig }).slice(0, -1);
|
|
@@ -447,20 +114,6 @@ function ts2esm2cjs({ sync = true, prettierOptions = {}, typescriptCompilerOptio
|
|
|
447
114
|
return transformer;
|
|
448
115
|
}
|
|
449
116
|
export {
|
|
450
|
-
|
|
451
|
-
escapeNewLines,
|
|
452
|
-
esmToCjs,
|
|
453
|
-
makeTsCompilerOptions,
|
|
454
|
-
prettierFormatCode,
|
|
455
|
-
restoreNewLines,
|
|
456
|
-
ts2esm2cjs,
|
|
457
|
-
tsToEsm
|
|
117
|
+
ts2esm2cjs
|
|
458
118
|
};
|
|
459
|
-
/**
|
|
460
|
-
* @license MIT
|
|
461
|
-
* @copyright 2018 Sid Vishnoi <sidvishnoi8@gmail.com>
|
|
462
|
-
* @see https://github.com/sidvishnoi/esm-to-cjs/blob/d032518da994b6d28cff5fd38ac2d0b45a13f0d9/src/core.js
|
|
463
|
-
*
|
|
464
|
-
* Modified code is line 340
|
|
465
|
-
*/
|
|
466
119
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../node_modules/tsup/assets/esm_shims.js","../src/esmToCjs.js","../src/index.ts"],"sourcesContent":["// Shim globals in esm bundle\nimport { fileURLToPath } from 'url'\nimport path from 'path'\n\nconst getFilename = () => fileURLToPath(import.meta.url)\nconst getDirname = () => path.dirname(getFilename())\n\nexport const __dirname = /* @__PURE__ */ getDirname()\nexport const __filename = /* @__PURE__ */ getFilename()\n","/* eslint-disable @typescript-eslint/no-invalid-this, @typescript-eslint/switch-exhaustiveness-check, no-extend-native, no-useless-concat */\n\n/**\n * @license MIT\n * @copyright 2018 Sid Vishnoi <sidvishnoi8@gmail.com>\n * @see https://github.com/sidvishnoi/esm-to-cjs/blob/d032518da994b6d28cff5fd38ac2d0b45a13f0d9/src/core.js\n *\n * Modified code is line 340\n */\n\nmodule.exports.runTransform = runTransform;\n\n// gives parsing errors better description\nlet LOOKING_FOR;\n// length/distance defaults during parsing\nconst DISTANCE = 6;\n\nconst defaultOptions = {\n\tquote: 'double',\n\tlenDestructure: 60,\n\tlenModuleName: 20,\n\tlenIdentifier: 20,\n\tindent: 2\n};\n\nfunction runTransform(str, options = {}) {\n\toptions = { ...defaultOptions, ...options };\n\toptions.quote = options.quote === 'single' ? \"'\" : '\"';\n\tconst buffer = [];\n\tconst exportBuffer = {\n\t\titems: [],\n\t\trequires: []\n\t};\n\tlet pos = 0;\n\n\tfor (const token of tokenize(str, options)) {\n\t\tbuffer.push(str.slice(pos, token.start));\n\t\tbuffer.push(transform(token, str, exportBuffer, options));\n\t\tpos = token.end + 1;\n\t}\n\n\t// add rest of input\n\tpos = skipNewLines(str, pos);\n\tbuffer.push(str.slice(pos, str.length));\n\n\tif (exportBuffer.items.length) {\n\t\tconst indent = ' '.repeat(options.indent);\n\t\t// add module.exports\n\t\tfor (const item of exportBuffer.requires) {\n\t\t\tbuffer.push(item);\n\t\t}\n\t\tbuffer.push('\\nmodule.exports = {\\n');\n\t\tconst exportNames = exportBuffer.items.map((item) => `${indent}${item[0]}${item[1] ? `: ${item[1]}` : ''}`);\n\t\tbuffer.push(exportNames.join(',\\n'));\n\t\tbuffer.push('\\n}');\n\t}\n\n\tbuffer.push('\\n'); // end file\n\treturn buffer.join('');\n}\n\nfunction transform(token, str, exportBuffer, { indent }) {\n\tindent = ' '.repeat(indent);\n\n\tconst { type } = token;\n\tswitch (type) {\n\t\tcase 'import': {\n\t\t\tconst identifiers = token.modules.map((s) => s.join(': ')).join(', ');\n\t\t\treturn `const { ${identifiers} } = require(${token.moduleName})`;\n\t\t}\n\t\tcase 'import*':\n\t\tcase 'importDefault': {\n\t\t\tconst { identifier, moduleName } = token;\n\t\t\treturn `const ${identifier} = require(${moduleName})${token.isDefaultImport ? '.default' : ''}`;\n\t\t}\n\t\tcase 'awaitImport': {\n\t\t\treturn `require(${token.moduleName})`;\n\t\t}\n\t\tcase 'export': {\n\t\t\texportBuffer.items.push(token.modules);\n\t\t\treturn '';\n\t\t}\n\t\tcase 'reExport': {\n\t\t\tconst { moduleName } = token;\n\n\t\t\tif (token.modules.length === 1) {\n\t\t\t\tconst [original, alias] = token.modules[0];\n\t\t\t\texportBuffer.items.push([alias ? alias : original, `require(${moduleName}).${original}`]);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\texportBuffer.requires.push('const {\\n');\n\t\t\tconst names = token.modules.map(([original]) => `${indent}${original}: __${original}__`).join(',\\n');\n\t\t\texportBuffer.requires.push(names);\n\t\t\texportBuffer.requires.push(`\\n} = require(${moduleName});`);\n\n\t\t\tfor (const [original, alias] of token.modules) {\n\t\t\t\texportBuffer.items.push([alias ? alias : original, `__${original}__`]);\n\t\t\t}\n\n\t\t\treturn '';\n\t\t}\n\t\tcase 'reExportImported': {\n\t\t\texportBuffer.items.push(...token.modules);\n\t\t\treturn '';\n\t\t}\n\t\tdefault:\n\t\t\tthrow new Error('should not reach here');\n\t}\n}\n\nString.prototype.indexWithin = indexWithin;\n\nfunction* tokenize(str, options) {\n\tconst { quote, lenDestructure, lenModuleName, lenIdentifier } = options;\n\n\tlet start = 0;\n\tlet pos;\n\n\tconst types = new Map([\n\t\t['import', 'import '],\n\t\t['export', 'export '],\n\t\t['awaitImport', 'await import(']\n\t]);\n\n\twhile (types.size !== 0) {\n\t\t// look for first matching pattern\n\t\tpos = Number.POSITIVE_INFINITY;\n\t\tlet type;\n\t\tfor (const t of types.keys()) {\n\t\t\tconst idx = str.indexOf(types.get(t), start);\n\t\t\tif (idx === -1) {\n\t\t\t\ttypes.delete(t);\n\t\t\t} else if (idx < pos) {\n\t\t\t\tpos = idx;\n\t\t\t\ttype = t;\n\t\t\t}\n\t\t}\n\n\t\tswitch (type) {\n\t\t\tcase 'import':\n\t\t\t\tyield handleImport();\n\t\t\t\tbreak;\n\t\t\tcase 'export':\n\t\t\t\tyield handleExport();\n\t\t\t\tbreak;\n\t\t\tcase 'awaitImport':\n\t\t\t\tyield handleAwaitImport();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t// import { ... } from \"MODULE\"\n\t// import * as IDENTIFIER from \"MODULE\"\n\tfunction handleImport() {\n\t\tLOOKING_FOR = 'import names';\n\t\tconst braceStart = str.indexWithin('{', pos + 7, DISTANCE, false);\n\t\t// 7 === \"import \".length\n\t\tif (braceStart === -1) {\n\t\t\t// try to see if it's `import *`\n\t\t\treturn handleImportStar();\n\t\t}\n\n\t\tconst braceEnd = str.indexWithin('}', braceStart + 1, lenDestructure);\n\n\t\tLOOKING_FOR = 'name of imported module';\n\t\tlet moduleStart = str.indexWithin('from ', braceEnd + 1, DISTANCE);\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1, 5);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'import',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1)),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// await import(...)\n\tfunction handleAwaitImport() {\n\t\tLOOKING_FOR = 'name of imported module for await import()';\n\t\tconst moduleStart = str.indexWithin('(', pos + 12, 10) + 1;\n\t\t// 12 === \"await import\".length\n\t\tconst moduleEnd = str.indexWithin(')', moduleStart + 1, lenIdentifier) - 1;\n\n\t\tstart = moduleEnd + 2;\n\t\treturn {\n\t\t\ttype: 'awaitImport',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd + 1,\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// export [default] const IDENTIFIER = ...\n\t// export [default] function IDENTIFIER(...) ...\n\t// export [default] class IDENTIFIER ...\n\t// export { ... } >> handleReExport()\n\t// export { ... } from \"MODULE\" >> handleReExport()\n\tfunction handleExport() {\n\t\tLOOKING_FOR = 'export pattern';\n\t\tlet skipStart = pos + 'export '.length;\n\n\t\tif (str.indexWithin('{', skipStart, 5, false) !== -1) {\n\t\t\treturn handleReExport();\n\t\t} else if (str.indexWithin('*', skipStart, 5, false) !== -1) {\n\t\t\treturn handleExportStar();\n\t\t}\n\n\t\tLOOKING_FOR = 'identifier type (function|class|const) for export';\n\n\t\tif (str.indexWithin('async ', skipStart, DISTANCE, false) !== -1) {\n\t\t\tskipStart += 6; // 6 === \"async \".length;\n\t\t}\n\n\t\tlet isDefaultExport = false;\n\t\tif (str.indexWithin('default ', skipStart, DISTANCE, false) !== -1) {\n\t\t\tskipStart += 8; // 8 === \"default \".length;\n\t\t\tisDefaultExport = true;\n\t\t}\n\n\t\tconst typeEnd = str.indexWithin(' ', skipStart, 9);\n\t\t// 9 === \"function\".length + 1\n\t\tconst exportType = str.slice(skipStart, typeEnd);\n\n\t\tLOOKING_FOR = 'export identifiers';\n\t\tconst identifierStart = str.indexWithin(' ', skipStart + exportType.length, 5) + 1;\n\t\tconst identifierEnd = str.indexWithin(exportType === 'function' ? '(' : ' ', identifierStart, lenIdentifier) - 1;\n\n\t\tconst end = pos + 6 + (isDefaultExport ? 8 : 0);\n\t\t// 6 == \"export\".length, 8 === \"default \".length;\n\n\t\tconst modules = isDefaultExport\n\t\t\t? ['default', str.slice(identifierStart, identifierEnd + 1)]\n\t\t\t: [str.slice(identifierStart, identifierEnd + 1)];\n\n\t\tstart = end + 1;\n\t\treturn {\n\t\t\ttype: 'export',\n\t\t\tstart: pos,\n\t\t\tend,\n\t\t\tmodules\n\t\t};\n\t}\n\n\t// import * as IDENTIFIER from \"MODULE\"\n\tfunction handleImportStar() {\n\t\tLOOKING_FOR = 'import name for import*';\n\t\tlet identifierStart = str.indexWithin('* as ', pos + 7, DISTANCE, false);\n\t\tif (identifierStart === -1) {\n\t\t\treturn handleDefaultImport();\n\t\t}\n\t\tidentifierStart += 5;\n\t\t// 7 === \"import \".length, 5 === \"* as \".length\n\t\tconst identifierEnd = str.indexWithin(' ', identifierStart + 1, lenIdentifier);\n\n\t\tLOOKING_FOR = 'name of imported module for import*';\n\t\tlet moduleStart = str.indexWithin('from ', identifierEnd + 1) + 'from'.length;\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'import*',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tidentifier: str.slice(identifierStart, identifierEnd),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// import IDENTIFIER from \"MODULE\"\n\tfunction handleDefaultImport() {\n\t\tLOOKING_FOR = 'import name for default import';\n\t\tconst identifierStart = pos + 7; // 7 === \"import \".length\n\t\tconst identifierEnd = str.indexWithin(' ', identifierStart, DISTANCE);\n\n\t\tLOOKING_FOR = 'name of imported module for import*';\n\t\tlet moduleStart = str.indexWithin('from ', identifierEnd + 1) + 'from'.length;\n\t\tmoduleStart = str.indexWithin(quote, moduleStart + 1);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tstart = moduleEnd + 1;\n\t\treturn {\n\t\t\ttype: 'importDefault',\n\t\t\tstart: pos,\n\t\t\tend: moduleEnd,\n\t\t\tisDefaultImport: true,\n\t\t\tidentifier: str.slice(identifierStart, identifierEnd),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\t// export { ... } from \"...\" | export { ... }\n\tfunction handleReExport() {\n\t\tLOOKING_FOR = 'export pattern for re-export';\n\t\tconst braceStart = str.indexWithin('{', pos + 'export '.length, 5);\n\t\tconst braceEnd = str.indexWithin('}', braceStart + 1, lenDestructure);\n\n\t\tLOOKING_FOR = 'name of re-exported module';\n\t\tlet moduleStart = str.indexWithin('from ', braceEnd + 1, 10, false);\n\n\t\tif (moduleStart === -1) {\n\t\t\t// export { ... }\n\t\t\tconst end = skipNewLines(str, braceEnd);\n\n\t\t\tstart = end + 1;\n\t\t\treturn {\n\t\t\t\ttype: 'reExportImported',\n\t\t\t\tstart: pos,\n\t\t\t\tend,\n\t\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1))\n\t\t\t};\n\t\t}\n\n\t\tmoduleStart = str.indexWithin(quote, moduleStart, 'from '.length + 4);\n\t\tconst moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName);\n\n\t\tconst end = skipNewLines(str, moduleEnd);\n\n\t\tstart = end + 1;\n\t\treturn {\n\t\t\ttype: 'reExport',\n\t\t\tstart: pos,\n\t\t\tend,\n\t\t\tmodules: destructureModules(str.slice(braceStart, braceEnd + 1)),\n\t\t\tmoduleName: str.slice(moduleStart, moduleEnd + 1)\n\t\t};\n\t}\n\n\tfunction handleExportStar() {\n\t\tthrow new Error('not implemented');\n\t}\n\n\tfunction destructureModules(objLiteral) {\n\t\treturn objLiteral\n\t\t\t.trim()\n\t\t\t.slice(1, -1)\n\t\t\t.split(/,\\s*/)\n\t\t\t.map((i) => i.trim())\n\t\t\t.filter((i) => i)\n\t\t\t.map((i) => i.split(/\\s*\\bas\\b\\s*/)); // Modified line to match https://github.com/sidvishnoi/esm-to-cjs/pull/7\n\t}\n}\n\n// this is same as indexOf, but can stop searching earlier\nfunction indexWithin(needle, from, within = 99, throws = true) {\n\tfor (let i = from, L = from + within, j = 0; i < L; ++i) {\n\t\tif (this.charCodeAt(i) === needle.charCodeAt(j)) {\n\t\t\twhile (j < needle.length) {\n\t\t\t\tif (this.charCodeAt(i + j) === needle.charCodeAt(j)) {\n\t\t\t\t\t++j;\n\t\t\t\t} else {\n\t\t\t\t\tj = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (j === needle.length) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t}\n\tif (throws) {\n\t\tthrow new Error(\n\t\t\t`ParseError: Failed to find \\`${needle}\\` within ${within} characters from position ${from}${\n\t\t\t\tLOOKING_FOR ? ` while looking for ${LOOKING_FOR}` : ''\n\t\t\t}\\n\\nINPUT STRING:` + `\\n${'*'.repeat(20)}\\n${this}\\n${'*'.repeat(20)}\\n`\n\t\t);\n\t} else {\n\t\treturn -1;\n\t}\n}\n\nfunction skipNewLines(str, i) {\n\tif (str.charAt(i + 1) === ';') ++i;\n\twhile (i < str.length && /\\s/.test(str.charAt(i))) {\n\t\t++i;\n\t}\n\treturn i;\n}\n","import sapphirePrettierConfig from '@sapphire/prettier-config';\nimport { runTransform } from './esmToCjs';\nimport prettier, { Options } from 'prettier';\nimport ts, { CompilerOptions } from 'typescript';\n\nexport const documentationPrettierConfig: Options = {\n\t...sapphirePrettierConfig,\n\ttabWidth: 2,\n\tuseTabs: false,\n\tprintWidth: 120,\n\tparser: 'babel'\n};\n\nexport const 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 */\nexport const 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 */\nexport const esmToCjs = (code: string): string =>\n\trunTransform(code, { quote: 'single', lenDestructure: 512, lenModuleName: 512, lenIdentifier: 512 });\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 */\nexport const 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 */\nexport const 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 */\nexport const 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":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA;AACA;AAFA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAEA,AAQA,WAAO,QAAQ,eAAe;AAG9B,QAAI;AAEJ,QAAM,WAAW;AAEjB,QAAM,iBAAiB;AAAA,MACtB,OAAO;AAAA,MACP,gBAAgB;AAAA,MAChB,eAAe;AAAA,MACf,eAAe;AAAA,MACf,QAAQ;AAAA;AAGT,2BAAsB,KAAK,UAAU,IAAI;AACxC,gBAAU,KAAK,mBAAmB;A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AAA;AASD,oBACN,EAAE,OAAO,MAAM,kBAAkB,IAAI,4BAA4B,OAAsB;AAAA,EACtF,MAAM;AAAA,EACN,iBAAiB;AAAA,EACjB,2BAA2B;AAAA,GAE3B;AACD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,cAAc,CAAC,SAAc;AAClC,QAAI,KAAK,SAAS,YAAY,KAAK,MAAM,SAAS,gBAAgB;AACjE,wBAAkB;AAAA;AAGnB,QAAI,UAAU,OAAO;AACpB,oBAAc;AACd,aAAO,cAAc,MAAM,EAAE,MAAM,iBAAiB;AAAA;AAGrD,QAAI,MAAM,QAAQ,KAAK,WAAW;AACjC,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,SAAS,YAAY,KAAK,SAAS;AACzC,YAAI,QAAQ;AACX,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG;AAClC,mBAAS,OAAO;AAAA,eACV;AACN,mBAAS;AAAA;AAAA;AAAA;AAKZ,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ;AAAA;AAGvB,WAAO;AAAA;AAGR,SAAO;AAAA;","names":[]}
|
|
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;AAAA;AAGT,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;AAAA;AAQzB,IAAM,UAAU,CAAC,MAAc,YAC9B,GAAG,gBAAgB,MAAM,EAAE,mBAAmB,OAAO,iBAAiB,sBAAsB,QAAQ;AAOrG,IAAM,WAAW,CAAC,SAAyB,aAAa,MAAM,EAAE,OAAO,UAAU,gBAAgB,KAAK,eAAe,KAAK,eAAe;AAOzI,IAAM,iBAAiB,CAAC,SAAiB,KAAK,QAAQ,SAAS;AAO/D,IAAM,kBAAkB,CAAC,SAAyB,KAAK,QAAQ,wBA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B;AAAA,EACjB,2BAA2B;AAAA,GAE3B;AACD,MAAI,cAAc;AAClB,MAAI,kBAAkB;AAEtB,QAAM,cAAc,CAAC,SAAc;AAClC,QAAI,KAAK,SAAS,YAAY,KAAK,MAAM,SAAS,gBAAgB;AACjE,wBAAkB;AAAA;AAGnB,QAAI,UAAU,OAAO;AACpB,oBAAc;AACd,aAAO,cAAc,MAAM,EAAE,MAAM,iBAAiB;AAAA;AAGrD,QAAI,MAAM,QAAQ,KAAK,WAAW;AACjC,UAAI,QAAQ;AACZ,aAAO,QAAQ,KAAK,SAAS,QAAQ;AACpC,cAAM,SAAS,YAAY,KAAK,SAAS;AACzC,YAAI,QAAQ;AACX,eAAK,SAAS,OAAO,OAAO,GAAG,GAAG;AAClC,mBAAS,OAAO;AAAA,eACV;AACN,mBAAS;AAAA;AAAA;AAAA;AAKZ,QAAI,KAAK,SAAS,UAAU,eAAe,CAAC,iBAAiB;AAC5D,WAAK,SAAS,QAAQ;AAAA;AAGvB,WAAO;AAAA;AAGR,SAAO;AAAA;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/docusaurus-plugin-ts2esm2cjs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3-next.2528066.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,6 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@sapphire/prettier-config": "^1.2.8",
|
|
22
|
+
"esm-to-cjs": "^1.2.1",
|
|
22
23
|
"prettier": "^2.5.1",
|
|
23
24
|
"typescript": "^4.5.4"
|
|
24
25
|
},
|
|
@@ -54,5 +55,5 @@
|
|
|
54
55
|
"publishConfig": {
|
|
55
56
|
"access": "public"
|
|
56
57
|
},
|
|
57
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "25280666506769c87974f07ca488e55f549d60f2"
|
|
58
59
|
}
|