vite-plugin-dts 3.0.0-beta.1 → 3.0.0-beta.3
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 +302 -302
- package/README.zh-CN.md +301 -301
- package/dist/index.cjs +114 -78
- package/dist/index.mjs +111 -75
- package/package.json +5 -1
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
const node_path = require('node:path');
|
|
4
4
|
const node_fs = require('node:fs');
|
|
5
5
|
const promises = require('node:fs/promises');
|
|
6
|
-
const
|
|
6
|
+
const node_os = require('node:os');
|
|
7
7
|
const ts = require('typescript');
|
|
8
|
-
const vite = require('vite');
|
|
9
8
|
const pluginutils = require('@rollup/pluginutils');
|
|
10
9
|
const languageCore = require('@vue/language-core');
|
|
11
10
|
const vueTsc = require('vue-tsc');
|
|
@@ -118,7 +117,7 @@ const windowsSlashRE = /\\/g;
|
|
|
118
117
|
function slash(p) {
|
|
119
118
|
return p.replace(windowsSlashRE, "/");
|
|
120
119
|
}
|
|
121
|
-
const isWindows =
|
|
120
|
+
const isWindows = node_os.platform() === "win32";
|
|
122
121
|
function normalizePath(id) {
|
|
123
122
|
return node_path.posix.normalize(isWindows ? slash(id) : id);
|
|
124
123
|
}
|
|
@@ -129,7 +128,7 @@ function isRegExp(value) {
|
|
|
129
128
|
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
130
129
|
}
|
|
131
130
|
function isPromise(value) {
|
|
132
|
-
return !!value && typeof value
|
|
131
|
+
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
133
132
|
}
|
|
134
133
|
function ensureAbsolute(path, root) {
|
|
135
134
|
return path ? node_path.isAbsolute(path) ? path : node_path.resolve(root, path) : root;
|
|
@@ -283,7 +282,7 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
283
282
|
if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
|
|
284
283
|
return str;
|
|
285
284
|
}
|
|
286
|
-
const truthPath = node_path.isAbsolute(matchedAlias.replacement) ?
|
|
285
|
+
const truthPath = node_path.isAbsolute(matchedAlias.replacement) ? normalizePath(node_path.relative(node_path.dirname(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
287
286
|
return str.replace(
|
|
288
287
|
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
289
288
|
`$1'${matchResult[1].replace(
|
|
@@ -301,6 +300,7 @@ function removePureImport(content) {
|
|
|
301
300
|
return content.replace(pureImportRE, "");
|
|
302
301
|
}
|
|
303
302
|
|
|
303
|
+
const vueRE = /\.vue$/;
|
|
304
304
|
const tsRE = /\.(m|c)?tsx?$/;
|
|
305
305
|
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
306
306
|
const tjsRE = /\.(m|c)?(t|j)sx?$/;
|
|
@@ -342,20 +342,24 @@ function dtsPlugin(options = {}) {
|
|
|
342
342
|
beforeWriteFile = noop,
|
|
343
343
|
afterBuild = noop
|
|
344
344
|
} = options;
|
|
345
|
-
let compilerOptions;
|
|
346
|
-
let rawCompilerOptions;
|
|
347
345
|
let root = ensureAbsolute(options.root ?? "", process.cwd());
|
|
348
346
|
let entryRoot = options.entryRoot ?? "";
|
|
347
|
+
let compilerOptions;
|
|
348
|
+
let rawCompilerOptions;
|
|
349
|
+
let outDirs;
|
|
349
350
|
let entries;
|
|
350
351
|
let include;
|
|
351
352
|
let exclude;
|
|
352
|
-
let outDirs;
|
|
353
353
|
let aliases;
|
|
354
354
|
let libName;
|
|
355
355
|
let indexName;
|
|
356
356
|
let logger;
|
|
357
|
-
let
|
|
357
|
+
let host;
|
|
358
358
|
let program;
|
|
359
|
+
let filter;
|
|
360
|
+
let bundled = false;
|
|
361
|
+
const rootFiles = /* @__PURE__ */ new Set();
|
|
362
|
+
const outputFiles = /* @__PURE__ */ new Map();
|
|
359
363
|
return {
|
|
360
364
|
name: "vite:dts",
|
|
361
365
|
apply: "build",
|
|
@@ -377,8 +381,8 @@ function dtsPlugin(options = {}) {
|
|
|
377
381
|
);
|
|
378
382
|
}
|
|
379
383
|
},
|
|
380
|
-
configResolved(config) {
|
|
381
|
-
logger = logLevel ? vite.createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
384
|
+
async configResolved(config) {
|
|
385
|
+
logger = logLevel ? (await import('vite')).createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
382
386
|
root = ensureAbsolute(options.root ?? "", config.root);
|
|
383
387
|
if (config.build.lib) {
|
|
384
388
|
const input = typeof config.build.lib.entry === "string" ? [config.build.lib.entry] : config.build.lib.entry;
|
|
@@ -432,21 +436,15 @@ ${kolorist.cyan(
|
|
|
432
436
|
indexName = defaultIndex;
|
|
433
437
|
bundleDebug("parse options");
|
|
434
438
|
},
|
|
435
|
-
watchChange(id) {
|
|
436
|
-
if (watchExtensionRE.test(id)) {
|
|
437
|
-
bundled = false;
|
|
438
|
-
program?.getSourceFile(normalizePath(id));
|
|
439
|
-
}
|
|
440
|
-
},
|
|
441
439
|
async buildStart() {
|
|
442
440
|
if (program)
|
|
443
441
|
return;
|
|
442
|
+
bundleDebug("begin buildStart");
|
|
444
443
|
const configPath = tsconfigPath ? ensureAbsolute(tsconfigPath, root) : ts__default.findConfigFile(root, ts__default.sys.fileExists);
|
|
445
|
-
const content = configPath
|
|
444
|
+
const content = configPath ? languageCore.createParsedCommandLine(ts__default, ts__default.sys, configPath) : void 0;
|
|
446
445
|
const config = content ? {
|
|
447
446
|
include: content.raw.include,
|
|
448
447
|
exclude: content.raw.exclude,
|
|
449
|
-
fileNames: content.fileNames,
|
|
450
448
|
raw: content.raw,
|
|
451
449
|
options: {
|
|
452
450
|
...content.options,
|
|
@@ -463,16 +461,14 @@ ${kolorist.cyan(
|
|
|
463
461
|
);
|
|
464
462
|
compilerOptions = { ...config.options, outDir: outDirs[0] };
|
|
465
463
|
rawCompilerOptions = config.raw?.compilerOptions || {};
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
config.fileNames?.filter((name) => dtsRE.test(name)) || []
|
|
471
|
-
),
|
|
472
|
-
options: compilerOptions
|
|
473
|
-
});
|
|
464
|
+
filter = pluginutils.createFilter(include, exclude, { resolve: root });
|
|
465
|
+
const rootNames = Object.values(entries).concat(content?.fileNames.filter(filter) || []).map(normalizePath);
|
|
466
|
+
host = ts__default.createCompilerHost(compilerOptions, true);
|
|
467
|
+
program = vueTsc.createProgram({ host, rootNames, options: compilerOptions });
|
|
474
468
|
libName = libName || "_default";
|
|
475
469
|
indexName = indexName || defaultIndex;
|
|
470
|
+
entryRoot = entryRoot || queryPublicPath(rootNames);
|
|
471
|
+
entryRoot = ensureAbsolute(entryRoot, root);
|
|
476
472
|
const diagnostics = program.getDeclarationDiagnostics();
|
|
477
473
|
if (diagnostics?.length) {
|
|
478
474
|
logger.error(ts__default.formatDiagnostics(diagnostics, host));
|
|
@@ -481,70 +477,110 @@ ${kolorist.cyan(
|
|
|
481
477
|
const result = afterDiagnostic(diagnostics);
|
|
482
478
|
isPromise(result) && await result;
|
|
483
479
|
}
|
|
480
|
+
rootNames.forEach((file) => {
|
|
481
|
+
this.addWatchFile(file);
|
|
482
|
+
rootFiles.add(file);
|
|
483
|
+
});
|
|
484
484
|
bundleDebug("create ts program");
|
|
485
485
|
},
|
|
486
|
+
transform(_, id) {
|
|
487
|
+
if (!program || !filter(id) || id.includes(".vue?vue") || !tsRE.test(id) && !vueRE.test(id)) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
id = normalizePath(id);
|
|
491
|
+
rootFiles.delete(id);
|
|
492
|
+
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
493
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
494
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
495
|
+
}
|
|
496
|
+
if (!sourceFile)
|
|
497
|
+
return;
|
|
498
|
+
const outDir = outDirs[0];
|
|
499
|
+
const service = program.__vue.languageService;
|
|
500
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
501
|
+
outputFiles.set(normalizePath(outputFile.name), outputFile.text);
|
|
502
|
+
}
|
|
503
|
+
const dtsId = id.replace(tjsRE, ".d.ts");
|
|
504
|
+
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
505
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(
|
|
506
|
+
resolve(outDir, node_path.relative(entryRoot, dtsSourceFile.fileName)),
|
|
507
|
+
dtsSourceFile.getFullText()
|
|
508
|
+
);
|
|
509
|
+
},
|
|
510
|
+
watchChange(id) {
|
|
511
|
+
if (host && program && watchExtensionRE.test(id)) {
|
|
512
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts__default.ScriptTarget.ESNext);
|
|
513
|
+
if (sourceFile && filter(sourceFile.fileName)) {
|
|
514
|
+
!vueRE.test(id) && rootFiles.add(sourceFile.fileName);
|
|
515
|
+
program.__vue.projectVersion++;
|
|
516
|
+
bundled = false;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
},
|
|
486
520
|
async writeBundle() {
|
|
487
|
-
if (!
|
|
521
|
+
if (!program || bundled)
|
|
488
522
|
return;
|
|
523
|
+
bundled = true;
|
|
524
|
+
bundleDebug("begin writeBundle");
|
|
489
525
|
logger.info(kolorist.green(`
|
|
490
526
|
${logPrefix} Start generate declaration files...`));
|
|
491
|
-
|
|
527
|
+
const startTime = Date.now();
|
|
492
528
|
const outDir = outDirs[0];
|
|
493
529
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
494
|
-
const filter = pluginutils.createFilter(include, exclude, { resolve: root });
|
|
495
530
|
const service = program.__vue.languageService;
|
|
496
531
|
const sourceFiles = program.getSourceFiles();
|
|
497
|
-
const
|
|
532
|
+
for (const sourceFile of sourceFiles) {
|
|
498
533
|
if (!filter(sourceFile.fileName))
|
|
499
|
-
|
|
500
|
-
const output = [];
|
|
534
|
+
continue;
|
|
501
535
|
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
return output.concat(
|
|
508
|
-
service.getEmitOutput(sourceFile.fileName, true).outputFiles.map((outputFile) => {
|
|
509
|
-
return {
|
|
510
|
-
path: resolve(root, node_path.relative(outDir, outputFile.name)),
|
|
511
|
-
content: outputFile.text
|
|
512
|
-
};
|
|
513
|
-
})
|
|
514
|
-
);
|
|
515
|
-
}).flat();
|
|
516
|
-
bundleDebug("emit output");
|
|
517
|
-
entryRoot = entryRoot || queryPublicPath(outputFiles.map((file) => file.path));
|
|
518
|
-
entryRoot = ensureAbsolute(entryRoot, root);
|
|
519
|
-
await runParallel(os.cpus().length, outputFiles, async ({ path, content }) => {
|
|
520
|
-
const isMapFile = path.endsWith(".map");
|
|
521
|
-
if (!isMapFile && content) {
|
|
522
|
-
content = clearPureImport ? removePureImport(content) : content;
|
|
523
|
-
content = transformAliasImport(path, content, aliases, aliasesExclude);
|
|
524
|
-
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
536
|
+
outputFiles.set(
|
|
537
|
+
resolve(outDir, node_path.relative(entryRoot, sourceFile.fileName)),
|
|
538
|
+
sourceFile.getFullText()
|
|
539
|
+
);
|
|
525
540
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
);
|
|
530
|
-
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
531
|
-
if (typeof beforeWriteFile === "function") {
|
|
532
|
-
const result = beforeWriteFile(path, content);
|
|
533
|
-
if (result === false)
|
|
534
|
-
return;
|
|
535
|
-
if (result && isNativeObj(result)) {
|
|
536
|
-
path = result.filePath || path;
|
|
537
|
-
content = result.content ?? content;
|
|
541
|
+
if (rootFiles.has(sourceFile.fileName)) {
|
|
542
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
543
|
+
outputFiles.set(normalizePath(outputFile.name), outputFile.text);
|
|
538
544
|
}
|
|
545
|
+
rootFiles.delete(sourceFile.fileName);
|
|
539
546
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
547
|
+
}
|
|
548
|
+
bundleDebug("emit output patch");
|
|
549
|
+
await runParallel(
|
|
550
|
+
node_os.cpus().length,
|
|
551
|
+
Array.from(outputFiles.entries()),
|
|
552
|
+
async ([path, content]) => {
|
|
553
|
+
const isMapFile = path.endsWith(".map");
|
|
554
|
+
if (!isMapFile && content) {
|
|
555
|
+
content = clearPureImport ? removePureImport(content) : content;
|
|
556
|
+
content = transformAliasImport(
|
|
557
|
+
resolve(entryRoot, node_path.relative(outDir, path)),
|
|
558
|
+
content,
|
|
559
|
+
aliases,
|
|
560
|
+
aliasesExclude
|
|
561
|
+
);
|
|
562
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
563
|
+
}
|
|
564
|
+
path = cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
565
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
566
|
+
if (typeof beforeWriteFile === "function") {
|
|
567
|
+
const result = beforeWriteFile(path, content);
|
|
568
|
+
if (result === false)
|
|
569
|
+
return;
|
|
570
|
+
if (result && isNativeObj(result)) {
|
|
571
|
+
path = result.filePath || path;
|
|
572
|
+
content = result.content ?? content;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
path = normalizePath(path);
|
|
576
|
+
const dir = node_path.dirname(path);
|
|
577
|
+
if (!node_fs.existsSync(dir)) {
|
|
578
|
+
await promises.mkdir(dir, { recursive: true });
|
|
579
|
+
}
|
|
580
|
+
await promises.writeFile(path, content, "utf-8");
|
|
581
|
+
emittedFiles.set(path, content);
|
|
544
582
|
}
|
|
545
|
-
|
|
546
|
-
emittedFiles.set(path, content);
|
|
547
|
-
});
|
|
583
|
+
);
|
|
548
584
|
bundleDebug("write output");
|
|
549
585
|
if (insertTypesEntry || rollupTypes) {
|
|
550
586
|
const pkgPath = resolve(root, "package.json");
|
|
@@ -629,7 +665,7 @@ export default ${libName}
|
|
|
629
665
|
emittedFiles.delete(typesPath);
|
|
630
666
|
rollupFiles.add(typesPath);
|
|
631
667
|
}
|
|
632
|
-
await runParallel(
|
|
668
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles.keys()), (f) => promises.unlink(f));
|
|
633
669
|
removeDirIfEmpty(outDir);
|
|
634
670
|
emittedFiles.clear();
|
|
635
671
|
for (const file of rollupFiles) {
|
|
@@ -640,7 +676,7 @@ export default ${libName}
|
|
|
640
676
|
}
|
|
641
677
|
if (outDirs.length > 1) {
|
|
642
678
|
const dirs = outDirs.slice(1);
|
|
643
|
-
await runParallel(
|
|
679
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles), async ([wroteFile, content]) => {
|
|
644
680
|
const relativePath = node_path.relative(outDir, wroteFile);
|
|
645
681
|
await Promise.all(
|
|
646
682
|
dirs.map(async (dir) => {
|
|
@@ -659,7 +695,7 @@ export default ${libName}
|
|
|
659
695
|
isPromise(result) && await result;
|
|
660
696
|
}
|
|
661
697
|
bundleDebug("finish");
|
|
662
|
-
logger.info(kolorist.green(`${logPrefix} Declaration files built.
|
|
698
|
+
logger.info(kolorist.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
663
699
|
`));
|
|
664
700
|
}
|
|
665
701
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -8,9 +8,8 @@ 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, writeFile, mkdir, unlink } from 'node:fs/promises';
|
|
11
|
-
import { platform, cpus } from 'os';
|
|
11
|
+
import { platform, cpus } from 'node:os';
|
|
12
12
|
import ts from 'typescript';
|
|
13
|
-
import { normalizePath as normalizePath$1, createLogger } from 'vite';
|
|
14
13
|
import { createFilter } from '@rollup/pluginutils';
|
|
15
14
|
import { createParsedCommandLine } from '@vue/language-core';
|
|
16
15
|
import { createProgram } from 'vue-tsc';
|
|
@@ -129,7 +128,7 @@ function isRegExp(value) {
|
|
|
129
128
|
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
130
129
|
}
|
|
131
130
|
function isPromise(value) {
|
|
132
|
-
return !!value && typeof value
|
|
131
|
+
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
133
132
|
}
|
|
134
133
|
function ensureAbsolute(path, root) {
|
|
135
134
|
return path ? isAbsolute(path) ? path : resolve$1(root, path) : root;
|
|
@@ -283,7 +282,7 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
283
282
|
if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
|
|
284
283
|
return str;
|
|
285
284
|
}
|
|
286
|
-
const truthPath = isAbsolute(matchedAlias.replacement) ? normalizePath
|
|
285
|
+
const truthPath = isAbsolute(matchedAlias.replacement) ? normalizePath(relative(dirname(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
287
286
|
return str.replace(
|
|
288
287
|
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
289
288
|
`$1'${matchResult[1].replace(
|
|
@@ -301,6 +300,7 @@ function removePureImport(content) {
|
|
|
301
300
|
return content.replace(pureImportRE, "");
|
|
302
301
|
}
|
|
303
302
|
|
|
303
|
+
const vueRE = /\.vue$/;
|
|
304
304
|
const tsRE = /\.(m|c)?tsx?$/;
|
|
305
305
|
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
306
306
|
const tjsRE = /\.(m|c)?(t|j)sx?$/;
|
|
@@ -342,20 +342,24 @@ function dtsPlugin(options = {}) {
|
|
|
342
342
|
beforeWriteFile = noop,
|
|
343
343
|
afterBuild = noop
|
|
344
344
|
} = options;
|
|
345
|
-
let compilerOptions;
|
|
346
|
-
let rawCompilerOptions;
|
|
347
345
|
let root = ensureAbsolute(options.root ?? "", process.cwd());
|
|
348
346
|
let entryRoot = options.entryRoot ?? "";
|
|
347
|
+
let compilerOptions;
|
|
348
|
+
let rawCompilerOptions;
|
|
349
|
+
let outDirs;
|
|
349
350
|
let entries;
|
|
350
351
|
let include;
|
|
351
352
|
let exclude;
|
|
352
|
-
let outDirs;
|
|
353
353
|
let aliases;
|
|
354
354
|
let libName;
|
|
355
355
|
let indexName;
|
|
356
356
|
let logger;
|
|
357
|
-
let
|
|
357
|
+
let host;
|
|
358
358
|
let program;
|
|
359
|
+
let filter;
|
|
360
|
+
let bundled = false;
|
|
361
|
+
const rootFiles = /* @__PURE__ */ new Set();
|
|
362
|
+
const outputFiles = /* @__PURE__ */ new Map();
|
|
359
363
|
return {
|
|
360
364
|
name: "vite:dts",
|
|
361
365
|
apply: "build",
|
|
@@ -377,8 +381,8 @@ function dtsPlugin(options = {}) {
|
|
|
377
381
|
);
|
|
378
382
|
}
|
|
379
383
|
},
|
|
380
|
-
configResolved(config) {
|
|
381
|
-
logger = logLevel ? createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
384
|
+
async configResolved(config) {
|
|
385
|
+
logger = logLevel ? (await import('vite')).createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
382
386
|
root = ensureAbsolute(options.root ?? "", config.root);
|
|
383
387
|
if (config.build.lib) {
|
|
384
388
|
const input = typeof config.build.lib.entry === "string" ? [config.build.lib.entry] : config.build.lib.entry;
|
|
@@ -432,21 +436,15 @@ ${cyan(
|
|
|
432
436
|
indexName = defaultIndex;
|
|
433
437
|
bundleDebug("parse options");
|
|
434
438
|
},
|
|
435
|
-
watchChange(id) {
|
|
436
|
-
if (watchExtensionRE.test(id)) {
|
|
437
|
-
bundled = false;
|
|
438
|
-
program?.getSourceFile(normalizePath(id));
|
|
439
|
-
}
|
|
440
|
-
},
|
|
441
439
|
async buildStart() {
|
|
442
440
|
if (program)
|
|
443
441
|
return;
|
|
442
|
+
bundleDebug("begin buildStart");
|
|
444
443
|
const configPath = tsconfigPath ? ensureAbsolute(tsconfigPath, root) : ts.findConfigFile(root, ts.sys.fileExists);
|
|
445
|
-
const content = configPath
|
|
444
|
+
const content = configPath ? createParsedCommandLine(ts, ts.sys, configPath) : void 0;
|
|
446
445
|
const config = content ? {
|
|
447
446
|
include: content.raw.include,
|
|
448
447
|
exclude: content.raw.exclude,
|
|
449
|
-
fileNames: content.fileNames,
|
|
450
448
|
raw: content.raw,
|
|
451
449
|
options: {
|
|
452
450
|
...content.options,
|
|
@@ -463,16 +461,14 @@ ${cyan(
|
|
|
463
461
|
);
|
|
464
462
|
compilerOptions = { ...config.options, outDir: outDirs[0] };
|
|
465
463
|
rawCompilerOptions = config.raw?.compilerOptions || {};
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
config.fileNames?.filter((name) => dtsRE.test(name)) || []
|
|
471
|
-
),
|
|
472
|
-
options: compilerOptions
|
|
473
|
-
});
|
|
464
|
+
filter = createFilter(include, exclude, { resolve: root });
|
|
465
|
+
const rootNames = Object.values(entries).concat(content?.fileNames.filter(filter) || []).map(normalizePath);
|
|
466
|
+
host = ts.createCompilerHost(compilerOptions, true);
|
|
467
|
+
program = createProgram({ host, rootNames, options: compilerOptions });
|
|
474
468
|
libName = libName || "_default";
|
|
475
469
|
indexName = indexName || defaultIndex;
|
|
470
|
+
entryRoot = entryRoot || queryPublicPath(rootNames);
|
|
471
|
+
entryRoot = ensureAbsolute(entryRoot, root);
|
|
476
472
|
const diagnostics = program.getDeclarationDiagnostics();
|
|
477
473
|
if (diagnostics?.length) {
|
|
478
474
|
logger.error(ts.formatDiagnostics(diagnostics, host));
|
|
@@ -481,70 +477,110 @@ ${cyan(
|
|
|
481
477
|
const result = afterDiagnostic(diagnostics);
|
|
482
478
|
isPromise(result) && await result;
|
|
483
479
|
}
|
|
480
|
+
rootNames.forEach((file) => {
|
|
481
|
+
this.addWatchFile(file);
|
|
482
|
+
rootFiles.add(file);
|
|
483
|
+
});
|
|
484
484
|
bundleDebug("create ts program");
|
|
485
485
|
},
|
|
486
|
+
transform(_, id) {
|
|
487
|
+
if (!program || !filter(id) || id.includes(".vue?vue") || !tsRE.test(id) && !vueRE.test(id)) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
id = normalizePath(id);
|
|
491
|
+
rootFiles.delete(id);
|
|
492
|
+
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
493
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
494
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
495
|
+
}
|
|
496
|
+
if (!sourceFile)
|
|
497
|
+
return;
|
|
498
|
+
const outDir = outDirs[0];
|
|
499
|
+
const service = program.__vue.languageService;
|
|
500
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
501
|
+
outputFiles.set(normalizePath(outputFile.name), outputFile.text);
|
|
502
|
+
}
|
|
503
|
+
const dtsId = id.replace(tjsRE, ".d.ts");
|
|
504
|
+
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
505
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(
|
|
506
|
+
resolve(outDir, relative(entryRoot, dtsSourceFile.fileName)),
|
|
507
|
+
dtsSourceFile.getFullText()
|
|
508
|
+
);
|
|
509
|
+
},
|
|
510
|
+
watchChange(id) {
|
|
511
|
+
if (host && program && watchExtensionRE.test(id)) {
|
|
512
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts.ScriptTarget.ESNext);
|
|
513
|
+
if (sourceFile && filter(sourceFile.fileName)) {
|
|
514
|
+
!vueRE.test(id) && rootFiles.add(sourceFile.fileName);
|
|
515
|
+
program.__vue.projectVersion++;
|
|
516
|
+
bundled = false;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
},
|
|
486
520
|
async writeBundle() {
|
|
487
|
-
if (!
|
|
521
|
+
if (!program || bundled)
|
|
488
522
|
return;
|
|
523
|
+
bundled = true;
|
|
524
|
+
bundleDebug("begin writeBundle");
|
|
489
525
|
logger.info(green(`
|
|
490
526
|
${logPrefix} Start generate declaration files...`));
|
|
491
|
-
|
|
527
|
+
const startTime = Date.now();
|
|
492
528
|
const outDir = outDirs[0];
|
|
493
529
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
494
|
-
const filter = createFilter(include, exclude, { resolve: root });
|
|
495
530
|
const service = program.__vue.languageService;
|
|
496
531
|
const sourceFiles = program.getSourceFiles();
|
|
497
|
-
const
|
|
532
|
+
for (const sourceFile of sourceFiles) {
|
|
498
533
|
if (!filter(sourceFile.fileName))
|
|
499
|
-
|
|
500
|
-
const output = [];
|
|
534
|
+
continue;
|
|
501
535
|
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
return output.concat(
|
|
508
|
-
service.getEmitOutput(sourceFile.fileName, true).outputFiles.map((outputFile) => {
|
|
509
|
-
return {
|
|
510
|
-
path: resolve(root, relative(outDir, outputFile.name)),
|
|
511
|
-
content: outputFile.text
|
|
512
|
-
};
|
|
513
|
-
})
|
|
514
|
-
);
|
|
515
|
-
}).flat();
|
|
516
|
-
bundleDebug("emit output");
|
|
517
|
-
entryRoot = entryRoot || queryPublicPath(outputFiles.map((file) => file.path));
|
|
518
|
-
entryRoot = ensureAbsolute(entryRoot, root);
|
|
519
|
-
await runParallel(cpus().length, outputFiles, async ({ path, content }) => {
|
|
520
|
-
const isMapFile = path.endsWith(".map");
|
|
521
|
-
if (!isMapFile && content) {
|
|
522
|
-
content = clearPureImport ? removePureImport(content) : content;
|
|
523
|
-
content = transformAliasImport(path, content, aliases, aliasesExclude);
|
|
524
|
-
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
536
|
+
outputFiles.set(
|
|
537
|
+
resolve(outDir, relative(entryRoot, sourceFile.fileName)),
|
|
538
|
+
sourceFile.getFullText()
|
|
539
|
+
);
|
|
525
540
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
);
|
|
530
|
-
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
531
|
-
if (typeof beforeWriteFile === "function") {
|
|
532
|
-
const result = beforeWriteFile(path, content);
|
|
533
|
-
if (result === false)
|
|
534
|
-
return;
|
|
535
|
-
if (result && isNativeObj(result)) {
|
|
536
|
-
path = result.filePath || path;
|
|
537
|
-
content = result.content ?? content;
|
|
541
|
+
if (rootFiles.has(sourceFile.fileName)) {
|
|
542
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
543
|
+
outputFiles.set(normalizePath(outputFile.name), outputFile.text);
|
|
538
544
|
}
|
|
545
|
+
rootFiles.delete(sourceFile.fileName);
|
|
539
546
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
547
|
+
}
|
|
548
|
+
bundleDebug("emit output patch");
|
|
549
|
+
await runParallel(
|
|
550
|
+
cpus().length,
|
|
551
|
+
Array.from(outputFiles.entries()),
|
|
552
|
+
async ([path, content]) => {
|
|
553
|
+
const isMapFile = path.endsWith(".map");
|
|
554
|
+
if (!isMapFile && content) {
|
|
555
|
+
content = clearPureImport ? removePureImport(content) : content;
|
|
556
|
+
content = transformAliasImport(
|
|
557
|
+
resolve(entryRoot, relative(outDir, path)),
|
|
558
|
+
content,
|
|
559
|
+
aliases,
|
|
560
|
+
aliasesExclude
|
|
561
|
+
);
|
|
562
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
563
|
+
}
|
|
564
|
+
path = cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
565
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
566
|
+
if (typeof beforeWriteFile === "function") {
|
|
567
|
+
const result = beforeWriteFile(path, content);
|
|
568
|
+
if (result === false)
|
|
569
|
+
return;
|
|
570
|
+
if (result && isNativeObj(result)) {
|
|
571
|
+
path = result.filePath || path;
|
|
572
|
+
content = result.content ?? content;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
path = normalizePath(path);
|
|
576
|
+
const dir = dirname(path);
|
|
577
|
+
if (!existsSync(dir)) {
|
|
578
|
+
await mkdir(dir, { recursive: true });
|
|
579
|
+
}
|
|
580
|
+
await writeFile(path, content, "utf-8");
|
|
581
|
+
emittedFiles.set(path, content);
|
|
544
582
|
}
|
|
545
|
-
|
|
546
|
-
emittedFiles.set(path, content);
|
|
547
|
-
});
|
|
583
|
+
);
|
|
548
584
|
bundleDebug("write output");
|
|
549
585
|
if (insertTypesEntry || rollupTypes) {
|
|
550
586
|
const pkgPath = resolve(root, "package.json");
|
|
@@ -659,7 +695,7 @@ export default ${libName}
|
|
|
659
695
|
isPromise(result) && await result;
|
|
660
696
|
}
|
|
661
697
|
bundleDebug("finish");
|
|
662
|
-
logger.info(green(`${logPrefix} Declaration files built.
|
|
698
|
+
logger.info(green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
663
699
|
`));
|
|
664
700
|
}
|
|
665
701
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-dts",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "qmhc",
|
|
@@ -100,5 +100,9 @@
|
|
|
100
100
|
},
|
|
101
101
|
"peerDependencies": {
|
|
102
102
|
"typescript": "*"
|
|
103
|
+
},
|
|
104
|
+
"optionalDependencies": {
|
|
105
|
+
"rollup": "*",
|
|
106
|
+
"vite": "*"
|
|
103
107
|
}
|
|
104
108
|
}
|