vite-plugin-dts 1.0.4 → 1.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 +192 -185
- package/README.zh-CN.md +191 -184
- package/dist/index.d.ts +1 -0
- package/dist/index.js +173 -33
- package/dist/index.mjs +175 -32
- package/package.json +3 -1
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
});
|
|
8
8
|
|
|
9
9
|
// src/plugin.ts
|
|
10
|
-
import { resolve as
|
|
10
|
+
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "path";
|
|
11
11
|
import fs from "fs-extra";
|
|
12
12
|
import os from "os";
|
|
13
13
|
import chalk from "chalk";
|
|
@@ -23,6 +23,7 @@ import { normalizePath } from "vite";
|
|
|
23
23
|
|
|
24
24
|
// src/utils.ts
|
|
25
25
|
import { resolve, isAbsolute, dirname, normalize, sep } from "path";
|
|
26
|
+
import { existsSync, readdirSync, lstatSync, rmdirSync } from "fs";
|
|
26
27
|
function isNativeObj(value) {
|
|
27
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
28
29
|
}
|
|
@@ -125,6 +126,26 @@ function queryPublicPath(paths) {
|
|
|
125
126
|
}
|
|
126
127
|
return publicPath.slice(0, -1);
|
|
127
128
|
}
|
|
129
|
+
function removeDirIfEmpty(dir) {
|
|
130
|
+
if (!existsSync(dir)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
let onlyHasDir = true;
|
|
134
|
+
for (const file of readdirSync(dir)) {
|
|
135
|
+
const abs = resolve(dir, file);
|
|
136
|
+
if (lstatSync(abs).isDirectory()) {
|
|
137
|
+
if (!removeDirIfEmpty(abs)) {
|
|
138
|
+
onlyHasDir = false;
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
onlyHasDir = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (onlyHasDir) {
|
|
145
|
+
rmdirSync(dir);
|
|
146
|
+
}
|
|
147
|
+
return onlyHasDir;
|
|
148
|
+
}
|
|
128
149
|
|
|
129
150
|
// src/transform.ts
|
|
130
151
|
var globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
@@ -270,33 +291,120 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
270
291
|
content += "\nexport default _sfc_main\n";
|
|
271
292
|
ext = scriptSetup.lang || "js";
|
|
272
293
|
} else if (script && script.content) {
|
|
273
|
-
content = script.content;
|
|
294
|
+
content = rewriteDefault(script.content, "_sfc_main");
|
|
295
|
+
content += "\nexport default _sfc_main\n";
|
|
274
296
|
ext = script.lang || "js";
|
|
275
297
|
}
|
|
276
298
|
}
|
|
277
299
|
return { content, ext };
|
|
278
300
|
}
|
|
279
301
|
|
|
302
|
+
// src/rollup.ts
|
|
303
|
+
import { resolve as resolve2 } from "path";
|
|
304
|
+
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
305
|
+
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
306
|
+
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|
|
307
|
+
import {
|
|
308
|
+
DtsRollupGenerator,
|
|
309
|
+
DtsRollupKind
|
|
310
|
+
} from "@microsoft/api-extractor/lib/generators/DtsRollupGenerator";
|
|
311
|
+
import { PackageJsonLookup } from "@rushstack/node-core-library";
|
|
312
|
+
var dtsRE = /\.d\.tsx?$/;
|
|
313
|
+
function rollupDeclarationFiles({
|
|
314
|
+
root,
|
|
315
|
+
tsConfigPath,
|
|
316
|
+
outputDir,
|
|
317
|
+
entryPath,
|
|
318
|
+
fileName
|
|
319
|
+
}) {
|
|
320
|
+
const configObjectFullPath = resolve2(root, "api-extractor.json");
|
|
321
|
+
const packageJsonLookup = new PackageJsonLookup();
|
|
322
|
+
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
323
|
+
if (!dtsRE.test(fileName)) {
|
|
324
|
+
fileName += ".d.ts";
|
|
325
|
+
}
|
|
326
|
+
const extractorConfig = ExtractorConfig.prepare({
|
|
327
|
+
configObject: {
|
|
328
|
+
projectFolder: root,
|
|
329
|
+
mainEntryPointFilePath: entryPath,
|
|
330
|
+
compiler: {
|
|
331
|
+
tsconfigFilePath: tsConfigPath
|
|
332
|
+
},
|
|
333
|
+
apiReport: {
|
|
334
|
+
enabled: false,
|
|
335
|
+
reportFileName: "<unscopedPackageName>.api.md"
|
|
336
|
+
},
|
|
337
|
+
docModel: {
|
|
338
|
+
enabled: false
|
|
339
|
+
},
|
|
340
|
+
dtsRollup: {
|
|
341
|
+
enabled: true,
|
|
342
|
+
publicTrimmedFilePath: resolve2(outputDir, fileName)
|
|
343
|
+
},
|
|
344
|
+
tsdocMetadata: {
|
|
345
|
+
enabled: false
|
|
346
|
+
},
|
|
347
|
+
messages: {
|
|
348
|
+
compilerMessageReporting: {
|
|
349
|
+
default: {
|
|
350
|
+
logLevel: "none"
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
extractorMessageReporting: {
|
|
354
|
+
default: {
|
|
355
|
+
logLevel: "none"
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
configObjectFullPath,
|
|
361
|
+
packageJsonFullPath
|
|
362
|
+
});
|
|
363
|
+
const compilerState = CompilerState.create(extractorConfig, {
|
|
364
|
+
localBuild: true,
|
|
365
|
+
showVerboseMessages: false
|
|
366
|
+
});
|
|
367
|
+
const messageRouter = new MessageRouter({
|
|
368
|
+
workingPackageFolder: root,
|
|
369
|
+
messageCallback: void 0,
|
|
370
|
+
messagesConfig: extractorConfig.messages,
|
|
371
|
+
showVerboseMessages: false,
|
|
372
|
+
showDiagnostics: false,
|
|
373
|
+
tsdocConfiguration: extractorConfig.tsdocConfiguration
|
|
374
|
+
});
|
|
375
|
+
const collector = new Collector({
|
|
376
|
+
program: compilerState.program,
|
|
377
|
+
messageRouter,
|
|
378
|
+
extractorConfig
|
|
379
|
+
});
|
|
380
|
+
collector.analyze();
|
|
381
|
+
DtsRollupGenerator.writeTypingsFile(collector, extractorConfig.publicTrimmedFilePath, DtsRollupKind.PublicRelease, extractorConfig.newlineKind);
|
|
382
|
+
}
|
|
383
|
+
|
|
280
384
|
// src/plugin.ts
|
|
281
385
|
var noneExport = "export {};\n";
|
|
282
386
|
var virtualPrefix = "\0";
|
|
283
387
|
var vueRE = /\.vue$/;
|
|
284
388
|
var tsRE = /\.tsx?$/;
|
|
285
389
|
var jsRE = /\.jsx?$/;
|
|
286
|
-
var
|
|
390
|
+
var dtsRE2 = /\.d\.tsx?$/;
|
|
287
391
|
var tjsRE = /\.(t|j)sx?$/;
|
|
288
392
|
var watchExtensionRE = /\.(vue|(t|j)sx?)$/;
|
|
393
|
+
var fullRelativeRE = /^\.\.?\//;
|
|
394
|
+
var defaultIndex = "index.d.ts";
|
|
289
395
|
var noop = () => {
|
|
290
396
|
};
|
|
397
|
+
var logPrefix = chalk.cyan("[vite:dts]");
|
|
291
398
|
var bundleDebug = debug("vite-plugin-dts:bundle");
|
|
292
399
|
function dtsPlugin(options = {}) {
|
|
293
|
-
var _a;
|
|
400
|
+
var _a, _b;
|
|
294
401
|
const {
|
|
295
402
|
tsConfigFilePath = "tsconfig.json",
|
|
296
403
|
cleanVueFileName = false,
|
|
297
404
|
staticImport = false,
|
|
298
405
|
clearPureImport = true,
|
|
299
406
|
insertTypesEntry = false,
|
|
407
|
+
rollupTypes = false,
|
|
300
408
|
noEmitOnError = false,
|
|
301
409
|
skipDiagnostics = true,
|
|
302
410
|
logDiagnostics = false,
|
|
@@ -307,7 +415,9 @@ function dtsPlugin(options = {}) {
|
|
|
307
415
|
} = options;
|
|
308
416
|
const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
|
|
309
417
|
let root;
|
|
310
|
-
let entryRoot;
|
|
418
|
+
let entryRoot = (_b = options.entryRoot) != null ? _b : "";
|
|
419
|
+
let libName;
|
|
420
|
+
let indexName;
|
|
311
421
|
let aliases;
|
|
312
422
|
let entries;
|
|
313
423
|
let logger;
|
|
@@ -336,7 +446,7 @@ function dtsPlugin(options = {}) {
|
|
|
336
446
|
}
|
|
337
447
|
},
|
|
338
448
|
configResolved(config) {
|
|
339
|
-
var _a2,
|
|
449
|
+
var _a2, _b2, _c;
|
|
340
450
|
if (isBundle)
|
|
341
451
|
return;
|
|
342
452
|
logger = config.logger;
|
|
@@ -344,8 +454,17 @@ function dtsPlugin(options = {}) {
|
|
|
344
454
|
logger.warn(chalk.yellow(`
|
|
345
455
|
${chalk.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
|
|
346
456
|
`));
|
|
457
|
+
libName = "_default";
|
|
458
|
+
indexName = defaultIndex;
|
|
459
|
+
} else {
|
|
460
|
+
const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
|
|
461
|
+
libName = config.build.lib.name || "_default";
|
|
462
|
+
indexName = typeof filename === "string" ? filename : filename("es");
|
|
463
|
+
if (!dtsRE2.test(indexName)) {
|
|
464
|
+
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
465
|
+
}
|
|
347
466
|
}
|
|
348
|
-
root = ensureAbsolute((
|
|
467
|
+
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
349
468
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
350
469
|
outputDir = options.outputDir ? ensureAbsolute(options.outputDir, root) : ensureAbsolute(config.build.outDir, root);
|
|
351
470
|
if (!outputDir) {
|
|
@@ -369,10 +488,10 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
369
488
|
tsConfigFilePath: tsConfigPath,
|
|
370
489
|
skipAddingFilesFromTsConfig: true
|
|
371
490
|
});
|
|
372
|
-
allowJs = (
|
|
491
|
+
allowJs = (_c = project.getCompilerOptions().allowJs) != null ? _c : false;
|
|
373
492
|
},
|
|
374
493
|
buildStart(inputOptions) {
|
|
375
|
-
if (insertTypesEntry) {
|
|
494
|
+
if (!isBundle && (insertTypesEntry || rollupTypes)) {
|
|
376
495
|
entries = Array.isArray(inputOptions.input) ? inputOptions.input : Object.values(inputOptions.input);
|
|
377
496
|
}
|
|
378
497
|
},
|
|
@@ -402,17 +521,17 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
402
521
|
}
|
|
403
522
|
},
|
|
404
523
|
async closeBundle() {
|
|
405
|
-
var _a2,
|
|
524
|
+
var _a2, _b2, _c, _d, _e, _f;
|
|
406
525
|
if (!outputDir || !project || isBundle)
|
|
407
526
|
return;
|
|
408
527
|
logger.info(chalk.green(`
|
|
409
|
-
${
|
|
528
|
+
${logPrefix} Start generate declaration files...`));
|
|
410
529
|
bundleDebug("start");
|
|
411
530
|
isBundle = true;
|
|
412
531
|
sourceDtsFiles.clear();
|
|
413
532
|
const startTime = Date.now();
|
|
414
533
|
const tsConfig = (_a2 = readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config) != null ? _a2 : {};
|
|
415
|
-
const include = (_c = (
|
|
534
|
+
const include = (_c = (_b2 = options.include) != null ? _b2 : tsConfig.include) != null ? _c : "**/*";
|
|
416
535
|
const exclude = (_d = options.exclude) != null ? _d : tsConfig.exclude;
|
|
417
536
|
bundleDebug("read config");
|
|
418
537
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
@@ -423,7 +542,7 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
423
542
|
ignore: ensureArray(exclude != null ? exclude : ["node_modules/**"]).map(normalizeGlob)
|
|
424
543
|
});
|
|
425
544
|
files.forEach((file) => {
|
|
426
|
-
if (
|
|
545
|
+
if (dtsRE2.test(file)) {
|
|
427
546
|
if (!copyDtsFiles) {
|
|
428
547
|
return;
|
|
429
548
|
}
|
|
@@ -454,21 +573,23 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
454
573
|
}
|
|
455
574
|
bundleDebug("diagnostics");
|
|
456
575
|
}
|
|
576
|
+
const dtsOutputFiles = Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
577
|
+
path: sourceFile.getFilePath(),
|
|
578
|
+
content: sourceFile.getFullText()
|
|
579
|
+
}));
|
|
457
580
|
const service = project.getLanguageService();
|
|
458
581
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
459
582
|
path: outputFile.getFilePath(),
|
|
460
583
|
content: outputFile.getText()
|
|
461
|
-
}))).flat().concat(
|
|
462
|
-
|
|
463
|
-
content: sourceFile.getFullText()
|
|
464
|
-
})));
|
|
584
|
+
}))).flat().concat(dtsOutputFiles);
|
|
585
|
+
bundleDebug("emit");
|
|
465
586
|
if (!entryRoot) {
|
|
466
587
|
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
467
588
|
}
|
|
468
589
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
469
|
-
|
|
590
|
+
const wroteFiles = /* @__PURE__ */ new Set();
|
|
470
591
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
471
|
-
var _a3,
|
|
592
|
+
var _a3, _b3;
|
|
472
593
|
let filePath = outputFile.path;
|
|
473
594
|
let content = outputFile.content;
|
|
474
595
|
const isMapFile = filePath.endsWith(".map");
|
|
@@ -478,31 +599,34 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
478
599
|
if (!isMapFile && content && content !== noneExport) {
|
|
479
600
|
content = clearPureImport ? removePureImport(content) : content;
|
|
480
601
|
content = transformAliasImport(filePath, content, aliases);
|
|
481
|
-
content = staticImport ? transformDynamicImport(content) : content;
|
|
602
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
482
603
|
}
|
|
483
|
-
filePath =
|
|
604
|
+
filePath = resolve3(outputDir, relative2(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath));
|
|
484
605
|
if (typeof beforeWriteFile === "function") {
|
|
485
606
|
const result = beforeWriteFile(filePath, content);
|
|
486
607
|
if (result && isNativeObj(result)) {
|
|
487
608
|
filePath = (_a3 = result.filePath) != null ? _a3 : filePath;
|
|
488
|
-
content = (
|
|
609
|
+
content = (_b3 = result.content) != null ? _b3 : content;
|
|
489
610
|
}
|
|
490
611
|
}
|
|
491
612
|
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
492
613
|
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf8");
|
|
614
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
493
615
|
});
|
|
494
616
|
bundleDebug("output");
|
|
495
|
-
if (insertTypesEntry) {
|
|
496
|
-
const pkgPath =
|
|
617
|
+
if (insertTypesEntry || rollupTypes) {
|
|
618
|
+
const pkgPath = resolve3(root, "package.json");
|
|
497
619
|
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
|
498
|
-
let typesPath = pkg.types ?
|
|
620
|
+
let typesPath = pkg.types ? resolve3(root, pkg.types) : resolve3(outputDir, indexName);
|
|
499
621
|
if (!fs.existsSync(typesPath)) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
622
|
+
const entry = entries[0];
|
|
623
|
+
let filePath = normalizePath2(relative2(dirname3(typesPath), resolve3(outputDir, relative2(entryRoot, entry))));
|
|
624
|
+
filePath = filePath.replace(tsRE, "");
|
|
625
|
+
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
626
|
+
let content = `import ${libName} from '${filePath}'
|
|
627
|
+
export default ${libName}
|
|
628
|
+
export * from '${filePath}'
|
|
629
|
+
`;
|
|
506
630
|
if (typeof beforeWriteFile === "function") {
|
|
507
631
|
const result = beforeWriteFile(typesPath, content);
|
|
508
632
|
if (result && isNativeObj(result)) {
|
|
@@ -513,13 +637,32 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
513
637
|
await fs.writeFile(typesPath, content, "utf-8");
|
|
514
638
|
}
|
|
515
639
|
bundleDebug("insert index");
|
|
640
|
+
if (rollupTypes) {
|
|
641
|
+
logger.info(chalk.green(`${logPrefix} Start rollup declaration files...`));
|
|
642
|
+
rollupDeclarationFiles({
|
|
643
|
+
root,
|
|
644
|
+
tsConfigPath,
|
|
645
|
+
outputDir,
|
|
646
|
+
entryPath: typesPath,
|
|
647
|
+
fileName: basename(typesPath)
|
|
648
|
+
});
|
|
649
|
+
wroteFiles.delete(normalizePath2(typesPath));
|
|
650
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), (f) => fs.unlink(f));
|
|
651
|
+
removeDirIfEmpty(outputDir);
|
|
652
|
+
if (copyDtsFiles) {
|
|
653
|
+
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
654
|
+
await fs.writeFile(resolve3(outputDir, basename(path)), content, "utf8");
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
bundleDebug("rollup");
|
|
658
|
+
}
|
|
516
659
|
}
|
|
517
660
|
if (typeof afterBuild === "function") {
|
|
518
661
|
const result = afterBuild();
|
|
519
662
|
isPromise(result) && await result;
|
|
520
663
|
}
|
|
521
664
|
bundleDebug("finish");
|
|
522
|
-
logger.info(chalk.green(`${
|
|
665
|
+
logger.info(chalk.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
523
666
|
`));
|
|
524
667
|
}
|
|
525
668
|
};
|
package/package.json
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
"url": "https://github.com/qmhc/vite-plugin-dts/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
+
"@microsoft/api-extractor": "^7.20.0",
|
|
10
|
+
"@rushstack/node-core-library": "^3.45.1",
|
|
9
11
|
"chalk": "^4.1.2",
|
|
10
12
|
"debug": "^4.3.4",
|
|
11
13
|
"fast-glob": "^3.2.11",
|
|
@@ -91,5 +93,5 @@
|
|
|
91
93
|
"test:e2e": "cd example && cross-env DEBUG=\"vite-plugin-dts:bundle\" vite build"
|
|
92
94
|
},
|
|
93
95
|
"types": "dist/index.d.ts",
|
|
94
|
-
"version": "1.
|
|
96
|
+
"version": "1.1.1"
|
|
95
97
|
}
|