@bunup/dts 0.14.30 → 0.14.32

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.
Files changed (2) hide show
  1. package/dist/index.js +96 -47
  2. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { resolveTsImportPath } from "ts-import-resolver";
7
7
  // src/constants.ts
8
8
  var EMPTY_EXPORT = "export {};";
9
9
 
10
- // src/fake-js.ts
10
+ // src/fake/dts-to-fake-js.ts
11
11
  import { parse } from "@babel/parser";
12
12
 
13
13
  // src/re.ts
@@ -53,6 +53,35 @@ function hasDefaultExportModifier(node, text) {
53
53
  function isDefaultReExport(node) {
54
54
  return node.type === "ExportDefaultDeclaration" && node.declaration?.type === "Identifier";
55
55
  }
56
+ function isUnnamedDefaultExport(node) {
57
+ if (node.type !== "ExportDefaultDeclaration") {
58
+ return false;
59
+ }
60
+ const declaration = node.declaration;
61
+ if (!declaration) {
62
+ return false;
63
+ }
64
+ if ((declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") && !declaration.id) {
65
+ return true;
66
+ }
67
+ if (declaration.type === "ClassDeclaration" && !declaration.id) {
68
+ return true;
69
+ }
70
+ return false;
71
+ }
72
+ function assignNameToUnnamedDefaultExport(text, varName) {
73
+ const functionPattern = /export\s+default\s+function\s*([(<])/;
74
+ const functionMatch = text.match(functionPattern);
75
+ if (functionMatch) {
76
+ return text.replace(functionPattern, `export default function ${varName}$1`);
77
+ }
78
+ const classPattern = /export\s+default\s+class\s*([{]|extends|implements)/;
79
+ const classMatch = text.match(classPattern);
80
+ if (classMatch) {
81
+ return text.replace(classPattern, `export default class ${varName} $1`);
82
+ }
83
+ return text;
84
+ }
56
85
  function getName(node, source) {
57
86
  if (!node)
58
87
  return null;
@@ -165,6 +194,11 @@ function getShortFilePath(filePath, maxLength = 3) {
165
194
  const shortPath = fileParts.slice(-maxLength).join("/");
166
195
  return shortPath;
167
196
  }
197
+ function generateVarName(index) {
198
+ const letter = String.fromCharCode(97 + index % 26);
199
+ const suffix = Math.floor(index / 26);
200
+ return suffix === 0 ? letter : `${letter}${suffix - 1}`;
201
+ }
168
202
  function generateRandomString(length = 10) {
169
203
  return Array.from({ length }, () => String.fromCharCode(97 + Math.floor(Math.random() * 26))).join("");
170
204
  }
@@ -222,7 +256,20 @@ function minifyDts(dts) {
222
256
  }).code;
223
257
  }
224
258
 
225
- // src/fake-js.ts
259
+ // src/fake/utils.ts
260
+ var MARKERS = {
261
+ NEWLINE: "_NL_",
262
+ TAB: "_TB_"
263
+ };
264
+ function escapeNewlinesAndTabs(text) {
265
+ return text.replace(/\n/g, MARKERS.NEWLINE).replace(/\t/g, MARKERS.TAB);
266
+ }
267
+ function unescapeNewlinesAndTabs(text) {
268
+ return text.replace(new RegExp(MARKERS.NEWLINE, "g"), `
269
+ `).replace(new RegExp(MARKERS.TAB, "g"), "\t");
270
+ }
271
+
272
+ // src/fake/dts-to-fake-js.ts
226
273
  async function dtsToFakeJs(dtsContent) {
227
274
  const parsed = parse(dtsContent, {
228
275
  sourceType: "module",
@@ -234,19 +281,23 @@ async function dtsToFakeJs(dtsContent) {
234
281
  for (const name of getAllImportNames(parsed.program.body)) {
235
282
  referencedNames.add(name);
236
283
  }
237
- for (const statement of parsed.program.body) {
284
+ for (const [index, statement] of parsed.program.body.entries()) {
238
285
  if (isNullOrUndefined(statement.start) || isNullOrUndefined(statement.end)) {
239
286
  continue;
240
287
  }
241
- const statementText = dtsContent.substring(statement.start, statement.end);
288
+ let statementText = dtsContent.substring(statement.start, statement.end);
242
289
  const name = getName(statement, dtsContent);
290
+ const jsVarName = name || generateVarName(index);
243
291
  if (name) {
244
292
  referencedNames.add(name);
245
293
  }
246
294
  const isDefaultExport = hasDefaultExportModifier(statement, statementText);
247
- const varName = name || generateRandomString();
295
+ if (isDefaultExport && isUnnamedDefaultExport(statement)) {
296
+ statementText = assignNameToUnnamedDefaultExport(statementText, jsVarName);
297
+ referencedNames.add(jsVarName);
298
+ }
248
299
  if (isDefaultExport) {
249
- result.push(`export { ${varName} as default };`);
300
+ result.push(`export { ${jsVarName} as default }`);
250
301
  if (isDefaultReExport(statement)) {
251
302
  continue;
252
303
  }
@@ -261,24 +312,55 @@ async function dtsToFakeJs(dtsContent) {
261
312
  }
262
313
  let leadingComment = null;
263
314
  leadingComment = getCommentText(statement.leadingComments);
264
- let statementTextWithCommentsAttatched = `${leadingComment ? `${leadingComment}
315
+ let statementTextWithCommentsAttached = `${leadingComment ? `${leadingComment}
265
316
  ` : ""}${statementText}`;
266
317
  const isExported = hasExportModifier(statement, statementText);
267
318
  if (isExported) {
268
- statementTextWithCommentsAttatched = removeExportSyntaxes(statementTextWithCommentsAttatched);
319
+ statementTextWithCommentsAttached = removeExportSyntaxes(statementTextWithCommentsAttached);
269
320
  }
270
- const tokens = tokenizeText(statementTextWithCommentsAttatched, referencedNames);
271
- result.push(`var ${varName} = [${tokens.join(", ")}];`);
272
- if (isExported && !isDefaultExport && !exportedNames.has(varName)) {
273
- result.push(`export { ${varName} };`);
274
- exportedNames.add(varName);
321
+ const tokens = tokenizeText(statementTextWithCommentsAttached, referencedNames);
322
+ result.push(`var ${jsVarName} = [${tokens.join(", ")}];`);
323
+ if (isExported && !isDefaultExport && !exportedNames.has(jsVarName)) {
324
+ if (isDefaultExport) {
325
+ result.push(`export { ${jsVarName} as default };`);
326
+ } else {
327
+ result.push(`export { ${jsVarName} };`);
328
+ }
329
+ exportedNames.add(jsVarName);
275
330
  }
276
331
  }
277
332
  return result.join(`
278
333
  `);
279
334
  }
335
+ function jsifyImportExport(text) {
336
+ let result = text.replace(IMPORT_TYPE_RE, "import ").replace(EXPORT_TYPE_RE, "export ").replace(IMPORT_EXPORT_NAMES_RE, (_, keyword, names) => `${keyword} {${names.replace(TYPE_WORD_RE, "")}}`);
337
+ result = result.replace(IMPORT_EXPORT_WITH_DEFAULT_RE, (_, keyword, defaultPart = "", names = "") => {
338
+ const cleanedNames = names.replace(TYPE_WORD_RE, "");
339
+ return `${keyword}${defaultPart}{${cleanedNames}}`;
340
+ });
341
+ return result;
342
+ }
343
+ function tokenizeText(text, referencedNames) {
344
+ const tokens = [];
345
+ let match;
346
+ TOKENIZE_RE.lastIndex = 0;
347
+ while (true) {
348
+ match = TOKENIZE_RE.exec(text);
349
+ if (match === null)
350
+ break;
351
+ const token = match[0];
352
+ if (isLikelyVariableOrTypeName(token) || referencedNames.has(token)) {
353
+ tokens.push(token);
354
+ } else {
355
+ tokens.push(JSON.stringify(escapeNewlinesAndTabs(token)));
356
+ }
357
+ }
358
+ return tokens;
359
+ }
360
+ // src/fake/fake-js-to-dts.ts
361
+ import { parse as parse2 } from "@babel/parser";
280
362
  async function fakeJsToDts(fakeJsContent) {
281
- const parseResult = parse(fakeJsContent, {
363
+ const parseResult = parse2(fakeJsContent, {
282
364
  sourceType: "module",
283
365
  attachComment: false
284
366
  });
@@ -318,31 +400,6 @@ async function fakeJsToDts(fakeJsContent) {
318
400
  return resultParts.join(`
319
401
  `);
320
402
  }
321
- function jsifyImportExport(text) {
322
- let result = text.replace(IMPORT_TYPE_RE, "import ").replace(EXPORT_TYPE_RE, "export ").replace(IMPORT_EXPORT_NAMES_RE, (_, keyword, names) => `${keyword} {${names.replace(TYPE_WORD_RE, "")}}`);
323
- result = result.replace(IMPORT_EXPORT_WITH_DEFAULT_RE, (_, keyword, defaultPart = "", names = "") => {
324
- const cleanedNames = names.replace(TYPE_WORD_RE, "");
325
- return `${keyword}${defaultPart}{${cleanedNames}}`;
326
- });
327
- return result;
328
- }
329
- function tokenizeText(text, referencedNames) {
330
- const tokens = [];
331
- let match;
332
- TOKENIZE_RE.lastIndex = 0;
333
- while (true) {
334
- match = TOKENIZE_RE.exec(text);
335
- if (match === null)
336
- break;
337
- const token = match[0];
338
- if (isLikelyVariableOrTypeName(token) || referencedNames.has(token)) {
339
- tokens.push(token);
340
- } else {
341
- tokens.push(JSON.stringify(escapeNewlinesAndTabs(token)));
342
- }
343
- }
344
- return tokens;
345
- }
346
403
  function processTokenArray(arrayLiteral) {
347
404
  if (arrayLiteral.type !== "ArrayExpression") {
348
405
  return null;
@@ -379,13 +436,6 @@ function processTokenElement(element) {
379
436
  }
380
437
  return null;
381
438
  }
382
- function escapeNewlinesAndTabs(text) {
383
- return text.replace(/\n/g, "__bunup_dts_intermediate_new__line__").replace(/\t/g, "__bunup_dts_intermediate__tab__");
384
- }
385
- function unescapeNewlinesAndTabs(text) {
386
- return text.replace(/__bunup_dts_intermediate_new__line__/g, `
387
- `).replace(/__bunup_dts_intermediate__tab__/g, "\t");
388
- }
389
439
  function handleNamespace(stmt) {
390
440
  const expr = stmt.expression;
391
441
  if (!expr || expr.type !== "CallExpression" || expr.callee?.type !== "Identifier" || expr.arguments?.length !== 2 || expr.arguments[0]?.type !== "Identifier" || expr.arguments[1]?.type !== "ObjectExpression") {
@@ -407,7 +457,6 @@ function handleNamespace(stmt) {
407
457
  export { ${properties.join(", ")} };
408
458
  }`;
409
459
  }
410
-
411
460
  // src/resolver.ts
412
461
  import { dirname } from "node:path";
413
462
  import process2 from "node:process";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.30",
3
+ "version": "0.14.32",
4
4
  "description": "An extremely fast TypeScript declaration bundler built on Bun's native bundler.",
5
5
  "homepage": "https://github.com/bunup/dts#readme",
6
6
  "bugs": {
@@ -31,6 +31,7 @@
31
31
  "lint": "biome check .",
32
32
  "lint:fix": "biome check --write .",
33
33
  "release": "bumpp --commit --push --tag",
34
+ "play": "bun run playground/play.ts",
34
35
  "test": "bun test",
35
36
  "test:coverage": "bun test --coverage",
36
37
  "test:watch": "bun test --watch",