@bunup/dts 0.14.52 → 0.14.53

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 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,15 +1,10 @@
1
1
  // src/generate.ts
2
2
  import { rm } from "node:fs/promises";
3
3
  import path3 from "node:path";
4
+ import { parse as parse3 } from "@babel/parser";
4
5
  import { isolatedDeclaration } from "oxc-transform";
5
6
  import { resolveTsImportPath } from "ts-import-resolver";
6
7
 
7
- // src/constants.ts
8
- var EMPTY_EXPORT = "export {};";
9
-
10
- // src/fake/dts-to-fake-js.ts
11
- import { parse } from "@babel/parser";
12
-
13
8
  // src/re.ts
14
9
  var IMPORT_TYPE_RE = /import\s+type\s+/g;
15
10
  var EXPORT_TYPE_RE = /export\s+type\s+/g;
@@ -148,6 +143,31 @@ function getAllImportNames(body) {
148
143
  }
149
144
  return importNames;
150
145
  }
146
+ function getNamespaceImports(body) {
147
+ const result = [];
148
+ for (const statement of body) {
149
+ if (isImportDeclaration(statement)) {
150
+ const importDecl = statement;
151
+ if (importDecl.specifiers) {
152
+ for (const specifier of importDecl.specifiers) {
153
+ if (specifier.type === "ImportNamespaceSpecifier") {
154
+ result.push({
155
+ alias: specifier.local.name,
156
+ specifier: importDecl.source.value
157
+ });
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ return result;
164
+ }
165
+
166
+ // src/constants.ts
167
+ var EMPTY_EXPORT = "export {};";
168
+
169
+ // src/fake/dts-to-fake-js.ts
170
+ import { parse } from "@babel/parser";
151
171
 
152
172
  // src/utils.ts
153
173
  import { existsSync } from "node:fs";
@@ -528,7 +548,7 @@ function tokenizeText(text, referencedNames, staticImportedVars) {
528
548
  }
529
549
  // src/fake/fake-js-to-dts.ts
530
550
  import { parse as parse2 } from "@babel/parser";
531
- async function fakeJsToDts(fakeJsContent) {
551
+ async function fakeJsToDts(fakeJsContent, namespaceAliasMap) {
532
552
  const parseResult = parse2(fakeJsContent, {
533
553
  sourceType: "module",
534
554
  attachComment: false
@@ -541,15 +561,18 @@ async function fakeJsToDts(fakeJsContent) {
541
561
  }
542
562
  const statementText = fakeJsContent.substring(statement.start, statement.end);
543
563
  if (isImportDeclaration(statement) || isExportAllDeclaration(statement) || isReExportStatement(statement)) {
544
- if (isImportDeclaration(statement)) {
545
- resultParts.push(statementText.replace(/\.(mjs|cjs|js)\b/g, ""));
546
- continue;
564
+ if (namespaceAliasMap?.size && isReExportStatement(statement)) {
565
+ const fixedText = fixExportSpecifiers(statement, namespaceAliasMap);
566
+ if (fixedText) {
567
+ resultParts.push(fixedText);
568
+ continue;
569
+ }
547
570
  }
548
571
  resultParts.push(statementText);
549
572
  continue;
550
573
  }
551
574
  if (statement.type === "ExpressionStatement") {
552
- const namespaceDecl = handleNamespace(statement);
575
+ const namespaceDecl = handleNamespace(statement, namespaceAliasMap);
553
576
  if (namespaceDecl) {
554
577
  resultParts.push(namespaceDecl);
555
578
  continue;
@@ -570,7 +593,7 @@ async function fakeJsToDts(fakeJsContent) {
570
593
  }
571
594
  }
572
595
  if (declaration.init?.type === "ArrayExpression") {
573
- const dtsContent = processTokenArray(declaration.init);
596
+ const dtsContent = processTokenArray(declaration.init, namespaceAliasMap);
574
597
  if (dtsContent) {
575
598
  resultParts.push(dtsContent);
576
599
  }
@@ -619,7 +642,7 @@ function convertCallExpressionToString(node) {
619
642
  }).filter(Boolean).join(", ");
620
643
  return `${callee}(${args})`;
621
644
  }
622
- function processTokenArray(arrayLiteral) {
645
+ function processTokenArray(arrayLiteral, namespaceAliasMap) {
623
646
  if (arrayLiteral.type !== "ArrayExpression") {
624
647
  return null;
625
648
  }
@@ -627,19 +650,19 @@ function processTokenArray(arrayLiteral) {
627
650
  for (const element of arrayLiteral.elements) {
628
651
  if (!element)
629
652
  continue;
630
- const processed = processTokenElement(element);
653
+ const processed = processTokenElement(element, namespaceAliasMap);
631
654
  if (processed !== null) {
632
655
  tokens.push(processed);
633
656
  }
634
657
  }
635
658
  return tokens.join("");
636
659
  }
637
- function processTokenElement(element) {
660
+ function processTokenElement(element, namespaceAliasMap) {
638
661
  if (element.type === "StringLiteral" && typeof element.value === "string") {
639
662
  return unescapeNewlinesAndTabs(element.value);
640
663
  }
641
664
  if (element.type === "Identifier") {
642
- return element.name;
665
+ return namespaceAliasMap?.get(element.name) ?? element.name;
643
666
  }
644
667
  if (element.type === "TemplateLiteral") {
645
668
  const parts = [];
@@ -647,7 +670,7 @@ function processTokenElement(element) {
647
670
  for (let i = 0;i < element.expressions.length; i++) {
648
671
  const expr = element.expressions[i];
649
672
  if (expr?.type === "Identifier") {
650
- parts.push(expr.name);
673
+ parts.push(namespaceAliasMap?.get(expr.name) ?? expr.name);
651
674
  }
652
675
  parts.push(unescapeNewlinesAndTabs(element.quasis[i + 1]?.value?.raw || ""));
653
676
  }
@@ -655,12 +678,13 @@ function processTokenElement(element) {
655
678
  }
656
679
  return null;
657
680
  }
658
- function handleNamespace(stmt) {
681
+ function handleNamespace(stmt, namespaceAliasMap) {
659
682
  const expr = stmt.expression;
660
683
  if (!expr || expr.type !== "CallExpression" || expr.callee?.type !== "Identifier" || expr.arguments?.length !== 2 || expr.arguments[0]?.type !== "Identifier" || expr.arguments[1]?.type !== "ObjectExpression") {
661
684
  return null;
662
685
  }
663
- const namespaceName = expr.arguments[0].name;
686
+ const rawName = expr.arguments[0].name;
687
+ const namespaceName = namespaceAliasMap?.get(rawName) ?? rawName;
664
688
  const properties = expr.arguments[1].properties.filter((prop) => prop.type === "ObjectProperty").map((prop) => {
665
689
  if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.value.type === "ArrowFunctionExpression" && prop.value.body.type === "Identifier") {
666
690
  const keyName = prop.key.name;
@@ -676,6 +700,35 @@ function handleNamespace(stmt) {
676
700
  export { ${properties.join(", ")} };
677
701
  }`;
678
702
  }
703
+ function fixExportSpecifiers(statement, namespaceAliasMap) {
704
+ if (statement.type !== "ExportNamedDeclaration") {
705
+ return null;
706
+ }
707
+ const specifiers = statement.specifiers;
708
+ if (!specifiers?.length) {
709
+ return null;
710
+ }
711
+ let hasChange = false;
712
+ const fixedParts = [];
713
+ for (const spec of specifiers) {
714
+ if (spec.type !== "ExportSpecifier") {
715
+ return null;
716
+ }
717
+ const localName = spec.local.name;
718
+ const exportedName = spec.exported.type === "Identifier" ? spec.exported.name : spec.exported.value;
719
+ const alias = namespaceAliasMap.get(localName);
720
+ if (alias) {
721
+ hasChange = true;
722
+ fixedParts.push(alias === exportedName ? exportedName : `${alias} as ${exportedName}`);
723
+ } else {
724
+ fixedParts.push(localName === exportedName ? localName : `${localName} as ${exportedName}`);
725
+ }
726
+ }
727
+ if (!hasChange) {
728
+ return null;
729
+ }
730
+ return `export { ${fixedParts.join(", ")} };`;
731
+ }
679
732
  // src/resolver.ts
680
733
  import { dirname } from "node:path";
681
734
  import process2 from "node:process";
@@ -800,6 +853,7 @@ async function generateDts(entrypoints, options = {}) {
800
853
  tsconfig: tsconfig.filepath
801
854
  });
802
855
  let tsCompiledDist;
856
+ const namespaceAliasMap = new Map;
803
857
  try {
804
858
  const fakeJsPlugin = {
805
859
  name: "fake-js",
@@ -858,6 +912,28 @@ async function generateDts(entrypoints, options = {}) {
858
912
  }
859
913
  }
860
914
  if (declaration) {
915
+ if (!NODE_MODULES_RE.test(args.path)) {
916
+ try {
917
+ const parsed = parse3(declaration, {
918
+ sourceType: "module",
919
+ plugins: ["typescript"]
920
+ });
921
+ const nsImports = getNamespaceImports(parsed.program.body);
922
+ for (const nsImport of nsImports) {
923
+ const resolved = resolveTsImportPath({
924
+ importer: args.path,
925
+ path: nsImport.specifier,
926
+ cwd,
927
+ tsconfig: tsconfig.config
928
+ });
929
+ if (resolved) {
930
+ const stem = path3.basename(resolved).replace(/\.[^.]+$/, "");
931
+ const mangledName = `exports_${stem}`;
932
+ namespaceAliasMap.set(mangledName, nsImport.alias);
933
+ }
934
+ }
935
+ } catch {}
936
+ }
861
937
  fakeJsContent = await dtsToFakeJs(declaration);
862
938
  } else {
863
939
  fakeJsContent = EMPTY_EXPORT;
@@ -922,7 +998,7 @@ Ensure all your entrypoints match these patterns, or add them explicitly.
922
998
  const bundledFiles = [];
923
999
  for (const output of outputs) {
924
1000
  const bundledFakeJsContent = await output.text();
925
- const dtsContent = await fakeJsToDts(bundledFakeJsContent);
1001
+ const dtsContent = await fakeJsToDts(bundledFakeJsContent, namespaceAliasMap);
926
1002
  const entrypoint = output.kind === "entry-point" ? cleanPath(getOriginalEntrypointFromOutputPath(result.metafile, output.path, cwd)) : undefined;
927
1003
  const chunkFileName = output.kind === "chunk" ? replaceExtension(path3.basename(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))) : undefined;
928
1004
  const outputPath = cleanPath(replaceExtension(cleanPath(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.52",
3
+ "version": "0.14.53",
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": {