vite-plugin-dts 3.7.3 → 3.8.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/LICENSE +21 -21
- package/dist/index.cjs +187 -112
- package/dist/index.mjs +186 -112
- package/package.json +22 -21
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021-present qmhc
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present qmhc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -13,11 +13,13 @@ const vueTsc = require('vue-tsc');
|
|
|
13
13
|
const debug = require('debug');
|
|
14
14
|
const kolorist = require('kolorist');
|
|
15
15
|
const apiExtractor = require('@microsoft/api-extractor');
|
|
16
|
+
const MagicString = require('magic-string');
|
|
16
17
|
|
|
17
18
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
18
19
|
|
|
19
20
|
const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
|
|
20
21
|
const debug__default = /*#__PURE__*/_interopDefaultCompat(debug);
|
|
22
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
21
23
|
|
|
22
24
|
const windowsSlashRE = /\\+/g;
|
|
23
25
|
function slash(p) {
|
|
@@ -38,7 +40,7 @@ function isRegExp(value) {
|
|
|
38
40
|
function isPromise(value) {
|
|
39
41
|
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
40
42
|
}
|
|
41
|
-
async function
|
|
43
|
+
async function unwrapPromise(maybePromise) {
|
|
42
44
|
return isPromise(maybePromise) ? await maybePromise : maybePromise;
|
|
43
45
|
}
|
|
44
46
|
function ensureAbsolute(path, root) {
|
|
@@ -415,46 +417,13 @@ function normalizeGlob(path) {
|
|
|
415
417
|
}
|
|
416
418
|
return path;
|
|
417
419
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const importMap = /* @__PURE__ */ new Map();
|
|
423
|
-
const defaultMap = /* @__PURE__ */ new Map();
|
|
424
|
-
let defaultCount = 1;
|
|
425
|
-
content = content.replace(globalDynamicTypeRE, (str) => {
|
|
426
|
-
const matchResult = str.match(dynamicTypeRE);
|
|
427
|
-
const libName = matchResult[1];
|
|
428
|
-
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
429
|
-
let usedType = matchResult[2];
|
|
430
|
-
if (usedType === "default") {
|
|
431
|
-
usedType = defaultMap.get(libName) ?? defaultMap.set(libName, `__DTS_${defaultCount++}__`).get(libName);
|
|
432
|
-
importSet.add(`default as ${usedType}`);
|
|
433
|
-
} else {
|
|
434
|
-
importSet.add(usedType);
|
|
435
|
-
}
|
|
436
|
-
return usedType + matchResult[3];
|
|
437
|
-
});
|
|
438
|
-
importMap.forEach((importSet, libName) => {
|
|
439
|
-
const importReg = new RegExp(
|
|
440
|
-
`import\\s?(?:type)?\\s?\\{[^;\\n]+\\}\\s?from\\s?['"]${libName}['"]`,
|
|
441
|
-
"g"
|
|
442
|
-
);
|
|
443
|
-
const matchResult = content.match(importReg);
|
|
444
|
-
if (matchResult?.[0]) {
|
|
445
|
-
matchResult[0].match(importTypesRE)[1].trim().split(",").forEach((type) => {
|
|
446
|
-
type && importSet.add(type.trim());
|
|
447
|
-
});
|
|
448
|
-
content = content.replace(
|
|
449
|
-
matchResult[0],
|
|
450
|
-
`import { ${Array.from(importSet).join(", ")} } from '${libName}'`
|
|
451
|
-
);
|
|
452
|
-
} else {
|
|
453
|
-
content = `import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
454
|
-
` + content;
|
|
420
|
+
function walkSourceFile(sourceFile, callback) {
|
|
421
|
+
function walkNode(node, parent, callback2) {
|
|
422
|
+
if (callback2(node, parent) !== false) {
|
|
423
|
+
node.forEachChild((child) => walkNode(child, node, callback2));
|
|
455
424
|
}
|
|
456
|
-
}
|
|
457
|
-
|
|
425
|
+
}
|
|
426
|
+
sourceFile.forEachChild((child) => walkNode(child, sourceFile, callback));
|
|
458
427
|
}
|
|
459
428
|
function isAliasMatch(alias, importer) {
|
|
460
429
|
if (isRegExp(alias.find))
|
|
@@ -465,54 +434,136 @@ function isAliasMatch(alias, importer) {
|
|
|
465
434
|
return true;
|
|
466
435
|
return importer.indexOf(alias.find) === 0 && (alias.find.endsWith("/") || importer.substring(alias.find.length)[0] === "/");
|
|
467
436
|
}
|
|
468
|
-
function
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
const
|
|
473
|
-
const
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
let matchResult = str.match(staticImportRE);
|
|
481
|
-
let isDynamic = false;
|
|
482
|
-
if (!matchResult) {
|
|
483
|
-
matchResult = str.match(dynamicImportRE);
|
|
484
|
-
isDynamic = true;
|
|
437
|
+
function transformAlias(importer, dir, aliases, aliasesExclude) {
|
|
438
|
+
if (aliases.length && !aliasesExclude.some((e) => isRegExp(e) ? e.test(importer) : String(e) === importer)) {
|
|
439
|
+
const matchedAlias = aliases.find((alias) => isAliasMatch(alias, importer));
|
|
440
|
+
if (matchedAlias) {
|
|
441
|
+
const replacement = node_path.isAbsolute(matchedAlias.replacement) ? normalizePath(node_path.relative(dir, matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
442
|
+
const endsWithSlash = typeof matchedAlias.find === "string" ? matchedAlias.find.endsWith("/") : importer.match(matchedAlias.find)[0].endsWith("/");
|
|
443
|
+
const truthPath = importer.replace(
|
|
444
|
+
matchedAlias.find,
|
|
445
|
+
replacement + (endsWithSlash ? "/" : "")
|
|
446
|
+
);
|
|
447
|
+
const normalizedPath = normalizePath(node_path.relative(dir, node_path.resolve(dir, truthPath)));
|
|
448
|
+
return normalizedPath.startsWith(".") ? normalizedPath : `./${normalizedPath}`;
|
|
485
449
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
450
|
+
}
|
|
451
|
+
return importer;
|
|
452
|
+
}
|
|
453
|
+
function transformCode(options) {
|
|
454
|
+
const s = new MagicString__default(options.content);
|
|
455
|
+
const ast = ts__default.createSourceFile("a.ts", options.content, ts__default.ScriptTarget.Latest);
|
|
456
|
+
const dir = node_path.dirname(options.filePath);
|
|
457
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
458
|
+
const usedDefault = /* @__PURE__ */ new Map();
|
|
459
|
+
const declareModules = [];
|
|
460
|
+
let indexCount = 0;
|
|
461
|
+
walkSourceFile(ast, (node, parent) => {
|
|
462
|
+
if (ts__default.isImportDeclaration(node)) {
|
|
463
|
+
if (!node.importClause) {
|
|
464
|
+
options.clearPureImport && s.remove(node.pos, node.end);
|
|
465
|
+
} else if (ts__default.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts__default.isNamedImports(node.importClause.namedBindings))) {
|
|
466
|
+
const libName = transformAlias(
|
|
467
|
+
node.moduleSpecifier.text,
|
|
468
|
+
dir,
|
|
469
|
+
options.aliases,
|
|
470
|
+
options.aliasesExclude
|
|
503
471
|
);
|
|
472
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
473
|
+
if (node.importClause.name && !usedDefault.has(libName)) {
|
|
474
|
+
const usedType = node.importClause.name.escapedText;
|
|
475
|
+
usedDefault.set(libName, usedType);
|
|
476
|
+
importSet.add(`default as ${usedType}`);
|
|
477
|
+
}
|
|
478
|
+
if (node.importClause.namedBindings && ts__default.isNamedImports(node.importClause.namedBindings)) {
|
|
479
|
+
node.importClause.namedBindings.elements.forEach((element) => {
|
|
480
|
+
if (element.propertyName) {
|
|
481
|
+
importSet.add(`${element.propertyName.escapedText} as ${element.name.escapedText}`);
|
|
482
|
+
} else {
|
|
483
|
+
importSet.add(element.name.escapedText);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
s.remove(node.pos, node.end);
|
|
488
|
+
}
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
if (ts__default.isImportTypeNode(node) && node.qualifier && ts__default.isLiteralTypeNode(node.argument) && ts__default.isIdentifier(node.qualifier) && ts__default.isStringLiteral(node.argument.literal)) {
|
|
492
|
+
const libName = transformAlias(
|
|
493
|
+
node.argument.literal.text,
|
|
494
|
+
dir,
|
|
495
|
+
options.aliases,
|
|
496
|
+
options.aliasesExclude
|
|
497
|
+
);
|
|
498
|
+
if (!options.staticImport) {
|
|
499
|
+
s.update(node.argument.literal.pos, node.argument.literal.end, `'${libName}'`);
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
503
|
+
let usedType = node.qualifier.escapedText;
|
|
504
|
+
if (usedType === "default") {
|
|
505
|
+
usedType = usedDefault.get(libName) ?? usedDefault.set(libName, `__DTS_DEFAULT_${indexCount++}__`).get(libName);
|
|
506
|
+
importSet.add(`default as ${usedType}`);
|
|
507
|
+
s.update(node.qualifier.pos, node.qualifier.end, usedType);
|
|
508
|
+
} else {
|
|
509
|
+
importSet.add(usedType);
|
|
504
510
|
}
|
|
511
|
+
if (ts__default.isImportTypeNode(parent) && parent.typeArguments && parent.typeArguments[0] === node) {
|
|
512
|
+
s.remove(node.pos, node.argument.end + 2);
|
|
513
|
+
} else {
|
|
514
|
+
s.update(node.pos, node.argument.end + 2, " ");
|
|
515
|
+
}
|
|
516
|
+
return !!node.typeArguments;
|
|
517
|
+
}
|
|
518
|
+
if (ts__default.isCallExpression(node) && node.expression.kind === ts__default.SyntaxKind.ImportKeyword && ts__default.isStringLiteral(node.arguments[0])) {
|
|
519
|
+
const libName = transformAlias(
|
|
520
|
+
node.arguments[0].text,
|
|
521
|
+
dir,
|
|
522
|
+
options.aliases,
|
|
523
|
+
options.aliasesExclude
|
|
524
|
+
);
|
|
525
|
+
s.update(node.arguments[0].pos, node.arguments[0].end, `'${libName}'`);
|
|
526
|
+
return false;
|
|
527
|
+
}
|
|
528
|
+
if (ts__default.isExportDeclaration(node) && node.moduleSpecifier && ts__default.isStringLiteral(node.moduleSpecifier)) {
|
|
529
|
+
const libName = transformAlias(
|
|
530
|
+
node.moduleSpecifier.text,
|
|
531
|
+
dir,
|
|
532
|
+
options.aliases,
|
|
533
|
+
options.aliasesExclude
|
|
534
|
+
);
|
|
535
|
+
s.update(node.moduleSpecifier.pos, node.moduleSpecifier.end, ` '${libName}'`);
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
if (ts__default.isModuleDeclaration(node)) {
|
|
539
|
+
declareModules.push(s.slice(node.pos, node.end + 1));
|
|
505
540
|
}
|
|
506
|
-
return str;
|
|
507
541
|
});
|
|
542
|
+
importMap.forEach((importSet, libName) => {
|
|
543
|
+
s.prepend(`import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
544
|
+
`);
|
|
545
|
+
});
|
|
546
|
+
return {
|
|
547
|
+
content: s.toString(),
|
|
548
|
+
declareModules
|
|
549
|
+
};
|
|
508
550
|
}
|
|
509
|
-
const pureImportRE = /^import\s?['"][^;\n]+?['"];?\n?/g;
|
|
510
|
-
function removePureImport(content) {
|
|
511
|
-
return content.replace(pureImportRE, "");
|
|
512
|
-
}
|
|
513
|
-
const asDefaultRE = /export\s*\{.*\w+\s*\bas\s+default\b.*\}\s*from\s*['"].+['"]/;
|
|
514
551
|
function hasExportDefault(content) {
|
|
515
|
-
|
|
552
|
+
const ast = ts__default.createSourceFile("a.ts", content, ts__default.ScriptTarget.Latest);
|
|
553
|
+
let has = false;
|
|
554
|
+
walkSourceFile(ast, (node) => {
|
|
555
|
+
if (ts__default.isExportAssignment(node)) {
|
|
556
|
+
has = true;
|
|
557
|
+
} else if (ts__default.isExportDeclaration(node) && node.exportClause && ts__default.isNamedExports(node.exportClause)) {
|
|
558
|
+
for (const element of node.exportClause.elements) {
|
|
559
|
+
if (element.name.escapedText === "default") {
|
|
560
|
+
has = true;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return false;
|
|
565
|
+
});
|
|
566
|
+
return has;
|
|
516
567
|
}
|
|
517
568
|
|
|
518
569
|
const jsRE = /\.(m|c)?jsx?$/;
|
|
@@ -523,7 +574,8 @@ const mtjsRE = /\.m(t|j)sx?$/;
|
|
|
523
574
|
const ctjsRE = /\.c(t|j)sx?$/;
|
|
524
575
|
const fullRelativeRE = /^\.\.?\//;
|
|
525
576
|
const defaultIndex = "index.d.ts";
|
|
526
|
-
const
|
|
577
|
+
const pluginName = "vite:dts";
|
|
578
|
+
const logPrefix = kolorist.cyan(`[${pluginName}]`);
|
|
527
579
|
const bundleDebug = debug__default("vite-plugin-dts:bundle");
|
|
528
580
|
const fixedCompilerOptions = {
|
|
529
581
|
noEmit: false,
|
|
@@ -539,6 +591,7 @@ const fixedCompilerOptions = {
|
|
|
539
591
|
const noop = () => {
|
|
540
592
|
};
|
|
541
593
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
594
|
+
const tsToDts = (path) => `${path.replace(tsRE, "")}.d.ts`;
|
|
542
595
|
const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
|
|
543
596
|
const asteriskRE = /[*]+/g;
|
|
544
597
|
function dtsPlugin(options = {}) {
|
|
@@ -589,8 +642,11 @@ function dtsPlugin(options = {}) {
|
|
|
589
642
|
const outputFiles = /* @__PURE__ */ new Map();
|
|
590
643
|
const rollupConfig = { ...options.rollupConfig || {} };
|
|
591
644
|
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [];
|
|
645
|
+
const cleanPath = (path) => {
|
|
646
|
+
return cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
647
|
+
};
|
|
592
648
|
return {
|
|
593
|
-
name:
|
|
649
|
+
name: pluginName,
|
|
594
650
|
apply: "build",
|
|
595
651
|
enforce: "pre",
|
|
596
652
|
config(config) {
|
|
@@ -745,12 +801,16 @@ ${logPrefix} ${kolorist.yellow(
|
|
|
745
801
|
publicRoot = normalizePath(publicRoot);
|
|
746
802
|
entryRoot = entryRoot || publicRoot;
|
|
747
803
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
748
|
-
const diagnostics =
|
|
804
|
+
const diagnostics = [
|
|
805
|
+
...program.getDeclarationDiagnostics(),
|
|
806
|
+
...program.getSemanticDiagnostics(),
|
|
807
|
+
...program.getSyntacticDiagnostics()
|
|
808
|
+
];
|
|
749
809
|
if (diagnostics?.length) {
|
|
750
810
|
logger.error(ts__default.formatDiagnosticsWithColorAndContext(diagnostics, host));
|
|
751
811
|
}
|
|
752
812
|
if (typeof afterDiagnostic === "function") {
|
|
753
|
-
await
|
|
813
|
+
await unwrapPromise(afterDiagnostic(diagnostics));
|
|
754
814
|
}
|
|
755
815
|
rootNames.forEach((file) => {
|
|
756
816
|
this.addWatchFile(file);
|
|
@@ -826,9 +886,10 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
826
886
|
const startTime = Date.now();
|
|
827
887
|
const outDir = outDirs[0];
|
|
828
888
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
889
|
+
const declareModules = [];
|
|
829
890
|
const writeOutput = async (path, content, outDir2, record = true) => {
|
|
830
891
|
if (typeof beforeWriteFile === "function") {
|
|
831
|
-
const result = await
|
|
892
|
+
const result = await unwrapPromise(beforeWriteFile(path, content));
|
|
832
893
|
if (result === false)
|
|
833
894
|
return;
|
|
834
895
|
if (result) {
|
|
@@ -872,17 +933,27 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
872
933
|
await runParallel(
|
|
873
934
|
node_os.cpus().length,
|
|
874
935
|
Array.from(outputFiles.entries()),
|
|
875
|
-
async ([
|
|
876
|
-
const isMapFile =
|
|
877
|
-
const baseDir = node_path.dirname(
|
|
936
|
+
async ([filePath, content]) => {
|
|
937
|
+
const isMapFile = filePath.endsWith(".map");
|
|
938
|
+
const baseDir = node_path.dirname(filePath);
|
|
878
939
|
if (!isMapFile && content) {
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
940
|
+
const result = transformCode({
|
|
941
|
+
filePath,
|
|
942
|
+
content,
|
|
943
|
+
aliases,
|
|
944
|
+
aliasesExclude,
|
|
945
|
+
staticImport,
|
|
946
|
+
clearPureImport
|
|
947
|
+
});
|
|
948
|
+
content = result.content;
|
|
949
|
+
declareModules.push(...result.declareModules);
|
|
882
950
|
}
|
|
883
|
-
|
|
951
|
+
filePath = resolve(
|
|
884
952
|
outDir,
|
|
885
|
-
node_path.relative(
|
|
953
|
+
node_path.relative(
|
|
954
|
+
entryRoot,
|
|
955
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
956
|
+
)
|
|
886
957
|
);
|
|
887
958
|
content = cleanVueFileName ? content.replace(vuePathRE, '"$1"') : content;
|
|
888
959
|
if (isMapFile) {
|
|
@@ -891,17 +962,17 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
891
962
|
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
892
963
|
return normalizePath(
|
|
893
964
|
node_path.relative(
|
|
894
|
-
node_path.dirname(
|
|
965
|
+
node_path.dirname(filePath),
|
|
895
966
|
resolve(currentDir, node_path.relative(publicRoot, baseDir), source)
|
|
896
967
|
)
|
|
897
968
|
);
|
|
898
969
|
});
|
|
899
970
|
content = JSON.stringify(sourceMap);
|
|
900
971
|
} catch (e) {
|
|
901
|
-
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${
|
|
972
|
+
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${filePath}`);
|
|
902
973
|
}
|
|
903
974
|
}
|
|
904
|
-
await writeOutput(
|
|
975
|
+
await writeOutput(filePath, content, outDir);
|
|
905
976
|
}
|
|
906
977
|
);
|
|
907
978
|
bundleDebug("write output");
|
|
@@ -915,9 +986,6 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
915
986
|
const entryNames = Object.keys(entries);
|
|
916
987
|
const types = findTypesPath(pkg.publishConfig, pkg);
|
|
917
988
|
const multiple = entryNames.length > 1;
|
|
918
|
-
const cleanPath = (path) => {
|
|
919
|
-
return cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
920
|
-
};
|
|
921
989
|
let typesPath = cleanPath(types ? resolve(root, types) : resolve(outDir, indexName));
|
|
922
990
|
if (!multiple && !dtsRE.test(typesPath)) {
|
|
923
991
|
logger.warn(
|
|
@@ -930,23 +998,23 @@ ${logPrefix} ${kolorist.yellow(
|
|
|
930
998
|
typesPath = `${typesPath.replace(tjsRE, "")}.d.${extPrefix(typesPath)}ts`;
|
|
931
999
|
}
|
|
932
1000
|
for (const name of entryNames) {
|
|
933
|
-
const
|
|
934
|
-
if (node_fs.existsSync(
|
|
1001
|
+
const entryDtsPath = multiple ? cleanPath(resolve(outDir, tsToDts(name))) : typesPath;
|
|
1002
|
+
if (node_fs.existsSync(entryDtsPath))
|
|
935
1003
|
continue;
|
|
936
|
-
const
|
|
937
|
-
resolve(outDir, node_path.relative(entryRoot,
|
|
1004
|
+
const sourceEntry = normalizePath(
|
|
1005
|
+
cleanPath(resolve(outDir, node_path.relative(entryRoot, tsToDts(entries[name]))))
|
|
938
1006
|
);
|
|
939
|
-
let fromPath = normalizePath(node_path.relative(node_path.dirname(
|
|
1007
|
+
let fromPath = normalizePath(node_path.relative(node_path.dirname(entryDtsPath), sourceEntry));
|
|
940
1008
|
fromPath = fromPath.replace(dtsRE, "");
|
|
941
1009
|
fromPath = fullRelativeRE.test(fromPath) ? fromPath : `./${fromPath}`;
|
|
942
1010
|
let content = `export * from '${fromPath}'
|
|
943
1011
|
`;
|
|
944
|
-
if (
|
|
1012
|
+
if (emittedFiles.has(sourceEntry) && hasExportDefault(emittedFiles.get(sourceEntry))) {
|
|
945
1013
|
content += `import ${libName} from '${fromPath}'
|
|
946
1014
|
export default ${libName}
|
|
947
1015
|
`;
|
|
948
1016
|
}
|
|
949
|
-
await writeOutput(cleanPath(
|
|
1017
|
+
await writeOutput(cleanPath(entryDtsPath), content, outDir);
|
|
950
1018
|
}
|
|
951
1019
|
bundleDebug("insert index");
|
|
952
1020
|
if (rollupTypes) {
|
|
@@ -964,7 +1032,7 @@ export default ${libName}
|
|
|
964
1032
|
const compilerOptions2 = configPath ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions;
|
|
965
1033
|
if (multiple) {
|
|
966
1034
|
for (const name of entryNames) {
|
|
967
|
-
const path = cleanPath(resolve(outDir,
|
|
1035
|
+
const path = cleanPath(resolve(outDir, tsToDts(name)));
|
|
968
1036
|
rollupDeclarationFiles({
|
|
969
1037
|
root,
|
|
970
1038
|
configPath,
|
|
@@ -997,9 +1065,16 @@ export default ${libName}
|
|
|
997
1065
|
await runParallel(node_os.cpus().length, Array.from(emittedFiles.keys()), (f) => promises.unlink(f));
|
|
998
1066
|
removeDirIfEmpty(outDir);
|
|
999
1067
|
emittedFiles.clear();
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1068
|
+
const declared = declareModules.join("\n");
|
|
1069
|
+
await runParallel(node_os.cpus().length, [...rollupFiles], async (filePath) => {
|
|
1070
|
+
await writeOutput(
|
|
1071
|
+
filePath,
|
|
1072
|
+
await promises.readFile(filePath, "utf-8") + (declared ? `
|
|
1073
|
+
${declared}
|
|
1074
|
+
` : ""),
|
|
1075
|
+
node_path.dirname(filePath)
|
|
1076
|
+
);
|
|
1077
|
+
});
|
|
1003
1078
|
bundleDebug("rollup output");
|
|
1004
1079
|
}
|
|
1005
1080
|
}
|
|
@@ -1021,7 +1096,7 @@ export default ${libName}
|
|
|
1021
1096
|
});
|
|
1022
1097
|
}
|
|
1023
1098
|
if (typeof afterBuild === "function") {
|
|
1024
|
-
await
|
|
1099
|
+
await unwrapPromise(afterBuild(emittedFiles));
|
|
1025
1100
|
}
|
|
1026
1101
|
bundleDebug("finish");
|
|
1027
1102
|
logger.info(
|
package/dist/index.mjs
CHANGED
|
@@ -16,6 +16,7 @@ import { createProgram } from 'vue-tsc';
|
|
|
16
16
|
import debug from 'debug';
|
|
17
17
|
import { cyan, yellow, green } from 'kolorist';
|
|
18
18
|
import { ExtractorConfig, Extractor } from '@microsoft/api-extractor';
|
|
19
|
+
import MagicString from 'magic-string';
|
|
19
20
|
|
|
20
21
|
const windowsSlashRE = /\\+/g;
|
|
21
22
|
function slash(p) {
|
|
@@ -36,7 +37,7 @@ function isRegExp(value) {
|
|
|
36
37
|
function isPromise(value) {
|
|
37
38
|
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
38
39
|
}
|
|
39
|
-
async function
|
|
40
|
+
async function unwrapPromise(maybePromise) {
|
|
40
41
|
return isPromise(maybePromise) ? await maybePromise : maybePromise;
|
|
41
42
|
}
|
|
42
43
|
function ensureAbsolute(path, root) {
|
|
@@ -413,46 +414,13 @@ function normalizeGlob(path) {
|
|
|
413
414
|
}
|
|
414
415
|
return path;
|
|
415
416
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
const importMap = /* @__PURE__ */ new Map();
|
|
421
|
-
const defaultMap = /* @__PURE__ */ new Map();
|
|
422
|
-
let defaultCount = 1;
|
|
423
|
-
content = content.replace(globalDynamicTypeRE, (str) => {
|
|
424
|
-
const matchResult = str.match(dynamicTypeRE);
|
|
425
|
-
const libName = matchResult[1];
|
|
426
|
-
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
427
|
-
let usedType = matchResult[2];
|
|
428
|
-
if (usedType === "default") {
|
|
429
|
-
usedType = defaultMap.get(libName) ?? defaultMap.set(libName, `__DTS_${defaultCount++}__`).get(libName);
|
|
430
|
-
importSet.add(`default as ${usedType}`);
|
|
431
|
-
} else {
|
|
432
|
-
importSet.add(usedType);
|
|
433
|
-
}
|
|
434
|
-
return usedType + matchResult[3];
|
|
435
|
-
});
|
|
436
|
-
importMap.forEach((importSet, libName) => {
|
|
437
|
-
const importReg = new RegExp(
|
|
438
|
-
`import\\s?(?:type)?\\s?\\{[^;\\n]+\\}\\s?from\\s?['"]${libName}['"]`,
|
|
439
|
-
"g"
|
|
440
|
-
);
|
|
441
|
-
const matchResult = content.match(importReg);
|
|
442
|
-
if (matchResult?.[0]) {
|
|
443
|
-
matchResult[0].match(importTypesRE)[1].trim().split(",").forEach((type) => {
|
|
444
|
-
type && importSet.add(type.trim());
|
|
445
|
-
});
|
|
446
|
-
content = content.replace(
|
|
447
|
-
matchResult[0],
|
|
448
|
-
`import { ${Array.from(importSet).join(", ")} } from '${libName}'`
|
|
449
|
-
);
|
|
450
|
-
} else {
|
|
451
|
-
content = `import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
452
|
-
` + content;
|
|
417
|
+
function walkSourceFile(sourceFile, callback) {
|
|
418
|
+
function walkNode(node, parent, callback2) {
|
|
419
|
+
if (callback2(node, parent) !== false) {
|
|
420
|
+
node.forEachChild((child) => walkNode(child, node, callback2));
|
|
453
421
|
}
|
|
454
|
-
}
|
|
455
|
-
|
|
422
|
+
}
|
|
423
|
+
sourceFile.forEachChild((child) => walkNode(child, sourceFile, callback));
|
|
456
424
|
}
|
|
457
425
|
function isAliasMatch(alias, importer) {
|
|
458
426
|
if (isRegExp(alias.find))
|
|
@@ -463,54 +431,136 @@ function isAliasMatch(alias, importer) {
|
|
|
463
431
|
return true;
|
|
464
432
|
return importer.indexOf(alias.find) === 0 && (alias.find.endsWith("/") || importer.substring(alias.find.length)[0] === "/");
|
|
465
433
|
}
|
|
466
|
-
function
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
const
|
|
471
|
-
const
|
|
472
|
-
const
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
let matchResult = str.match(staticImportRE);
|
|
479
|
-
let isDynamic = false;
|
|
480
|
-
if (!matchResult) {
|
|
481
|
-
matchResult = str.match(dynamicImportRE);
|
|
482
|
-
isDynamic = true;
|
|
434
|
+
function transformAlias(importer, dir, aliases, aliasesExclude) {
|
|
435
|
+
if (aliases.length && !aliasesExclude.some((e) => isRegExp(e) ? e.test(importer) : String(e) === importer)) {
|
|
436
|
+
const matchedAlias = aliases.find((alias) => isAliasMatch(alias, importer));
|
|
437
|
+
if (matchedAlias) {
|
|
438
|
+
const replacement = isAbsolute(matchedAlias.replacement) ? normalizePath(relative(dir, matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
439
|
+
const endsWithSlash = typeof matchedAlias.find === "string" ? matchedAlias.find.endsWith("/") : importer.match(matchedAlias.find)[0].endsWith("/");
|
|
440
|
+
const truthPath = importer.replace(
|
|
441
|
+
matchedAlias.find,
|
|
442
|
+
replacement + (endsWithSlash ? "/" : "")
|
|
443
|
+
);
|
|
444
|
+
const normalizedPath = normalizePath(relative(dir, resolve$1(dir, truthPath)));
|
|
445
|
+
return normalizedPath.startsWith(".") ? normalizedPath : `./${normalizedPath}`;
|
|
483
446
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
447
|
+
}
|
|
448
|
+
return importer;
|
|
449
|
+
}
|
|
450
|
+
function transformCode(options) {
|
|
451
|
+
const s = new MagicString(options.content);
|
|
452
|
+
const ast = ts.createSourceFile("a.ts", options.content, ts.ScriptTarget.Latest);
|
|
453
|
+
const dir = dirname(options.filePath);
|
|
454
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
455
|
+
const usedDefault = /* @__PURE__ */ new Map();
|
|
456
|
+
const declareModules = [];
|
|
457
|
+
let indexCount = 0;
|
|
458
|
+
walkSourceFile(ast, (node, parent) => {
|
|
459
|
+
if (ts.isImportDeclaration(node)) {
|
|
460
|
+
if (!node.importClause) {
|
|
461
|
+
options.clearPureImport && s.remove(node.pos, node.end);
|
|
462
|
+
} else if (ts.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings))) {
|
|
463
|
+
const libName = transformAlias(
|
|
464
|
+
node.moduleSpecifier.text,
|
|
465
|
+
dir,
|
|
466
|
+
options.aliases,
|
|
467
|
+
options.aliasesExclude
|
|
501
468
|
);
|
|
469
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
470
|
+
if (node.importClause.name && !usedDefault.has(libName)) {
|
|
471
|
+
const usedType = node.importClause.name.escapedText;
|
|
472
|
+
usedDefault.set(libName, usedType);
|
|
473
|
+
importSet.add(`default as ${usedType}`);
|
|
474
|
+
}
|
|
475
|
+
if (node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings)) {
|
|
476
|
+
node.importClause.namedBindings.elements.forEach((element) => {
|
|
477
|
+
if (element.propertyName) {
|
|
478
|
+
importSet.add(`${element.propertyName.escapedText} as ${element.name.escapedText}`);
|
|
479
|
+
} else {
|
|
480
|
+
importSet.add(element.name.escapedText);
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
s.remove(node.pos, node.end);
|
|
485
|
+
}
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
if (ts.isImportTypeNode(node) && node.qualifier && ts.isLiteralTypeNode(node.argument) && ts.isIdentifier(node.qualifier) && ts.isStringLiteral(node.argument.literal)) {
|
|
489
|
+
const libName = transformAlias(
|
|
490
|
+
node.argument.literal.text,
|
|
491
|
+
dir,
|
|
492
|
+
options.aliases,
|
|
493
|
+
options.aliasesExclude
|
|
494
|
+
);
|
|
495
|
+
if (!options.staticImport) {
|
|
496
|
+
s.update(node.argument.literal.pos, node.argument.literal.end, `'${libName}'`);
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
500
|
+
let usedType = node.qualifier.escapedText;
|
|
501
|
+
if (usedType === "default") {
|
|
502
|
+
usedType = usedDefault.get(libName) ?? usedDefault.set(libName, `__DTS_DEFAULT_${indexCount++}__`).get(libName);
|
|
503
|
+
importSet.add(`default as ${usedType}`);
|
|
504
|
+
s.update(node.qualifier.pos, node.qualifier.end, usedType);
|
|
505
|
+
} else {
|
|
506
|
+
importSet.add(usedType);
|
|
502
507
|
}
|
|
508
|
+
if (ts.isImportTypeNode(parent) && parent.typeArguments && parent.typeArguments[0] === node) {
|
|
509
|
+
s.remove(node.pos, node.argument.end + 2);
|
|
510
|
+
} else {
|
|
511
|
+
s.update(node.pos, node.argument.end + 2, " ");
|
|
512
|
+
}
|
|
513
|
+
return !!node.typeArguments;
|
|
514
|
+
}
|
|
515
|
+
if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword && ts.isStringLiteral(node.arguments[0])) {
|
|
516
|
+
const libName = transformAlias(
|
|
517
|
+
node.arguments[0].text,
|
|
518
|
+
dir,
|
|
519
|
+
options.aliases,
|
|
520
|
+
options.aliasesExclude
|
|
521
|
+
);
|
|
522
|
+
s.update(node.arguments[0].pos, node.arguments[0].end, `'${libName}'`);
|
|
523
|
+
return false;
|
|
524
|
+
}
|
|
525
|
+
if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
526
|
+
const libName = transformAlias(
|
|
527
|
+
node.moduleSpecifier.text,
|
|
528
|
+
dir,
|
|
529
|
+
options.aliases,
|
|
530
|
+
options.aliasesExclude
|
|
531
|
+
);
|
|
532
|
+
s.update(node.moduleSpecifier.pos, node.moduleSpecifier.end, ` '${libName}'`);
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
if (ts.isModuleDeclaration(node)) {
|
|
536
|
+
declareModules.push(s.slice(node.pos, node.end + 1));
|
|
503
537
|
}
|
|
504
|
-
return str;
|
|
505
538
|
});
|
|
539
|
+
importMap.forEach((importSet, libName) => {
|
|
540
|
+
s.prepend(`import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
541
|
+
`);
|
|
542
|
+
});
|
|
543
|
+
return {
|
|
544
|
+
content: s.toString(),
|
|
545
|
+
declareModules
|
|
546
|
+
};
|
|
506
547
|
}
|
|
507
|
-
const pureImportRE = /^import\s?['"][^;\n]+?['"];?\n?/g;
|
|
508
|
-
function removePureImport(content) {
|
|
509
|
-
return content.replace(pureImportRE, "");
|
|
510
|
-
}
|
|
511
|
-
const asDefaultRE = /export\s*\{.*\w+\s*\bas\s+default\b.*\}\s*from\s*['"].+['"]/;
|
|
512
548
|
function hasExportDefault(content) {
|
|
513
|
-
|
|
549
|
+
const ast = ts.createSourceFile("a.ts", content, ts.ScriptTarget.Latest);
|
|
550
|
+
let has = false;
|
|
551
|
+
walkSourceFile(ast, (node) => {
|
|
552
|
+
if (ts.isExportAssignment(node)) {
|
|
553
|
+
has = true;
|
|
554
|
+
} else if (ts.isExportDeclaration(node) && node.exportClause && ts.isNamedExports(node.exportClause)) {
|
|
555
|
+
for (const element of node.exportClause.elements) {
|
|
556
|
+
if (element.name.escapedText === "default") {
|
|
557
|
+
has = true;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return false;
|
|
562
|
+
});
|
|
563
|
+
return has;
|
|
514
564
|
}
|
|
515
565
|
|
|
516
566
|
const jsRE = /\.(m|c)?jsx?$/;
|
|
@@ -521,7 +571,8 @@ const mtjsRE = /\.m(t|j)sx?$/;
|
|
|
521
571
|
const ctjsRE = /\.c(t|j)sx?$/;
|
|
522
572
|
const fullRelativeRE = /^\.\.?\//;
|
|
523
573
|
const defaultIndex = "index.d.ts";
|
|
524
|
-
const
|
|
574
|
+
const pluginName = "vite:dts";
|
|
575
|
+
const logPrefix = cyan(`[${pluginName}]`);
|
|
525
576
|
const bundleDebug = debug("vite-plugin-dts:bundle");
|
|
526
577
|
const fixedCompilerOptions = {
|
|
527
578
|
noEmit: false,
|
|
@@ -537,6 +588,7 @@ const fixedCompilerOptions = {
|
|
|
537
588
|
const noop = () => {
|
|
538
589
|
};
|
|
539
590
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
591
|
+
const tsToDts = (path) => `${path.replace(tsRE, "")}.d.ts`;
|
|
540
592
|
const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
|
|
541
593
|
const asteriskRE = /[*]+/g;
|
|
542
594
|
function dtsPlugin(options = {}) {
|
|
@@ -587,8 +639,11 @@ function dtsPlugin(options = {}) {
|
|
|
587
639
|
const outputFiles = /* @__PURE__ */ new Map();
|
|
588
640
|
const rollupConfig = { ...options.rollupConfig || {} };
|
|
589
641
|
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [];
|
|
642
|
+
const cleanPath = (path) => {
|
|
643
|
+
return cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
644
|
+
};
|
|
590
645
|
return {
|
|
591
|
-
name:
|
|
646
|
+
name: pluginName,
|
|
592
647
|
apply: "build",
|
|
593
648
|
enforce: "pre",
|
|
594
649
|
config(config) {
|
|
@@ -743,12 +798,16 @@ ${logPrefix} ${yellow(
|
|
|
743
798
|
publicRoot = normalizePath(publicRoot);
|
|
744
799
|
entryRoot = entryRoot || publicRoot;
|
|
745
800
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
746
|
-
const diagnostics =
|
|
801
|
+
const diagnostics = [
|
|
802
|
+
...program.getDeclarationDiagnostics(),
|
|
803
|
+
...program.getSemanticDiagnostics(),
|
|
804
|
+
...program.getSyntacticDiagnostics()
|
|
805
|
+
];
|
|
747
806
|
if (diagnostics?.length) {
|
|
748
807
|
logger.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, host));
|
|
749
808
|
}
|
|
750
809
|
if (typeof afterDiagnostic === "function") {
|
|
751
|
-
await
|
|
810
|
+
await unwrapPromise(afterDiagnostic(diagnostics));
|
|
752
811
|
}
|
|
753
812
|
rootNames.forEach((file) => {
|
|
754
813
|
this.addWatchFile(file);
|
|
@@ -824,9 +883,10 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
824
883
|
const startTime = Date.now();
|
|
825
884
|
const outDir = outDirs[0];
|
|
826
885
|
const emittedFiles = /* @__PURE__ */ new Map();
|
|
886
|
+
const declareModules = [];
|
|
827
887
|
const writeOutput = async (path, content, outDir2, record = true) => {
|
|
828
888
|
if (typeof beforeWriteFile === "function") {
|
|
829
|
-
const result = await
|
|
889
|
+
const result = await unwrapPromise(beforeWriteFile(path, content));
|
|
830
890
|
if (result === false)
|
|
831
891
|
return;
|
|
832
892
|
if (result) {
|
|
@@ -870,17 +930,27 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
870
930
|
await runParallel(
|
|
871
931
|
cpus().length,
|
|
872
932
|
Array.from(outputFiles.entries()),
|
|
873
|
-
async ([
|
|
874
|
-
const isMapFile =
|
|
875
|
-
const baseDir = dirname(
|
|
933
|
+
async ([filePath, content]) => {
|
|
934
|
+
const isMapFile = filePath.endsWith(".map");
|
|
935
|
+
const baseDir = dirname(filePath);
|
|
876
936
|
if (!isMapFile && content) {
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
937
|
+
const result = transformCode({
|
|
938
|
+
filePath,
|
|
939
|
+
content,
|
|
940
|
+
aliases,
|
|
941
|
+
aliasesExclude,
|
|
942
|
+
staticImport,
|
|
943
|
+
clearPureImport
|
|
944
|
+
});
|
|
945
|
+
content = result.content;
|
|
946
|
+
declareModules.push(...result.declareModules);
|
|
880
947
|
}
|
|
881
|
-
|
|
948
|
+
filePath = resolve(
|
|
882
949
|
outDir,
|
|
883
|
-
relative(
|
|
950
|
+
relative(
|
|
951
|
+
entryRoot,
|
|
952
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
953
|
+
)
|
|
884
954
|
);
|
|
885
955
|
content = cleanVueFileName ? content.replace(vuePathRE, '"$1"') : content;
|
|
886
956
|
if (isMapFile) {
|
|
@@ -889,17 +959,17 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
889
959
|
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
890
960
|
return normalizePath(
|
|
891
961
|
relative(
|
|
892
|
-
dirname(
|
|
962
|
+
dirname(filePath),
|
|
893
963
|
resolve(currentDir, relative(publicRoot, baseDir), source)
|
|
894
964
|
)
|
|
895
965
|
);
|
|
896
966
|
});
|
|
897
967
|
content = JSON.stringify(sourceMap);
|
|
898
968
|
} catch (e) {
|
|
899
|
-
logger.warn(`${logPrefix} ${yellow("Processing source map fail:")} ${
|
|
969
|
+
logger.warn(`${logPrefix} ${yellow("Processing source map fail:")} ${filePath}`);
|
|
900
970
|
}
|
|
901
971
|
}
|
|
902
|
-
await writeOutput(
|
|
972
|
+
await writeOutput(filePath, content, outDir);
|
|
903
973
|
}
|
|
904
974
|
);
|
|
905
975
|
bundleDebug("write output");
|
|
@@ -913,9 +983,6 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
913
983
|
const entryNames = Object.keys(entries);
|
|
914
984
|
const types = findTypesPath(pkg.publishConfig, pkg);
|
|
915
985
|
const multiple = entryNames.length > 1;
|
|
916
|
-
const cleanPath = (path) => {
|
|
917
|
-
return cleanVueFileName ? path.replace(".vue.d.ts", ".d.ts") : path;
|
|
918
|
-
};
|
|
919
986
|
let typesPath = cleanPath(types ? resolve(root, types) : resolve(outDir, indexName));
|
|
920
987
|
if (!multiple && !dtsRE.test(typesPath)) {
|
|
921
988
|
logger.warn(
|
|
@@ -928,23 +995,23 @@ ${logPrefix} ${yellow(
|
|
|
928
995
|
typesPath = `${typesPath.replace(tjsRE, "")}.d.${extPrefix(typesPath)}ts`;
|
|
929
996
|
}
|
|
930
997
|
for (const name of entryNames) {
|
|
931
|
-
const
|
|
932
|
-
if (existsSync(
|
|
998
|
+
const entryDtsPath = multiple ? cleanPath(resolve(outDir, tsToDts(name))) : typesPath;
|
|
999
|
+
if (existsSync(entryDtsPath))
|
|
933
1000
|
continue;
|
|
934
|
-
const
|
|
935
|
-
resolve(outDir, relative(entryRoot,
|
|
1001
|
+
const sourceEntry = normalizePath(
|
|
1002
|
+
cleanPath(resolve(outDir, relative(entryRoot, tsToDts(entries[name]))))
|
|
936
1003
|
);
|
|
937
|
-
let fromPath = normalizePath(relative(dirname(
|
|
1004
|
+
let fromPath = normalizePath(relative(dirname(entryDtsPath), sourceEntry));
|
|
938
1005
|
fromPath = fromPath.replace(dtsRE, "");
|
|
939
1006
|
fromPath = fullRelativeRE.test(fromPath) ? fromPath : `./${fromPath}`;
|
|
940
1007
|
let content = `export * from '${fromPath}'
|
|
941
1008
|
`;
|
|
942
|
-
if (
|
|
1009
|
+
if (emittedFiles.has(sourceEntry) && hasExportDefault(emittedFiles.get(sourceEntry))) {
|
|
943
1010
|
content += `import ${libName} from '${fromPath}'
|
|
944
1011
|
export default ${libName}
|
|
945
1012
|
`;
|
|
946
1013
|
}
|
|
947
|
-
await writeOutput(cleanPath(
|
|
1014
|
+
await writeOutput(cleanPath(entryDtsPath), content, outDir);
|
|
948
1015
|
}
|
|
949
1016
|
bundleDebug("insert index");
|
|
950
1017
|
if (rollupTypes) {
|
|
@@ -962,7 +1029,7 @@ export default ${libName}
|
|
|
962
1029
|
const compilerOptions2 = configPath ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions;
|
|
963
1030
|
if (multiple) {
|
|
964
1031
|
for (const name of entryNames) {
|
|
965
|
-
const path = cleanPath(resolve(outDir,
|
|
1032
|
+
const path = cleanPath(resolve(outDir, tsToDts(name)));
|
|
966
1033
|
rollupDeclarationFiles({
|
|
967
1034
|
root,
|
|
968
1035
|
configPath,
|
|
@@ -995,9 +1062,16 @@ export default ${libName}
|
|
|
995
1062
|
await runParallel(cpus().length, Array.from(emittedFiles.keys()), (f) => unlink(f));
|
|
996
1063
|
removeDirIfEmpty(outDir);
|
|
997
1064
|
emittedFiles.clear();
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1065
|
+
const declared = declareModules.join("\n");
|
|
1066
|
+
await runParallel(cpus().length, [...rollupFiles], async (filePath) => {
|
|
1067
|
+
await writeOutput(
|
|
1068
|
+
filePath,
|
|
1069
|
+
await readFile(filePath, "utf-8") + (declared ? `
|
|
1070
|
+
${declared}
|
|
1071
|
+
` : ""),
|
|
1072
|
+
dirname(filePath)
|
|
1073
|
+
);
|
|
1074
|
+
});
|
|
1001
1075
|
bundleDebug("rollup output");
|
|
1002
1076
|
}
|
|
1003
1077
|
}
|
|
@@ -1019,7 +1093,7 @@ export default ${libName}
|
|
|
1019
1093
|
});
|
|
1020
1094
|
}
|
|
1021
1095
|
if (typeof afterBuild === "function") {
|
|
1022
|
-
await
|
|
1096
|
+
await unwrapPromise(afterBuild(emittedFiles));
|
|
1023
1097
|
}
|
|
1024
1098
|
bundleDebug("finish");
|
|
1025
1099
|
logger.info(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-dts",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "qmhc",
|
|
@@ -58,43 +58,44 @@
|
|
|
58
58
|
"volar"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@microsoft/api-extractor": "7.
|
|
61
|
+
"@microsoft/api-extractor": "7.43.0",
|
|
62
62
|
"@rollup/pluginutils": "^5.1.0",
|
|
63
|
-
"@vue/language-core": "^1.8.
|
|
63
|
+
"@vue/language-core": "^1.8.27",
|
|
64
64
|
"debug": "^4.3.4",
|
|
65
65
|
"kolorist": "^1.8.0",
|
|
66
|
-
"
|
|
66
|
+
"magic-string": "^0.30.8",
|
|
67
|
+
"vue-tsc": "^1.8.27"
|
|
67
68
|
},
|
|
68
69
|
"devDependencies": {
|
|
69
|
-
"@commitlint/cli": "^
|
|
70
|
+
"@commitlint/cli": "^19.2.1",
|
|
70
71
|
"@types/debug": "^4.1.12",
|
|
71
72
|
"@types/minimist": "^1.2.5",
|
|
72
|
-
"@types/node": "^20.
|
|
73
|
+
"@types/node": "^20.11.30",
|
|
73
74
|
"@types/prompts": "^2.4.9",
|
|
74
|
-
"@types/semver": "^7.5.
|
|
75
|
-
"@vexip-ui/commitlint-config": "^0.
|
|
76
|
-
"@vexip-ui/eslint-config": "^0.
|
|
75
|
+
"@types/semver": "^7.5.8",
|
|
76
|
+
"@vexip-ui/commitlint-config": "^0.4.0",
|
|
77
|
+
"@vexip-ui/eslint-config": "^0.12.0",
|
|
77
78
|
"@vexip-ui/prettier-config": "^0.2.0",
|
|
78
79
|
"@vue/eslint-config-standard": "^8.0.1",
|
|
79
|
-
"@vue/eslint-config-typescript": "^
|
|
80
|
+
"@vue/eslint-config-typescript": "^13.0.0",
|
|
80
81
|
"conventional-changelog-cli": "^4.1.0",
|
|
81
|
-
"eslint": "^8.
|
|
82
|
+
"eslint": "^8.57.0",
|
|
82
83
|
"execa": "^8.0.1",
|
|
83
|
-
"husky": "^
|
|
84
|
+
"husky": "^9.0.11",
|
|
84
85
|
"is-ci": "^3.0.1",
|
|
85
|
-
"lint-staged": "^15.2.
|
|
86
|
+
"lint-staged": "^15.2.2",
|
|
86
87
|
"minimist": "^1.2.8",
|
|
87
88
|
"pinst": "^3.0.0",
|
|
88
|
-
"prettier": "^3.
|
|
89
|
-
"pretty-quick": "^
|
|
89
|
+
"prettier": "^3.2.5",
|
|
90
|
+
"pretty-quick": "^4.0.0",
|
|
90
91
|
"prompts": "^2.4.2",
|
|
91
92
|
"rimraf": "^5.0.5",
|
|
92
|
-
"semver": "^7.
|
|
93
|
-
"tsx": "^4.7.
|
|
94
|
-
"typescript": "5.
|
|
93
|
+
"semver": "^7.6.0",
|
|
94
|
+
"tsx": "^4.7.1",
|
|
95
|
+
"typescript": "5.4.3",
|
|
95
96
|
"unbuild": "^2.0.0",
|
|
96
|
-
"vite": "^5.
|
|
97
|
-
"vitest": "^1.
|
|
97
|
+
"vite": "^5.2.6",
|
|
98
|
+
"vitest": "^1.4.0"
|
|
98
99
|
},
|
|
99
100
|
"peerDependencies": {
|
|
100
101
|
"typescript": "*",
|
|
@@ -107,7 +108,7 @@
|
|
|
107
108
|
},
|
|
108
109
|
"pnpm": {
|
|
109
110
|
"patchedDependencies": {
|
|
110
|
-
"@microsoft/api-extractor@7.
|
|
111
|
+
"@microsoft/api-extractor@7.43.0": "patches/@microsoft__api-extractor@7.43.0.patch"
|
|
111
112
|
}
|
|
112
113
|
}
|
|
113
114
|
}
|