vite-plugin-dts 3.0.0-beta.1 → 3.0.0-beta.2
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 +105 -77
- package/dist/index.mjs +102 -74
- 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
|
}
|
|
@@ -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,14 +461,10 @@ ${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;
|
|
476
470
|
const diagnostics = program.getDeclarationDiagnostics();
|
|
@@ -481,70 +475,104 @@ ${kolorist.cyan(
|
|
|
481
475
|
const result = afterDiagnostic(diagnostics);
|
|
482
476
|
isPromise(result) && await result;
|
|
483
477
|
}
|
|
478
|
+
rootNames.forEach((file) => {
|
|
479
|
+
this.addWatchFile(file);
|
|
480
|
+
rootFiles.add(file);
|
|
481
|
+
});
|
|
484
482
|
bundleDebug("create ts program");
|
|
485
483
|
},
|
|
484
|
+
transform(_, id) {
|
|
485
|
+
if (!program || !filter(id) || id.includes(".vue?vue") || !tsRE.test(id) && !vueRE.test(id)) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
id = normalizePath(id);
|
|
489
|
+
rootFiles.delete(id);
|
|
490
|
+
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
491
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
492
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
493
|
+
}
|
|
494
|
+
if (!sourceFile)
|
|
495
|
+
return;
|
|
496
|
+
const outDir = outDirs[0];
|
|
497
|
+
const service = program.__vue.languageService;
|
|
498
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
499
|
+
outputFiles.set(resolve(root, node_path.relative(outDir, outputFile.name)), outputFile.text);
|
|
500
|
+
}
|
|
501
|
+
const dtsId = id.replace(tjsRE, ".d.ts");
|
|
502
|
+
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
503
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(dtsSourceFile.fileName, dtsSourceFile.getFullText());
|
|
504
|
+
},
|
|
505
|
+
watchChange(id) {
|
|
506
|
+
if (host && program && watchExtensionRE.test(id)) {
|
|
507
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts__default.ScriptTarget.ESNext);
|
|
508
|
+
if (sourceFile && filter(sourceFile.fileName)) {
|
|
509
|
+
!vueRE.test(id) && rootFiles.add(sourceFile.fileName);
|
|
510
|
+
program.__vue.projectVersion++;
|
|
511
|
+
bundled = false;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
},
|
|
486
515
|
async writeBundle() {
|
|
487
|
-
if (!
|
|
516
|
+
if (!program || bundled)
|
|
488
517
|
return;
|
|
518
|
+
bundled = true;
|
|
519
|
+
bundleDebug("begin writeBundle");
|
|
489
520
|
logger.info(kolorist.green(`
|
|
490
521
|
${logPrefix} Start generate declaration files...`));
|
|
491
|
-
|
|
522
|
+
const startTime = Date.now();
|
|
492
523
|
const outDir = outDirs[0];
|
|
493
524
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
494
|
-
const filter = pluginutils.createFilter(include, exclude, { resolve: root });
|
|
495
525
|
const service = program.__vue.languageService;
|
|
496
526
|
const sourceFiles = program.getSourceFiles();
|
|
497
|
-
const
|
|
527
|
+
for (const sourceFile of sourceFiles) {
|
|
498
528
|
if (!filter(sourceFile.fileName))
|
|
499
|
-
|
|
500
|
-
const output = [];
|
|
529
|
+
continue;
|
|
501
530
|
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
502
|
-
|
|
503
|
-
path: sourceFile.fileName,
|
|
504
|
-
content: sourceFile.getFullText()
|
|
505
|
-
});
|
|
531
|
+
outputFiles.set(sourceFile.fileName, sourceFile.getFullText());
|
|
506
532
|
}
|
|
507
|
-
|
|
508
|
-
service.getEmitOutput(sourceFile.fileName, true).outputFiles
|
|
509
|
-
|
|
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;
|
|
525
|
-
}
|
|
526
|
-
path = resolve(
|
|
527
|
-
outDir,
|
|
528
|
-
node_path.relative(entryRoot, cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path)
|
|
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;
|
|
533
|
+
if (rootFiles.has(sourceFile.fileName)) {
|
|
534
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
535
|
+
outputFiles.set(resolve(root, node_path.relative(outDir, outputFile.name)), outputFile.text);
|
|
538
536
|
}
|
|
537
|
+
rootFiles.delete(sourceFile.fileName);
|
|
539
538
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
539
|
+
}
|
|
540
|
+
bundleDebug("emit output patch");
|
|
541
|
+
entryRoot = entryRoot || queryPublicPath(Array.from(outputFiles.keys()));
|
|
542
|
+
entryRoot = ensureAbsolute(entryRoot, root);
|
|
543
|
+
await runParallel(
|
|
544
|
+
node_os.cpus().length,
|
|
545
|
+
Array.from(outputFiles.entries()),
|
|
546
|
+
async ([path, content]) => {
|
|
547
|
+
const isMapFile = path.endsWith(".map");
|
|
548
|
+
if (!isMapFile && content) {
|
|
549
|
+
content = clearPureImport ? removePureImport(content) : content;
|
|
550
|
+
content = transformAliasImport(path, content, aliases, aliasesExclude);
|
|
551
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
552
|
+
}
|
|
553
|
+
path = resolve(
|
|
554
|
+
outDir,
|
|
555
|
+
node_path.relative(entryRoot, cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path)
|
|
556
|
+
);
|
|
557
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
558
|
+
if (typeof beforeWriteFile === "function") {
|
|
559
|
+
const result = beforeWriteFile(path, content);
|
|
560
|
+
if (result === false)
|
|
561
|
+
return;
|
|
562
|
+
if (result && isNativeObj(result)) {
|
|
563
|
+
path = result.filePath || path;
|
|
564
|
+
content = result.content ?? content;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
path = normalizePath(path);
|
|
568
|
+
const dir = node_path.dirname(path);
|
|
569
|
+
if (!node_fs.existsSync(dir)) {
|
|
570
|
+
await promises.mkdir(dir, { recursive: true });
|
|
571
|
+
}
|
|
572
|
+
await promises.writeFile(path, content, "utf-8");
|
|
573
|
+
emittedFiles.set(path, content);
|
|
544
574
|
}
|
|
545
|
-
|
|
546
|
-
emittedFiles.set(path, content);
|
|
547
|
-
});
|
|
575
|
+
);
|
|
548
576
|
bundleDebug("write output");
|
|
549
577
|
if (insertTypesEntry || rollupTypes) {
|
|
550
578
|
const pkgPath = resolve(root, "package.json");
|
|
@@ -629,7 +657,7 @@ export default ${libName}
|
|
|
629
657
|
emittedFiles.delete(typesPath);
|
|
630
658
|
rollupFiles.add(typesPath);
|
|
631
659
|
}
|
|
632
|
-
await runParallel(
|
|
660
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles.keys()), (f) => promises.unlink(f));
|
|
633
661
|
removeDirIfEmpty(outDir);
|
|
634
662
|
emittedFiles.clear();
|
|
635
663
|
for (const file of rollupFiles) {
|
|
@@ -640,7 +668,7 @@ export default ${libName}
|
|
|
640
668
|
}
|
|
641
669
|
if (outDirs.length > 1) {
|
|
642
670
|
const dirs = outDirs.slice(1);
|
|
643
|
-
await runParallel(
|
|
671
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles), async ([wroteFile, content]) => {
|
|
644
672
|
const relativePath = node_path.relative(outDir, wroteFile);
|
|
645
673
|
await Promise.all(
|
|
646
674
|
dirs.map(async (dir) => {
|
|
@@ -659,7 +687,7 @@ export default ${libName}
|
|
|
659
687
|
isPromise(result) && await result;
|
|
660
688
|
}
|
|
661
689
|
bundleDebug("finish");
|
|
662
|
-
logger.info(kolorist.green(`${logPrefix} Declaration files built.
|
|
690
|
+
logger.info(kolorist.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
663
691
|
`));
|
|
664
692
|
}
|
|
665
693
|
};
|
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';
|
|
@@ -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,14 +461,10 @@ ${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;
|
|
476
470
|
const diagnostics = program.getDeclarationDiagnostics();
|
|
@@ -481,70 +475,104 @@ ${cyan(
|
|
|
481
475
|
const result = afterDiagnostic(diagnostics);
|
|
482
476
|
isPromise(result) && await result;
|
|
483
477
|
}
|
|
478
|
+
rootNames.forEach((file) => {
|
|
479
|
+
this.addWatchFile(file);
|
|
480
|
+
rootFiles.add(file);
|
|
481
|
+
});
|
|
484
482
|
bundleDebug("create ts program");
|
|
485
483
|
},
|
|
484
|
+
transform(_, id) {
|
|
485
|
+
if (!program || !filter(id) || id.includes(".vue?vue") || !tsRE.test(id) && !vueRE.test(id)) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
id = normalizePath(id);
|
|
489
|
+
rootFiles.delete(id);
|
|
490
|
+
let sourceFile = program.getSourceFile(normalizePath(id));
|
|
491
|
+
if (!sourceFile && vueRE.test(id)) {
|
|
492
|
+
sourceFile = program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
493
|
+
}
|
|
494
|
+
if (!sourceFile)
|
|
495
|
+
return;
|
|
496
|
+
const outDir = outDirs[0];
|
|
497
|
+
const service = program.__vue.languageService;
|
|
498
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
499
|
+
outputFiles.set(resolve(root, relative(outDir, outputFile.name)), outputFile.text);
|
|
500
|
+
}
|
|
501
|
+
const dtsId = id.replace(tjsRE, ".d.ts");
|
|
502
|
+
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
503
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && outputFiles.set(dtsSourceFile.fileName, dtsSourceFile.getFullText());
|
|
504
|
+
},
|
|
505
|
+
watchChange(id) {
|
|
506
|
+
if (host && program && watchExtensionRE.test(id)) {
|
|
507
|
+
const sourceFile = host.getSourceFile(normalizePath(id), ts.ScriptTarget.ESNext);
|
|
508
|
+
if (sourceFile && filter(sourceFile.fileName)) {
|
|
509
|
+
!vueRE.test(id) && rootFiles.add(sourceFile.fileName);
|
|
510
|
+
program.__vue.projectVersion++;
|
|
511
|
+
bundled = false;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
},
|
|
486
515
|
async writeBundle() {
|
|
487
|
-
if (!
|
|
516
|
+
if (!program || bundled)
|
|
488
517
|
return;
|
|
518
|
+
bundled = true;
|
|
519
|
+
bundleDebug("begin writeBundle");
|
|
489
520
|
logger.info(green(`
|
|
490
521
|
${logPrefix} Start generate declaration files...`));
|
|
491
|
-
|
|
522
|
+
const startTime = Date.now();
|
|
492
523
|
const outDir = outDirs[0];
|
|
493
524
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
494
|
-
const filter = createFilter(include, exclude, { resolve: root });
|
|
495
525
|
const service = program.__vue.languageService;
|
|
496
526
|
const sourceFiles = program.getSourceFiles();
|
|
497
|
-
const
|
|
527
|
+
for (const sourceFile of sourceFiles) {
|
|
498
528
|
if (!filter(sourceFile.fileName))
|
|
499
|
-
|
|
500
|
-
const output = [];
|
|
529
|
+
continue;
|
|
501
530
|
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
502
|
-
|
|
503
|
-
path: sourceFile.fileName,
|
|
504
|
-
content: sourceFile.getFullText()
|
|
505
|
-
});
|
|
531
|
+
outputFiles.set(sourceFile.fileName, sourceFile.getFullText());
|
|
506
532
|
}
|
|
507
|
-
|
|
508
|
-
service.getEmitOutput(sourceFile.fileName, true).outputFiles
|
|
509
|
-
|
|
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;
|
|
525
|
-
}
|
|
526
|
-
path = resolve(
|
|
527
|
-
outDir,
|
|
528
|
-
relative(entryRoot, cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path)
|
|
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;
|
|
533
|
+
if (rootFiles.has(sourceFile.fileName)) {
|
|
534
|
+
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
|
|
535
|
+
outputFiles.set(resolve(root, relative(outDir, outputFile.name)), outputFile.text);
|
|
538
536
|
}
|
|
537
|
+
rootFiles.delete(sourceFile.fileName);
|
|
539
538
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
539
|
+
}
|
|
540
|
+
bundleDebug("emit output patch");
|
|
541
|
+
entryRoot = entryRoot || queryPublicPath(Array.from(outputFiles.keys()));
|
|
542
|
+
entryRoot = ensureAbsolute(entryRoot, root);
|
|
543
|
+
await runParallel(
|
|
544
|
+
cpus().length,
|
|
545
|
+
Array.from(outputFiles.entries()),
|
|
546
|
+
async ([path, content]) => {
|
|
547
|
+
const isMapFile = path.endsWith(".map");
|
|
548
|
+
if (!isMapFile && content) {
|
|
549
|
+
content = clearPureImport ? removePureImport(content) : content;
|
|
550
|
+
content = transformAliasImport(path, content, aliases, aliasesExclude);
|
|
551
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
552
|
+
}
|
|
553
|
+
path = resolve(
|
|
554
|
+
outDir,
|
|
555
|
+
relative(entryRoot, cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path)
|
|
556
|
+
);
|
|
557
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
558
|
+
if (typeof beforeWriteFile === "function") {
|
|
559
|
+
const result = beforeWriteFile(path, content);
|
|
560
|
+
if (result === false)
|
|
561
|
+
return;
|
|
562
|
+
if (result && isNativeObj(result)) {
|
|
563
|
+
path = result.filePath || path;
|
|
564
|
+
content = result.content ?? content;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
path = normalizePath(path);
|
|
568
|
+
const dir = dirname(path);
|
|
569
|
+
if (!existsSync(dir)) {
|
|
570
|
+
await mkdir(dir, { recursive: true });
|
|
571
|
+
}
|
|
572
|
+
await writeFile(path, content, "utf-8");
|
|
573
|
+
emittedFiles.set(path, content);
|
|
544
574
|
}
|
|
545
|
-
|
|
546
|
-
emittedFiles.set(path, content);
|
|
547
|
-
});
|
|
575
|
+
);
|
|
548
576
|
bundleDebug("write output");
|
|
549
577
|
if (insertTypesEntry || rollupTypes) {
|
|
550
578
|
const pkgPath = resolve(root, "package.json");
|
|
@@ -659,7 +687,7 @@ export default ${libName}
|
|
|
659
687
|
isPromise(result) && await result;
|
|
660
688
|
}
|
|
661
689
|
bundleDebug("finish");
|
|
662
|
-
logger.info(green(`${logPrefix} Declaration files built.
|
|
690
|
+
logger.info(green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
663
691
|
`));
|
|
664
692
|
}
|
|
665
693
|
};
|
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.2",
|
|
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
|
}
|