@bunup/dts 0.14.47 → 0.14.49

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 +22 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -273,6 +273,15 @@ function unescapeNewlinesAndTabs(text) {
273
273
  return text.replace(new RegExp(MARKERS.NEWLINE, "g"), `
274
274
  `).replace(new RegExp(MARKERS.TAB, "g"), "\t");
275
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
+ }
276
285
 
277
286
  // src/fake/dts-to-fake-js.ts
278
287
  async function dtsToFakeJs(dtsContent) {
@@ -282,6 +291,7 @@ async function dtsToFakeJs(dtsContent) {
282
291
  });
283
292
  const referencedNames = new Set;
284
293
  const exportedNames = new Set;
294
+ const staticImportedVars = new Set;
285
295
  const result = [];
286
296
  for (const name of getAllImportNames(parsed.program.body)) {
287
297
  referencedNames.add(name);
@@ -323,7 +333,7 @@ async function dtsToFakeJs(dtsContent) {
323
333
  if (isExported) {
324
334
  statementTextWithCommentsAttached = removeExportSyntaxes(statementTextWithCommentsAttached);
325
335
  }
326
- const { tokens, extras } = tokenizeText(statementTextWithCommentsAttached, referencedNames);
336
+ const { tokens, extras } = tokenizeText(statementTextWithCommentsAttached, referencedNames, staticImportedVars);
327
337
  for (const extra of extras) {
328
338
  result.push(extra);
329
339
  }
@@ -348,7 +358,7 @@ function jsifyImportExport(text) {
348
358
  });
349
359
  return result;
350
360
  }
351
- function tokenizeText(text, referencedNames) {
361
+ function tokenizeText(text, referencedNames, staticImportedVars) {
352
362
  const tokens = [];
353
363
  const extras = [];
354
364
  let match;
@@ -360,8 +370,11 @@ function tokenizeText(text, referencedNames) {
360
370
  const token = match[0];
361
371
  if (token.startsWith("import(")) {
362
372
  const staticImport = convertDynamicImportToStatic(token);
363
- extras.push(staticImport.declarations);
364
373
  tokens.push(staticImport.variableName);
374
+ if (!staticImportedVars.has(staticImport.variableName)) {
375
+ extras.push(staticImport.declarations);
376
+ staticImportedVars.add(staticImport.variableName);
377
+ }
365
378
  } else if (isLikelyVariableOrTypeName(token) || referencedNames.has(token)) {
366
379
  tokens.push(token);
367
380
  } else {
@@ -378,7 +391,7 @@ function convertDynamicImportToStatic(dynamicImport) {
378
391
  const modulePath = importMatch[2];
379
392
  const propertyAccess = importMatch[3] || "";
380
393
  if (!propertyAccess) {
381
- const importIdentifier = `import_${generateRandomString()}`;
394
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
382
395
  return {
383
396
  declarations: `import * as ${importIdentifier} from '${modulePath}';`,
384
397
  variableName: importIdentifier
@@ -387,12 +400,12 @@ function convertDynamicImportToStatic(dynamicImport) {
387
400
  const firstProperty = extractFirstProperty(propertyAccess);
388
401
  const remainingAccess = propertyAccess.slice(firstProperty.accessLength);
389
402
  if (firstProperty.isValidIdentifier) {
390
- const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateRandomString()}`;
403
+ const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateFixedStringFromString(firstProperty.name)}`;
391
404
  let declarations = `import { ${firstProperty.name} as ${uniqueName} } from '${modulePath}';`;
392
405
  let finalVariable = uniqueName;
393
406
  if (remainingAccess) {
394
407
  const lastProperty = extractLastProperty(remainingAccess);
395
- const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
408
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
396
409
  declarations += `
397
410
  var ${varName} = ${uniqueName}${remainingAccess};`;
398
411
  finalVariable = varName;
@@ -402,9 +415,9 @@ var ${varName} = ${uniqueName}${remainingAccess};`;
402
415
  variableName: finalVariable
403
416
  };
404
417
  } else {
405
- const importIdentifier = `import_${generateRandomString()}`;
418
+ const importIdentifier = `import_${generateFixedStringFromString(modulePath ?? "import")}`;
406
419
  const lastProperty = extractLastProperty(propertyAccess);
407
- const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
420
+ const varName = `${createValidIdentifier(lastProperty)}_${generateFixedStringFromString(lastProperty)}`;
408
421
  const declarations = `import * as ${importIdentifier} from '${modulePath}';
409
422
  var ${varName} = ${importIdentifier}${propertyAccess};`;
410
423
  return {
@@ -852,7 +865,7 @@ Ensure all your entrypoints match these patterns, or add them explicitly.
852
865
  for (const output of outputs) {
853
866
  const bundledFakeJsContent = await output.text();
854
867
  const dtsContent = await fakeJsToDts(bundledFakeJsContent);
855
- const entrypoint = output.kind === "entry-point" ? getOriginalEntrypointFromOutputPath(result.metafile, output.path, cwd) : undefined;
868
+ const entrypoint = output.kind === "entry-point" ? cleanPath(getOriginalEntrypointFromOutputPath(result.metafile, output.path, cwd)) : undefined;
856
869
  const chunkFileName = output.kind === "chunk" ? replaceExtension(path3.basename(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))) : undefined;
857
870
  const outputPath = cleanPath(replaceExtension(cleanPath(output.path), getDeclarationExtensionFromJsExtension(getExtension(output.path))));
858
871
  const treeshakedDts = isolatedDeclaration(`${generateRandomString()}.d.ts`, dtsContent);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunup/dts",
3
- "version": "0.14.47",
3
+ "version": "0.14.49",
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": {