vite-plugin-dts 1.6.0 → 1.6.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 +214 -213
- package/README.zh-CN.md +213 -212
- package/dist/index.cjs +831 -0
- package/dist/index.js +103 -126
- package/dist/index.mjs +18 -12
- package/package.json +18 -14
package/dist/index.js
CHANGED
|
@@ -1,54 +1,29 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
24
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
-
|
|
26
|
-
// src/index.ts
|
|
27
|
-
var src_exports = {};
|
|
28
|
-
__export(src_exports, {
|
|
29
|
-
default: () => src_default
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
30
7
|
});
|
|
31
|
-
module.exports = dtsPlugin;
|
|
32
|
-
dtsPlugin['default'] = dtsPlugin;
|
|
33
8
|
|
|
34
9
|
// src/plugin.ts
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
10
|
+
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "node:path";
|
|
11
|
+
import fs from "fs-extra";
|
|
12
|
+
import os from "os";
|
|
13
|
+
import chalk from "chalk";
|
|
14
|
+
import glob from "fast-glob";
|
|
15
|
+
import { debug } from "debug";
|
|
16
|
+
import { Project } from "ts-morph";
|
|
17
|
+
import { normalizePath as normalizePath2 } from "vite";
|
|
18
|
+
import { readConfigFile } from "typescript";
|
|
44
19
|
|
|
45
20
|
// src/transform.ts
|
|
46
|
-
|
|
47
|
-
|
|
21
|
+
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "node:path";
|
|
22
|
+
import { normalizePath } from "vite";
|
|
48
23
|
|
|
49
24
|
// src/utils.ts
|
|
50
|
-
|
|
51
|
-
|
|
25
|
+
import { resolve, isAbsolute, dirname, normalize, sep } from "node:path";
|
|
26
|
+
import { existsSync, readdirSync, lstatSync, rmdirSync } from "node:fs";
|
|
52
27
|
function isNativeObj(value) {
|
|
53
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
54
29
|
}
|
|
@@ -92,7 +67,7 @@ function mergeObjects(sourceObj, targetObj) {
|
|
|
92
67
|
return sourceObj;
|
|
93
68
|
}
|
|
94
69
|
function ensureAbsolute(path, root) {
|
|
95
|
-
return path ?
|
|
70
|
+
return path ? isAbsolute(path) ? path : resolve(root, path) : root;
|
|
96
71
|
}
|
|
97
72
|
function ensureArray(value) {
|
|
98
73
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -118,16 +93,16 @@ function queryPublicPath(paths) {
|
|
|
118
93
|
if (paths.length === 0) {
|
|
119
94
|
return "";
|
|
120
95
|
} else if (paths.length === 1) {
|
|
121
|
-
return
|
|
96
|
+
return dirname(paths[0]);
|
|
122
97
|
}
|
|
123
|
-
let publicPath =
|
|
98
|
+
let publicPath = normalize(dirname(paths[0])) + sep;
|
|
124
99
|
let publicUnits = publicPath.split(speRE);
|
|
125
100
|
let index2 = publicUnits.length - 1;
|
|
126
101
|
for (const path of paths.slice(1)) {
|
|
127
102
|
if (!index2) {
|
|
128
103
|
return publicPath;
|
|
129
104
|
}
|
|
130
|
-
const dirPath =
|
|
105
|
+
const dirPath = normalize(dirname(path)) + sep;
|
|
131
106
|
if (dirPath.startsWith(publicPath)) {
|
|
132
107
|
continue;
|
|
133
108
|
}
|
|
@@ -144,7 +119,7 @@ function queryPublicPath(paths) {
|
|
|
144
119
|
}
|
|
145
120
|
index2 = i - 1;
|
|
146
121
|
publicUnits = publicUnits.slice(0, index2 + 1);
|
|
147
|
-
publicPath = publicUnits.join(
|
|
122
|
+
publicPath = publicUnits.join(sep) + sep;
|
|
148
123
|
break;
|
|
149
124
|
}
|
|
150
125
|
}
|
|
@@ -152,13 +127,13 @@ function queryPublicPath(paths) {
|
|
|
152
127
|
return publicPath.slice(0, -1);
|
|
153
128
|
}
|
|
154
129
|
function removeDirIfEmpty(dir) {
|
|
155
|
-
if (!
|
|
130
|
+
if (!existsSync(dir)) {
|
|
156
131
|
return;
|
|
157
132
|
}
|
|
158
133
|
let onlyHasDir = true;
|
|
159
|
-
for (const file of
|
|
160
|
-
const abs =
|
|
161
|
-
if (
|
|
134
|
+
for (const file of readdirSync(dir)) {
|
|
135
|
+
const abs = resolve(dir, file);
|
|
136
|
+
if (lstatSync(abs).isDirectory()) {
|
|
162
137
|
if (!removeDirIfEmpty(abs)) {
|
|
163
138
|
onlyHasDir = false;
|
|
164
139
|
}
|
|
@@ -167,7 +142,7 @@ function removeDirIfEmpty(dir) {
|
|
|
167
142
|
}
|
|
168
143
|
}
|
|
169
144
|
if (onlyHasDir) {
|
|
170
|
-
|
|
145
|
+
rmdirSync(dir);
|
|
171
146
|
}
|
|
172
147
|
return onlyHasDir;
|
|
173
148
|
}
|
|
@@ -244,7 +219,7 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
244
219
|
if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
|
|
245
220
|
return str;
|
|
246
221
|
}
|
|
247
|
-
const truthPath = (
|
|
222
|
+
const truthPath = isAbsolute2(matchedAlias.replacement) ? normalizePath(relative(dirname2(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
248
223
|
return str.replace(
|
|
249
224
|
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
250
225
|
`$1'${matchResult[1].replace(
|
|
@@ -272,8 +247,6 @@ function transferSetupPosition(content) {
|
|
|
272
247
|
}
|
|
273
248
|
|
|
274
249
|
// src/compile.ts
|
|
275
|
-
var import_node_module = require("module");
|
|
276
|
-
var import_meta = {};
|
|
277
250
|
var exportDefaultRE = /export\s+default/;
|
|
278
251
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
279
252
|
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
@@ -281,21 +254,20 @@ var index = 1;
|
|
|
281
254
|
var compileRoot = null;
|
|
282
255
|
var compiler;
|
|
283
256
|
var vue;
|
|
284
|
-
var _require = (0, import_node_module.createRequire)(import_meta.url);
|
|
285
257
|
function requireCompiler() {
|
|
286
258
|
if (!compiler) {
|
|
287
259
|
if (compileRoot) {
|
|
288
260
|
try {
|
|
289
|
-
compiler =
|
|
261
|
+
compiler = __require(__require.resolve("vue/compiler-sfc", { paths: [compileRoot] }));
|
|
290
262
|
} catch (e) {
|
|
291
263
|
}
|
|
292
264
|
}
|
|
293
265
|
if (!compiler) {
|
|
294
266
|
try {
|
|
295
|
-
compiler =
|
|
267
|
+
compiler = __require("vue/compiler-sfc");
|
|
296
268
|
} catch (e) {
|
|
297
269
|
try {
|
|
298
|
-
compiler =
|
|
270
|
+
compiler = __require("@vue/compiler-sfc");
|
|
299
271
|
} catch (e2) {
|
|
300
272
|
throw new Error("@vue/compiler-sfc is not present in the dependency tree.\n");
|
|
301
273
|
}
|
|
@@ -308,13 +280,13 @@ function isVue3() {
|
|
|
308
280
|
if (!vue) {
|
|
309
281
|
if (compileRoot) {
|
|
310
282
|
try {
|
|
311
|
-
vue =
|
|
283
|
+
vue = __require(__require.resolve("vue", { paths: [compileRoot] }));
|
|
312
284
|
} catch (e) {
|
|
313
285
|
}
|
|
314
286
|
}
|
|
315
287
|
if (!vue) {
|
|
316
288
|
try {
|
|
317
|
-
vue =
|
|
289
|
+
vue = __require("vue");
|
|
318
290
|
} catch (e) {
|
|
319
291
|
throw new Error("vue is not present in the dependency tree.\n");
|
|
320
292
|
}
|
|
@@ -384,12 +356,15 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
384
356
|
}
|
|
385
357
|
|
|
386
358
|
// src/rollup.ts
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
359
|
+
import { resolve as resolve2 } from "node:path";
|
|
360
|
+
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
361
|
+
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
362
|
+
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|
|
363
|
+
import {
|
|
364
|
+
DtsRollupGenerator,
|
|
365
|
+
DtsRollupKind
|
|
366
|
+
} from "@microsoft/api-extractor/lib/generators/DtsRollupGenerator";
|
|
367
|
+
import { PackageJsonLookup } from "@rushstack/node-core-library";
|
|
393
368
|
var dtsRE = /\.d\.tsx?$/;
|
|
394
369
|
function rollupDeclarationFiles({
|
|
395
370
|
root,
|
|
@@ -399,13 +374,13 @@ function rollupDeclarationFiles({
|
|
|
399
374
|
fileName,
|
|
400
375
|
compilerOptions
|
|
401
376
|
}) {
|
|
402
|
-
const configObjectFullPath = (
|
|
403
|
-
const packageJsonLookup = new
|
|
377
|
+
const configObjectFullPath = resolve2(root, "api-extractor.json");
|
|
378
|
+
const packageJsonLookup = new PackageJsonLookup();
|
|
404
379
|
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
405
380
|
if (!dtsRE.test(fileName)) {
|
|
406
381
|
fileName += ".d.ts";
|
|
407
382
|
}
|
|
408
|
-
const extractorConfig =
|
|
383
|
+
const extractorConfig = ExtractorConfig.prepare({
|
|
409
384
|
configObject: {
|
|
410
385
|
projectFolder: root,
|
|
411
386
|
mainEntryPointFilePath: entryPath,
|
|
@@ -422,7 +397,7 @@ function rollupDeclarationFiles({
|
|
|
422
397
|
},
|
|
423
398
|
dtsRollup: {
|
|
424
399
|
enabled: true,
|
|
425
|
-
publicTrimmedFilePath: (
|
|
400
|
+
publicTrimmedFilePath: resolve2(outputDir, fileName)
|
|
426
401
|
},
|
|
427
402
|
tsdocMetadata: {
|
|
428
403
|
enabled: false
|
|
@@ -443,11 +418,11 @@ function rollupDeclarationFiles({
|
|
|
443
418
|
configObjectFullPath,
|
|
444
419
|
packageJsonFullPath
|
|
445
420
|
});
|
|
446
|
-
const compilerState =
|
|
421
|
+
const compilerState = CompilerState.create(extractorConfig, {
|
|
447
422
|
localBuild: true,
|
|
448
423
|
showVerboseMessages: false
|
|
449
424
|
});
|
|
450
|
-
const messageRouter = new
|
|
425
|
+
const messageRouter = new MessageRouter({
|
|
451
426
|
workingPackageFolder: root,
|
|
452
427
|
messageCallback: void 0,
|
|
453
428
|
messagesConfig: extractorConfig.messages,
|
|
@@ -455,16 +430,16 @@ function rollupDeclarationFiles({
|
|
|
455
430
|
showDiagnostics: false,
|
|
456
431
|
tsdocConfiguration: extractorConfig.tsdocConfiguration
|
|
457
432
|
});
|
|
458
|
-
const collector = new
|
|
433
|
+
const collector = new Collector({
|
|
459
434
|
program: compilerState.program,
|
|
460
435
|
messageRouter,
|
|
461
436
|
extractorConfig
|
|
462
437
|
});
|
|
463
438
|
collector.analyze();
|
|
464
|
-
|
|
439
|
+
DtsRollupGenerator.writeTypingsFile(
|
|
465
440
|
collector,
|
|
466
441
|
extractorConfig.publicTrimmedFilePath,
|
|
467
|
-
|
|
442
|
+
DtsRollupKind.PublicRelease,
|
|
468
443
|
extractorConfig.newlineKind
|
|
469
444
|
);
|
|
470
445
|
}
|
|
@@ -482,8 +457,8 @@ var fullRelativeRE = /^\.\.?\//;
|
|
|
482
457
|
var defaultIndex = "index.d.ts";
|
|
483
458
|
var noop = () => {
|
|
484
459
|
};
|
|
485
|
-
var logPrefix =
|
|
486
|
-
var bundleDebug =
|
|
460
|
+
var logPrefix = chalk.cyan("[vite:dts]");
|
|
461
|
+
var bundleDebug = debug("vite-plugin-dts:bundle");
|
|
487
462
|
function dtsPlugin(options = {}) {
|
|
488
463
|
const {
|
|
489
464
|
tsConfigFilePath = "tsconfig.json",
|
|
@@ -549,9 +524,9 @@ function dtsPlugin(options = {}) {
|
|
|
549
524
|
logger = config.logger;
|
|
550
525
|
if (!config.build.lib) {
|
|
551
526
|
logger.warn(
|
|
552
|
-
|
|
527
|
+
chalk.yellow(
|
|
553
528
|
`
|
|
554
|
-
${
|
|
529
|
+
${chalk.cyan(
|
|
555
530
|
"[vite:dts]"
|
|
556
531
|
)} You building not a library that may not need to generate declaration files.
|
|
557
532
|
`
|
|
@@ -572,9 +547,9 @@ ${import_chalk.default.cyan(
|
|
|
572
547
|
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
573
548
|
if (!outputDirs[0]) {
|
|
574
549
|
logger.error(
|
|
575
|
-
|
|
550
|
+
chalk.red(
|
|
576
551
|
`
|
|
577
|
-
${
|
|
552
|
+
${chalk.cyan(
|
|
578
553
|
"[vite:dts]"
|
|
579
554
|
)} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
580
555
|
`
|
|
@@ -584,7 +559,7 @@ ${import_chalk.default.cyan(
|
|
|
584
559
|
}
|
|
585
560
|
setCompileRoot(root);
|
|
586
561
|
compilerOptions.rootDir || (compilerOptions.rootDir = root);
|
|
587
|
-
project = new
|
|
562
|
+
project = new Project({
|
|
588
563
|
compilerOptions: mergeObjects(compilerOptions, {
|
|
589
564
|
noEmitOnError,
|
|
590
565
|
outDir: ".",
|
|
@@ -624,7 +599,7 @@ ${import_chalk.default.cyan(
|
|
|
624
599
|
if (watchExtensionRE.test(id)) {
|
|
625
600
|
isBundle = false;
|
|
626
601
|
if (project) {
|
|
627
|
-
const sourceFile = project.getSourceFile((
|
|
602
|
+
const sourceFile = project.getSourceFile(normalizePath2(id));
|
|
628
603
|
sourceFile && project.removeSourceFile(sourceFile);
|
|
629
604
|
}
|
|
630
605
|
}
|
|
@@ -632,21 +607,21 @@ ${import_chalk.default.cyan(
|
|
|
632
607
|
async closeBundle() {
|
|
633
608
|
if (!outputDirs || !project || isBundle)
|
|
634
609
|
return;
|
|
635
|
-
logger.info(
|
|
610
|
+
logger.info(chalk.green(`
|
|
636
611
|
${logPrefix} Start generate declaration files...`));
|
|
637
612
|
bundleDebug("start");
|
|
638
613
|
isBundle = true;
|
|
639
614
|
sourceDtsFiles.clear();
|
|
640
615
|
const startTime = Date.now();
|
|
641
|
-
const tsConfig =
|
|
616
|
+
const tsConfig = readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
|
|
642
617
|
const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
|
|
643
|
-
const parentTsConfig = parentTsConfigPath ?
|
|
618
|
+
const parentTsConfig = parentTsConfigPath ? readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
|
|
644
619
|
const include = options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*";
|
|
645
620
|
const exclude = options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**";
|
|
646
621
|
bundleDebug("read config");
|
|
647
622
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
648
623
|
if (include && include.length) {
|
|
649
|
-
const files = await (
|
|
624
|
+
const files = await glob(ensureArray(include).map(normalizeGlob), {
|
|
650
625
|
cwd: root,
|
|
651
626
|
absolute: true,
|
|
652
627
|
ignore: ensureArray(exclude).map(normalizeGlob)
|
|
@@ -665,8 +640,8 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
665
640
|
if (hasJsVue) {
|
|
666
641
|
if (!allowJs) {
|
|
667
642
|
logger.warn(
|
|
668
|
-
|
|
669
|
-
`${
|
|
643
|
+
chalk.yellow(
|
|
644
|
+
`${chalk.cyan(
|
|
670
645
|
"[vite:dts]"
|
|
671
646
|
)} Some js files are referenced, but you may not enable the 'allowJs' option.`
|
|
672
647
|
)
|
|
@@ -676,6 +651,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
676
651
|
}
|
|
677
652
|
bundleDebug("collect files");
|
|
678
653
|
}
|
|
654
|
+
project.resolveSourceFileDependencies();
|
|
679
655
|
bundleDebug("resolve");
|
|
680
656
|
if (!skipDiagnostics) {
|
|
681
657
|
const diagnostics = project.getPreEmitDiagnostics();
|
|
@@ -695,7 +671,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
695
671
|
const service = project.getLanguageService();
|
|
696
672
|
const outputFiles = project.getSourceFiles().map(
|
|
697
673
|
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
698
|
-
path: (
|
|
674
|
+
path: normalizePath2(resolve3(root, outputFile.compilerObject.name)),
|
|
699
675
|
content: outputFile.getText()
|
|
700
676
|
}))
|
|
701
677
|
).flat().concat(dtsOutputFiles);
|
|
@@ -706,7 +682,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
706
682
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
707
683
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
708
684
|
const outputDir = outputDirs[0];
|
|
709
|
-
await runParallel(
|
|
685
|
+
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
710
686
|
let filePath = outputFile.path;
|
|
711
687
|
let content = outputFile.content;
|
|
712
688
|
const isMapFile = filePath.endsWith(".map");
|
|
@@ -718,9 +694,9 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
718
694
|
content = transformAliasImport(filePath, content, aliases, aliasesExclude);
|
|
719
695
|
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
720
696
|
}
|
|
721
|
-
filePath = (
|
|
697
|
+
filePath = resolve3(
|
|
722
698
|
outputDir,
|
|
723
|
-
(
|
|
699
|
+
relative2(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
724
700
|
);
|
|
725
701
|
if (typeof beforeWriteFile === "function") {
|
|
726
702
|
const result = beforeWriteFile(filePath, content);
|
|
@@ -731,30 +707,30 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
731
707
|
content = result.content ?? content;
|
|
732
708
|
}
|
|
733
709
|
}
|
|
734
|
-
await
|
|
735
|
-
await
|
|
710
|
+
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
711
|
+
await fs.writeFile(
|
|
736
712
|
filePath,
|
|
737
713
|
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
738
714
|
"utf-8"
|
|
739
715
|
);
|
|
740
|
-
wroteFiles.add((
|
|
716
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
741
717
|
});
|
|
742
718
|
bundleDebug("output");
|
|
743
719
|
if (insertTypesEntry || rollupTypes) {
|
|
744
|
-
const pkgPath = (
|
|
745
|
-
const pkg =
|
|
720
|
+
const pkgPath = resolve3(root, "package.json");
|
|
721
|
+
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
|
746
722
|
const types = pkg.types || pkg.typings;
|
|
747
|
-
let typesPath = types ? (
|
|
748
|
-
if (!
|
|
723
|
+
let typesPath = types ? resolve3(root, types) : resolve3(outputDir, indexName);
|
|
724
|
+
if (!fs.existsSync(typesPath)) {
|
|
749
725
|
const entry = entries[0];
|
|
750
|
-
const outputIndex = (
|
|
751
|
-
let filePath = (
|
|
726
|
+
const outputIndex = resolve3(outputDir, relative2(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
727
|
+
let filePath = normalizePath2(relative2(dirname3(typesPath), outputIndex));
|
|
752
728
|
filePath = filePath.replace(dtsRE2, "");
|
|
753
729
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
754
730
|
let content = `export * from '${filePath}'
|
|
755
731
|
`;
|
|
756
|
-
if (
|
|
757
|
-
const entryCodes = await
|
|
732
|
+
if (fs.existsSync(outputIndex)) {
|
|
733
|
+
const entryCodes = await fs.readFile(outputIndex, "utf-8");
|
|
758
734
|
if (entryCodes.includes("export default")) {
|
|
759
735
|
content += `import ${libName} from '${filePath}'
|
|
760
736
|
export default ${libName}
|
|
@@ -770,32 +746,32 @@ export default ${libName}
|
|
|
770
746
|
}
|
|
771
747
|
}
|
|
772
748
|
if (result !== false) {
|
|
773
|
-
await
|
|
774
|
-
wroteFiles.add((
|
|
749
|
+
await fs.writeFile(typesPath, content, "utf-8");
|
|
750
|
+
wroteFiles.add(normalizePath2(typesPath));
|
|
775
751
|
}
|
|
776
752
|
}
|
|
777
753
|
bundleDebug("insert index");
|
|
778
754
|
if (rollupTypes) {
|
|
779
|
-
logger.info(
|
|
755
|
+
logger.info(chalk.green(`${logPrefix} Start rollup declaration files...`));
|
|
780
756
|
rollupDeclarationFiles({
|
|
781
757
|
root,
|
|
782
758
|
tsConfigPath,
|
|
783
759
|
compilerOptions,
|
|
784
760
|
outputDir,
|
|
785
761
|
entryPath: typesPath,
|
|
786
|
-
fileName:
|
|
762
|
+
fileName: basename(typesPath)
|
|
787
763
|
});
|
|
788
|
-
const wroteFile = (
|
|
764
|
+
const wroteFile = normalizePath2(typesPath);
|
|
789
765
|
wroteFiles.delete(wroteFile);
|
|
790
|
-
await runParallel(
|
|
766
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), (f) => fs.unlink(f));
|
|
791
767
|
removeDirIfEmpty(outputDir);
|
|
792
768
|
wroteFiles.clear();
|
|
793
769
|
wroteFiles.add(wroteFile);
|
|
794
770
|
if (copyDtsFiles) {
|
|
795
|
-
await runParallel(
|
|
796
|
-
const filePath = (
|
|
797
|
-
await
|
|
798
|
-
wroteFiles.add((
|
|
771
|
+
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
772
|
+
const filePath = resolve3(outputDir, basename(path));
|
|
773
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
774
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
799
775
|
});
|
|
800
776
|
}
|
|
801
777
|
bundleDebug("rollup");
|
|
@@ -803,14 +779,14 @@ export default ${libName}
|
|
|
803
779
|
}
|
|
804
780
|
if (outputDirs.length > 1) {
|
|
805
781
|
const dirs = outputDirs.slice(1);
|
|
806
|
-
await runParallel(
|
|
807
|
-
const relativePath = (
|
|
808
|
-
const content = await
|
|
782
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
783
|
+
const relativePath = relative2(outputDir, wroteFile);
|
|
784
|
+
const content = await fs.readFile(wroteFile, "utf-8");
|
|
809
785
|
await Promise.all(
|
|
810
786
|
dirs.map(async (dir) => {
|
|
811
|
-
const filePath = (
|
|
812
|
-
await
|
|
813
|
-
await
|
|
787
|
+
const filePath = resolve3(dir, relativePath);
|
|
788
|
+
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
789
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
814
790
|
})
|
|
815
791
|
);
|
|
816
792
|
});
|
|
@@ -821,7 +797,7 @@ export default ${libName}
|
|
|
821
797
|
}
|
|
822
798
|
bundleDebug("finish");
|
|
823
799
|
logger.info(
|
|
824
|
-
|
|
800
|
+
chalk.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
825
801
|
`)
|
|
826
802
|
);
|
|
827
803
|
}
|
|
@@ -830,5 +806,6 @@ export default ${libName}
|
|
|
830
806
|
|
|
831
807
|
// src/index.ts
|
|
832
808
|
var src_default = dtsPlugin;
|
|
833
|
-
|
|
834
|
-
|
|
809
|
+
export {
|
|
810
|
+
src_default as default
|
|
811
|
+
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
1
9
|
// src/plugin.ts
|
|
2
|
-
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "path";
|
|
10
|
+
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "node:path";
|
|
3
11
|
import fs from "fs-extra";
|
|
4
12
|
import os from "os";
|
|
5
13
|
import chalk from "chalk";
|
|
@@ -10,12 +18,12 @@ import { normalizePath as normalizePath2 } from "vite";
|
|
|
10
18
|
import { readConfigFile } from "typescript";
|
|
11
19
|
|
|
12
20
|
// src/transform.ts
|
|
13
|
-
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "path";
|
|
21
|
+
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "node:path";
|
|
14
22
|
import { normalizePath } from "vite";
|
|
15
23
|
|
|
16
24
|
// src/utils.ts
|
|
17
|
-
import { resolve, isAbsolute, dirname, normalize, sep } from "path";
|
|
18
|
-
import { existsSync, readdirSync, lstatSync, rmdirSync } from "fs";
|
|
25
|
+
import { resolve, isAbsolute, dirname, normalize, sep } from "node:path";
|
|
26
|
+
import { existsSync, readdirSync, lstatSync, rmdirSync } from "node:fs";
|
|
19
27
|
function isNativeObj(value) {
|
|
20
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
21
29
|
}
|
|
@@ -239,7 +247,6 @@ function transferSetupPosition(content) {
|
|
|
239
247
|
}
|
|
240
248
|
|
|
241
249
|
// src/compile.ts
|
|
242
|
-
import { createRequire } from "node:module";
|
|
243
250
|
var exportDefaultRE = /export\s+default/;
|
|
244
251
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
245
252
|
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
@@ -247,21 +254,20 @@ var index = 1;
|
|
|
247
254
|
var compileRoot = null;
|
|
248
255
|
var compiler;
|
|
249
256
|
var vue;
|
|
250
|
-
var _require = createRequire(import.meta.url);
|
|
251
257
|
function requireCompiler() {
|
|
252
258
|
if (!compiler) {
|
|
253
259
|
if (compileRoot) {
|
|
254
260
|
try {
|
|
255
|
-
compiler =
|
|
261
|
+
compiler = __require(__require.resolve("vue/compiler-sfc", { paths: [compileRoot] }));
|
|
256
262
|
} catch (e) {
|
|
257
263
|
}
|
|
258
264
|
}
|
|
259
265
|
if (!compiler) {
|
|
260
266
|
try {
|
|
261
|
-
compiler =
|
|
267
|
+
compiler = __require("vue/compiler-sfc");
|
|
262
268
|
} catch (e) {
|
|
263
269
|
try {
|
|
264
|
-
compiler =
|
|
270
|
+
compiler = __require("@vue/compiler-sfc");
|
|
265
271
|
} catch (e2) {
|
|
266
272
|
throw new Error("@vue/compiler-sfc is not present in the dependency tree.\n");
|
|
267
273
|
}
|
|
@@ -274,13 +280,13 @@ function isVue3() {
|
|
|
274
280
|
if (!vue) {
|
|
275
281
|
if (compileRoot) {
|
|
276
282
|
try {
|
|
277
|
-
vue =
|
|
283
|
+
vue = __require(__require.resolve("vue", { paths: [compileRoot] }));
|
|
278
284
|
} catch (e) {
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
287
|
if (!vue) {
|
|
282
288
|
try {
|
|
283
|
-
vue =
|
|
289
|
+
vue = __require("vue");
|
|
284
290
|
} catch (e) {
|
|
285
291
|
throw new Error("vue is not present in the dependency tree.\n");
|
|
286
292
|
}
|
|
@@ -350,7 +356,7 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
350
356
|
}
|
|
351
357
|
|
|
352
358
|
// src/rollup.ts
|
|
353
|
-
import { resolve as resolve2 } from "path";
|
|
359
|
+
import { resolve as resolve2 } from "node:path";
|
|
354
360
|
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
355
361
|
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
356
362
|
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|