vite-plugin-dts 1.5.0 → 1.6.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 +2 -1
- package/README.zh-CN.md +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +145 -82
- package/dist/index.mjs +119 -60
- package/package.json +35 -34
package/README.md
CHANGED
|
@@ -157,8 +157,9 @@ export interface PluginOptions {
|
|
|
157
157
|
|
|
158
158
|
// Before declaration file be writed hook
|
|
159
159
|
// You can transform declaration file-path and content through it
|
|
160
|
+
// The file will be skipped when return exact false
|
|
160
161
|
// Default: () => {}
|
|
161
|
-
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
162
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
162
163
|
|
|
163
164
|
// After build hook
|
|
164
165
|
// It wil be called after all declaration files are written
|
package/README.zh-CN.md
CHANGED
|
@@ -156,8 +156,9 @@ export interface PluginOptions {
|
|
|
156
156
|
|
|
157
157
|
// 类型声明文件被写入前的钩子
|
|
158
158
|
// 可以在钩子里转换文件路径和文件内容
|
|
159
|
+
// 当返回 false 时会跳过该文件
|
|
159
160
|
// 默认值: () => {}
|
|
160
|
-
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
161
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
161
162
|
|
|
162
163
|
// 构建后回调钩子
|
|
163
164
|
// 将会在所有类型文件被写入后调用
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ interface PluginOptions {
|
|
|
24
24
|
skipDiagnostics?: boolean;
|
|
25
25
|
logDiagnostics?: boolean;
|
|
26
26
|
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>;
|
|
27
|
-
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile;
|
|
27
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile;
|
|
28
28
|
afterBuild?: () => void | Promise<void>;
|
|
29
29
|
}
|
|
30
30
|
declare function dtsPlugin(options?: PluginOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -16,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
}
|
|
17
18
|
return to;
|
|
18
19
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
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
|
+
));
|
|
20
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
25
|
|
|
22
26
|
// src/index.ts
|
|
@@ -28,7 +32,7 @@ module.exports = dtsPlugin;
|
|
|
28
32
|
dtsPlugin['default'] = dtsPlugin;
|
|
29
33
|
|
|
30
34
|
// src/plugin.ts
|
|
31
|
-
var
|
|
35
|
+
var import_node_path4 = require("path");
|
|
32
36
|
var import_fs_extra = __toESM(require("fs-extra"));
|
|
33
37
|
var import_os = __toESM(require("os"));
|
|
34
38
|
var import_chalk = __toESM(require("chalk"));
|
|
@@ -39,12 +43,12 @@ var import_vite2 = require("vite");
|
|
|
39
43
|
var import_typescript = require("typescript");
|
|
40
44
|
|
|
41
45
|
// src/transform.ts
|
|
42
|
-
var
|
|
46
|
+
var import_node_path2 = require("path");
|
|
43
47
|
var import_vite = require("vite");
|
|
44
48
|
|
|
45
49
|
// src/utils.ts
|
|
46
|
-
var
|
|
47
|
-
var
|
|
50
|
+
var import_node_path = require("path");
|
|
51
|
+
var import_node_fs = require("fs");
|
|
48
52
|
function isNativeObj(value) {
|
|
49
53
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
50
54
|
}
|
|
@@ -88,7 +92,7 @@ function mergeObjects(sourceObj, targetObj) {
|
|
|
88
92
|
return sourceObj;
|
|
89
93
|
}
|
|
90
94
|
function ensureAbsolute(path, root) {
|
|
91
|
-
return path ? (0,
|
|
95
|
+
return path ? (0, import_node_path.isAbsolute)(path) ? path : (0, import_node_path.resolve)(root, path) : root;
|
|
92
96
|
}
|
|
93
97
|
function ensureArray(value) {
|
|
94
98
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -114,16 +118,16 @@ function queryPublicPath(paths) {
|
|
|
114
118
|
if (paths.length === 0) {
|
|
115
119
|
return "";
|
|
116
120
|
} else if (paths.length === 1) {
|
|
117
|
-
return (0,
|
|
121
|
+
return (0, import_node_path.dirname)(paths[0]);
|
|
118
122
|
}
|
|
119
|
-
let publicPath = (0,
|
|
123
|
+
let publicPath = (0, import_node_path.normalize)((0, import_node_path.dirname)(paths[0])) + import_node_path.sep;
|
|
120
124
|
let publicUnits = publicPath.split(speRE);
|
|
121
125
|
let index2 = publicUnits.length - 1;
|
|
122
126
|
for (const path of paths.slice(1)) {
|
|
123
127
|
if (!index2) {
|
|
124
128
|
return publicPath;
|
|
125
129
|
}
|
|
126
|
-
const dirPath = (0,
|
|
130
|
+
const dirPath = (0, import_node_path.normalize)((0, import_node_path.dirname)(path)) + import_node_path.sep;
|
|
127
131
|
if (dirPath.startsWith(publicPath)) {
|
|
128
132
|
continue;
|
|
129
133
|
}
|
|
@@ -140,7 +144,7 @@ function queryPublicPath(paths) {
|
|
|
140
144
|
}
|
|
141
145
|
index2 = i - 1;
|
|
142
146
|
publicUnits = publicUnits.slice(0, index2 + 1);
|
|
143
|
-
publicPath = publicUnits.join(
|
|
147
|
+
publicPath = publicUnits.join(import_node_path.sep) + import_node_path.sep;
|
|
144
148
|
break;
|
|
145
149
|
}
|
|
146
150
|
}
|
|
@@ -148,13 +152,13 @@ function queryPublicPath(paths) {
|
|
|
148
152
|
return publicPath.slice(0, -1);
|
|
149
153
|
}
|
|
150
154
|
function removeDirIfEmpty(dir) {
|
|
151
|
-
if (!(0,
|
|
155
|
+
if (!(0, import_node_fs.existsSync)(dir)) {
|
|
152
156
|
return;
|
|
153
157
|
}
|
|
154
158
|
let onlyHasDir = true;
|
|
155
|
-
for (const file of (0,
|
|
156
|
-
const abs = (0,
|
|
157
|
-
if ((0,
|
|
159
|
+
for (const file of (0, import_node_fs.readdirSync)(dir)) {
|
|
160
|
+
const abs = (0, import_node_path.resolve)(dir, file);
|
|
161
|
+
if ((0, import_node_fs.lstatSync)(abs).isDirectory()) {
|
|
158
162
|
if (!removeDirIfEmpty(abs)) {
|
|
159
163
|
onlyHasDir = false;
|
|
160
164
|
}
|
|
@@ -163,7 +167,7 @@ function removeDirIfEmpty(dir) {
|
|
|
163
167
|
}
|
|
164
168
|
}
|
|
165
169
|
if (onlyHasDir) {
|
|
166
|
-
(0,
|
|
170
|
+
(0, import_node_fs.rmdirSync)(dir);
|
|
167
171
|
}
|
|
168
172
|
return onlyHasDir;
|
|
169
173
|
}
|
|
@@ -184,20 +188,25 @@ var importTypesRE = /import\s?(?:type)?\s?\{(.+)\}\s?from\s?['"].+['"]/;
|
|
|
184
188
|
function transformDynamicImport(content) {
|
|
185
189
|
const importMap = /* @__PURE__ */ new Map();
|
|
186
190
|
content = content.replace(globalDynamicTypeRE, (str) => {
|
|
187
|
-
var _a;
|
|
188
191
|
const matchResult = str.match(dynamicTypeRE);
|
|
189
192
|
const libName = matchResult[1];
|
|
190
|
-
const importSet =
|
|
193
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
191
194
|
const usedType = matchResult[2];
|
|
192
195
|
importSet.add(usedType);
|
|
193
196
|
return usedType + matchResult[3];
|
|
194
197
|
});
|
|
195
198
|
importMap.forEach((importSet, libName) => {
|
|
196
|
-
const importReg = new RegExp(
|
|
199
|
+
const importReg = new RegExp(
|
|
200
|
+
`import\\s?(?:type)?\\s?\\{[^;\\n]+\\}\\s?from\\s?['"]${libName}['"]`,
|
|
201
|
+
"g"
|
|
202
|
+
);
|
|
197
203
|
const matchResult = content.match(importReg);
|
|
198
204
|
if (matchResult == null ? void 0 : matchResult[0]) {
|
|
199
205
|
const importedTypes = matchResult[0].match(importTypesRE)[1].trim().split(",");
|
|
200
|
-
content = content.replace(
|
|
206
|
+
content = content.replace(
|
|
207
|
+
matchResult[0],
|
|
208
|
+
`import type { ${Array.from(importSet).concat(importedTypes).join(", ")} } from '${libName}'`
|
|
209
|
+
);
|
|
201
210
|
} else {
|
|
202
211
|
content = `import type { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
203
212
|
` + content;
|
|
@@ -235,8 +244,14 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
235
244
|
if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
|
|
236
245
|
return str;
|
|
237
246
|
}
|
|
238
|
-
const truthPath = (0,
|
|
239
|
-
return str.replace(
|
|
247
|
+
const truthPath = (0, import_node_path2.isAbsolute)(matchedAlias.replacement) ? (0, import_vite.normalizePath)((0, import_node_path2.relative)((0, import_node_path2.dirname)(filePath), matchedAlias.replacement)) : (0, import_vite.normalizePath)(matchedAlias.replacement);
|
|
248
|
+
return str.replace(
|
|
249
|
+
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
250
|
+
`$1'${matchResult[1].replace(
|
|
251
|
+
matchedAlias.find,
|
|
252
|
+
(truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : "")
|
|
253
|
+
)}'${isDynamic ? ")" : ""}`
|
|
254
|
+
);
|
|
240
255
|
}
|
|
241
256
|
}
|
|
242
257
|
return str;
|
|
@@ -332,7 +347,7 @@ function compileVueCode(code) {
|
|
|
332
347
|
id: `${index++}`
|
|
333
348
|
});
|
|
334
349
|
const classMatch = compiled.content.match(exportDefaultClassRE);
|
|
335
|
-
const plugins = scriptSetup.lang === "ts" ? ["typescript"] : void 0;
|
|
350
|
+
const plugins = scriptSetup.lang === "ts" ? ["typescript", "decorators-legacy"] : void 0;
|
|
336
351
|
if (classMatch) {
|
|
337
352
|
content = compiled.content.replace(exportDefaultClassRE, "\nclass $1") + `
|
|
338
353
|
const _sfc_main = ${classMatch[1]}`;
|
|
@@ -343,10 +358,18 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
343
358
|
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
344
359
|
}
|
|
345
360
|
content = transferSetupPosition(content);
|
|
361
|
+
content = content.replace(/(const __returned__\s?=\s?\{[\s\S]+?)(props)(\s?\})/, "$1props: props as any$3").replace(
|
|
362
|
+
/(const __returned__\s?=\s?\{[\s\S]+?)(props,)([\s\S]+?)/,
|
|
363
|
+
"$1props: props as any,$3"
|
|
364
|
+
);
|
|
346
365
|
content += "\nexport default _sfc_main\n";
|
|
347
366
|
ext = scriptSetup.lang || "js";
|
|
348
367
|
} else if (script && script.content) {
|
|
349
|
-
content = rewriteDefault(
|
|
368
|
+
content = rewriteDefault(
|
|
369
|
+
script.content,
|
|
370
|
+
"_sfc_main",
|
|
371
|
+
script.lang === "ts" ? ["typescript"] : void 0
|
|
372
|
+
);
|
|
350
373
|
content += "\nexport default _sfc_main\n";
|
|
351
374
|
ext = script.lang || "js";
|
|
352
375
|
}
|
|
@@ -358,7 +381,7 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
358
381
|
}
|
|
359
382
|
|
|
360
383
|
// src/rollup.ts
|
|
361
|
-
var
|
|
384
|
+
var import_node_path3 = require("path");
|
|
362
385
|
var import_api_extractor = require("@microsoft/api-extractor");
|
|
363
386
|
var import_Collector = require("@microsoft/api-extractor/lib/collector/Collector");
|
|
364
387
|
var import_MessageRouter = require("@microsoft/api-extractor/lib/collector/MessageRouter");
|
|
@@ -373,7 +396,7 @@ function rollupDeclarationFiles({
|
|
|
373
396
|
fileName,
|
|
374
397
|
compilerOptions
|
|
375
398
|
}) {
|
|
376
|
-
const configObjectFullPath = (0,
|
|
399
|
+
const configObjectFullPath = (0, import_node_path3.resolve)(root, "api-extractor.json");
|
|
377
400
|
const packageJsonLookup = new import_node_core_library.PackageJsonLookup();
|
|
378
401
|
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
379
402
|
if (!dtsRE.test(fileName)) {
|
|
@@ -396,7 +419,7 @@ function rollupDeclarationFiles({
|
|
|
396
419
|
},
|
|
397
420
|
dtsRollup: {
|
|
398
421
|
enabled: true,
|
|
399
|
-
publicTrimmedFilePath: (0,
|
|
422
|
+
publicTrimmedFilePath: (0, import_node_path3.resolve)(outputDir, fileName)
|
|
400
423
|
},
|
|
401
424
|
tsdocMetadata: {
|
|
402
425
|
enabled: false
|
|
@@ -435,7 +458,12 @@ function rollupDeclarationFiles({
|
|
|
435
458
|
extractorConfig
|
|
436
459
|
});
|
|
437
460
|
collector.analyze();
|
|
438
|
-
import_DtsRollupGenerator.DtsRollupGenerator.writeTypingsFile(
|
|
461
|
+
import_DtsRollupGenerator.DtsRollupGenerator.writeTypingsFile(
|
|
462
|
+
collector,
|
|
463
|
+
extractorConfig.publicTrimmedFilePath,
|
|
464
|
+
import_DtsRollupGenerator.DtsRollupKind.PublicRelease,
|
|
465
|
+
extractorConfig.newlineKind
|
|
466
|
+
);
|
|
439
467
|
}
|
|
440
468
|
|
|
441
469
|
// src/plugin.ts
|
|
@@ -454,7 +482,6 @@ var noop = () => {
|
|
|
454
482
|
var logPrefix = import_chalk.default.cyan("[vite:dts]");
|
|
455
483
|
var bundleDebug = (0, import_debug.debug)("vite-plugin-dts:bundle");
|
|
456
484
|
function dtsPlugin(options = {}) {
|
|
457
|
-
var _a, _b;
|
|
458
485
|
const {
|
|
459
486
|
tsConfigFilePath = "tsconfig.json",
|
|
460
487
|
aliasesExclude = [],
|
|
@@ -471,9 +498,9 @@ function dtsPlugin(options = {}) {
|
|
|
471
498
|
beforeWriteFile = noop,
|
|
472
499
|
afterBuild = noop
|
|
473
500
|
} = options;
|
|
474
|
-
const compilerOptions =
|
|
501
|
+
const compilerOptions = options.compilerOptions ?? {};
|
|
475
502
|
let root;
|
|
476
|
-
let entryRoot =
|
|
503
|
+
let entryRoot = options.entryRoot ?? "";
|
|
477
504
|
let libName;
|
|
478
505
|
let indexName;
|
|
479
506
|
let aliases;
|
|
@@ -491,10 +518,10 @@ function dtsPlugin(options = {}) {
|
|
|
491
518
|
apply: "build",
|
|
492
519
|
enforce: "pre",
|
|
493
520
|
config(config) {
|
|
494
|
-
var
|
|
521
|
+
var _a;
|
|
495
522
|
if (isBundle)
|
|
496
523
|
return;
|
|
497
|
-
const aliasOptions = (
|
|
524
|
+
const aliasOptions = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) ?? [];
|
|
498
525
|
if (isNativeObj(aliasOptions)) {
|
|
499
526
|
aliases = Object.entries(aliasOptions).map(([key, value]) => {
|
|
500
527
|
return { find: key, replacement: value };
|
|
@@ -503,38 +530,53 @@ function dtsPlugin(options = {}) {
|
|
|
503
530
|
aliases = ensureArray(aliasOptions);
|
|
504
531
|
}
|
|
505
532
|
if (aliasesExclude.length > 0) {
|
|
506
|
-
aliases = aliases.filter(
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
533
|
+
aliases = aliases.filter(
|
|
534
|
+
({ find }) => !aliasesExclude.some(
|
|
535
|
+
(alias) => {
|
|
536
|
+
var _a2;
|
|
537
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a2 = find.match(alias)) == null ? void 0 : _a2[0] : find === alias);
|
|
538
|
+
}
|
|
539
|
+
)
|
|
540
|
+
);
|
|
510
541
|
}
|
|
511
542
|
},
|
|
512
543
|
configResolved(config) {
|
|
513
|
-
var _a2, _b2, _c;
|
|
514
544
|
if (isBundle)
|
|
515
545
|
return;
|
|
516
546
|
logger = config.logger;
|
|
517
547
|
if (!config.build.lib) {
|
|
518
|
-
logger.warn(
|
|
519
|
-
|
|
520
|
-
`
|
|
548
|
+
logger.warn(
|
|
549
|
+
import_chalk.default.yellow(
|
|
550
|
+
`
|
|
551
|
+
${import_chalk.default.cyan(
|
|
552
|
+
"[vite:dts]"
|
|
553
|
+
)} You building not a library that may not need to generate declaration files.
|
|
554
|
+
`
|
|
555
|
+
)
|
|
556
|
+
);
|
|
521
557
|
libName = "_default";
|
|
522
558
|
indexName = defaultIndex;
|
|
523
559
|
} else {
|
|
524
|
-
const filename =
|
|
560
|
+
const filename = config.build.lib.fileName ?? defaultIndex;
|
|
525
561
|
libName = config.build.lib.name || "_default";
|
|
526
562
|
indexName = typeof filename === "string" ? filename : filename("es");
|
|
527
563
|
if (!dtsRE2.test(indexName)) {
|
|
528
564
|
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
529
565
|
}
|
|
530
566
|
}
|
|
531
|
-
root = ensureAbsolute(
|
|
567
|
+
root = ensureAbsolute(options.root ?? "", config.root);
|
|
532
568
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
533
569
|
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
534
|
-
if (!outputDirs) {
|
|
535
|
-
logger.error(
|
|
536
|
-
|
|
537
|
-
`
|
|
570
|
+
if (!outputDirs[0]) {
|
|
571
|
+
logger.error(
|
|
572
|
+
import_chalk.default.red(
|
|
573
|
+
`
|
|
574
|
+
${import_chalk.default.cyan(
|
|
575
|
+
"[vite:dts]"
|
|
576
|
+
)} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
577
|
+
`
|
|
578
|
+
)
|
|
579
|
+
);
|
|
538
580
|
return;
|
|
539
581
|
}
|
|
540
582
|
setCompileRoot(root);
|
|
@@ -552,7 +594,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
552
594
|
tsConfigFilePath: tsConfigPath,
|
|
553
595
|
skipAddingFilesFromTsConfig: true
|
|
554
596
|
});
|
|
555
|
-
allowJs =
|
|
597
|
+
allowJs = project.getCompilerOptions().allowJs ?? false;
|
|
556
598
|
},
|
|
557
599
|
buildStart(inputOptions) {
|
|
558
600
|
if (!isBundle && (insertTypesEntry || rollupTypes)) {
|
|
@@ -585,7 +627,6 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
585
627
|
}
|
|
586
628
|
},
|
|
587
629
|
async closeBundle() {
|
|
588
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
|
|
589
630
|
if (!outputDirs || !project || isBundle)
|
|
590
631
|
return;
|
|
591
632
|
logger.info(import_chalk.default.green(`
|
|
@@ -594,11 +635,11 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
594
635
|
isBundle = true;
|
|
595
636
|
sourceDtsFiles.clear();
|
|
596
637
|
const startTime = Date.now();
|
|
597
|
-
const tsConfig = (
|
|
638
|
+
const tsConfig = (0, import_typescript.readConfigFile)(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
|
|
598
639
|
const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
|
|
599
640
|
const parentTsConfig = parentTsConfigPath ? (0, import_typescript.readConfigFile)(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
|
|
600
|
-
const include =
|
|
601
|
-
const exclude =
|
|
641
|
+
const include = options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*";
|
|
642
|
+
const exclude = options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**";
|
|
602
643
|
bundleDebug("read config");
|
|
603
644
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
604
645
|
if (include && include.length) {
|
|
@@ -620,13 +661,18 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
620
661
|
});
|
|
621
662
|
if (hasJsVue) {
|
|
622
663
|
if (!allowJs) {
|
|
623
|
-
logger.warn(
|
|
664
|
+
logger.warn(
|
|
665
|
+
import_chalk.default.yellow(
|
|
666
|
+
`${import_chalk.default.cyan(
|
|
667
|
+
"[vite:dts]"
|
|
668
|
+
)} Some js files are referenced, but you may not enable the 'allowJs' option.`
|
|
669
|
+
)
|
|
670
|
+
);
|
|
624
671
|
}
|
|
625
672
|
project.compilerOptions.set({ allowJs: true });
|
|
626
673
|
}
|
|
627
674
|
bundleDebug("collect files");
|
|
628
675
|
}
|
|
629
|
-
project.resolveSourceFileDependencies();
|
|
630
676
|
bundleDebug("resolve");
|
|
631
677
|
if (!skipDiagnostics) {
|
|
632
678
|
const diagnostics = project.getPreEmitDiagnostics();
|
|
@@ -644,10 +690,12 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
644
690
|
content: sourceFile.getFullText()
|
|
645
691
|
}));
|
|
646
692
|
const service = project.getLanguageService();
|
|
647
|
-
const outputFiles = project.getSourceFiles().map(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
693
|
+
const outputFiles = project.getSourceFiles().map(
|
|
694
|
+
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
695
|
+
path: (0, import_vite2.normalizePath)((0, import_node_path4.resolve)(root, outputFile.compilerObject.name)),
|
|
696
|
+
content: outputFile.getText()
|
|
697
|
+
}))
|
|
698
|
+
).flat().concat(dtsOutputFiles);
|
|
651
699
|
bundleDebug("emit");
|
|
652
700
|
if (!entryRoot) {
|
|
653
701
|
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
@@ -656,7 +704,6 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
656
704
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
657
705
|
const outputDir = outputDirs[0];
|
|
658
706
|
await runParallel(import_os.default.cpus().length, outputFiles, async (outputFile) => {
|
|
659
|
-
var _a3, _b3;
|
|
660
707
|
let filePath = outputFile.path;
|
|
661
708
|
let content = outputFile.content;
|
|
662
709
|
const isMapFile = filePath.endsWith(".map");
|
|
@@ -668,28 +715,37 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
668
715
|
content = transformAliasImport(filePath, content, aliases, aliasesExclude);
|
|
669
716
|
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
670
717
|
}
|
|
671
|
-
filePath = (0,
|
|
718
|
+
filePath = (0, import_node_path4.resolve)(
|
|
719
|
+
outputDir,
|
|
720
|
+
(0, import_node_path4.relative)(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
721
|
+
);
|
|
672
722
|
if (typeof beforeWriteFile === "function") {
|
|
673
723
|
const result = beforeWriteFile(filePath, content);
|
|
724
|
+
if (result === false)
|
|
725
|
+
return;
|
|
674
726
|
if (result && isNativeObj(result)) {
|
|
675
|
-
filePath =
|
|
676
|
-
content =
|
|
727
|
+
filePath = result.filePath ?? filePath;
|
|
728
|
+
content = result.content ?? content;
|
|
677
729
|
}
|
|
678
730
|
}
|
|
679
|
-
await import_fs_extra.default.mkdir((0,
|
|
680
|
-
await import_fs_extra.default.writeFile(
|
|
731
|
+
await import_fs_extra.default.mkdir((0, import_node_path4.dirname)(filePath), { recursive: true });
|
|
732
|
+
await import_fs_extra.default.writeFile(
|
|
733
|
+
filePath,
|
|
734
|
+
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
735
|
+
"utf-8"
|
|
736
|
+
);
|
|
681
737
|
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
682
738
|
});
|
|
683
739
|
bundleDebug("output");
|
|
684
740
|
if (insertTypesEntry || rollupTypes) {
|
|
685
|
-
const pkgPath = (0,
|
|
741
|
+
const pkgPath = (0, import_node_path4.resolve)(root, "package.json");
|
|
686
742
|
const pkg = import_fs_extra.default.existsSync(pkgPath) ? JSON.parse(await import_fs_extra.default.readFile(pkgPath, "utf-8")) : {};
|
|
687
743
|
const types = pkg.types || pkg.typings;
|
|
688
|
-
let typesPath = types ? (0,
|
|
744
|
+
let typesPath = types ? (0, import_node_path4.resolve)(root, types) : (0, import_node_path4.resolve)(outputDir, indexName);
|
|
689
745
|
if (!import_fs_extra.default.existsSync(typesPath)) {
|
|
690
746
|
const entry = entries[0];
|
|
691
|
-
const outputIndex = (0,
|
|
692
|
-
let filePath = (0, import_vite2.normalizePath)((0,
|
|
747
|
+
const outputIndex = (0, import_node_path4.resolve)(outputDir, (0, import_node_path4.relative)(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
748
|
+
let filePath = (0, import_vite2.normalizePath)((0, import_node_path4.relative)((0, import_node_path4.dirname)(typesPath), outputIndex));
|
|
693
749
|
filePath = filePath.replace(dtsRE2, "");
|
|
694
750
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
695
751
|
let content = `export * from '${filePath}'
|
|
@@ -702,15 +758,18 @@ export default ${libName}
|
|
|
702
758
|
`;
|
|
703
759
|
}
|
|
704
760
|
}
|
|
761
|
+
let result;
|
|
705
762
|
if (typeof beforeWriteFile === "function") {
|
|
706
|
-
|
|
763
|
+
result = beforeWriteFile(typesPath, content);
|
|
707
764
|
if (result && isNativeObj(result)) {
|
|
708
|
-
typesPath =
|
|
709
|
-
content =
|
|
765
|
+
typesPath = result.filePath ?? typesPath;
|
|
766
|
+
content = result.content ?? content;
|
|
710
767
|
}
|
|
711
768
|
}
|
|
712
|
-
|
|
713
|
-
|
|
769
|
+
if (result !== false) {
|
|
770
|
+
await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
|
|
771
|
+
wroteFiles.add((0, import_vite2.normalizePath)(typesPath));
|
|
772
|
+
}
|
|
714
773
|
}
|
|
715
774
|
bundleDebug("insert index");
|
|
716
775
|
if (rollupTypes) {
|
|
@@ -721,7 +780,7 @@ export default ${libName}
|
|
|
721
780
|
compilerOptions,
|
|
722
781
|
outputDir,
|
|
723
782
|
entryPath: typesPath,
|
|
724
|
-
fileName: (0,
|
|
783
|
+
fileName: (0, import_node_path4.basename)(typesPath)
|
|
725
784
|
});
|
|
726
785
|
const wroteFile = (0, import_vite2.normalizePath)(typesPath);
|
|
727
786
|
wroteFiles.delete(wroteFile);
|
|
@@ -731,7 +790,7 @@ export default ${libName}
|
|
|
731
790
|
wroteFiles.add(wroteFile);
|
|
732
791
|
if (copyDtsFiles) {
|
|
733
792
|
await runParallel(import_os.default.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
734
|
-
const filePath = (0,
|
|
793
|
+
const filePath = (0, import_node_path4.resolve)(outputDir, (0, import_node_path4.basename)(path));
|
|
735
794
|
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
736
795
|
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
737
796
|
});
|
|
@@ -742,13 +801,15 @@ export default ${libName}
|
|
|
742
801
|
if (outputDirs.length > 1) {
|
|
743
802
|
const dirs = outputDirs.slice(1);
|
|
744
803
|
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
745
|
-
const relativePath = (0,
|
|
804
|
+
const relativePath = (0, import_node_path4.relative)(outputDir, wroteFile);
|
|
746
805
|
const content = await import_fs_extra.default.readFile(wroteFile, "utf-8");
|
|
747
|
-
await Promise.all(
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
806
|
+
await Promise.all(
|
|
807
|
+
dirs.map(async (dir) => {
|
|
808
|
+
const filePath = (0, import_node_path4.resolve)(dir, relativePath);
|
|
809
|
+
await import_fs_extra.default.mkdir((0, import_node_path4.dirname)(filePath), { recursive: true });
|
|
810
|
+
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
811
|
+
})
|
|
812
|
+
);
|
|
752
813
|
});
|
|
753
814
|
}
|
|
754
815
|
if (typeof afterBuild === "function") {
|
|
@@ -756,8 +817,10 @@ export default ${libName}
|
|
|
756
817
|
isPromise(result) && await result;
|
|
757
818
|
}
|
|
758
819
|
bundleDebug("finish");
|
|
759
|
-
logger.info(
|
|
760
|
-
|
|
820
|
+
logger.info(
|
|
821
|
+
import_chalk.default.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
822
|
+
`)
|
|
823
|
+
);
|
|
761
824
|
}
|
|
762
825
|
};
|
|
763
826
|
}
|
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 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";
|
|
11
11
|
import fs from "fs-extra";
|
|
12
12
|
import os from "os";
|
|
13
13
|
import chalk from "chalk";
|
|
@@ -18,12 +18,12 @@ import { normalizePath as normalizePath2 } from "vite";
|
|
|
18
18
|
import { readConfigFile } from "typescript";
|
|
19
19
|
|
|
20
20
|
// src/transform.ts
|
|
21
|
-
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "path";
|
|
21
|
+
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "node:path";
|
|
22
22
|
import { normalizePath } from "vite";
|
|
23
23
|
|
|
24
24
|
// src/utils.ts
|
|
25
|
-
import { resolve, isAbsolute, dirname, normalize, sep } from "path";
|
|
26
|
-
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";
|
|
27
27
|
function isNativeObj(value) {
|
|
28
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
29
29
|
}
|
|
@@ -163,20 +163,25 @@ var importTypesRE = /import\s?(?:type)?\s?\{(.+)\}\s?from\s?['"].+['"]/;
|
|
|
163
163
|
function transformDynamicImport(content) {
|
|
164
164
|
const importMap = /* @__PURE__ */ new Map();
|
|
165
165
|
content = content.replace(globalDynamicTypeRE, (str) => {
|
|
166
|
-
var _a;
|
|
167
166
|
const matchResult = str.match(dynamicTypeRE);
|
|
168
167
|
const libName = matchResult[1];
|
|
169
|
-
const importSet =
|
|
168
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
170
169
|
const usedType = matchResult[2];
|
|
171
170
|
importSet.add(usedType);
|
|
172
171
|
return usedType + matchResult[3];
|
|
173
172
|
});
|
|
174
173
|
importMap.forEach((importSet, libName) => {
|
|
175
|
-
const importReg = new RegExp(
|
|
174
|
+
const importReg = new RegExp(
|
|
175
|
+
`import\\s?(?:type)?\\s?\\{[^;\\n]+\\}\\s?from\\s?['"]${libName}['"]`,
|
|
176
|
+
"g"
|
|
177
|
+
);
|
|
176
178
|
const matchResult = content.match(importReg);
|
|
177
179
|
if (matchResult == null ? void 0 : matchResult[0]) {
|
|
178
180
|
const importedTypes = matchResult[0].match(importTypesRE)[1].trim().split(",");
|
|
179
|
-
content = content.replace(
|
|
181
|
+
content = content.replace(
|
|
182
|
+
matchResult[0],
|
|
183
|
+
`import type { ${Array.from(importSet).concat(importedTypes).join(", ")} } from '${libName}'`
|
|
184
|
+
);
|
|
180
185
|
} else {
|
|
181
186
|
content = `import type { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
182
187
|
` + content;
|
|
@@ -215,7 +220,13 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
215
220
|
return str;
|
|
216
221
|
}
|
|
217
222
|
const truthPath = isAbsolute2(matchedAlias.replacement) ? normalizePath(relative(dirname2(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
218
|
-
return str.replace(
|
|
223
|
+
return str.replace(
|
|
224
|
+
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
225
|
+
`$1'${matchResult[1].replace(
|
|
226
|
+
matchedAlias.find,
|
|
227
|
+
(truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : "")
|
|
228
|
+
)}'${isDynamic ? ")" : ""}`
|
|
229
|
+
);
|
|
219
230
|
}
|
|
220
231
|
}
|
|
221
232
|
return str;
|
|
@@ -311,7 +322,7 @@ function compileVueCode(code) {
|
|
|
311
322
|
id: `${index++}`
|
|
312
323
|
});
|
|
313
324
|
const classMatch = compiled.content.match(exportDefaultClassRE);
|
|
314
|
-
const plugins = scriptSetup.lang === "ts" ? ["typescript"] : void 0;
|
|
325
|
+
const plugins = scriptSetup.lang === "ts" ? ["typescript", "decorators-legacy"] : void 0;
|
|
315
326
|
if (classMatch) {
|
|
316
327
|
content = compiled.content.replace(exportDefaultClassRE, "\nclass $1") + `
|
|
317
328
|
const _sfc_main = ${classMatch[1]}`;
|
|
@@ -322,10 +333,18 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
322
333
|
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
323
334
|
}
|
|
324
335
|
content = transferSetupPosition(content);
|
|
336
|
+
content = content.replace(/(const __returned__\s?=\s?\{[\s\S]+?)(props)(\s?\})/, "$1props: props as any$3").replace(
|
|
337
|
+
/(const __returned__\s?=\s?\{[\s\S]+?)(props,)([\s\S]+?)/,
|
|
338
|
+
"$1props: props as any,$3"
|
|
339
|
+
);
|
|
325
340
|
content += "\nexport default _sfc_main\n";
|
|
326
341
|
ext = scriptSetup.lang || "js";
|
|
327
342
|
} else if (script && script.content) {
|
|
328
|
-
content = rewriteDefault(
|
|
343
|
+
content = rewriteDefault(
|
|
344
|
+
script.content,
|
|
345
|
+
"_sfc_main",
|
|
346
|
+
script.lang === "ts" ? ["typescript"] : void 0
|
|
347
|
+
);
|
|
329
348
|
content += "\nexport default _sfc_main\n";
|
|
330
349
|
ext = script.lang || "js";
|
|
331
350
|
}
|
|
@@ -337,7 +356,7 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
337
356
|
}
|
|
338
357
|
|
|
339
358
|
// src/rollup.ts
|
|
340
|
-
import { resolve as resolve2 } from "path";
|
|
359
|
+
import { resolve as resolve2 } from "node:path";
|
|
341
360
|
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
342
361
|
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
343
362
|
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|
|
@@ -417,7 +436,12 @@ function rollupDeclarationFiles({
|
|
|
417
436
|
extractorConfig
|
|
418
437
|
});
|
|
419
438
|
collector.analyze();
|
|
420
|
-
DtsRollupGenerator.writeTypingsFile(
|
|
439
|
+
DtsRollupGenerator.writeTypingsFile(
|
|
440
|
+
collector,
|
|
441
|
+
extractorConfig.publicTrimmedFilePath,
|
|
442
|
+
DtsRollupKind.PublicRelease,
|
|
443
|
+
extractorConfig.newlineKind
|
|
444
|
+
);
|
|
421
445
|
}
|
|
422
446
|
|
|
423
447
|
// src/plugin.ts
|
|
@@ -436,7 +460,6 @@ var noop = () => {
|
|
|
436
460
|
var logPrefix = chalk.cyan("[vite:dts]");
|
|
437
461
|
var bundleDebug = debug("vite-plugin-dts:bundle");
|
|
438
462
|
function dtsPlugin(options = {}) {
|
|
439
|
-
var _a, _b;
|
|
440
463
|
const {
|
|
441
464
|
tsConfigFilePath = "tsconfig.json",
|
|
442
465
|
aliasesExclude = [],
|
|
@@ -453,9 +476,9 @@ function dtsPlugin(options = {}) {
|
|
|
453
476
|
beforeWriteFile = noop,
|
|
454
477
|
afterBuild = noop
|
|
455
478
|
} = options;
|
|
456
|
-
const compilerOptions =
|
|
479
|
+
const compilerOptions = options.compilerOptions ?? {};
|
|
457
480
|
let root;
|
|
458
|
-
let entryRoot =
|
|
481
|
+
let entryRoot = options.entryRoot ?? "";
|
|
459
482
|
let libName;
|
|
460
483
|
let indexName;
|
|
461
484
|
let aliases;
|
|
@@ -473,10 +496,10 @@ function dtsPlugin(options = {}) {
|
|
|
473
496
|
apply: "build",
|
|
474
497
|
enforce: "pre",
|
|
475
498
|
config(config) {
|
|
476
|
-
var
|
|
499
|
+
var _a;
|
|
477
500
|
if (isBundle)
|
|
478
501
|
return;
|
|
479
|
-
const aliasOptions = (
|
|
502
|
+
const aliasOptions = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) ?? [];
|
|
480
503
|
if (isNativeObj(aliasOptions)) {
|
|
481
504
|
aliases = Object.entries(aliasOptions).map(([key, value]) => {
|
|
482
505
|
return { find: key, replacement: value };
|
|
@@ -485,38 +508,53 @@ function dtsPlugin(options = {}) {
|
|
|
485
508
|
aliases = ensureArray(aliasOptions);
|
|
486
509
|
}
|
|
487
510
|
if (aliasesExclude.length > 0) {
|
|
488
|
-
aliases = aliases.filter(
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
511
|
+
aliases = aliases.filter(
|
|
512
|
+
({ find }) => !aliasesExclude.some(
|
|
513
|
+
(alias) => {
|
|
514
|
+
var _a2;
|
|
515
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a2 = find.match(alias)) == null ? void 0 : _a2[0] : find === alias);
|
|
516
|
+
}
|
|
517
|
+
)
|
|
518
|
+
);
|
|
492
519
|
}
|
|
493
520
|
},
|
|
494
521
|
configResolved(config) {
|
|
495
|
-
var _a2, _b2, _c;
|
|
496
522
|
if (isBundle)
|
|
497
523
|
return;
|
|
498
524
|
logger = config.logger;
|
|
499
525
|
if (!config.build.lib) {
|
|
500
|
-
logger.warn(
|
|
501
|
-
|
|
502
|
-
`
|
|
526
|
+
logger.warn(
|
|
527
|
+
chalk.yellow(
|
|
528
|
+
`
|
|
529
|
+
${chalk.cyan(
|
|
530
|
+
"[vite:dts]"
|
|
531
|
+
)} You building not a library that may not need to generate declaration files.
|
|
532
|
+
`
|
|
533
|
+
)
|
|
534
|
+
);
|
|
503
535
|
libName = "_default";
|
|
504
536
|
indexName = defaultIndex;
|
|
505
537
|
} else {
|
|
506
|
-
const filename =
|
|
538
|
+
const filename = config.build.lib.fileName ?? defaultIndex;
|
|
507
539
|
libName = config.build.lib.name || "_default";
|
|
508
540
|
indexName = typeof filename === "string" ? filename : filename("es");
|
|
509
541
|
if (!dtsRE2.test(indexName)) {
|
|
510
542
|
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
511
543
|
}
|
|
512
544
|
}
|
|
513
|
-
root = ensureAbsolute(
|
|
545
|
+
root = ensureAbsolute(options.root ?? "", config.root);
|
|
514
546
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
515
547
|
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
516
|
-
if (!outputDirs) {
|
|
517
|
-
logger.error(
|
|
518
|
-
|
|
519
|
-
`
|
|
548
|
+
if (!outputDirs[0]) {
|
|
549
|
+
logger.error(
|
|
550
|
+
chalk.red(
|
|
551
|
+
`
|
|
552
|
+
${chalk.cyan(
|
|
553
|
+
"[vite:dts]"
|
|
554
|
+
)} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
555
|
+
`
|
|
556
|
+
)
|
|
557
|
+
);
|
|
520
558
|
return;
|
|
521
559
|
}
|
|
522
560
|
setCompileRoot(root);
|
|
@@ -534,7 +572,7 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
534
572
|
tsConfigFilePath: tsConfigPath,
|
|
535
573
|
skipAddingFilesFromTsConfig: true
|
|
536
574
|
});
|
|
537
|
-
allowJs =
|
|
575
|
+
allowJs = project.getCompilerOptions().allowJs ?? false;
|
|
538
576
|
},
|
|
539
577
|
buildStart(inputOptions) {
|
|
540
578
|
if (!isBundle && (insertTypesEntry || rollupTypes)) {
|
|
@@ -567,7 +605,6 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
567
605
|
}
|
|
568
606
|
},
|
|
569
607
|
async closeBundle() {
|
|
570
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
|
|
571
608
|
if (!outputDirs || !project || isBundle)
|
|
572
609
|
return;
|
|
573
610
|
logger.info(chalk.green(`
|
|
@@ -576,11 +613,11 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
576
613
|
isBundle = true;
|
|
577
614
|
sourceDtsFiles.clear();
|
|
578
615
|
const startTime = Date.now();
|
|
579
|
-
const tsConfig =
|
|
616
|
+
const tsConfig = readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
|
|
580
617
|
const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
|
|
581
618
|
const parentTsConfig = parentTsConfigPath ? readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
|
|
582
|
-
const include =
|
|
583
|
-
const exclude =
|
|
619
|
+
const include = options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*";
|
|
620
|
+
const exclude = options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**";
|
|
584
621
|
bundleDebug("read config");
|
|
585
622
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
586
623
|
if (include && include.length) {
|
|
@@ -602,13 +639,18 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
602
639
|
});
|
|
603
640
|
if (hasJsVue) {
|
|
604
641
|
if (!allowJs) {
|
|
605
|
-
logger.warn(
|
|
642
|
+
logger.warn(
|
|
643
|
+
chalk.yellow(
|
|
644
|
+
`${chalk.cyan(
|
|
645
|
+
"[vite:dts]"
|
|
646
|
+
)} Some js files are referenced, but you may not enable the 'allowJs' option.`
|
|
647
|
+
)
|
|
648
|
+
);
|
|
606
649
|
}
|
|
607
650
|
project.compilerOptions.set({ allowJs: true });
|
|
608
651
|
}
|
|
609
652
|
bundleDebug("collect files");
|
|
610
653
|
}
|
|
611
|
-
project.resolveSourceFileDependencies();
|
|
612
654
|
bundleDebug("resolve");
|
|
613
655
|
if (!skipDiagnostics) {
|
|
614
656
|
const diagnostics = project.getPreEmitDiagnostics();
|
|
@@ -626,10 +668,12 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
626
668
|
content: sourceFile.getFullText()
|
|
627
669
|
}));
|
|
628
670
|
const service = project.getLanguageService();
|
|
629
|
-
const outputFiles = project.getSourceFiles().map(
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
671
|
+
const outputFiles = project.getSourceFiles().map(
|
|
672
|
+
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
673
|
+
path: normalizePath2(resolve3(root, outputFile.compilerObject.name)),
|
|
674
|
+
content: outputFile.getText()
|
|
675
|
+
}))
|
|
676
|
+
).flat().concat(dtsOutputFiles);
|
|
633
677
|
bundleDebug("emit");
|
|
634
678
|
if (!entryRoot) {
|
|
635
679
|
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
@@ -638,7 +682,6 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
638
682
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
639
683
|
const outputDir = outputDirs[0];
|
|
640
684
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
641
|
-
var _a3, _b3;
|
|
642
685
|
let filePath = outputFile.path;
|
|
643
686
|
let content = outputFile.content;
|
|
644
687
|
const isMapFile = filePath.endsWith(".map");
|
|
@@ -650,16 +693,25 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
650
693
|
content = transformAliasImport(filePath, content, aliases, aliasesExclude);
|
|
651
694
|
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
652
695
|
}
|
|
653
|
-
filePath = resolve3(
|
|
696
|
+
filePath = resolve3(
|
|
697
|
+
outputDir,
|
|
698
|
+
relative2(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
699
|
+
);
|
|
654
700
|
if (typeof beforeWriteFile === "function") {
|
|
655
701
|
const result = beforeWriteFile(filePath, content);
|
|
702
|
+
if (result === false)
|
|
703
|
+
return;
|
|
656
704
|
if (result && isNativeObj(result)) {
|
|
657
|
-
filePath =
|
|
658
|
-
content =
|
|
705
|
+
filePath = result.filePath ?? filePath;
|
|
706
|
+
content = result.content ?? content;
|
|
659
707
|
}
|
|
660
708
|
}
|
|
661
709
|
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
662
|
-
await fs.writeFile(
|
|
710
|
+
await fs.writeFile(
|
|
711
|
+
filePath,
|
|
712
|
+
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
713
|
+
"utf-8"
|
|
714
|
+
);
|
|
663
715
|
wroteFiles.add(normalizePath2(filePath));
|
|
664
716
|
});
|
|
665
717
|
bundleDebug("output");
|
|
@@ -684,15 +736,18 @@ export default ${libName}
|
|
|
684
736
|
`;
|
|
685
737
|
}
|
|
686
738
|
}
|
|
739
|
+
let result;
|
|
687
740
|
if (typeof beforeWriteFile === "function") {
|
|
688
|
-
|
|
741
|
+
result = beforeWriteFile(typesPath, content);
|
|
689
742
|
if (result && isNativeObj(result)) {
|
|
690
|
-
typesPath =
|
|
691
|
-
content =
|
|
743
|
+
typesPath = result.filePath ?? typesPath;
|
|
744
|
+
content = result.content ?? content;
|
|
692
745
|
}
|
|
693
746
|
}
|
|
694
|
-
|
|
695
|
-
|
|
747
|
+
if (result !== false) {
|
|
748
|
+
await fs.writeFile(typesPath, content, "utf-8");
|
|
749
|
+
wroteFiles.add(normalizePath2(typesPath));
|
|
750
|
+
}
|
|
696
751
|
}
|
|
697
752
|
bundleDebug("insert index");
|
|
698
753
|
if (rollupTypes) {
|
|
@@ -726,11 +781,13 @@ export default ${libName}
|
|
|
726
781
|
await runParallel(os.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
727
782
|
const relativePath = relative2(outputDir, wroteFile);
|
|
728
783
|
const content = await fs.readFile(wroteFile, "utf-8");
|
|
729
|
-
await Promise.all(
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
784
|
+
await Promise.all(
|
|
785
|
+
dirs.map(async (dir) => {
|
|
786
|
+
const filePath = resolve3(dir, relativePath);
|
|
787
|
+
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
788
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
789
|
+
})
|
|
790
|
+
);
|
|
734
791
|
});
|
|
735
792
|
}
|
|
736
793
|
if (typeof afterBuild === "function") {
|
|
@@ -738,8 +795,10 @@ export default ${libName}
|
|
|
738
795
|
isPromise(result) && await result;
|
|
739
796
|
}
|
|
740
797
|
bundleDebug("finish");
|
|
741
|
-
logger.info(
|
|
742
|
-
|
|
798
|
+
logger.info(
|
|
799
|
+
chalk.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
800
|
+
`)
|
|
801
|
+
);
|
|
743
802
|
}
|
|
744
803
|
};
|
|
745
804
|
}
|
package/package.json
CHANGED
|
@@ -6,51 +6,52 @@
|
|
|
6
6
|
"url": "https://github.com/qmhc/vite-plugin-dts/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@microsoft/api-extractor": "^7.
|
|
10
|
-
"@rushstack/node-core-library": "^3.
|
|
9
|
+
"@microsoft/api-extractor": "^7.31.2",
|
|
10
|
+
"@rushstack/node-core-library": "^3.52.0",
|
|
11
11
|
"chalk": "^4.1.2",
|
|
12
12
|
"debug": "^4.3.4",
|
|
13
|
-
"fast-glob": "^3.2.
|
|
14
|
-
"fs-extra": "^10.0
|
|
15
|
-
"ts-morph": "^
|
|
13
|
+
"fast-glob": "^3.2.12",
|
|
14
|
+
"fs-extra": "^10.1.0",
|
|
15
|
+
"ts-morph": "^16.0.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@commitlint/cli": "^
|
|
19
|
-
"@commitlint/config-conventional": "^
|
|
18
|
+
"@commitlint/cli": "^17.1.2",
|
|
19
|
+
"@commitlint/config-conventional": "^17.1.0",
|
|
20
20
|
"@types/debug": "^4.1.7",
|
|
21
21
|
"@types/fs-extra": "^9.0.13",
|
|
22
|
-
"@types/jest": "^
|
|
23
|
-
"@
|
|
24
|
-
"@typescript-eslint/
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"@vue/eslint-config-
|
|
28
|
-
"
|
|
22
|
+
"@types/jest": "^29.0.3",
|
|
23
|
+
"@types/node": "^18.7.23",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
|
25
|
+
"@typescript-eslint/parser": "^5.38.1",
|
|
26
|
+
"@vitejs/plugin-vue": "^3.1.0",
|
|
27
|
+
"@vue/eslint-config-standard": "^8.0.1",
|
|
28
|
+
"@vue/eslint-config-typescript": "^11.0.2",
|
|
29
|
+
"commitizen": "^4.2.5",
|
|
29
30
|
"conventional-changelog-cli": "^2.2.2",
|
|
30
31
|
"cross-env": "^7.0.3",
|
|
31
|
-
"cz-customizable": "^
|
|
32
|
+
"cz-customizable": "^7.0.0",
|
|
32
33
|
"enquirer": "^2.3.6",
|
|
33
|
-
"eslint": "^8.
|
|
34
|
-
"eslint-plugin-import": "^2.
|
|
34
|
+
"eslint": "^8.24.0",
|
|
35
|
+
"eslint-plugin-import": "^2.26.0",
|
|
35
36
|
"eslint-plugin-node": "^11.1.0",
|
|
36
|
-
"eslint-plugin-promise": "^6.0.
|
|
37
|
-
"eslint-plugin-vue": "^
|
|
37
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
38
|
+
"eslint-plugin-vue": "^9.5.1",
|
|
38
39
|
"execa": "^5.1.1",
|
|
39
|
-
"husky": "^
|
|
40
|
+
"husky": "^8.0.1",
|
|
40
41
|
"is-ci": "^3.0.1",
|
|
41
|
-
"jest": "^
|
|
42
|
-
"lint-staged": "^
|
|
43
|
-
"minimist": "^1.2.
|
|
42
|
+
"jest": "^29.0.3",
|
|
43
|
+
"lint-staged": "^13.0.3",
|
|
44
|
+
"minimist": "^1.2.6",
|
|
44
45
|
"pinst": "^3.0.0",
|
|
45
|
-
"prettier": "^2.
|
|
46
|
+
"prettier": "^2.7.1",
|
|
46
47
|
"pretty-quick": "^3.1.3",
|
|
47
48
|
"rimraf": "^3.0.2",
|
|
48
|
-
"semver": "^7.3.
|
|
49
|
-
"ts-jest": "^
|
|
50
|
-
"tsup": "^
|
|
51
|
-
"typescript": "4.
|
|
52
|
-
"vite": "^
|
|
53
|
-
"vue": "3.2.
|
|
49
|
+
"semver": "^7.3.7",
|
|
50
|
+
"ts-jest": "^29.0.2",
|
|
51
|
+
"tsup": "^6.2.3",
|
|
52
|
+
"typescript": "4.7.4",
|
|
53
|
+
"vite": "^3.1.3",
|
|
54
|
+
"vue": "3.2.40"
|
|
54
55
|
},
|
|
55
56
|
"engines": {
|
|
56
57
|
"node": ">=12.0.0"
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
"module": "dist/index.mjs",
|
|
71
72
|
"name": "vite-plugin-dts",
|
|
72
73
|
"peerDependencies": {
|
|
73
|
-
"vite": ">=
|
|
74
|
+
"vite": ">=3.0.0"
|
|
74
75
|
},
|
|
75
76
|
"repository": {
|
|
76
77
|
"type": "git",
|
|
@@ -85,13 +86,13 @@
|
|
|
85
86
|
"lint:example": "eslint --fix --ext .js,.jsx,.ts,.tsx,.vue example/{src,components}/**",
|
|
86
87
|
"_postinstall": "is-ci || husky install",
|
|
87
88
|
"postpublish": "pinst --enable",
|
|
88
|
-
"precommit": "lint-staged -c ./.husky/.lintstagedrc",
|
|
89
|
+
"precommit": "lint-staged -c ./.husky/.lintstagedrc -q",
|
|
89
90
|
"prepublishOnly": "pinst --disable",
|
|
90
91
|
"prettier": "pretty-quick --staged",
|
|
91
92
|
"release": "node scripts/release.js",
|
|
92
93
|
"test": "jest --runInBand",
|
|
93
|
-
"test:e2e": "
|
|
94
|
+
"test:e2e": "pnpm -C example build"
|
|
94
95
|
},
|
|
95
96
|
"types": "dist/index.d.ts",
|
|
96
|
-
"version": "1.
|
|
97
|
+
"version": "1.6.1"
|
|
97
98
|
}
|