@bunup/dts 0.14.50 → 0.14.52

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 +143 -85
  2. package/package.json +1 -1
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) {
@@ -292,7 +377,14 @@ async function dtsToFakeJs(dtsContent) {
292
377
  const referencedNames = new Set;
293
378
  const exportedNames = new Set;
294
379
  const staticImportedVars = new Set;
380
+ const namespaceMembers = new Map;
295
381
  const result = [];
382
+ const exportAssignmentNames = new Set;
383
+ for (const stmt of parsed.program.body) {
384
+ if (stmt.type === "TSExportAssignment" && stmt.expression.type === "Identifier") {
385
+ exportAssignmentNames.add(stmt.expression.name);
386
+ }
387
+ }
296
388
  for (const name of getAllImportNames(parsed.program.body)) {
297
389
  referencedNames.add(name);
298
390
  }
@@ -325,6 +417,57 @@ async function dtsToFakeJs(dtsContent) {
325
417
  result.push(jsImportExport);
326
418
  continue;
327
419
  }
420
+ if (statement.type === "TSExportAssignment") {
421
+ if (statement.expression.type === "Identifier") {
422
+ result.push(`export { ${statement.expression.name} as default }`);
423
+ const members = namespaceMembers.get(statement.expression.name);
424
+ if (members) {
425
+ for (const member of members) {
426
+ if (!exportedNames.has(member)) {
427
+ result.push(`export { ${member} };`);
428
+ exportedNames.add(member);
429
+ }
430
+ }
431
+ }
432
+ } else if (statement.expression.start && statement.expression.end) {
433
+ result.push(`export default ${dtsContent.substring(statement.expression.start, statement.expression.end)}`);
434
+ }
435
+ continue;
436
+ }
437
+ if (statement.type === "TSModuleDeclaration" && statement.body?.type === "TSModuleBlock" && statement.id?.type === "Identifier") {
438
+ const memberNames = [];
439
+ const shouldExportMembers = hasExportModifier(statement, statementText) || exportAssignmentNames.has(name ?? "");
440
+ for (const member of statement.body.body) {
441
+ if (isNullOrUndefined(member.start) || isNullOrUndefined(member.end)) {
442
+ continue;
443
+ }
444
+ const memberText = dtsContent.substring(member.start, member.end);
445
+ const memberName = getName(member, dtsContent);
446
+ if (!memberName)
447
+ continue;
448
+ memberNames.push(memberName);
449
+ referencedNames.add(memberName);
450
+ const leadingComment2 = getCommentText(member.leadingComments);
451
+ let memberTextWithComments = `${leadingComment2 ? `${leadingComment2}
452
+ ` : ""}${memberText}`;
453
+ if (hasExportModifier(member, memberText)) {
454
+ memberTextWithComments = removeExportSyntaxes(memberTextWithComments);
455
+ }
456
+ const { tokens: tokens2, extras: extras2 } = tokenizeText(memberTextWithComments, referencedNames, staticImportedVars);
457
+ for (const extra of extras2) {
458
+ result.push(extra);
459
+ }
460
+ result.push(`var ${memberName} = [${tokens2.join(", ")}];`);
461
+ if (shouldExportMembers && !exportedNames.has(memberName)) {
462
+ result.push(`export { ${memberName} };`);
463
+ exportedNames.add(memberName);
464
+ }
465
+ }
466
+ if (name) {
467
+ namespaceMembers.set(name, memberNames);
468
+ }
469
+ continue;
470
+ }
328
471
  let leadingComment = null;
329
472
  leadingComment = getCommentText(statement.leadingComments);
330
473
  let statementTextWithCommentsAttached = `${leadingComment ? `${leadingComment}
@@ -383,91 +526,6 @@ function tokenizeText(text, referencedNames, staticImportedVars) {
383
526
  }
384
527
  return { tokens, extras };
385
528
  }
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
529
  // src/fake/fake-js-to-dts.ts
472
530
  import { parse as parse2 } from "@babel/parser";
473
531
  async function fakeJsToDts(fakeJsContent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.50",
3
+ "version": "0.14.52",
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": {