vite-plugin-dts 3.9.0 → 4.0.0-beta.0

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.mjs CHANGED
@@ -9,10 +9,11 @@ import { relative, posix, resolve as resolve$1, isAbsolute, dirname, normalize,
9
9
  import { existsSync, readdirSync, lstatSync, rmdirSync } from 'node:fs';
10
10
  import { readFile, mkdir, writeFile, unlink } from 'node:fs/promises';
11
11
  import { cpus } from 'node:os';
12
- import { createParsedCommandLine } from '@vue/language-core';
12
+ import { createParsedCommandLine, resolveVueCompilerOptions, createVueLanguagePlugin } from '@vue/language-core';
13
+ import { proxyCreateProgram } from '@volar/typescript';
13
14
  import ts from 'typescript';
14
15
  import { createFilter } from '@rollup/pluginutils';
15
- import { createProgram } from 'vue-tsc';
16
+ import { removeEmitGlobalTypes } from 'vue-tsc';
16
17
  import debug from 'debug';
17
18
  import { cyan, yellow, green } from 'kolorist';
18
19
  import { ExtractorConfig, Extractor } from '@microsoft/api-extractor';
@@ -121,9 +122,10 @@ function removeDirIfEmpty(dir) {
121
122
  return onlyHasDir;
122
123
  }
123
124
  function getTsConfig(tsConfigPath, readFileSync) {
125
+ const baseConfig = ts.readConfigFile(tsConfigPath, readFileSync).config ?? {};
124
126
  const tsConfig = {
125
- compilerOptions: {},
126
- ...ts.readConfigFile(tsConfigPath, readFileSync).config ?? {}
127
+ ...baseConfig,
128
+ compilerOptions: {}
127
129
  };
128
130
  if (tsConfig.extends) {
129
131
  ensureArray(tsConfig.extends).forEach((configPath) => {
@@ -137,6 +139,7 @@ function getTsConfig(tsConfigPath, readFileSync) {
137
139
  }
138
140
  });
139
141
  }
142
+ Object.assign(tsConfig.compilerOptions, baseConfig.compilerOptions);
140
143
  return tsConfig;
141
144
  }
142
145
  const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
@@ -200,7 +203,7 @@ function findTypesPath(...pkgs) {
200
203
  for (const pkg of pkgs) {
201
204
  if (typeof pkg !== "object")
202
205
  continue;
203
- path = pkg.types || pkg.typings || pkg.exports?.["."]?.types || pkg.exports?.["./"]?.types;
206
+ path = pkg.types || pkg.typings || pkg.exports?.types || pkg.exports?.["."]?.types || pkg.exports?.["./"]?.types;
204
207
  if (path)
205
208
  return path;
206
209
  }
@@ -241,8 +244,27 @@ function editSourceMapDir(content, fromDir, toDir) {
241
244
  }
242
245
  return true;
243
246
  }
247
+ const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
248
+ const asteriskRE = /[*]+/g;
249
+ function parseTsAliases(basePath, paths) {
250
+ const result = [];
251
+ for (const [pathWithAsterisk, replacements] of Object.entries(paths)) {
252
+ const find = new RegExp(
253
+ `^${pathWithAsterisk.replace(regexpSymbolRE, "\\$1").replace(asteriskRE, "([^\\/]+)")}$`
254
+ );
255
+ let index = 1;
256
+ result.push({
257
+ find,
258
+ replacement: ensureAbsolute(
259
+ replacements[0].replace(asteriskRE, () => `$${index++}`),
260
+ basePath
261
+ )
262
+ });
263
+ }
264
+ return result;
265
+ }
244
266
 
245
- const dtsRE$1 = /\.d\.tsx?$/;
267
+ const dtsRE$1 = /\.d\.(m|c)?tsx?$/;
246
268
  function rollupDeclarationFiles({
247
269
  root,
248
270
  configPath,
@@ -362,16 +384,19 @@ function VueResolver() {
362
384
  supports(id) {
363
385
  return vueRE.test(id);
364
386
  },
365
- transform({ id, code, program, service }) {
387
+ transform({ id, code, program }) {
366
388
  const sourceFile = program.getSourceFile(id) || program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
367
389
  if (!sourceFile)
368
390
  return [];
369
- const outputs = service.getEmitOutput(sourceFile.fileName, true).outputFiles.map((file) => {
370
- return {
371
- path: file.name,
372
- content: file.text
373
- };
374
- });
391
+ const outputs = [];
392
+ program.emit(
393
+ sourceFile,
394
+ (path, content) => {
395
+ outputs.push({ path, content });
396
+ },
397
+ void 0,
398
+ true
399
+ );
375
400
  if (!program.getCompilerOptions().declarationMap)
376
401
  return outputs;
377
402
  const [beforeScript] = code.split(/\s*<script.*>/);
@@ -453,18 +478,17 @@ function transformCode(options) {
453
478
  const importMap = /* @__PURE__ */ new Map();
454
479
  const usedDefault = /* @__PURE__ */ new Map();
455
480
  const declareModules = [];
481
+ const toLibName = (origin) => {
482
+ const name = transformAlias(origin, dir, options.aliases, options.aliasesExclude);
483
+ return options.cleanVueFileName ? name.replace(/\.vue$/, "") : name;
484
+ };
456
485
  let indexCount = 0;
457
486
  walkSourceFile(ast, (node, parent) => {
458
487
  if (ts.isImportDeclaration(node)) {
459
488
  if (!node.importClause) {
460
489
  options.clearPureImport && s.remove(node.pos, node.end);
461
490
  } else if (ts.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings))) {
462
- const libName = transformAlias(
463
- node.moduleSpecifier.text,
464
- dir,
465
- options.aliases,
466
- options.aliasesExclude
467
- );
491
+ const libName = toLibName(node.moduleSpecifier.text);
468
492
  const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
469
493
  if (node.importClause.name && !usedDefault.has(libName)) {
470
494
  const usedType = node.importClause.name.escapedText;
@@ -485,12 +509,7 @@ function transformCode(options) {
485
509
  return false;
486
510
  }
487
511
  if (ts.isImportTypeNode(node) && node.qualifier && ts.isLiteralTypeNode(node.argument) && ts.isIdentifier(node.qualifier) && ts.isStringLiteral(node.argument.literal)) {
488
- const libName = transformAlias(
489
- node.argument.literal.text,
490
- dir,
491
- options.aliases,
492
- options.aliasesExclude
493
- );
512
+ const libName = toLibName(node.argument.literal.text);
494
513
  if (!options.staticImport) {
495
514
  s.update(node.argument.literal.pos, node.argument.literal.end, `'${libName}'`);
496
515
  return !!node.typeArguments;
@@ -512,23 +531,19 @@ function transformCode(options) {
512
531
  return !!node.typeArguments;
513
532
  }
514
533
  if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword && ts.isStringLiteral(node.arguments[0])) {
515
- const libName = transformAlias(
516
- node.arguments[0].text,
517
- dir,
518
- options.aliases,
519
- options.aliasesExclude
534
+ s.update(
535
+ node.arguments[0].pos,
536
+ node.arguments[0].end,
537
+ `'${toLibName(node.arguments[0].text)}'`
520
538
  );
521
- s.update(node.arguments[0].pos, node.arguments[0].end, `'${libName}'`);
522
539
  return false;
523
540
  }
524
541
  if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
525
- const libName = transformAlias(
526
- node.moduleSpecifier.text,
527
- dir,
528
- options.aliases,
529
- options.aliasesExclude
542
+ s.update(
543
+ node.moduleSpecifier.pos,
544
+ node.moduleSpecifier.end,
545
+ ` '${toLibName(node.moduleSpecifier.text)}'`
530
546
  );
531
- s.update(node.moduleSpecifier.pos, node.moduleSpecifier.end, ` '${libName}'`);
532
547
  return false;
533
548
  }
534
549
  if (ts.isModuleDeclaration(node)) {
@@ -540,10 +555,12 @@ function transformCode(options) {
540
555
  return false;
541
556
  }
542
557
  });
558
+ let prependImports = "";
543
559
  importMap.forEach((importSet, libName) => {
544
- s.prepend(`import { ${Array.from(importSet).join(", ")} } from '${libName}';
545
- `);
560
+ prependImports += `import { ${Array.from(importSet).join(", ")} } from '${libName}';
561
+ `;
546
562
  });
563
+ s.prepend(prependImports);
547
564
  return {
548
565
  content: s.toString(),
549
566
  declareModules
@@ -575,6 +592,26 @@ function hasExportDefault(content) {
575
592
  return has;
576
593
  }
577
594
 
595
+ const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts2, options) => {
596
+ const { configFilePath } = options.options;
597
+ const vueOptions = typeof configFilePath === "string" ? createParsedCommandLine(ts2, ts2.sys, configFilePath.replace(/\\/g, "/")).vueOptions : resolveVueCompilerOptions({});
598
+ if (options.host) {
599
+ const writeFile2 = options.host.writeFile.bind(options.host);
600
+ options.host.writeFile = (fileName, contents, ...args) => {
601
+ return writeFile2(fileName, removeEmitGlobalTypes(contents), ...args);
602
+ };
603
+ }
604
+ const vueLanguagePlugin = createVueLanguagePlugin(
605
+ ts2,
606
+ (id) => id,
607
+ options.host?.useCaseSensitiveFileNames?.() ?? false,
608
+ () => "",
609
+ () => options.rootNames.map((rootName) => rootName.replace(/\\/g, "/")),
610
+ options.options,
611
+ vueOptions
612
+ );
613
+ return [vueLanguagePlugin];
614
+ });
578
615
  const jsRE = /\.(m|c)?jsx?$/;
579
616
  const tsRE = /\.(m|c)?tsx?$/;
580
617
  const dtsRE = /\.d\.(m|c)?tsx?$/;
@@ -601,8 +638,6 @@ const noop = () => {
601
638
  };
602
639
  const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
603
640
  const tsToDts = (path) => `${path.replace(tsRE, "")}.d.ts`;
604
- const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
605
- const asteriskRE = /[*]+/g;
606
641
  function dtsPlugin(options = {}) {
607
642
  const {
608
643
  tsconfigPath,
@@ -640,6 +675,7 @@ function dtsPlugin(options = {}) {
640
675
  let host;
641
676
  let program;
642
677
  let filter;
678
+ let rebuildProgram;
643
679
  let bundled = false;
644
680
  let timeRecord = 0;
645
681
  const resolvers = parseResolvers([
@@ -650,6 +686,9 @@ function dtsPlugin(options = {}) {
650
686
  ]);
651
687
  const rootFiles = /* @__PURE__ */ new Set();
652
688
  const outputFiles = /* @__PURE__ */ new Map();
689
+ const setOutputFile = (path, content) => {
690
+ outputFiles.set(path, content);
691
+ };
653
692
  const rollupConfig = { ...options.rollupConfig || {} };
654
693
  rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [];
655
694
  const cleanPath = (path) => {
@@ -763,20 +802,9 @@ ${logPrefix} ${yellow(
763
802
  }
764
803
  const { baseUrl, paths } = compilerOptions;
765
804
  if (pathsToAliases && baseUrl && paths) {
766
- const basePath = ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root);
767
- for (const [pathWithAsterisk, replacements] of Object.entries(paths)) {
768
- const find = new RegExp(
769
- `^${pathWithAsterisk.replace(regexpSymbolRE, "\\$1").replace(asteriskRE, "(.+)")}$`
770
- );
771
- let index = 1;
772
- aliases.push({
773
- find,
774
- replacement: ensureAbsolute(
775
- replacements[0].replace(asteriskRE, () => `$${index++}`),
776
- basePath
777
- )
778
- });
779
- }
805
+ aliases.push(
806
+ ...parseTsAliases(ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root), paths)
807
+ );
780
808
  }
781
809
  const computeGlobs = (rootGlobs, tsGlobs, defaultGlob) => {
782
810
  if (rootGlobs?.length) {
@@ -795,11 +823,12 @@ ${logPrefix} ${yellow(
795
823
  )
796
824
  ];
797
825
  host = ts.createCompilerHost(compilerOptions);
798
- program = createProgram({
826
+ program = (rebuildProgram = () => createProgram({
799
827
  host,
800
828
  rootNames,
801
- options: compilerOptions
802
- });
829
+ options: compilerOptions,
830
+ projectReferences: content?.projectReferences
831
+ }))();
803
832
  libName = toCapitalCase(libName || "_default");
804
833
  indexName = indexName || defaultIndex;
805
834
  const maybeEmitted = (sourceFile) => {
@@ -837,7 +866,6 @@ ${logPrefix} ${yellow(
837
866
  }
838
867
  const startTime = Date.now();
839
868
  const outDir = outDirs[0];
840
- const service = program.__vue.languageService;
841
869
  id = id.split("?")[0];
842
870
  rootFiles.delete(id);
843
871
  if (resolver) {
@@ -847,11 +875,10 @@ ${logPrefix} ${yellow(
847
875
  root: publicRoot,
848
876
  outDir,
849
877
  host,
850
- program,
851
- service
878
+ program
852
879
  });
853
880
  for (const { path, content } of result) {
854
- outputFiles.set(
881
+ setOutputFile(
855
882
  resolve(publicRoot, relative(outDir, ensureAbsolute(path, outDir))),
856
883
  content
857
884
  );
@@ -859,17 +886,22 @@ ${logPrefix} ${yellow(
859
886
  } else {
860
887
  const sourceFile = program.getSourceFile(id);
861
888
  if (sourceFile) {
862
- for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
863
- outputFiles.set(
864
- resolve(publicRoot, relative(outDir, ensureAbsolute(outputFile.name, outDir))),
865
- outputFile.text
866
- );
867
- }
889
+ program.emit(
890
+ sourceFile,
891
+ (name, text) => {
892
+ setOutputFile(
893
+ resolve(publicRoot, relative(outDir, ensureAbsolute(name, outDir))),
894
+ text
895
+ );
896
+ },
897
+ void 0,
898
+ true
899
+ );
868
900
  }
869
901
  }
870
902
  const dtsId = id.replace(tjsRE, "") + ".d.ts";
871
903
  const dtsSourceFile = program.getSourceFile(dtsId);
872
- dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
904
+ dtsSourceFile && filter(dtsSourceFile.fileName) && setOutputFile(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
873
905
  timeRecord += Date.now() - startTime;
874
906
  },
875
907
  watchChange(id) {
@@ -881,9 +913,9 @@ ${logPrefix} ${yellow(
881
913
  const sourceFile = host.getSourceFile(id, ts.ScriptTarget.ESNext);
882
914
  if (sourceFile) {
883
915
  rootFiles.add(sourceFile.fileName);
884
- program.__vue.projectVersion++;
885
916
  bundled = false;
886
917
  timeRecord = 0;
918
+ program = rebuildProgram();
887
919
  }
888
920
  },
889
921
  async writeBundle() {
@@ -919,27 +951,30 @@ ${logPrefix} Start generate declaration files...`));
919
951
  await writeFile(path, content, "utf-8");
920
952
  record && emittedFiles.set(path, content);
921
953
  };
922
- const service = program.__vue.languageService;
923
954
  const sourceFiles = program.getSourceFiles();
924
955
  for (const sourceFile of sourceFiles) {
925
956
  if (!filter(sourceFile.fileName))
926
957
  continue;
927
958
  if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
928
- outputFiles.set(normalizePath(sourceFile.fileName), sourceFile.getFullText());
959
+ setOutputFile(normalizePath(sourceFile.fileName), sourceFile.getFullText());
929
960
  }
930
961
  if (rootFiles.has(sourceFile.fileName)) {
931
- for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
932
- outputFiles.set(
933
- resolve(publicRoot, relative(outDir, ensureAbsolute(outputFile.name, outDir))),
934
- outputFile.text
935
- );
936
- }
962
+ program.emit(
963
+ sourceFile,
964
+ (name, text) => {
965
+ setOutputFile(
966
+ resolve(publicRoot, relative(outDir, ensureAbsolute(name, outDir))),
967
+ text
968
+ );
969
+ },
970
+ void 0,
971
+ true
972
+ );
937
973
  rootFiles.delete(sourceFile.fileName);
938
974
  }
939
975
  }
940
976
  bundleDebug("emit output patch");
941
977
  const currentDir = host.getCurrentDirectory();
942
- const vuePathRE = /['"](.+)\.vue['"]/g;
943
978
  await runParallel(
944
979
  cpus().length,
945
980
  Array.from(outputFiles.entries()),
@@ -953,7 +988,8 @@ ${logPrefix} Start generate declaration files...`));
953
988
  aliases,
954
989
  aliasesExclude,
955
990
  staticImport,
956
- clearPureImport
991
+ clearPureImport,
992
+ cleanVueFileName
957
993
  });
958
994
  content = result.content;
959
995
  declareModules.push(...result.declareModules);
@@ -965,7 +1001,6 @@ ${logPrefix} Start generate declaration files...`));
965
1001
  cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
966
1002
  )
967
1003
  );
968
- content = cleanVueFileName ? content.replace(vuePathRE, '"$1"') : content;
969
1004
  if (isMapFile) {
970
1005
  try {
971
1006
  const sourceMap = JSON.parse(content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-dts",
3
- "version": "3.9.0",
3
+ "version": "4.0.0-beta.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "qmhc",
@@ -58,44 +58,45 @@
58
58
  "volar"
59
59
  ],
60
60
  "dependencies": {
61
- "@microsoft/api-extractor": "7.43.0",
61
+ "@microsoft/api-extractor": "7.47.2",
62
62
  "@rollup/pluginutils": "^5.1.0",
63
- "@vue/language-core": "^1.8.27",
64
- "debug": "^4.3.4",
63
+ "@volar/typescript": "^2.3.4",
64
+ "@vue/language-core": "2.0.19",
65
+ "debug": "^4.3.5",
65
66
  "kolorist": "^1.8.0",
66
- "magic-string": "^0.30.8",
67
- "vue-tsc": "^1.8.27"
67
+ "magic-string": "^0.30.10",
68
+ "vue-tsc": "2.0.19"
68
69
  },
69
70
  "devDependencies": {
70
- "@commitlint/cli": "^19.2.1",
71
+ "@commitlint/cli": "^19.3.0",
71
72
  "@types/debug": "^4.1.12",
72
73
  "@types/minimist": "^1.2.5",
73
- "@types/node": "^20.11.30",
74
+ "@types/node": "^20.14.11",
74
75
  "@types/prompts": "^2.4.9",
75
76
  "@types/semver": "^7.5.8",
76
- "@vexip-ui/commitlint-config": "^0.4.0",
77
- "@vexip-ui/eslint-config": "^0.12.0",
77
+ "@vexip-ui/commitlint-config": "^0.5.0",
78
+ "@vexip-ui/eslint-config": "^0.12.1",
78
79
  "@vexip-ui/prettier-config": "^0.2.0",
79
80
  "@vue/eslint-config-standard": "^8.0.1",
80
81
  "@vue/eslint-config-typescript": "^13.0.0",
81
- "conventional-changelog-cli": "^4.1.0",
82
+ "conventional-changelog-cli": "^5.0.0",
82
83
  "eslint": "^8.57.0",
83
84
  "execa": "^8.0.1",
84
- "husky": "^9.0.11",
85
+ "husky": "^8.0.3",
85
86
  "is-ci": "^3.0.1",
86
- "lint-staged": "^15.2.2",
87
+ "lint-staged": "^15.2.7",
87
88
  "minimist": "^1.2.8",
88
89
  "pinst": "^3.0.0",
89
- "prettier": "^3.2.5",
90
+ "prettier": "^3.3.3",
90
91
  "pretty-quick": "^4.0.0",
91
92
  "prompts": "^2.4.2",
92
- "rimraf": "^5.0.5",
93
- "semver": "^7.6.0",
94
- "tsx": "^4.7.1",
95
- "typescript": "5.4.3",
93
+ "rimraf": "^6.0.1",
94
+ "semver": "^7.6.3",
95
+ "tsx": "^4.16.2",
96
+ "typescript": "5.5.3",
96
97
  "unbuild": "^2.0.0",
97
- "vite": "^5.2.6",
98
- "vitest": "^1.4.0"
98
+ "vite": "^5.3.4",
99
+ "vitest": "^2.0.3"
99
100
  },
100
101
  "peerDependencies": {
101
102
  "typescript": "*",
@@ -108,7 +109,7 @@
108
109
  },
109
110
  "pnpm": {
110
111
  "patchedDependencies": {
111
- "@microsoft/api-extractor@7.43.0": "patches/@microsoft__api-extractor@7.43.0.patch"
112
+ "@microsoft/api-extractor@7.47.2": "patches/@microsoft__api-extractor@7.47.2.patch"
112
113
  }
113
114
  }
114
115
  }