@bunup/dts 0.14.49 → 0.14.51

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 +93 -85
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -282,6 +282,91 @@ function generateFixedStringFromString(str) {
282
282
  }
283
283
  return Math.abs(hash).toString(36);
284
284
  }
285
+ function convertDynamicImportToStatic(dynamicImport) {
286
+ const importMatch = dynamicImport.match(/^import\s*\(\s*(['"`])(.+?)\1\s*\)((?:\.[a-zA-Z_$][a-zA-Z0-9_$]*|\[(['"`]).+?\4\])*)$/);
287
+ if (!importMatch) {
288
+ throw new Error("Invalid dynamic import format");
289
+ }
290
+ const modulePath = importMatch[2];
291
+ const propertyAccess = importMatch[3] || "";
292
+ if (!propertyAccess) {
293
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
294
+ return {
295
+ declarations: `import * as ${importIdentifier} from '${modulePath}';`,
296
+ variableName: importIdentifier
297
+ };
298
+ }
299
+ const firstProperty = extractFirstProperty(propertyAccess);
300
+ const remainingAccess = propertyAccess.slice(firstProperty.accessLength);
301
+ if (firstProperty.isValidIdentifier) {
302
+ const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateFixedStringFromString(firstProperty.name)}`;
303
+ let declarations = `import { ${firstProperty.name} as ${uniqueName} } from '${modulePath}';`;
304
+ let finalVariable = uniqueName;
305
+ if (remainingAccess) {
306
+ const lastProperty = extractLastProperty(remainingAccess);
307
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
308
+ declarations += `
309
+ var ${varName} = ${uniqueName}${remainingAccess};`;
310
+ finalVariable = varName;
311
+ }
312
+ return {
313
+ declarations,
314
+ variableName: finalVariable
315
+ };
316
+ } else {
317
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
318
+ const lastProperty = extractLastProperty(propertyAccess);
319
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
320
+ const declarations = `import * as ${importIdentifier} from '${modulePath}';
321
+ var ${varName} = ${importIdentifier}${propertyAccess};`;
322
+ return {
323
+ declarations,
324
+ variableName: varName
325
+ };
326
+ }
327
+ }
328
+ function extractFirstProperty(propertyAccess) {
329
+ const dotMatch = propertyAccess.match(/^\.([a-zA-Z_$][a-zA-Z0-9_$]*)/);
330
+ if (dotMatch) {
331
+ return {
332
+ name: dotMatch[1],
333
+ accessLength: dotMatch[0].length,
334
+ isValidIdentifier: true
335
+ };
336
+ }
337
+ const bracketMatch = propertyAccess.match(/^\[(['"`])(.+?)\1\]/);
338
+ if (bracketMatch) {
339
+ const propName = bracketMatch[2];
340
+ const isValid = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName);
341
+ return {
342
+ name: propName,
343
+ accessLength: bracketMatch[0].length,
344
+ isValidIdentifier: isValid
345
+ };
346
+ }
347
+ throw new Error("Invalid property access");
348
+ }
349
+ function extractLastProperty(propertyAccess) {
350
+ const bracketMatch = propertyAccess.match(/\[(['"`])(.+?)\1\]$/);
351
+ if (bracketMatch) {
352
+ return bracketMatch[2];
353
+ }
354
+ const dotMatch = propertyAccess.match(/\.([a-zA-Z_$][a-zA-Z0-9_$]*)$/);
355
+ if (dotMatch) {
356
+ return dotMatch[1];
357
+ }
358
+ return "value";
359
+ }
360
+ function createValidIdentifier(name) {
361
+ let identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
362
+ if (/^\d/.test(identifier)) {
363
+ identifier = `_${identifier}`;
364
+ }
365
+ if (!identifier) {
366
+ identifier = "_value";
367
+ }
368
+ return identifier;
369
+ }
285
370
 
286
371
  // src/fake/dts-to-fake-js.ts
287
372
  async function dtsToFakeJs(dtsContent) {
@@ -325,6 +410,14 @@ async function dtsToFakeJs(dtsContent) {
325
410
  result.push(jsImportExport);
326
411
  continue;
327
412
  }
413
+ if (statement.type === "TSExportAssignment") {
414
+ if (statement.expression.type === "Identifier") {
415
+ result.push(`export { ${statement.expression.name} as default }`);
416
+ } else if (statement.expression.start && statement.expression.end) {
417
+ result.push(`export default ${dtsContent.substring(statement.expression.start, statement.expression.end)}`);
418
+ }
419
+ continue;
420
+ }
328
421
  let leadingComment = null;
329
422
  leadingComment = getCommentText(statement.leadingComments);
330
423
  let statementTextWithCommentsAttached = `${leadingComment ? `${leadingComment}
@@ -383,91 +476,6 @@ function tokenizeText(text, referencedNames, staticImportedVars) {
383
476
  }
384
477
  return { tokens, extras };
385
478
  }
386
- function convertDynamicImportToStatic(dynamicImport) {
387
- const importMatch = dynamicImport.match(/^import\s*\(\s*(['"`])(.+?)\1\s*\)((?:\.[a-zA-Z_$][a-zA-Z0-9_$]*|\[(['"`]).+?\4\])*)$/);
388
- if (!importMatch) {
389
- throw new Error("Invalid dynamic import format");
390
- }
391
- const modulePath = importMatch[2];
392
- const propertyAccess = importMatch[3] || "";
393
- if (!propertyAccess) {
394
- const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
395
- return {
396
- declarations: `import * as ${importIdentifier} from '${modulePath}';`,
397
- variableName: importIdentifier
398
- };
399
- }
400
- const firstProperty = extractFirstProperty(propertyAccess);
401
- const remainingAccess = propertyAccess.slice(firstProperty.accessLength);
402
- if (firstProperty.isValidIdentifier) {
403
- const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateFixedStringFromString(firstProperty.name)}`;
404
- let declarations = `import { ${firstProperty.name} as ${uniqueName} } from '${modulePath}';`;
405
- let finalVariable = uniqueName;
406
- if (remainingAccess) {
407
- const lastProperty = extractLastProperty(remainingAccess);
408
- const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
409
- declarations += `
410
- var ${varName} = ${uniqueName}${remainingAccess};`;
411
- finalVariable = varName;
412
- }
413
- return {
414
- declarations,
415
- variableName: finalVariable
416
- };
417
- } else {
418
- const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
419
- const lastProperty = extractLastProperty(propertyAccess);
420
- const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
421
- const declarations = `import * as ${importIdentifier} from '${modulePath}';
422
- var ${varName} = ${importIdentifier}${propertyAccess};`;
423
- return {
424
- declarations,
425
- variableName: varName
426
- };
427
- }
428
- }
429
- function extractFirstProperty(propertyAccess) {
430
- const dotMatch = propertyAccess.match(/^\.([a-zA-Z_$][a-zA-Z0-9_$]*)/);
431
- if (dotMatch) {
432
- return {
433
- name: dotMatch[1],
434
- accessLength: dotMatch[0].length,
435
- isValidIdentifier: true
436
- };
437
- }
438
- const bracketMatch = propertyAccess.match(/^\[(['"`])(.+?)\1\]/);
439
- if (bracketMatch) {
440
- const propName = bracketMatch[2];
441
- const isValid = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName);
442
- return {
443
- name: propName,
444
- accessLength: bracketMatch[0].length,
445
- isValidIdentifier: isValid
446
- };
447
- }
448
- throw new Error("Invalid property access");
449
- }
450
- function extractLastProperty(propertyAccess) {
451
- const bracketMatch = propertyAccess.match(/\[(['"`])(.+?)\1\]$/);
452
- if (bracketMatch) {
453
- return bracketMatch[2];
454
- }
455
- const dotMatch = propertyAccess.match(/\.([a-zA-Z_$][a-zA-Z0-9_$]*)$/);
456
- if (dotMatch) {
457
- return dotMatch[1];
458
- }
459
- return "value";
460
- }
461
- function createValidIdentifier(name) {
462
- let identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
463
- if (/^\d/.test(identifier)) {
464
- identifier = `_${identifier}`;
465
- }
466
- if (!identifier) {
467
- identifier = "_value";
468
- }
469
- return identifier;
470
- }
471
479
  // src/fake/fake-js-to-dts.ts
472
480
  import { parse as parse2 } from "@babel/parser";
473
481
  async function fakeJsToDts(fakeJsContent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.49",
3
+ "version": "0.14.51",
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": {
@@ -27,7 +27,7 @@
27
27
  "scripts": {
28
28
  "build": "bunup --exports --unused",
29
29
  "dev": "bunup --watch",
30
- "postinstall": "bun simple-git-hooks",
30
+ "prepare": "bun simple-git-hooks",
31
31
  "lint": "biome check .",
32
32
  "lint:fix": "biome check --write .",
33
33
  "release": "bumpp --commit --push --tag",