vite-plugin-dts 3.9.1 → 4.0.0-beta.1
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/LICENSE +21 -21
- package/README.md +408 -400
- package/README.zh-CN.md +408 -400
- package/dist/index.cjs +115 -52
- package/dist/index.d.cts +0 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +117 -54
- package/package.json +24 -21
package/dist/index.mjs
CHANGED
|
@@ -9,13 +9,16 @@ 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 {
|
|
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';
|
|
20
|
+
import { getPackageInfoSync, resolveModule } from 'local-pkg';
|
|
21
|
+
import { compare } from 'compare-versions';
|
|
19
22
|
import MagicString from 'magic-string';
|
|
20
23
|
|
|
21
24
|
const windowsSlashRE = /\\+/g;
|
|
@@ -121,9 +124,10 @@ function removeDirIfEmpty(dir) {
|
|
|
121
124
|
return onlyHasDir;
|
|
122
125
|
}
|
|
123
126
|
function getTsConfig(tsConfigPath, readFileSync) {
|
|
127
|
+
const baseConfig = ts.readConfigFile(tsConfigPath, readFileSync).config ?? {};
|
|
124
128
|
const tsConfig = {
|
|
125
|
-
|
|
126
|
-
|
|
129
|
+
...baseConfig,
|
|
130
|
+
compilerOptions: {}
|
|
127
131
|
};
|
|
128
132
|
if (tsConfig.extends) {
|
|
129
133
|
ensureArray(tsConfig.extends).forEach((configPath) => {
|
|
@@ -137,6 +141,7 @@ function getTsConfig(tsConfigPath, readFileSync) {
|
|
|
137
141
|
}
|
|
138
142
|
});
|
|
139
143
|
}
|
|
144
|
+
Object.assign(tsConfig.compilerOptions, baseConfig.compilerOptions);
|
|
140
145
|
return tsConfig;
|
|
141
146
|
}
|
|
142
147
|
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
@@ -200,7 +205,7 @@ function findTypesPath(...pkgs) {
|
|
|
200
205
|
for (const pkg of pkgs) {
|
|
201
206
|
if (typeof pkg !== "object")
|
|
202
207
|
continue;
|
|
203
|
-
path = pkg.types || pkg.typings || pkg.exports?.["."]?.types || pkg.exports?.["./"]?.types;
|
|
208
|
+
path = pkg.types || pkg.typings || pkg.exports?.types || pkg.exports?.["."]?.types || pkg.exports?.["./"]?.types;
|
|
204
209
|
if (path)
|
|
205
210
|
return path;
|
|
206
211
|
}
|
|
@@ -241,8 +246,27 @@ function editSourceMapDir(content, fromDir, toDir) {
|
|
|
241
246
|
}
|
|
242
247
|
return true;
|
|
243
248
|
}
|
|
249
|
+
const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
|
|
250
|
+
const asteriskRE = /[*]+/g;
|
|
251
|
+
function parseTsAliases(basePath, paths) {
|
|
252
|
+
const result = [];
|
|
253
|
+
for (const [pathWithAsterisk, replacements] of Object.entries(paths)) {
|
|
254
|
+
const find = new RegExp(
|
|
255
|
+
`^${pathWithAsterisk.replace(regexpSymbolRE, "\\$1").replace(asteriskRE, "([^\\/]+)")}$`
|
|
256
|
+
);
|
|
257
|
+
let index = 1;
|
|
258
|
+
result.push({
|
|
259
|
+
find,
|
|
260
|
+
replacement: ensureAbsolute(
|
|
261
|
+
replacements[0].replace(asteriskRE, () => `$${index++}`),
|
|
262
|
+
basePath
|
|
263
|
+
)
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
244
268
|
|
|
245
|
-
const dtsRE$1 = /\.d\.tsx?$/;
|
|
269
|
+
const dtsRE$1 = /\.d\.(m|c)?tsx?$/;
|
|
246
270
|
function rollupDeclarationFiles({
|
|
247
271
|
root,
|
|
248
272
|
configPath,
|
|
@@ -338,6 +362,17 @@ export default _default;
|
|
|
338
362
|
}
|
|
339
363
|
|
|
340
364
|
const svelteRE = /\.svelte$/;
|
|
365
|
+
let lowerVersion;
|
|
366
|
+
function querySvelteVersion() {
|
|
367
|
+
if (typeof lowerVersion === "boolean")
|
|
368
|
+
return;
|
|
369
|
+
try {
|
|
370
|
+
const version = getPackageInfoSync("svelte")?.version ?? getPackageInfoSync("svelte", { paths: [resolveModule("svelte") || process.cwd()] })?.version;
|
|
371
|
+
lowerVersion = version ? compare(version, "4.0.0", "<") : false;
|
|
372
|
+
} catch {
|
|
373
|
+
lowerVersion = false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
341
376
|
function SvelteResolver() {
|
|
342
377
|
return {
|
|
343
378
|
name: "svelte",
|
|
@@ -345,10 +380,12 @@ function SvelteResolver() {
|
|
|
345
380
|
return svelteRE.test(id);
|
|
346
381
|
},
|
|
347
382
|
transform({ id, root }) {
|
|
383
|
+
querySvelteVersion();
|
|
348
384
|
return [
|
|
349
385
|
{
|
|
350
386
|
path: relative(root, `${id}.d.ts`),
|
|
351
|
-
content:
|
|
387
|
+
content: `export { ${lowerVersion ? "SvelteComponentTyped" : "SvelteComponent"} as default } from 'svelte';
|
|
388
|
+
`
|
|
352
389
|
}
|
|
353
390
|
];
|
|
354
391
|
}
|
|
@@ -362,16 +399,19 @@ function VueResolver() {
|
|
|
362
399
|
supports(id) {
|
|
363
400
|
return vueRE.test(id);
|
|
364
401
|
},
|
|
365
|
-
transform({ id, code, program
|
|
402
|
+
transform({ id, code, program }) {
|
|
366
403
|
const sourceFile = program.getSourceFile(id) || program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
367
404
|
if (!sourceFile)
|
|
368
405
|
return [];
|
|
369
|
-
const outputs =
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
406
|
+
const outputs = [];
|
|
407
|
+
program.emit(
|
|
408
|
+
sourceFile,
|
|
409
|
+
(path, content) => {
|
|
410
|
+
outputs.push({ path, content });
|
|
411
|
+
},
|
|
412
|
+
void 0,
|
|
413
|
+
true
|
|
414
|
+
);
|
|
375
415
|
if (!program.getCompilerOptions().declarationMap)
|
|
376
416
|
return outputs;
|
|
377
417
|
const [beforeScript] = code.split(/\s*<script.*>/);
|
|
@@ -567,6 +607,26 @@ function hasExportDefault(content) {
|
|
|
567
607
|
return has;
|
|
568
608
|
}
|
|
569
609
|
|
|
610
|
+
const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts2, options) => {
|
|
611
|
+
const { configFilePath } = options.options;
|
|
612
|
+
const vueOptions = typeof configFilePath === "string" ? createParsedCommandLine(ts2, ts2.sys, configFilePath.replace(/\\/g, "/")).vueOptions : resolveVueCompilerOptions({});
|
|
613
|
+
if (options.host) {
|
|
614
|
+
const writeFile2 = options.host.writeFile.bind(options.host);
|
|
615
|
+
options.host.writeFile = (fileName, contents, ...args) => {
|
|
616
|
+
return writeFile2(fileName, removeEmitGlobalTypes(contents), ...args);
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
const vueLanguagePlugin = createVueLanguagePlugin(
|
|
620
|
+
ts2,
|
|
621
|
+
(id) => id,
|
|
622
|
+
options.host?.useCaseSensitiveFileNames?.() ?? false,
|
|
623
|
+
() => "",
|
|
624
|
+
() => options.rootNames.map((rootName) => rootName.replace(/\\/g, "/")),
|
|
625
|
+
options.options,
|
|
626
|
+
vueOptions
|
|
627
|
+
);
|
|
628
|
+
return [vueLanguagePlugin];
|
|
629
|
+
});
|
|
570
630
|
const jsRE = /\.(m|c)?jsx?$/;
|
|
571
631
|
const tsRE = /\.(m|c)?tsx?$/;
|
|
572
632
|
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
@@ -593,8 +653,6 @@ const noop = () => {
|
|
|
593
653
|
};
|
|
594
654
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
595
655
|
const tsToDts = (path) => `${path.replace(tsRE, "")}.d.ts`;
|
|
596
|
-
const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
|
|
597
|
-
const asteriskRE = /[*]+/g;
|
|
598
656
|
function dtsPlugin(options = {}) {
|
|
599
657
|
const {
|
|
600
658
|
tsconfigPath,
|
|
@@ -632,6 +690,7 @@ function dtsPlugin(options = {}) {
|
|
|
632
690
|
let host;
|
|
633
691
|
let program;
|
|
634
692
|
let filter;
|
|
693
|
+
let rebuildProgram;
|
|
635
694
|
let bundled = false;
|
|
636
695
|
let timeRecord = 0;
|
|
637
696
|
const resolvers = parseResolvers([
|
|
@@ -642,6 +701,9 @@ function dtsPlugin(options = {}) {
|
|
|
642
701
|
]);
|
|
643
702
|
const rootFiles = /* @__PURE__ */ new Set();
|
|
644
703
|
const outputFiles = /* @__PURE__ */ new Map();
|
|
704
|
+
const setOutputFile = (path, content) => {
|
|
705
|
+
outputFiles.set(path, content);
|
|
706
|
+
};
|
|
645
707
|
const rollupConfig = { ...options.rollupConfig || {} };
|
|
646
708
|
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [];
|
|
647
709
|
const cleanPath = (path) => {
|
|
@@ -755,20 +817,9 @@ ${logPrefix} ${yellow(
|
|
|
755
817
|
}
|
|
756
818
|
const { baseUrl, paths } = compilerOptions;
|
|
757
819
|
if (pathsToAliases && baseUrl && paths) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
`^${pathWithAsterisk.replace(regexpSymbolRE, "\\$1").replace(asteriskRE, "(.+)")}$`
|
|
762
|
-
);
|
|
763
|
-
let index = 1;
|
|
764
|
-
aliases.push({
|
|
765
|
-
find,
|
|
766
|
-
replacement: ensureAbsolute(
|
|
767
|
-
replacements[0].replace(asteriskRE, () => `$${index++}`),
|
|
768
|
-
basePath
|
|
769
|
-
)
|
|
770
|
-
});
|
|
771
|
-
}
|
|
820
|
+
aliases.push(
|
|
821
|
+
...parseTsAliases(ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root), paths)
|
|
822
|
+
);
|
|
772
823
|
}
|
|
773
824
|
const computeGlobs = (rootGlobs, tsGlobs, defaultGlob) => {
|
|
774
825
|
if (rootGlobs?.length) {
|
|
@@ -778,7 +829,11 @@ ${logPrefix} ${yellow(
|
|
|
778
829
|
(glob) => normalizeGlob(ensureAbsolute(glob, configPath ? dirname(configPath) : root))
|
|
779
830
|
);
|
|
780
831
|
};
|
|
781
|
-
include = computeGlobs(
|
|
832
|
+
include = computeGlobs(
|
|
833
|
+
options.include,
|
|
834
|
+
[...ensureArray(content?.raw.include ?? []), ...ensureArray(content?.raw.files ?? [])],
|
|
835
|
+
"**/*"
|
|
836
|
+
);
|
|
782
837
|
exclude = computeGlobs(options.exclude, content?.raw.exclude, "node_modules/**");
|
|
783
838
|
filter = createFilter(include, exclude);
|
|
784
839
|
const rootNames = [
|
|
@@ -787,11 +842,12 @@ ${logPrefix} ${yellow(
|
|
|
787
842
|
)
|
|
788
843
|
];
|
|
789
844
|
host = ts.createCompilerHost(compilerOptions);
|
|
790
|
-
program = createProgram({
|
|
845
|
+
program = (rebuildProgram = () => createProgram({
|
|
791
846
|
host,
|
|
792
847
|
rootNames,
|
|
793
|
-
options: compilerOptions
|
|
794
|
-
|
|
848
|
+
options: compilerOptions,
|
|
849
|
+
projectReferences: content?.projectReferences
|
|
850
|
+
}))();
|
|
795
851
|
libName = toCapitalCase(libName || "_default");
|
|
796
852
|
indexName = indexName || defaultIndex;
|
|
797
853
|
const maybeEmitted = (sourceFile) => {
|
|
@@ -829,7 +885,6 @@ ${logPrefix} ${yellow(
|
|
|
829
885
|
}
|
|
830
886
|
const startTime = Date.now();
|
|
831
887
|
const outDir = outDirs[0];
|
|
832
|
-
const service = program.__vue.languageService;
|
|
833
888
|
id = id.split("?")[0];
|
|
834
889
|
rootFiles.delete(id);
|
|
835
890
|
if (resolver) {
|
|
@@ -839,11 +894,10 @@ ${logPrefix} ${yellow(
|
|
|
839
894
|
root: publicRoot,
|
|
840
895
|
outDir,
|
|
841
896
|
host,
|
|
842
|
-
program
|
|
843
|
-
service
|
|
897
|
+
program
|
|
844
898
|
});
|
|
845
899
|
for (const { path, content } of result) {
|
|
846
|
-
|
|
900
|
+
setOutputFile(
|
|
847
901
|
resolve(publicRoot, relative(outDir, ensureAbsolute(path, outDir))),
|
|
848
902
|
content
|
|
849
903
|
);
|
|
@@ -851,17 +905,22 @@ ${logPrefix} ${yellow(
|
|
|
851
905
|
} else {
|
|
852
906
|
const sourceFile = program.getSourceFile(id);
|
|
853
907
|
if (sourceFile) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
908
|
+
program.emit(
|
|
909
|
+
sourceFile,
|
|
910
|
+
(name, text) => {
|
|
911
|
+
setOutputFile(
|
|
912
|
+
resolve(publicRoot, relative(outDir, ensureAbsolute(name, outDir))),
|
|
913
|
+
text
|
|
914
|
+
);
|
|
915
|
+
},
|
|
916
|
+
void 0,
|
|
917
|
+
true
|
|
918
|
+
);
|
|
860
919
|
}
|
|
861
920
|
}
|
|
862
921
|
const dtsId = id.replace(tjsRE, "") + ".d.ts";
|
|
863
922
|
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
864
|
-
dtsSourceFile && filter(dtsSourceFile.fileName) &&
|
|
923
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && setOutputFile(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
|
|
865
924
|
timeRecord += Date.now() - startTime;
|
|
866
925
|
},
|
|
867
926
|
watchChange(id) {
|
|
@@ -873,9 +932,9 @@ ${logPrefix} ${yellow(
|
|
|
873
932
|
const sourceFile = host.getSourceFile(id, ts.ScriptTarget.ESNext);
|
|
874
933
|
if (sourceFile) {
|
|
875
934
|
rootFiles.add(sourceFile.fileName);
|
|
876
|
-
program.__vue.projectVersion++;
|
|
877
935
|
bundled = false;
|
|
878
936
|
timeRecord = 0;
|
|
937
|
+
program = rebuildProgram();
|
|
879
938
|
}
|
|
880
939
|
},
|
|
881
940
|
async writeBundle() {
|
|
@@ -911,21 +970,25 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
911
970
|
await writeFile(path, content, "utf-8");
|
|
912
971
|
record && emittedFiles.set(path, content);
|
|
913
972
|
};
|
|
914
|
-
const service = program.__vue.languageService;
|
|
915
973
|
const sourceFiles = program.getSourceFiles();
|
|
916
974
|
for (const sourceFile of sourceFiles) {
|
|
917
975
|
if (!filter(sourceFile.fileName))
|
|
918
976
|
continue;
|
|
919
977
|
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
920
|
-
|
|
978
|
+
setOutputFile(normalizePath(sourceFile.fileName), sourceFile.getFullText());
|
|
921
979
|
}
|
|
922
980
|
if (rootFiles.has(sourceFile.fileName)) {
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
981
|
+
program.emit(
|
|
982
|
+
sourceFile,
|
|
983
|
+
(name, text) => {
|
|
984
|
+
setOutputFile(
|
|
985
|
+
resolve(publicRoot, relative(outDir, ensureAbsolute(name, outDir))),
|
|
986
|
+
text
|
|
987
|
+
);
|
|
988
|
+
},
|
|
989
|
+
void 0,
|
|
990
|
+
true
|
|
991
|
+
);
|
|
929
992
|
rootFiles.delete(sourceFile.fileName);
|
|
930
993
|
}
|
|
931
994
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-dts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "qmhc",
|
|
@@ -58,44 +58,47 @@
|
|
|
58
58
|
"volar"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@microsoft/api-extractor": "7.
|
|
61
|
+
"@microsoft/api-extractor": "7.47.2",
|
|
62
62
|
"@rollup/pluginutils": "^5.1.0",
|
|
63
|
-
"@
|
|
64
|
-
"
|
|
63
|
+
"@volar/typescript": "^2.3.4",
|
|
64
|
+
"@vue/language-core": "2.0.19",
|
|
65
|
+
"compare-versions": "^6.1.1",
|
|
66
|
+
"debug": "^4.3.5",
|
|
65
67
|
"kolorist": "^1.8.0",
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
+
"local-pkg": "^0.5.0",
|
|
69
|
+
"magic-string": "^0.30.10",
|
|
70
|
+
"vue-tsc": "2.0.19"
|
|
68
71
|
},
|
|
69
72
|
"devDependencies": {
|
|
70
|
-
"@commitlint/cli": "^19.
|
|
73
|
+
"@commitlint/cli": "^19.3.0",
|
|
71
74
|
"@types/debug": "^4.1.12",
|
|
72
75
|
"@types/minimist": "^1.2.5",
|
|
73
|
-
"@types/node": "^20.11
|
|
76
|
+
"@types/node": "^20.14.11",
|
|
74
77
|
"@types/prompts": "^2.4.9",
|
|
75
78
|
"@types/semver": "^7.5.8",
|
|
76
|
-
"@vexip-ui/commitlint-config": "^0.
|
|
77
|
-
"@vexip-ui/eslint-config": "^0.12.
|
|
79
|
+
"@vexip-ui/commitlint-config": "^0.5.0",
|
|
80
|
+
"@vexip-ui/eslint-config": "^0.12.1",
|
|
78
81
|
"@vexip-ui/prettier-config": "^0.2.0",
|
|
79
82
|
"@vue/eslint-config-standard": "^8.0.1",
|
|
80
83
|
"@vue/eslint-config-typescript": "^13.0.0",
|
|
81
|
-
"conventional-changelog-cli": "^
|
|
84
|
+
"conventional-changelog-cli": "^5.0.0",
|
|
82
85
|
"eslint": "^8.57.0",
|
|
83
86
|
"execa": "^8.0.1",
|
|
84
|
-
"husky": "^
|
|
87
|
+
"husky": "^8.0.3",
|
|
85
88
|
"is-ci": "^3.0.1",
|
|
86
|
-
"lint-staged": "^15.2.
|
|
89
|
+
"lint-staged": "^15.2.7",
|
|
87
90
|
"minimist": "^1.2.8",
|
|
88
91
|
"pinst": "^3.0.0",
|
|
89
|
-
"prettier": "^3.
|
|
92
|
+
"prettier": "^3.3.3",
|
|
90
93
|
"pretty-quick": "^4.0.0",
|
|
91
94
|
"prompts": "^2.4.2",
|
|
92
|
-
"rimraf": "^
|
|
93
|
-
"semver": "^7.6.
|
|
94
|
-
"tsx": "^4.
|
|
95
|
-
"typescript": "5.
|
|
95
|
+
"rimraf": "^6.0.1",
|
|
96
|
+
"semver": "^7.6.3",
|
|
97
|
+
"tsx": "^4.16.2",
|
|
98
|
+
"typescript": "5.5.3",
|
|
96
99
|
"unbuild": "^2.0.0",
|
|
97
|
-
"vite": "^5.
|
|
98
|
-
"vitest": "^
|
|
100
|
+
"vite": "^5.3.4",
|
|
101
|
+
"vitest": "^2.0.3"
|
|
99
102
|
},
|
|
100
103
|
"peerDependencies": {
|
|
101
104
|
"typescript": "*",
|
|
@@ -108,7 +111,7 @@
|
|
|
108
111
|
},
|
|
109
112
|
"pnpm": {
|
|
110
113
|
"patchedDependencies": {
|
|
111
|
-
"@microsoft/api-extractor@7.
|
|
114
|
+
"@microsoft/api-extractor@7.47.2": "patches/@microsoft__api-extractor@7.47.2.patch"
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
}
|