@bunup/dts 0.14.46 → 0.14.48

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 +52 -28
  2. package/package.json +9 -9
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/generate.ts
2
2
  import { rm } from "node:fs/promises";
3
- import path2 from "node:path";
3
+ import path3 from "node:path";
4
4
  import { isolatedDeclaration } from "oxc-transform";
5
5
  import { resolveTsImportPath } from "ts-import-resolver";
6
6
 
@@ -155,13 +155,14 @@ import { normalize } from "node:path";
155
155
  import { loadConfig } from "coffi";
156
156
  import { minify } from "oxc-minify";
157
157
  import { isCI, isDevelopment } from "std-env";
158
- function isTypeScriptFile(path) {
159
- if (!path)
158
+ import path from "node:path";
159
+ function isTypeScriptFile(path2) {
160
+ if (!path2)
160
161
  return false;
161
- return TS_RE.test(path);
162
+ return TS_RE.test(path2);
162
163
  }
163
- function returnPathIfExists(path) {
164
- return existsSync(path) ? path : null;
164
+ function returnPathIfExists(path2) {
165
+ return existsSync(path2) ? path2 : null;
165
166
  }
166
167
  function getExtension(filename) {
167
168
  const match = filename.match(EXTENSION_REGEX);
@@ -208,8 +209,8 @@ function isDev() {
208
209
  function isNullOrUndefined(value) {
209
210
  return value === undefined || value === null;
210
211
  }
211
- function cleanPath(path) {
212
- let cleaned = normalize(path).replace(/\\/g, "/");
212
+ function cleanPath(path2) {
213
+ let cleaned = normalize(path2).replace(/\\/g, "/");
213
214
  cleaned = cleaned.replace(/^[a-zA-Z]:\//, "");
214
215
  cleaned = cleaned.replace(/^\/+/, "");
215
216
  cleaned = cleaned.replace(/\/+/g, "/");
@@ -255,6 +256,10 @@ function minifyDts(dts) {
255
256
  sourcemap: false
256
257
  }).code;
257
258
  }
259
+ function getOriginalEntrypointFromOutputPath(metafile, outputPath, rootDir) {
260
+ const entryPoint = metafile?.outputs[outputPath]?.entryPoint;
261
+ return path.relative(rootDir, entryPoint);
262
+ }
258
263
 
259
264
  // src/fake/utils.ts
260
265
  var MARKERS = {
@@ -268,6 +273,15 @@ function unescapeNewlinesAndTabs(text) {
268
273
  return text.replace(new RegExp(MARKERS.NEWLINE, "g"), `
269
274
  `).replace(new RegExp(MARKERS.TAB, "g"), "\t");
270
275
  }
276
+ function generateFixedStringFromString(str) {
277
+ let hash = 0;
278
+ for (let i = 0;i < str.length; i++) {
279
+ const char = str.charCodeAt(i);
280
+ hash = (hash << 5) - hash + char;
281
+ hash = hash & hash;
282
+ }
283
+ return Math.abs(hash).toString(36);
284
+ }
271
285
 
272
286
  // src/fake/dts-to-fake-js.ts
273
287
  async function dtsToFakeJs(dtsContent) {
@@ -277,6 +291,7 @@ async function dtsToFakeJs(dtsContent) {
277
291
  });
278
292
  const referencedNames = new Set;
279
293
  const exportedNames = new Set;
294
+ const staticImportedVars = new Set;
280
295
  const result = [];
281
296
  for (const name of getAllImportNames(parsed.program.body)) {
282
297
  referencedNames.add(name);
@@ -318,7 +333,7 @@ async function dtsToFakeJs(dtsContent) {
318
333
  if (isExported) {
319
334
  statementTextWithCommentsAttached = removeExportSyntaxes(statementTextWithCommentsAttached);
320
335
  }
321
- const { tokens, extras } = tokenizeText(statementTextWithCommentsAttached, referencedNames);
336
+ const { tokens, extras } = tokenizeText(statementTextWithCommentsAttached, referencedNames, staticImportedVars);
322
337
  for (const extra of extras) {
323
338
  result.push(extra);
324
339
  }
@@ -343,7 +358,7 @@ function jsifyImportExport(text) {
343
358
  });
344
359
  return result;
345
360
  }
346
- function tokenizeText(text, referencedNames) {
361
+ function tokenizeText(text, referencedNames, staticImportedVars) {
347
362
  const tokens = [];
348
363
  const extras = [];
349
364
  let match;
@@ -355,8 +370,11 @@ function tokenizeText(text, referencedNames) {
355
370
  const token = match[0];
356
371
  if (token.startsWith("import(")) {
357
372
  const staticImport = convertDynamicImportToStatic(token);
358
- extras.push(staticImport.declarations);
359
373
  tokens.push(staticImport.variableName);
374
+ if (!staticImportedVars.has(staticImport.variableName)) {
375
+ extras.push(staticImport.declarations);
376
+ staticImportedVars.add(staticImport.variableName);
377
+ }
360
378
  } else if (isLikelyVariableOrTypeName(token) || referencedNames.has(token)) {
361
379
  tokens.push(token);
362
380
  } else {
@@ -373,7 +391,7 @@ function convertDynamicImportToStatic(dynamicImport) {
373
391
  const modulePath = importMatch[2];
374
392
  const propertyAccess = importMatch[3] || "";
375
393
  if (!propertyAccess) {
376
- const importIdentifier = `import_${generateRandomString()}`;
394
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
377
395
  return {
378
396
  declarations: `import * as ${importIdentifier} from '${modulePath}';`,
379
397
  variableName: importIdentifier
@@ -382,12 +400,12 @@ function convertDynamicImportToStatic(dynamicImport) {
382
400
  const firstProperty = extractFirstProperty(propertyAccess);
383
401
  const remainingAccess = propertyAccess.slice(firstProperty.accessLength);
384
402
  if (firstProperty.isValidIdentifier) {
385
- const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateRandomString()}`;
403
+ const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateFixedStringFromString(firstProperty.name)}`;
386
404
  let declarations = `import { ${firstProperty.name} as ${uniqueName} } from '${modulePath}';`;
387
405
  let finalVariable = uniqueName;
388
406
  if (remainingAccess) {
389
407
  const lastProperty = extractLastProperty(remainingAccess);
390
- const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
408
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
391
409
  declarations += `
392
410
  var ${varName} = ${uniqueName}${remainingAccess};`;
393
411
  finalVariable = varName;
@@ -397,9 +415,9 @@ var ${varName} = ${uniqueName}${remainingAccess};`;
397
415
  variableName: finalVariable
398
416
  };
399
417
  } else {
400
- const importIdentifier = `import_${generateRandomString()}`;
418
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
401
419
  const lastProperty = extractLastProperty(propertyAccess);
402
- const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
420
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
403
421
  const declarations = `import * as ${importIdentifier} from '${modulePath}';
404
422
  var ${varName} = ${importIdentifier}${propertyAccess};`;
405
423
  return {
@@ -654,7 +672,7 @@ function createResolver({
654
672
  // src/typescript-compiler.ts
655
673
  import { mkdtemp } from "node:fs/promises";
656
674
  import { tmpdir } from "node:os";
657
- import path from "node:path";
675
+ import path2 from "node:path";
658
676
  async function runTypescriptCompiler(root, tsgo, tsconfig) {
659
677
  let executable = "tsc";
660
678
  if (tsgo) {
@@ -662,7 +680,7 @@ async function runTypescriptCompiler(root, tsgo, tsconfig) {
662
680
  const { default: getExePath } = await import(new URL("lib/getExePath.js", tsgoPackage).href);
663
681
  executable = getExePath();
664
682
  }
665
- const dist = await mkdtemp(path.join(tmpdir(), "bunup-dts-"));
683
+ const dist = await mkdtemp(path2.join(tmpdir(), "bunup-dts-"));
666
684
  const proc = Bun.spawn([
667
685
  executable,
668
686
  "--noEmit",
@@ -691,7 +709,7 @@ async function runTypescriptCompiler(root, tsgo, tsconfig) {
691
709
  // src/generate.ts
692
710
  async function generateDts(entrypoints, options = {}) {
693
711
  const { resolve, preferredTsconfig, naming } = options;
694
- const cwd = options.cwd ? path2.resolve(options.cwd) : process.cwd();
712
+ const cwd = options.cwd ? path3.resolve(options.cwd) : process.cwd();
695
713
  const tsconfig = await loadTsConfig(cwd, preferredTsconfig);
696
714
  if (options.inferTypes && !tsconfig.filepath) {
697
715
  throw new Error(`The "inferTypes" option requires a tsconfig.json file. Please create a tsconfig.json file in your project root with at least a basic configuration:
@@ -707,14 +725,14 @@ async function generateDts(entrypoints, options = {}) {
707
725
 
708
726
  ` + 'Alternatively, you can specify a custom path using the "preferredTsconfig" option.');
709
727
  }
710
- const nonAbsoluteEntrypoints = entrypoints.filter((entrypoint) => !path2.isAbsolute(entrypoint));
728
+ const nonAbsoluteEntrypoints = entrypoints.filter((entrypoint) => !path3.isAbsolute(entrypoint));
711
729
  const resolvedEntrypoints = await getFilesFromGlobs(nonAbsoluteEntrypoints, cwd);
712
- const absoluteEntrypoints = entrypoints.filter((entrypoint) => path2.isAbsolute(entrypoint));
730
+ const absoluteEntrypoints = entrypoints.filter((entrypoint) => path3.isAbsolute(entrypoint));
713
731
  if (!filterTypescriptFiles([...resolvedEntrypoints, ...absoluteEntrypoints]).length) {
714
732
  throw new Error("One or more of the entrypoints you provided do not exist. Please check that each entrypoint points to a valid file.");
715
733
  }
716
734
  const finalEntryPoints = [
717
- ...filterTypescriptFiles(resolvedEntrypoints).map((entry) => path2.resolve(path2.join(cwd, entry))),
735
+ ...filterTypescriptFiles(resolvedEntrypoints).map((entry) => path3.resolve(path3.join(cwd, entry))),
718
736
  ...filterTypescriptFiles(absoluteEntrypoints)
719
737
  ];
720
738
  const collectedErrors = [];
@@ -760,8 +778,8 @@ async function generateDts(entrypoints, options = {}) {
760
778
  declaration = sourceText;
761
779
  } else {
762
780
  if (options.inferTypes && tsCompiledDist) {
763
- const relativePath = path2.relative(cwd, args.path);
764
- const declarationPath = replaceExtension(path2.join(tsCompiledDist, relativePath), ".d.ts");
781
+ const relativePath = path3.relative(cwd, args.path);
782
+ const declarationPath = replaceExtension(path3.join(tsCompiledDist, relativePath), ".d.ts");
765
783
  declaration = await Bun.file(declarationPath).text();
766
784
  } else {
767
785
  const isolatedDeclarationResult = isolatedDeclaration(args.path, sourceText);
@@ -804,7 +822,8 @@ async function generateDts(entrypoints, options = {}) {
804
822
  minify: options.minify,
805
823
  root: options.root,
806
824
  throw: false,
807
- tsconfig: options.preferredTsconfig ? path2.resolve(cwd, options.preferredTsconfig) : undefined
825
+ tsconfig: options.preferredTsconfig ? path3.resolve(cwd, options.preferredTsconfig) : undefined,
826
+ metafile: true
808
827
  });
809
828
  if (!result.success) {
810
829
  const logsStr = `${result.logs}`;
@@ -813,7 +832,7 @@ async function generateDts(entrypoints, options = {}) {
813
832
 
814
833
  `;
815
834
  if (tsconfig.filepath) {
816
- errorMsg += `Using: ${path2.relative(cwd, tsconfig.filepath)}
835
+ errorMsg += `Using: ${path3.relative(cwd, tsconfig.filepath)}
817
836
 
818
837
  `;
819
838
  }
@@ -846,13 +865,18 @@ Ensure all your entrypoints match these patterns, or add them explicitly.
846
865
  for (const output of outputs) {
847
866
  const bundledFakeJsContent = await output.text();
848
867
  const dtsContent = await fakeJsToDts(bundledFakeJsContent);
849
- const entrypoint = output.kind === "entry-point" ? entrypoints[bundledFiles.length] : undefined;
850
- const chunkFileName = output.kind === "chunk" ? replaceExtension(path2.basename(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))) : undefined;
868
+ const entrypoint = output.kind === "entry-point" ? getOriginalEntrypointFromOutputPath(result.metafile, output.path, cwd) : undefined;
869
+ const chunkFileName = output.kind === "chunk" ? replaceExtension(path3.basename(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))) : undefined;
851
870
  const outputPath = cleanPath(replaceExtension(cleanPath(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))));
852
871
  const treeshakedDts = isolatedDeclaration(`${generateRandomString()}.d.ts`, dtsContent);
853
872
  if (!treeshakedDts.code.length && !treeshakedDts.errors.length) {
854
873
  continue;
855
874
  }
875
+ if (treeshakedDts.errors.length && !treeshakedDts.code) {
876
+ throw new Error(`DTS treeshaking failed for ${entrypoint || outputPath}
877
+
878
+ ${JSON.stringify(treeshakedDts.errors, null, 2)}`);
879
+ }
856
880
  bundledFiles.push({
857
881
  kind: output.kind === "entry-point" ? "entry-point" : "chunk",
858
882
  entrypoint,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.46",
3
+ "version": "0.14.48",
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": {
@@ -15,13 +15,13 @@
15
15
  "url": "git+https://github.com/bunup/dts.git"
16
16
  },
17
17
  "dependencies": {
18
- "@babel/parser": "^7.28.4",
18
+ "@babel/parser": "^7.28.6",
19
19
  "coffi": "^0.1.37",
20
20
  "oxc-minify": "^0.93.0",
21
- "oxc-resolver": "^11.9.0",
21
+ "oxc-resolver": "^11.16.2",
22
22
  "oxc-transform": "^0.93.0",
23
23
  "picocolors": "^1.1.1",
24
- "std-env": "^3.9.0",
24
+ "std-env": "^3.10.0",
25
25
  "ts-import-resolver": "^0.1.23"
26
26
  },
27
27
  "scripts": {
@@ -37,11 +37,11 @@
37
37
  "type-check": "tsc --noEmit"
38
38
  },
39
39
  "devDependencies": {
40
- "@biomejs/biome": "^2.2.6",
41
- "@types/bun": "^1.3.0",
42
- "@typescript/native-preview": "^7.0.0-dev.20251019.1",
43
- "bumpp": "^10.3.1",
44
- "bunup": "^0.15.4",
40
+ "@biomejs/biome": "^2.3.11",
41
+ "@types/bun": "^1.3.6",
42
+ "@typescript/native-preview": "^7.0.0-dev.20260114.1",
43
+ "bumpp": "^10.4.0",
44
+ "bunup": "^0.16.17",
45
45
  "simple-git-hooks": "^2.13.1",
46
46
  "typescript": "^5.9.3"
47
47
  },