@bunup/dts 0.14.43 → 0.14.45
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.
- package/dist/index.js +185 -14
- package/package.json +1 -2
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ var IMPORT_EXPORT_WITH_DEFAULT_RE = /(import|export)(\s+[^{,]+,)?\s*{([^}]*)}/g;
|
|
|
18
18
|
var TYPE_WORD_RE = /\btype\s+/g;
|
|
19
19
|
var EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g;
|
|
20
20
|
var EXPORT_RE = /\bexport\s+/g;
|
|
21
|
-
var TOKENIZE_RE = /(\s+|\/\/.*?(?:\n|$)|\/\*[\s\S]*?\*\/|[a-zA-Z_$][a-zA-Z0-9_$]*|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|[(){}[\],.;:]|=>|&&|\|\||[=!<>]=?|\+\+|--|[-+*/%&|^!~?]|\.{3}|::|\.)/g;
|
|
21
|
+
var TOKENIZE_RE = /(\s+|\/\/.*?(?:\n|$)|\/\*[\s\S]*?\*\/|import\s*\((?:[^()]*|\([^()]*\))*\)(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*|\[(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')\])*|[a-zA-Z_$][a-zA-Z0-9_$]*|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|[(){}[\],.;:]|=>|&&|\|\||[=!<>]=?|\+\+|--|[-+*/%&|^!~?]|\.{3}|::|\.)/g;
|
|
22
22
|
var CAPITAL_LETTER_RE = /[A-Z]/;
|
|
23
23
|
var JS_RE = /\.[cm]?jsx?$/;
|
|
24
24
|
var TS_RE = /\.[cm]?tsx?$|\.d\.[cm]?ts$/;
|
|
@@ -318,7 +318,10 @@ async function dtsToFakeJs(dtsContent) {
|
|
|
318
318
|
if (isExported) {
|
|
319
319
|
statementTextWithCommentsAttached = removeExportSyntaxes(statementTextWithCommentsAttached);
|
|
320
320
|
}
|
|
321
|
-
const tokens = tokenizeText(statementTextWithCommentsAttached, referencedNames);
|
|
321
|
+
const { tokens, extras } = tokenizeText(statementTextWithCommentsAttached, referencedNames);
|
|
322
|
+
for (const extra of extras) {
|
|
323
|
+
result.push(extra);
|
|
324
|
+
}
|
|
322
325
|
result.push(`var ${jsVarName} = [${tokens.join(", ")}];`);
|
|
323
326
|
if (isExported && !isDefaultExport && !exportedNames.has(jsVarName)) {
|
|
324
327
|
if (isDefaultExport) {
|
|
@@ -342,6 +345,7 @@ function jsifyImportExport(text) {
|
|
|
342
345
|
}
|
|
343
346
|
function tokenizeText(text, referencedNames) {
|
|
344
347
|
const tokens = [];
|
|
348
|
+
const extras = [];
|
|
345
349
|
let match;
|
|
346
350
|
TOKENIZE_RE.lastIndex = 0;
|
|
347
351
|
while (true) {
|
|
@@ -349,13 +353,102 @@ function tokenizeText(text, referencedNames) {
|
|
|
349
353
|
if (match === null)
|
|
350
354
|
break;
|
|
351
355
|
const token = match[0];
|
|
352
|
-
if (
|
|
356
|
+
if (token.startsWith("import(")) {
|
|
357
|
+
const staticImport = convertDynamicImportToStatic(token);
|
|
358
|
+
extras.push(staticImport.declarations);
|
|
359
|
+
tokens.push(staticImport.variableName);
|
|
360
|
+
} else if (isLikelyVariableOrTypeName(token) || referencedNames.has(token)) {
|
|
353
361
|
tokens.push(token);
|
|
354
362
|
} else {
|
|
355
363
|
tokens.push(JSON.stringify(escapeNewlinesAndTabs(token)));
|
|
356
364
|
}
|
|
357
365
|
}
|
|
358
|
-
return tokens;
|
|
366
|
+
return { tokens, extras };
|
|
367
|
+
}
|
|
368
|
+
function convertDynamicImportToStatic(dynamicImport) {
|
|
369
|
+
const importMatch = dynamicImport.match(/^import\s*\(\s*(['"`])(.+?)\1\s*\)((?:\.[a-zA-Z_$][a-zA-Z0-9_$]*|\[(['"`]).+?\4\])*)$/);
|
|
370
|
+
if (!importMatch) {
|
|
371
|
+
throw new Error("Invalid dynamic import format");
|
|
372
|
+
}
|
|
373
|
+
const modulePath = importMatch[2];
|
|
374
|
+
const propertyAccess = importMatch[3] || "";
|
|
375
|
+
if (!propertyAccess) {
|
|
376
|
+
const importIdentifier = `import_${generateRandomString()}`;
|
|
377
|
+
return {
|
|
378
|
+
declarations: `import * as ${importIdentifier} from '${modulePath}';`,
|
|
379
|
+
variableName: importIdentifier
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const firstProperty = extractFirstProperty(propertyAccess);
|
|
383
|
+
const remainingAccess = propertyAccess.slice(firstProperty.accessLength);
|
|
384
|
+
if (firstProperty.isValidIdentifier) {
|
|
385
|
+
const uniqueName = `${createValidIdentifier(firstProperty.name)}_${generateRandomString()}`;
|
|
386
|
+
let declarations = `import { ${firstProperty.name} as ${uniqueName} } from '${modulePath}';`;
|
|
387
|
+
let finalVariable = uniqueName;
|
|
388
|
+
if (remainingAccess) {
|
|
389
|
+
const lastProperty = extractLastProperty(remainingAccess);
|
|
390
|
+
const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
|
|
391
|
+
declarations += `
|
|
392
|
+
var ${varName} = ${uniqueName}${remainingAccess};`;
|
|
393
|
+
finalVariable = varName;
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
declarations,
|
|
397
|
+
variableName: finalVariable
|
|
398
|
+
};
|
|
399
|
+
} else {
|
|
400
|
+
const importIdentifier = `import_${generateRandomString()}`;
|
|
401
|
+
const lastProperty = extractLastProperty(propertyAccess);
|
|
402
|
+
const varName = `${createValidIdentifier(lastProperty)}_${generateRandomString()}`;
|
|
403
|
+
const declarations = `import * as ${importIdentifier} from '${modulePath}';
|
|
404
|
+
var ${varName} = ${importIdentifier}${propertyAccess};`;
|
|
405
|
+
return {
|
|
406
|
+
declarations,
|
|
407
|
+
variableName: varName
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function extractFirstProperty(propertyAccess) {
|
|
412
|
+
const dotMatch = propertyAccess.match(/^\.([a-zA-Z_$][a-zA-Z0-9_$]*)/);
|
|
413
|
+
if (dotMatch) {
|
|
414
|
+
return {
|
|
415
|
+
name: dotMatch[1],
|
|
416
|
+
accessLength: dotMatch[0].length,
|
|
417
|
+
isValidIdentifier: true
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
const bracketMatch = propertyAccess.match(/^\[(['"`])(.+?)\1\]/);
|
|
421
|
+
if (bracketMatch) {
|
|
422
|
+
const propName = bracketMatch[2];
|
|
423
|
+
const isValid = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName);
|
|
424
|
+
return {
|
|
425
|
+
name: propName,
|
|
426
|
+
accessLength: bracketMatch[0].length,
|
|
427
|
+
isValidIdentifier: isValid
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
throw new Error("Invalid property access");
|
|
431
|
+
}
|
|
432
|
+
function extractLastProperty(propertyAccess) {
|
|
433
|
+
const bracketMatch = propertyAccess.match(/\[(['"`])(.+?)\1\]$/);
|
|
434
|
+
if (bracketMatch) {
|
|
435
|
+
return bracketMatch[2];
|
|
436
|
+
}
|
|
437
|
+
const dotMatch = propertyAccess.match(/\.([a-zA-Z_$][a-zA-Z0-9_$]*)$/);
|
|
438
|
+
if (dotMatch) {
|
|
439
|
+
return dotMatch[1];
|
|
440
|
+
}
|
|
441
|
+
return "value";
|
|
442
|
+
}
|
|
443
|
+
function createValidIdentifier(name) {
|
|
444
|
+
let identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
445
|
+
if (/^\d/.test(identifier)) {
|
|
446
|
+
identifier = `_${identifier}`;
|
|
447
|
+
}
|
|
448
|
+
if (!identifier) {
|
|
449
|
+
identifier = "_value";
|
|
450
|
+
}
|
|
451
|
+
return identifier;
|
|
359
452
|
}
|
|
360
453
|
// src/fake/fake-js-to-dts.ts
|
|
361
454
|
import { parse as parse2 } from "@babel/parser";
|
|
@@ -388,6 +481,18 @@ async function fakeJsToDts(fakeJsContent) {
|
|
|
388
481
|
}
|
|
389
482
|
if (statement.type === "VariableDeclaration") {
|
|
390
483
|
for (const declaration of statement.declarations) {
|
|
484
|
+
if (declaration.id.type === "Identifier") {
|
|
485
|
+
const init = declaration.init;
|
|
486
|
+
if (init?.type === "Identifier") {
|
|
487
|
+
resultParts.push(`type ${declaration.id.name} = ${init.name};`);
|
|
488
|
+
} else if (init?.type === "MemberExpression") {
|
|
489
|
+
const memberExpr = convertMemberExpressionToComputed(init);
|
|
490
|
+
resultParts.push(`type ${declaration.id.name} = ${memberExpr};`);
|
|
491
|
+
} else if (init?.type === "CallExpression") {
|
|
492
|
+
const callExpr = convertCallExpressionToString(init);
|
|
493
|
+
resultParts.push(`type ${declaration.id.name} = ${callExpr};`);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
391
496
|
if (declaration.init?.type === "ArrayExpression") {
|
|
392
497
|
const dtsContent = processTokenArray(declaration.init);
|
|
393
498
|
if (dtsContent) {
|
|
@@ -400,6 +505,44 @@ async function fakeJsToDts(fakeJsContent) {
|
|
|
400
505
|
return resultParts.join(`
|
|
401
506
|
`);
|
|
402
507
|
}
|
|
508
|
+
function convertMemberExpressionToComputed(node) {
|
|
509
|
+
let object = "";
|
|
510
|
+
if (node.object.type === "Identifier") {
|
|
511
|
+
object = node.object.name;
|
|
512
|
+
} else if (node.object.type === "MemberExpression") {
|
|
513
|
+
object = convertMemberExpressionToComputed(node.object);
|
|
514
|
+
}
|
|
515
|
+
let property = "";
|
|
516
|
+
if (node.property.type === "Identifier") {
|
|
517
|
+
property = `'${node.property.name}'`;
|
|
518
|
+
} else if (node.property.type === "StringLiteral") {
|
|
519
|
+
property = `'${node.property.value}'`;
|
|
520
|
+
} else if (node.property.type === "NumericLiteral") {
|
|
521
|
+
property = String(node.property.value);
|
|
522
|
+
}
|
|
523
|
+
return `${object}[${property}]`;
|
|
524
|
+
}
|
|
525
|
+
function convertCallExpressionToString(node) {
|
|
526
|
+
let callee = "";
|
|
527
|
+
if (node.callee.type === "Identifier") {
|
|
528
|
+
callee = node.callee.name;
|
|
529
|
+
} else if (node.callee.type === "MemberExpression") {
|
|
530
|
+
callee = convertMemberExpressionToComputed(node.callee);
|
|
531
|
+
}
|
|
532
|
+
const args = node.arguments.map((arg) => {
|
|
533
|
+
if (arg.type === "Identifier") {
|
|
534
|
+
return arg.name;
|
|
535
|
+
} else if (arg.type === "StringLiteral") {
|
|
536
|
+
return `'${arg.value}'`;
|
|
537
|
+
} else if (arg.type === "NumericLiteral") {
|
|
538
|
+
return String(arg.value);
|
|
539
|
+
} else if (arg.type === "MemberExpression") {
|
|
540
|
+
return convertMemberExpressionToComputed(arg);
|
|
541
|
+
}
|
|
542
|
+
return "";
|
|
543
|
+
}).filter(Boolean).join(", ");
|
|
544
|
+
return `${callee}(${args})`;
|
|
545
|
+
}
|
|
403
546
|
function processTokenArray(arrayLiteral) {
|
|
404
547
|
if (arrayLiteral.type !== "ArrayExpression") {
|
|
405
548
|
return null;
|
|
@@ -568,6 +711,10 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
568
711
|
if (!filterTypescriptFiles([...resolvedEntrypoints, ...absoluteEntrypoints]).length) {
|
|
569
712
|
throw new Error("One or more of the entrypoints you provided do not exist. Please check that each entrypoint points to a valid file.");
|
|
570
713
|
}
|
|
714
|
+
const finalEntryPoints = [
|
|
715
|
+
...filterTypescriptFiles(resolvedEntrypoints).map((entry) => path2.resolve(path2.join(cwd, entry))),
|
|
716
|
+
...filterTypescriptFiles(absoluteEntrypoints)
|
|
717
|
+
];
|
|
571
718
|
const collectedErrors = [];
|
|
572
719
|
const resolver = createResolver({
|
|
573
720
|
cwd,
|
|
@@ -645,10 +792,7 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
645
792
|
}
|
|
646
793
|
};
|
|
647
794
|
const result = await Bun.build({
|
|
648
|
-
entrypoints:
|
|
649
|
-
...filterTypescriptFiles(resolvedEntrypoints).map((entry) => path2.resolve(path2.join(cwd, entry))),
|
|
650
|
-
...filterTypescriptFiles(absoluteEntrypoints)
|
|
651
|
-
],
|
|
795
|
+
entrypoints: finalEntryPoints,
|
|
652
796
|
format: "esm",
|
|
653
797
|
target: "node",
|
|
654
798
|
naming,
|
|
@@ -661,7 +805,39 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
661
805
|
tsconfig: options.preferredTsconfig ? path2.resolve(cwd, options.preferredTsconfig) : undefined
|
|
662
806
|
});
|
|
663
807
|
if (!result.success) {
|
|
664
|
-
|
|
808
|
+
const logsStr = `${result.logs}`;
|
|
809
|
+
if (logsStr.includes("ENOENT: no such file or directory, open") && logsStr.endsWith(".d.ts'")) {
|
|
810
|
+
let errorMsg = `One or more of your entrypoints are not included in your TypeScript configuration.
|
|
811
|
+
|
|
812
|
+
`;
|
|
813
|
+
if (tsconfig.filepath) {
|
|
814
|
+
errorMsg += `Using: ${path2.relative(cwd, tsconfig.filepath)}
|
|
815
|
+
|
|
816
|
+
`;
|
|
817
|
+
}
|
|
818
|
+
const currentInclude = tsconfig.config?.include;
|
|
819
|
+
if (currentInclude && Array.isArray(currentInclude) && currentInclude.length > 0) {
|
|
820
|
+
errorMsg += `Current "include" patterns:
|
|
821
|
+
`;
|
|
822
|
+
for (const pattern of currentInclude) {
|
|
823
|
+
errorMsg += ` - "${pattern}"
|
|
824
|
+
`;
|
|
825
|
+
}
|
|
826
|
+
errorMsg += `
|
|
827
|
+
Ensure all your entrypoints match these patterns, or add them explicitly.
|
|
828
|
+
`;
|
|
829
|
+
} else {
|
|
830
|
+
errorMsg += `Add an "include" field to your tsconfig.json:
|
|
831
|
+
` + ` {
|
|
832
|
+
` + ` "include": ["src/**/*"]
|
|
833
|
+
` + ` }
|
|
834
|
+
|
|
835
|
+
` + `Make sure the pattern matches all your entrypoint files.
|
|
836
|
+
`;
|
|
837
|
+
}
|
|
838
|
+
throw new Error(errorMsg);
|
|
839
|
+
}
|
|
840
|
+
throw new Error(logsStr);
|
|
665
841
|
}
|
|
666
842
|
const outputs = result.outputs.filter((output) => output.kind === "chunk" || output.kind === "entry-point");
|
|
667
843
|
const bundledFiles = [];
|
|
@@ -675,11 +851,6 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
675
851
|
if (!treeshakedDts.code.length && !treeshakedDts.errors.length) {
|
|
676
852
|
continue;
|
|
677
853
|
}
|
|
678
|
-
if (treeshakedDts.errors.length && !treeshakedDts.code) {
|
|
679
|
-
throw new Error(`DTS treeshaking failed for ${entrypoint || outputPath}
|
|
680
|
-
|
|
681
|
-
${JSON.stringify(treeshakedDts.errors, null, 2)}`);
|
|
682
|
-
}
|
|
683
854
|
bundledFiles.push({
|
|
684
855
|
kind: output.kind === "entry-point" ? "entry-point" : "chunk",
|
|
685
856
|
entrypoint,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bunup/dts",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.45",
|
|
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": {
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"lint": "biome check .",
|
|
32
32
|
"lint:fix": "biome check --write .",
|
|
33
33
|
"release": "bumpp --commit --push --tag",
|
|
34
|
-
"play": "bun run playground/play.ts",
|
|
35
34
|
"test": "bun test",
|
|
36
35
|
"test:coverage": "bun test --coverage",
|
|
37
36
|
"test:watch": "bun test --watch",
|