rolldown-plugin-dts 0.27.11 → 0.27.12

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.mjs +134 -54
  2. package/package.json +11 -8
package/dist/index.mjs CHANGED
@@ -4,11 +4,9 @@ import { t as requireTS } from "./load-tsc-BKULZsrs.mjs";
4
4
  import { createRequire } from "node:module";
5
5
  import { createDebug } from "obug";
6
6
  import { importerId, include } from "rolldown/filter";
7
- import { b, is } from "yuku-ast";
8
- import { isIdentifierName } from "yuku-ast/identifier";
9
- import { nameOf } from "yuku-ast/utils";
7
+ import { b, is, isIdentifierName, nameOf, walk, walkAsync } from "yuku-ast";
10
8
  import { print } from "yuku-codegen";
11
- import { parse, walk } from "yuku-parser";
9
+ import { parse } from "yuku-parser";
12
10
  import { fork, spawn } from "node:child_process";
13
11
  import { existsSync } from "node:fs";
14
12
  import { access, mkdtemp, readFile, rm } from "node:fs/promises";
@@ -483,11 +481,11 @@ function createFakeJsPlugin({ sourcemap, cjsDefault, sideEffects }) {
483
481
  else if ("id" in decl && decl.id) {
484
482
  let binding = decl.id;
485
483
  if (binding.type === "TSQualifiedName") binding = getIdFromTSEntityName(binding);
486
- if (sideEffect) binding = b.identifier(`_${getIdentifierIndex(identifierMap, "")}`);
484
+ if (sideEffect) binding = b.Identifier({ name: `_${getIdentifierIndex(identifierMap, "")}` });
487
485
  if (binding.type !== "Identifier") throw new Error(`Unexpected ${binding.type} declaration id`);
488
486
  bindings.push(binding);
489
487
  } else {
490
- const binding = b.identifier("export_default");
488
+ const binding = b.Identifier({ name: "export_default" });
491
489
  bindings.push(binding);
492
490
  decl.id = binding;
493
491
  }
@@ -503,32 +501,64 @@ function createFakeJsPlugin({ sourcemap, cjsDefault, sideEffects }) {
503
501
  params,
504
502
  children
505
503
  });
506
- const declarationIdNode = b.numericLiteral(declarationId);
507
- const depsBody = b.arrayExpression(deps);
508
- const depsNode = b.arrowFunctionExpression(params.map(({ name }) => b.identifier(name)), depsBody);
509
- const childrenNode = b.arrayExpression(children.map((node) => {
510
- const placeholder = b.stringLiteral("");
511
- placeholder.start = node.start;
512
- placeholder.end = node.end;
513
- return placeholder;
514
- }));
515
- const sideEffectNode = sideEffect && b.callExpression(b.identifier("sideEffect"), [bindings[0]]);
504
+ const declarationIdNode = b.Literal({
505
+ value: declarationId,
506
+ raw: String(declarationId)
507
+ });
508
+ const depsBody = b.ArrayExpression({ elements: deps });
509
+ const depsNode = b.ArrowFunctionExpression({
510
+ id: null,
511
+ generator: false,
512
+ async: false,
513
+ params: params.map(({ name }) => b.Identifier({ name })),
514
+ body: depsBody,
515
+ expression: true
516
+ });
517
+ const childrenNode = b.ArrayExpression({ elements: children.map((node) => b.Literal({
518
+ value: "",
519
+ raw: "\"\"",
520
+ start: node.start,
521
+ end: node.end
522
+ })) });
523
+ const sideEffectNode = sideEffect && b.CallExpression({
524
+ callee: b.Identifier({ name: "sideEffect" }),
525
+ arguments: [bindings[0]],
526
+ optional: false
527
+ });
516
528
  const runtimeArrayNode = runtimeBindingArrayExpression([
517
529
  declarationIdNode,
518
530
  depsNode,
519
531
  childrenNode,
520
532
  ...sideEffectNode ? [sideEffectNode] : []
521
533
  ]);
522
- const runtimeAssignment = b.variableDeclaration("var", [b.variableDeclarator(b.arrayPattern(bindings.map((binding) => ({
523
- ...binding,
524
- typeAnnotation: null
525
- }))), runtimeArrayNode)]);
534
+ const runtimeAssignment = b.VariableDeclaration({
535
+ kind: "var",
536
+ declarations: [b.VariableDeclarator({
537
+ id: b.ArrayPattern({ elements: bindings.map((binding) => ({
538
+ ...binding,
539
+ typeAnnotation: null
540
+ })) }),
541
+ init: runtimeArrayNode
542
+ })]
543
+ });
526
544
  if (isDefaultExport) {
527
- appendStmts.push(b.exportNamedDeclaration(null, [b.exportSpecifier(bindings[0], b.identifier("default"))]));
545
+ appendStmts.push(b.ExportNamedDeclaration({
546
+ declaration: null,
547
+ specifiers: [b.ExportSpecifier({
548
+ local: bindings[0],
549
+ exported: b.Identifier({ name: "default" })
550
+ })],
551
+ source: null,
552
+ attributes: []
553
+ }));
528
554
  setStmt(runtimeAssignment);
529
555
  } else setDecl(runtimeAssignment);
530
556
  }
531
- if (sideEffects) appendStmts.push(b.expressionStatement(b.callExpression(b.identifier("sideEffect"), [])));
557
+ if (sideEffects) appendStmts.push(b.ExpressionStatement({ expression: b.CallExpression({
558
+ callee: b.Identifier({ name: "sideEffect" }),
559
+ arguments: [],
560
+ optional: false
561
+ }) }));
532
562
  program.body = [
533
563
  ...Array.from(namespaceStmts.values()).map(({ stmt }) => stmt),
534
564
  ...program.body,
@@ -597,7 +627,7 @@ function createFakeJsPlugin({ sourcemap, cjsDefault, sideEffects }) {
597
627
  for (const [i, originalDep] of declaration.deps.entries()) {
598
628
  let transformedDep = transformedDeps[i];
599
629
  if (transformedDep.type === "UnaryExpression" && transformedDep.operator === "void") {
600
- const undefinedDep = b.identifier("undefined");
630
+ const undefinedDep = b.Identifier({ name: "undefined" });
601
631
  undefinedDep.start = transformedDep.start;
602
632
  undefinedDep.end = transformedDep.end;
603
633
  transformedDep = undefinedDep;
@@ -820,27 +850,19 @@ async function collectDependencies(context, node, importer, namespaceStmts, chil
820
850
  const deps = /* @__PURE__ */ new Set();
821
851
  const seen = /* @__PURE__ */ new Set();
822
852
  const preserveImportTypeCache = /* @__PURE__ */ new Map();
823
- const importSources = /* @__PURE__ */ new Set();
824
- walk(node, { TSImportType(node) {
825
- importSources.add(node.source.value);
826
- } });
827
- if (importSources.size) await Promise.all(Array.from(importSources, async (source) => {
828
- const resolved = await context.resolve(source, importer);
829
- preserveImportTypeCache.set(source, !resolved || !!resolved.external);
830
- }));
831
853
  const inferredStack = [];
832
854
  let currentInferred = /* @__PURE__ */ new Set();
833
855
  function isInferred(node) {
834
856
  return node.type === "Identifier" && currentInferred.has(node.name);
835
857
  }
836
- walk(node, {
858
+ await walkAsync(node, {
837
859
  enter(node) {
838
860
  if (node.type === "TSConditionalType") {
839
861
  const inferred = collectInferredNames(node.extendsType);
840
862
  inferredStack.push(inferred);
841
863
  }
842
864
  },
843
- leave(node, path) {
865
+ async leave(node, path) {
844
866
  const { parent } = path;
845
867
  if (node.type === "TSConditionalType") inferredStack.pop();
846
868
  else if (parent?.type === "TSConditionalType") {
@@ -876,7 +898,12 @@ async function collectDependencies(context, node, importer, namespaceStmts, chil
876
898
  case "TSImportType": {
877
899
  seen.add(node);
878
900
  const { source, qualifier } = node;
879
- const dep = importNamespace(node, qualifier, source, namespaceStmts, identifierMap, preserveImportTypeCache);
901
+ const resolved = await context.resolve(source.value, importer);
902
+ if (!resolved || !!resolved.external) {
903
+ preserveImportTypeCache.set(source.value, true);
904
+ break;
905
+ }
906
+ const dep = importNamespace(node, qualifier, source, namespaceStmts, identifierMap);
880
907
  if (dep) addDependency(dep);
881
908
  break;
882
909
  }
@@ -890,25 +917,35 @@ async function collectDependencies(context, node, importer, namespaceStmts, chil
890
917
  deps.add(node);
891
918
  }
892
919
  }
893
- function importNamespace(node, imported, source, namespaceStmts, identifierMap, preserveCache) {
894
- if (preserveCache.get(source.value) ?? true) return;
920
+ function importNamespace(node, imported, source, namespaceStmts, identifierMap) {
895
921
  const sourceText = source.value.replaceAll(/\W/g, "_");
896
922
  const localName = `_$${isIdentifierName(source.value) ? source.value : `${sourceText}${getIdentifierIndex(identifierMap, sourceText)}`}`;
897
- let local = b.identifier(localName);
923
+ let local = b.Identifier({ name: localName });
898
924
  if (namespaceStmts.has(source.value)) local = namespaceStmts.get(source.value).local;
899
925
  else namespaceStmts.set(source.value, {
900
- stmt: b.importDeclaration([b.importNamespaceSpecifier(local)], source),
926
+ stmt: b.ImportDeclaration({
927
+ specifiers: [b.ImportNamespaceSpecifier({ local })],
928
+ source,
929
+ phase: null,
930
+ attributes: []
931
+ }),
901
932
  local
902
933
  });
903
934
  if (imported) {
904
935
  const importedLeft = getIdFromTSEntityName(imported);
905
936
  if (imported.type === "ThisExpression" || importedLeft.type === "ThisExpression") throw new Error("Cannot import `this` from module.");
906
- overwriteNode(importedLeft, b.tsQualifiedName(local, { ...importedLeft }));
937
+ overwriteNode(importedLeft, b.TSQualifiedName({
938
+ left: local,
939
+ right: { ...importedLeft }
940
+ }));
907
941
  local = imported;
908
942
  }
909
943
  let replacement = node;
910
944
  if (node.typeArguments) {
911
- overwriteNode(node, b.tsTypeReference(local, node.typeArguments));
945
+ overwriteNode(node, b.TSTypeReference({
946
+ typeName: local,
947
+ typeArguments: node.typeArguments
948
+ }));
912
949
  replacement = local;
913
950
  } else overwriteNode(node, local);
914
951
  return {
@@ -961,7 +998,7 @@ function isRuntimeBindingArrayElements(elements) {
961
998
  return is.NumericLiteral(declarationId) && deps?.type === "ArrowFunctionExpression" && children?.type === "ArrayExpression" && (!effect || effect.type === "CallExpression");
962
999
  }
963
1000
  function runtimeBindingArrayExpression(elements) {
964
- return b.arrayExpression([...elements]);
1001
+ return b.ArrayExpression({ elements: [...elements] });
965
1002
  }
966
1003
  function isThisExpression(node) {
967
1004
  return is.Identifier(node, "this") || node.type === "ThisExpression" || node.type === "MemberExpression" && isThisExpression(node.object);
@@ -1017,7 +1054,7 @@ function patchImportExport(node, exportInfo, cjsDefault) {
1017
1054
  }
1018
1055
  if (cjsDefault && node.type === "ExportNamedDeclaration" && !node.source && node.specifiers.length === 1 && node.specifiers[0].type === "ExportSpecifier" && nameOf(node.specifiers[0].exported) === "default") {
1019
1056
  const defaultExport = node.specifiers[0];
1020
- return b.tsExportAssignment(defaultExport.local);
1057
+ return b.TSExportAssignment({ expression: defaultExport.local });
1021
1058
  }
1022
1059
  }
1023
1060
  }
@@ -1037,14 +1074,25 @@ function patchTsNamespace(nodes) {
1037
1074
  if (!result) continue;
1038
1075
  const [binding, exports] = result;
1039
1076
  if (!exports.properties.length) continue;
1040
- const namespaceExport = b.exportNamedDeclaration(null, exports.properties.filter((property) => property.type === "Property").map((property) => {
1041
- const local = property.value.body;
1042
- const exported = property.key;
1043
- return b.exportSpecifier(local, exported);
1044
- }));
1045
- nodes[i] = b.tsModuleDeclaration(binding, b.tsModuleBlock([namespaceExport]), {
1077
+ const namespaceExport = b.ExportNamedDeclaration({
1078
+ declaration: null,
1079
+ specifiers: exports.properties.filter((property) => property.type === "Property").map((property) => {
1080
+ const local = property.value.body;
1081
+ const exported = property.key;
1082
+ return b.ExportSpecifier({
1083
+ local,
1084
+ exported
1085
+ });
1086
+ }),
1087
+ source: null,
1088
+ attributes: []
1089
+ });
1090
+ nodes[i] = b.TSModuleDeclaration({
1091
+ id: binding,
1092
+ body: b.TSModuleBlock({ body: [namespaceExport] }),
1046
1093
  kind: "namespace",
1047
- declare: true
1094
+ declare: true,
1095
+ global: false
1048
1096
  });
1049
1097
  }
1050
1098
  return nodes.filter((node) => !removed.has(node));
@@ -1062,7 +1110,18 @@ function patchReExport(nodes) {
1062
1110
  else if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && is.Identifier(node.expression.callee, "__reExport")) {
1063
1111
  const args = node.expression.arguments;
1064
1112
  exportsNames.set(args[0].name, args[1].name);
1065
- } else if (node.type === "VariableDeclaration" && node.declarations.length === 1 && node.declarations[0].init?.type === "MemberExpression" && node.declarations[0].init.object.type === "Identifier" && exportsNames.has(node.declarations[0].init.object.name)) nodes[i] = b.tsTypeAliasDeclaration(b.identifier(node.declarations[0].id.name), b.tsTypeReference(b.tsQualifiedName(b.identifier(exportsNames.get(node.declarations[0].init.object.name)), b.identifier(node.declarations[0].init.property.name))));
1113
+ } else if (node.type === "VariableDeclaration" && node.declarations.length === 1 && node.declarations[0].init?.type === "MemberExpression" && node.declarations[0].init.object.type === "Identifier" && exportsNames.has(node.declarations[0].init.object.name)) nodes[i] = b.TSTypeAliasDeclaration({
1114
+ id: b.Identifier({ name: node.declarations[0].id.name }),
1115
+ typeParameters: null,
1116
+ typeAnnotation: b.TSTypeReference({
1117
+ typeName: b.TSQualifiedName({
1118
+ left: b.Identifier({ name: exportsNames.get(node.declarations[0].init.object.name) }),
1119
+ right: b.Identifier({ name: node.declarations[0].init.property.name })
1120
+ }),
1121
+ typeArguments: null
1122
+ }),
1123
+ declare: false
1124
+ });
1066
1125
  else if (node.type === "ExportNamedDeclaration" && node.specifiers.length === 1 && node.specifiers[0].type === "ExportSpecifier" && node.specifiers[0].local.type === "Identifier" && exportsNames.has(node.specifiers[0].local.name)) node.specifiers[0].local.name = exportsNames.get(node.specifiers[0].local.name);
1067
1126
  return nodes;
1068
1127
  }
@@ -1077,13 +1136,34 @@ function rewriteImportExport(node, set) {
1077
1136
  node.exportKind = "value";
1078
1137
  return true;
1079
1138
  } else if (node.type === "TSImportEqualsDeclaration") {
1080
- if (node.moduleReference.type === "TSExternalModuleReference") set(b.importDeclaration([b.importDefaultSpecifier(node.id)], node.moduleReference.expression));
1139
+ if (node.moduleReference.type === "TSExternalModuleReference") set(b.ImportDeclaration({
1140
+ specifiers: [b.ImportDefaultSpecifier({ local: node.id })],
1141
+ source: node.moduleReference.expression,
1142
+ phase: null,
1143
+ attributes: []
1144
+ }));
1081
1145
  return true;
1082
1146
  } else if (node.type === "TSExportAssignment" && node.expression.type === "Identifier") {
1083
- set(b.exportNamedDeclaration(null, [b.exportSpecifier(node.expression, b.identifier("default"))]));
1147
+ set(b.ExportNamedDeclaration({
1148
+ declaration: null,
1149
+ specifiers: [b.ExportSpecifier({
1150
+ local: node.expression,
1151
+ exported: b.Identifier({ name: "default" })
1152
+ })],
1153
+ source: null,
1154
+ attributes: []
1155
+ }));
1084
1156
  return true;
1085
1157
  } else if (node.type === "ExportDefaultDeclaration" && node.declaration.type === "Identifier") {
1086
- set(b.exportNamedDeclaration(null, [b.exportSpecifier(node.declaration, b.identifier("default"))]));
1158
+ set(b.ExportNamedDeclaration({
1159
+ declaration: null,
1160
+ specifiers: [b.ExportSpecifier({
1161
+ local: node.declaration,
1162
+ exported: b.Identifier({ name: "default" })
1163
+ })],
1164
+ source: null,
1165
+ attributes: []
1166
+ }));
1087
1167
  return true;
1088
1168
  }
1089
1169
  return false;
@@ -1156,7 +1236,7 @@ var VolarContext = class {
1156
1236
  this.patterns = plugins.flatMap((plugin) => plugin.extensionPatterns);
1157
1237
  }
1158
1238
  isVolarFile(id) {
1159
- return this.plugins.some((plugin) => plugin.extensionPatterns.some((pattern) => pattern.test(id)));
1239
+ return this.patterns.some((pattern) => pattern.test(id));
1160
1240
  }
1161
1241
  getExtraFileExtensions() {
1162
1242
  if (!this.plugins.length) return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
3
  "type": "module",
4
- "version": "0.27.11",
4
+ "version": "0.27.12",
5
5
  "description": "A Rolldown plugin to generate and bundle dts files.",
6
6
  "author": "Kevin Deng <sxzz@sxzz.moe>",
7
7
  "license": "MIT",
@@ -17,10 +17,13 @@
17
17
  "keywords": [
18
18
  "rolldown",
19
19
  "plugin",
20
+ "rolldown-plugin",
21
+ "vite-plugin",
20
22
  "dts",
21
23
  "typescript",
22
24
  "vue",
23
- "jsdoc"
25
+ "jsdoc",
26
+ "volar"
24
27
  ],
25
28
  "exports": {
26
29
  ".": "./dist/index.mjs",
@@ -64,10 +67,10 @@
64
67
  "dependencies": {
65
68
  "dts-resolver": "^3.0.0",
66
69
  "get-tsconfig": "5.0.0-beta.5",
67
- "obug": "^2.1.3",
68
- "yuku-ast": "^0.1.7",
69
- "yuku-codegen": "^0.6.3",
70
- "yuku-parser": "^0.6.3"
70
+ "obug": "^2.1.4",
71
+ "yuku-ast": "^0.7.0",
72
+ "yuku-codegen": "^0.7.0",
73
+ "yuku-parser": "^0.7.0"
71
74
  },
72
75
  "devDependencies": {
73
76
  "@astrojs/ts-plugin": "^1.10.10",
@@ -89,11 +92,11 @@
89
92
  "rolldown": "^1.2.0",
90
93
  "rollup-plugin-dts": "^6.4.1",
91
94
  "ts-macro": "^0.3.7",
92
- "tsdown": "^0.22.8",
95
+ "tsdown": "^0.22.11",
93
96
  "tsnapi": "^1.0.0",
94
97
  "typescript": "^6.0.3",
95
98
  "vitest": "^4.1.10",
96
- "vue": "^3.5.39",
99
+ "vue": "^3.5.40",
97
100
  "vue-tsc": "^3.3.7",
98
101
  "zod": "^4.4.3"
99
102
  },