vite-plugin-dts 3.0.3 → 3.1.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/README.md +104 -97
- package/README.zh-CN.md +82 -73
- package/dist/index.cjs +95 -30
- package/dist/index.d.ts +73 -47
- package/dist/index.mjs +96 -31
- package/package.json +8 -11
package/dist/index.cjs
CHANGED
|
@@ -4,9 +4,9 @@ const node_path = require('node:path');
|
|
|
4
4
|
const node_fs = require('node:fs');
|
|
5
5
|
const promises = require('node:fs/promises');
|
|
6
6
|
const node_os = require('node:os');
|
|
7
|
+
const languageCore = require('@vue/language-core');
|
|
7
8
|
const ts = require('typescript');
|
|
8
9
|
const pluginutils = require('@rollup/pluginutils');
|
|
9
|
-
const languageCore = require('@vue/language-core');
|
|
10
10
|
const vueTsc = require('vue-tsc');
|
|
11
11
|
const debug = require('debug');
|
|
12
12
|
const kolorist = require('kolorist');
|
|
@@ -114,13 +114,33 @@ function rollupDeclarationFiles({
|
|
|
114
114
|
);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
const
|
|
117
|
+
const svelteRE = /\.svelte$/;
|
|
118
|
+
function SvelteResolver() {
|
|
119
|
+
return {
|
|
120
|
+
name: "svelte",
|
|
121
|
+
supports(id) {
|
|
122
|
+
return svelteRE.test(id);
|
|
123
|
+
},
|
|
124
|
+
transform({ id }) {
|
|
125
|
+
return [
|
|
126
|
+
{
|
|
127
|
+
path: `${id}.d.ts`,
|
|
128
|
+
content: "export { SvelteComponentTyped as default } from 'svelte';"
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const windowsSlashRE = /\\+/g;
|
|
118
136
|
function slash(p) {
|
|
119
137
|
return p.replace(windowsSlashRE, "/");
|
|
120
138
|
}
|
|
121
|
-
const isWindows = node_os.platform() === "win32";
|
|
122
139
|
function normalizePath(id) {
|
|
123
|
-
return node_path.posix.normalize(
|
|
140
|
+
return node_path.posix.normalize(slash(id));
|
|
141
|
+
}
|
|
142
|
+
function resolve(...paths) {
|
|
143
|
+
return normalizePath(node_path.resolve(...paths));
|
|
124
144
|
}
|
|
125
145
|
function isNativeObj(value) {
|
|
126
146
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
@@ -132,7 +152,7 @@ function isPromise(value) {
|
|
|
132
152
|
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
133
153
|
}
|
|
134
154
|
function ensureAbsolute(path, root) {
|
|
135
|
-
return normalizePath(path ? node_path.isAbsolute(path) ? path :
|
|
155
|
+
return normalizePath(path ? node_path.isAbsolute(path) ? path : resolve(root, path) : root);
|
|
136
156
|
}
|
|
137
157
|
function ensureArray(value) {
|
|
138
158
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -197,7 +217,7 @@ function removeDirIfEmpty(dir) {
|
|
|
197
217
|
}
|
|
198
218
|
let onlyHasDir = true;
|
|
199
219
|
for (const file of node_fs.readdirSync(dir)) {
|
|
200
|
-
const abs =
|
|
220
|
+
const abs = resolve(dir, file);
|
|
201
221
|
if (node_fs.lstatSync(abs).isDirectory()) {
|
|
202
222
|
if (!removeDirIfEmpty(abs)) {
|
|
203
223
|
onlyHasDir = false;
|
|
@@ -212,6 +232,38 @@ function removeDirIfEmpty(dir) {
|
|
|
212
232
|
return onlyHasDir;
|
|
213
233
|
}
|
|
214
234
|
|
|
235
|
+
const vueRE = /\.vue$/;
|
|
236
|
+
function VueResolver() {
|
|
237
|
+
return {
|
|
238
|
+
name: "vue",
|
|
239
|
+
supports(id) {
|
|
240
|
+
return vueRE.test(id);
|
|
241
|
+
},
|
|
242
|
+
transform({ id, root, program, service }) {
|
|
243
|
+
let sourceFile = program.getSourceFile(id);
|
|
244
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
245
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
246
|
+
}
|
|
247
|
+
if (!sourceFile)
|
|
248
|
+
return [];
|
|
249
|
+
return service.getEmitOutput(sourceFile.fileName, true).outputFiles.map((file) => {
|
|
250
|
+
return {
|
|
251
|
+
path: resolve(root, file.name),
|
|
252
|
+
content: file.text
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function parseResolvers(resolvers) {
|
|
260
|
+
const nameMap = /* @__PURE__ */ new Map();
|
|
261
|
+
for (const resolver of resolvers) {
|
|
262
|
+
resolver.name && nameMap.set(resolver.name, resolver);
|
|
263
|
+
}
|
|
264
|
+
return Array.from(nameMap.values());
|
|
265
|
+
}
|
|
266
|
+
|
|
215
267
|
const globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
216
268
|
function normalizeGlob(path) {
|
|
217
269
|
if (/[\\/]$/.test(path)) {
|
|
@@ -310,7 +362,6 @@ function removePureImport(content) {
|
|
|
310
362
|
return content.replace(pureImportRE, "");
|
|
311
363
|
}
|
|
312
364
|
|
|
313
|
-
const vueRE = /\.vue$/;
|
|
314
365
|
const jsRE = /\.(m|c)?jsx?$/;
|
|
315
366
|
const tsRE = /\.(m|c)?tsx?$/;
|
|
316
367
|
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
@@ -318,7 +369,6 @@ const tjsRE = /\.(m|c)?(t|j)sx?$/;
|
|
|
318
369
|
const mtjsRE = /\.m(t|j)sx?$/;
|
|
319
370
|
const ctjsRE = /\.c(t|j)sx?$/;
|
|
320
371
|
const fullRelativeRE = /^\.\.?\//;
|
|
321
|
-
const watchExtensionRE = /\.(vue|(m|c)?(t|j)sx?)$/;
|
|
322
372
|
const defaultIndex = "index.d.ts";
|
|
323
373
|
const logPrefix = kolorist.cyan("[vite:dts]");
|
|
324
374
|
const bundleDebug = debug__default("vite-plugin-dts:bundle");
|
|
@@ -336,7 +386,6 @@ const fixedCompilerOptions = {
|
|
|
336
386
|
const noop = () => {
|
|
337
387
|
};
|
|
338
388
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
339
|
-
const resolve = (...paths) => normalizePath(node_path.resolve(...paths));
|
|
340
389
|
function dtsPlugin(options = {}) {
|
|
341
390
|
const {
|
|
342
391
|
tsconfigPath,
|
|
@@ -373,6 +422,7 @@ function dtsPlugin(options = {}) {
|
|
|
373
422
|
let filter;
|
|
374
423
|
let bundled = false;
|
|
375
424
|
let timeRecord = 0;
|
|
425
|
+
const resolvers = parseResolvers([VueResolver(), SvelteResolver(), ...options.resolvers || []]);
|
|
376
426
|
const rootFiles = /* @__PURE__ */ new Set();
|
|
377
427
|
const outputFiles = /* @__PURE__ */ new Map();
|
|
378
428
|
return {
|
|
@@ -447,6 +497,7 @@ ${kolorist.cyan(
|
|
|
447
497
|
entries = { ...input };
|
|
448
498
|
}
|
|
449
499
|
logger = logger || console;
|
|
500
|
+
aliases = aliases || [];
|
|
450
501
|
libName = "_default";
|
|
451
502
|
indexName = defaultIndex;
|
|
452
503
|
bundleDebug("parse options");
|
|
@@ -503,37 +554,51 @@ ${kolorist.cyan(
|
|
|
503
554
|
bundleDebug("create ts program");
|
|
504
555
|
timeRecord += Date.now() - startTime;
|
|
505
556
|
},
|
|
506
|
-
transform(
|
|
507
|
-
|
|
557
|
+
async transform(code, id) {
|
|
558
|
+
let resolver;
|
|
559
|
+
id = normalizePath(id).split("?")[0];
|
|
560
|
+
if (!host || !program || !filter(id) || !(resolver = resolvers.find((r) => r.supports(id))) && !tjsRE.test(id)) {
|
|
508
561
|
return;
|
|
509
562
|
}
|
|
510
563
|
const startTime = Date.now();
|
|
511
|
-
id = normalizePath(id);
|
|
512
|
-
rootFiles.delete(id);
|
|
513
|
-
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
514
|
-
if (!sourceFile && vueRE.test(id)) {
|
|
515
|
-
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
516
|
-
}
|
|
517
|
-
if (!sourceFile)
|
|
518
|
-
return;
|
|
519
564
|
const service = program.__vue.languageService;
|
|
520
|
-
|
|
521
|
-
|
|
565
|
+
rootFiles.delete(id);
|
|
566
|
+
if (resolver) {
|
|
567
|
+
const result = await resolver.transform({
|
|
568
|
+
id,
|
|
569
|
+
code,
|
|
570
|
+
root: publicRoot,
|
|
571
|
+
host,
|
|
572
|
+
program,
|
|
573
|
+
service
|
|
574
|
+
});
|
|
575
|
+
for (const { path, content } of result) {
|
|
576
|
+
outputFiles.set(ensureAbsolute(path, publicRoot), content);
|
|
577
|
+
}
|
|
578
|
+
} else {
|
|
579
|
+
const sourceFile = program.getSourceFile(id);
|
|
580
|
+
if (sourceFile) {
|
|
581
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
582
|
+
outputFiles.set(resolve(publicRoot, outputFile.name), outputFile.text);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
522
585
|
}
|
|
523
|
-
const dtsId = id.replace(tjsRE, ".d.ts"
|
|
586
|
+
const dtsId = id.replace(tjsRE, "") + ".d.ts";
|
|
524
587
|
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
525
588
|
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
|
|
526
589
|
timeRecord += Date.now() - startTime;
|
|
527
590
|
},
|
|
528
591
|
watchChange(id) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
592
|
+
id = normalizePath(id).split("?")[0];
|
|
593
|
+
if (!host || !program || !filter(id) || !resolvers.find((r) => r.supports(id)) && !tjsRE.test(id)) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts__default.ScriptTarget.ESNext);
|
|
597
|
+
if (sourceFile) {
|
|
598
|
+
rootFiles.add(sourceFile.fileName);
|
|
599
|
+
program.__vue.projectVersion++;
|
|
600
|
+
bundled = false;
|
|
601
|
+
timeRecord = 0;
|
|
537
602
|
}
|
|
538
603
|
},
|
|
539
604
|
async writeBundle() {
|
package/dist/index.d.ts
CHANGED
|
@@ -2,43 +2,66 @@ import * as vite from 'vite';
|
|
|
2
2
|
import { LogLevel } from 'vite';
|
|
3
3
|
import ts from 'typescript';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
6
|
+
interface Resolver {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the resolver
|
|
9
|
+
*
|
|
10
|
+
* The later resolver with the same name will overwrite the earlier
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Determine whether the resolver supports the file
|
|
15
|
+
*/
|
|
16
|
+
supports: (id: string) => void | boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Transform source to declaration files
|
|
19
|
+
*/
|
|
20
|
+
transform: (payload: {
|
|
21
|
+
id: string;
|
|
22
|
+
code: string;
|
|
23
|
+
root: string;
|
|
24
|
+
host: ts.CompilerHost;
|
|
25
|
+
program: ts.Program;
|
|
26
|
+
service: ts.LanguageService;
|
|
27
|
+
}) => MaybePromise<{
|
|
28
|
+
path: string;
|
|
29
|
+
content: string;
|
|
30
|
+
}[]>;
|
|
8
31
|
}
|
|
9
32
|
interface PluginOptions {
|
|
10
33
|
/**
|
|
11
34
|
* Specify root directory
|
|
12
35
|
*
|
|
13
|
-
*
|
|
36
|
+
* Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup
|
|
14
37
|
*/
|
|
15
38
|
root?: string;
|
|
16
39
|
/**
|
|
17
|
-
*
|
|
40
|
+
* Output directory for declaration files
|
|
18
41
|
*
|
|
19
|
-
* Can be
|
|
42
|
+
* Can be an array to output to multiple directories
|
|
20
43
|
*
|
|
21
|
-
*
|
|
44
|
+
* Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup
|
|
22
45
|
*/
|
|
23
46
|
outDir?: string | string[];
|
|
24
47
|
/**
|
|
25
|
-
*
|
|
48
|
+
* Override root path of entry files (useful in monorepos)
|
|
26
49
|
*
|
|
27
|
-
* The output path of each file will be calculated
|
|
50
|
+
* The output path of each file will be calculated based on the value provided
|
|
28
51
|
*
|
|
29
|
-
*
|
|
52
|
+
* The default is the smallest public path for all source files
|
|
30
53
|
*/
|
|
31
54
|
entryRoot?: string;
|
|
32
55
|
/**
|
|
33
|
-
*
|
|
56
|
+
* Restrict declaration files output to `outDir`
|
|
34
57
|
*
|
|
35
|
-
*
|
|
58
|
+
* If true, generated declaration files outside `outDir` will be ignored
|
|
36
59
|
*
|
|
37
60
|
* @default true
|
|
38
61
|
*/
|
|
39
62
|
strictOutput?: boolean;
|
|
40
63
|
/**
|
|
41
|
-
*
|
|
64
|
+
* Override compilerOptions
|
|
42
65
|
*
|
|
43
66
|
* @default null
|
|
44
67
|
*/
|
|
@@ -46,67 +69,69 @@ interface PluginOptions {
|
|
|
46
69
|
/**
|
|
47
70
|
* Specify tsconfig.json path
|
|
48
71
|
*
|
|
49
|
-
* Plugin
|
|
72
|
+
* Plugin resolves `include` and `exclude` globs from tsconfig.json
|
|
50
73
|
*
|
|
51
|
-
*
|
|
74
|
+
* If not specified, plugin will find config file from root
|
|
52
75
|
*/
|
|
53
76
|
tsconfigPath?: string;
|
|
54
77
|
/**
|
|
55
|
-
*
|
|
78
|
+
* Specify custom resolvers
|
|
56
79
|
*
|
|
57
|
-
*
|
|
80
|
+
* @default []
|
|
81
|
+
*/
|
|
82
|
+
resolvers?: Resolver[];
|
|
83
|
+
/**
|
|
84
|
+
* Set which paths should be excluded when transforming aliases
|
|
58
85
|
*
|
|
59
86
|
* @default []
|
|
60
87
|
*/
|
|
61
88
|
aliasesExclude?: (string | RegExp)[];
|
|
62
89
|
/**
|
|
63
|
-
* Whether transform file
|
|
90
|
+
* Whether to transform file names ending in '.vue.d.ts' to '.d.ts'
|
|
64
91
|
*
|
|
65
92
|
* @default false
|
|
66
93
|
*/
|
|
67
94
|
cleanVueFileName?: boolean;
|
|
68
95
|
/**
|
|
69
|
-
* Whether transform dynamic import to
|
|
70
|
-
*
|
|
71
|
-
* Force `true` when `rollupTypes` is effective
|
|
96
|
+
* Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`)
|
|
72
97
|
*
|
|
73
|
-
*
|
|
98
|
+
* Value is forced to `true` when `rollupTypes` is `true`
|
|
74
99
|
*
|
|
75
100
|
* @default false
|
|
76
101
|
*/
|
|
77
102
|
staticImport?: boolean;
|
|
78
103
|
/**
|
|
79
|
-
*
|
|
104
|
+
* Override `include` glob
|
|
80
105
|
*
|
|
81
|
-
*
|
|
106
|
+
* Defaults to `include` property of tsconfig.json
|
|
82
107
|
*/
|
|
83
108
|
include?: string | string[];
|
|
84
109
|
/**
|
|
85
|
-
*
|
|
110
|
+
* Override `exclude` glob
|
|
86
111
|
*
|
|
87
|
-
*
|
|
112
|
+
* Defaults to `exclude` property of tsconfig.json or `'node_module/**'` if not supplied.
|
|
88
113
|
*/
|
|
89
114
|
exclude?: string | string[];
|
|
90
115
|
/**
|
|
91
|
-
* Whether remove
|
|
116
|
+
* Whether to remove `import 'xxx'`
|
|
92
117
|
*
|
|
93
118
|
* @default true
|
|
94
119
|
*/
|
|
95
120
|
clearPureImport?: boolean;
|
|
96
121
|
/**
|
|
97
|
-
* Whether generate types entry file
|
|
122
|
+
* Whether to generate types entry file(s)
|
|
98
123
|
*
|
|
99
|
-
* When `true
|
|
124
|
+
* When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`
|
|
100
125
|
*
|
|
101
|
-
*
|
|
126
|
+
* Value is forced to `true` when `rollupTypes` is `true`
|
|
102
127
|
*
|
|
103
128
|
* @default false
|
|
104
129
|
*/
|
|
105
130
|
insertTypesEntry?: boolean;
|
|
106
131
|
/**
|
|
107
|
-
*
|
|
132
|
+
* Rollup type declaration files after emitting them
|
|
108
133
|
*
|
|
109
|
-
*
|
|
134
|
+
* Powered by `@microsoft/api-extractor` - time-intensive operation
|
|
110
135
|
*
|
|
111
136
|
* @default false
|
|
112
137
|
*/
|
|
@@ -119,44 +144,45 @@ interface PluginOptions {
|
|
|
119
144
|
*/
|
|
120
145
|
bundledPackages?: string[];
|
|
121
146
|
/**
|
|
122
|
-
* Whether copy .d.ts source files
|
|
147
|
+
* Whether to copy .d.ts source files to `outDir`
|
|
123
148
|
*
|
|
124
149
|
* @default false
|
|
125
|
-
* @remarks Before 2.0
|
|
150
|
+
* @remarks Before 2.0, the default was `true`
|
|
126
151
|
*/
|
|
127
152
|
copyDtsFiles?: boolean;
|
|
128
153
|
/**
|
|
129
|
-
*
|
|
154
|
+
* Logging level for this plugin
|
|
130
155
|
*
|
|
131
|
-
*
|
|
156
|
+
* Defaults to the 'logLevel' property of your Vite config
|
|
132
157
|
*/
|
|
133
158
|
logLevel?: LogLevel;
|
|
134
159
|
/**
|
|
135
|
-
* Hook after diagnostic emitted
|
|
160
|
+
* Hook called after diagnostic is emitted
|
|
136
161
|
*
|
|
137
|
-
* According to the length
|
|
162
|
+
* According to the `diagnostics.length`, you can judge whether there is any type error
|
|
138
163
|
*
|
|
139
164
|
* @default () => {}
|
|
140
165
|
*/
|
|
141
|
-
afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) =>
|
|
166
|
+
afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>;
|
|
142
167
|
/**
|
|
143
|
-
* Hook
|
|
168
|
+
* Hook called prior to writing each declaration file
|
|
144
169
|
*
|
|
145
|
-
*
|
|
170
|
+
* This allows you to transform the path or content
|
|
146
171
|
*
|
|
147
|
-
* The file will be skipped when return
|
|
172
|
+
* The file will be skipped when the return value `false`
|
|
148
173
|
*
|
|
149
174
|
* @default () => {}
|
|
150
175
|
*/
|
|
151
|
-
beforeWriteFile?: (filePath: string, content: string) => void | false |
|
|
176
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | {
|
|
177
|
+
filePath?: string;
|
|
178
|
+
content?: string;
|
|
179
|
+
};
|
|
152
180
|
/**
|
|
153
|
-
* Hook after
|
|
154
|
-
*
|
|
155
|
-
* It wil be called after all declaration files are written
|
|
181
|
+
* Hook called after all declaration files are written
|
|
156
182
|
*
|
|
157
183
|
* @default () => {}
|
|
158
184
|
*/
|
|
159
|
-
afterBuild?: () =>
|
|
185
|
+
afterBuild?: () => MaybePromise<void>;
|
|
160
186
|
}
|
|
161
187
|
|
|
162
188
|
declare function dtsPlugin(options?: PluginOptions): vite.Plugin;
|
package/dist/index.mjs
CHANGED
|
@@ -8,10 +8,10 @@ const require = __cjs_mod__.createRequire(import.meta.url);
|
|
|
8
8
|
import { resolve as resolve$1, posix, isAbsolute, dirname, normalize, sep, relative, basename } from 'node:path';
|
|
9
9
|
import { existsSync, readdirSync, lstatSync, rmdirSync } from 'node:fs';
|
|
10
10
|
import { readFile, mkdir, writeFile, unlink } from 'node:fs/promises';
|
|
11
|
-
import {
|
|
11
|
+
import { cpus } from 'node:os';
|
|
12
|
+
import { createParsedCommandLine } from '@vue/language-core';
|
|
12
13
|
import ts from 'typescript';
|
|
13
14
|
import { createFilter } from '@rollup/pluginutils';
|
|
14
|
-
import { createParsedCommandLine } from '@vue/language-core';
|
|
15
15
|
import { createProgram } from 'vue-tsc';
|
|
16
16
|
import debug from 'debug';
|
|
17
17
|
import { cyan, yellow, green } from 'kolorist';
|
|
@@ -114,13 +114,33 @@ function rollupDeclarationFiles({
|
|
|
114
114
|
);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
const
|
|
117
|
+
const svelteRE = /\.svelte$/;
|
|
118
|
+
function SvelteResolver() {
|
|
119
|
+
return {
|
|
120
|
+
name: "svelte",
|
|
121
|
+
supports(id) {
|
|
122
|
+
return svelteRE.test(id);
|
|
123
|
+
},
|
|
124
|
+
transform({ id }) {
|
|
125
|
+
return [
|
|
126
|
+
{
|
|
127
|
+
path: `${id}.d.ts`,
|
|
128
|
+
content: "export { SvelteComponentTyped as default } from 'svelte';"
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const windowsSlashRE = /\\+/g;
|
|
118
136
|
function slash(p) {
|
|
119
137
|
return p.replace(windowsSlashRE, "/");
|
|
120
138
|
}
|
|
121
|
-
const isWindows = platform() === "win32";
|
|
122
139
|
function normalizePath(id) {
|
|
123
|
-
return posix.normalize(
|
|
140
|
+
return posix.normalize(slash(id));
|
|
141
|
+
}
|
|
142
|
+
function resolve(...paths) {
|
|
143
|
+
return normalizePath(resolve$1(...paths));
|
|
124
144
|
}
|
|
125
145
|
function isNativeObj(value) {
|
|
126
146
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
@@ -132,7 +152,7 @@ function isPromise(value) {
|
|
|
132
152
|
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
133
153
|
}
|
|
134
154
|
function ensureAbsolute(path, root) {
|
|
135
|
-
return normalizePath(path ? isAbsolute(path) ? path : resolve
|
|
155
|
+
return normalizePath(path ? isAbsolute(path) ? path : resolve(root, path) : root);
|
|
136
156
|
}
|
|
137
157
|
function ensureArray(value) {
|
|
138
158
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -197,7 +217,7 @@ function removeDirIfEmpty(dir) {
|
|
|
197
217
|
}
|
|
198
218
|
let onlyHasDir = true;
|
|
199
219
|
for (const file of readdirSync(dir)) {
|
|
200
|
-
const abs = resolve
|
|
220
|
+
const abs = resolve(dir, file);
|
|
201
221
|
if (lstatSync(abs).isDirectory()) {
|
|
202
222
|
if (!removeDirIfEmpty(abs)) {
|
|
203
223
|
onlyHasDir = false;
|
|
@@ -212,6 +232,38 @@ function removeDirIfEmpty(dir) {
|
|
|
212
232
|
return onlyHasDir;
|
|
213
233
|
}
|
|
214
234
|
|
|
235
|
+
const vueRE = /\.vue$/;
|
|
236
|
+
function VueResolver() {
|
|
237
|
+
return {
|
|
238
|
+
name: "vue",
|
|
239
|
+
supports(id) {
|
|
240
|
+
return vueRE.test(id);
|
|
241
|
+
},
|
|
242
|
+
transform({ id, root, program, service }) {
|
|
243
|
+
let sourceFile = program.getSourceFile(id);
|
|
244
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
245
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
246
|
+
}
|
|
247
|
+
if (!sourceFile)
|
|
248
|
+
return [];
|
|
249
|
+
return service.getEmitOutput(sourceFile.fileName, true).outputFiles.map((file) => {
|
|
250
|
+
return {
|
|
251
|
+
path: resolve(root, file.name),
|
|
252
|
+
content: file.text
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function parseResolvers(resolvers) {
|
|
260
|
+
const nameMap = /* @__PURE__ */ new Map();
|
|
261
|
+
for (const resolver of resolvers) {
|
|
262
|
+
resolver.name && nameMap.set(resolver.name, resolver);
|
|
263
|
+
}
|
|
264
|
+
return Array.from(nameMap.values());
|
|
265
|
+
}
|
|
266
|
+
|
|
215
267
|
const globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
216
268
|
function normalizeGlob(path) {
|
|
217
269
|
if (/[\\/]$/.test(path)) {
|
|
@@ -310,7 +362,6 @@ function removePureImport(content) {
|
|
|
310
362
|
return content.replace(pureImportRE, "");
|
|
311
363
|
}
|
|
312
364
|
|
|
313
|
-
const vueRE = /\.vue$/;
|
|
314
365
|
const jsRE = /\.(m|c)?jsx?$/;
|
|
315
366
|
const tsRE = /\.(m|c)?tsx?$/;
|
|
316
367
|
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
@@ -318,7 +369,6 @@ const tjsRE = /\.(m|c)?(t|j)sx?$/;
|
|
|
318
369
|
const mtjsRE = /\.m(t|j)sx?$/;
|
|
319
370
|
const ctjsRE = /\.c(t|j)sx?$/;
|
|
320
371
|
const fullRelativeRE = /^\.\.?\//;
|
|
321
|
-
const watchExtensionRE = /\.(vue|(m|c)?(t|j)sx?)$/;
|
|
322
372
|
const defaultIndex = "index.d.ts";
|
|
323
373
|
const logPrefix = cyan("[vite:dts]");
|
|
324
374
|
const bundleDebug = debug("vite-plugin-dts:bundle");
|
|
@@ -336,7 +386,6 @@ const fixedCompilerOptions = {
|
|
|
336
386
|
const noop = () => {
|
|
337
387
|
};
|
|
338
388
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
339
|
-
const resolve = (...paths) => normalizePath(resolve$1(...paths));
|
|
340
389
|
function dtsPlugin(options = {}) {
|
|
341
390
|
const {
|
|
342
391
|
tsconfigPath,
|
|
@@ -373,6 +422,7 @@ function dtsPlugin(options = {}) {
|
|
|
373
422
|
let filter;
|
|
374
423
|
let bundled = false;
|
|
375
424
|
let timeRecord = 0;
|
|
425
|
+
const resolvers = parseResolvers([VueResolver(), SvelteResolver(), ...options.resolvers || []]);
|
|
376
426
|
const rootFiles = /* @__PURE__ */ new Set();
|
|
377
427
|
const outputFiles = /* @__PURE__ */ new Map();
|
|
378
428
|
return {
|
|
@@ -447,6 +497,7 @@ ${cyan(
|
|
|
447
497
|
entries = { ...input };
|
|
448
498
|
}
|
|
449
499
|
logger = logger || console;
|
|
500
|
+
aliases = aliases || [];
|
|
450
501
|
libName = "_default";
|
|
451
502
|
indexName = defaultIndex;
|
|
452
503
|
bundleDebug("parse options");
|
|
@@ -503,37 +554,51 @@ ${cyan(
|
|
|
503
554
|
bundleDebug("create ts program");
|
|
504
555
|
timeRecord += Date.now() - startTime;
|
|
505
556
|
},
|
|
506
|
-
transform(
|
|
507
|
-
|
|
557
|
+
async transform(code, id) {
|
|
558
|
+
let resolver;
|
|
559
|
+
id = normalizePath(id).split("?")[0];
|
|
560
|
+
if (!host || !program || !filter(id) || !(resolver = resolvers.find((r) => r.supports(id))) && !tjsRE.test(id)) {
|
|
508
561
|
return;
|
|
509
562
|
}
|
|
510
563
|
const startTime = Date.now();
|
|
511
|
-
id = normalizePath(id);
|
|
512
|
-
rootFiles.delete(id);
|
|
513
|
-
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
514
|
-
if (!sourceFile && vueRE.test(id)) {
|
|
515
|
-
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
516
|
-
}
|
|
517
|
-
if (!sourceFile)
|
|
518
|
-
return;
|
|
519
564
|
const service = program.__vue.languageService;
|
|
520
|
-
|
|
521
|
-
|
|
565
|
+
rootFiles.delete(id);
|
|
566
|
+
if (resolver) {
|
|
567
|
+
const result = await resolver.transform({
|
|
568
|
+
id,
|
|
569
|
+
code,
|
|
570
|
+
root: publicRoot,
|
|
571
|
+
host,
|
|
572
|
+
program,
|
|
573
|
+
service
|
|
574
|
+
});
|
|
575
|
+
for (const { path, content } of result) {
|
|
576
|
+
outputFiles.set(ensureAbsolute(path, publicRoot), content);
|
|
577
|
+
}
|
|
578
|
+
} else {
|
|
579
|
+
const sourceFile = program.getSourceFile(id);
|
|
580
|
+
if (sourceFile) {
|
|
581
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
582
|
+
outputFiles.set(resolve(publicRoot, outputFile.name), outputFile.text);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
522
585
|
}
|
|
523
|
-
const dtsId = id.replace(tjsRE, ".d.ts"
|
|
586
|
+
const dtsId = id.replace(tjsRE, "") + ".d.ts";
|
|
524
587
|
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
525
588
|
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
|
|
526
589
|
timeRecord += Date.now() - startTime;
|
|
527
590
|
},
|
|
528
591
|
watchChange(id) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
592
|
+
id = normalizePath(id).split("?")[0];
|
|
593
|
+
if (!host || !program || !filter(id) || !resolvers.find((r) => r.supports(id)) && !tjsRE.test(id)) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts.ScriptTarget.ESNext);
|
|
597
|
+
if (sourceFile) {
|
|
598
|
+
rootFiles.add(sourceFile.fileName);
|
|
599
|
+
program.__vue.projectVersion++;
|
|
600
|
+
bundled = false;
|
|
601
|
+
timeRecord = 0;
|
|
537
602
|
}
|
|
538
603
|
},
|
|
539
604
|
async writeBundle() {
|