vite-plugin-dts 4.0.0-beta.2 → 4.0.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/README.md +408 -408
- package/README.zh-CN.md +408 -408
- package/dist/index.cjs +115 -39
- package/dist/index.mjs +114 -39
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -14,17 +14,19 @@ const pluginutils = require('@rollup/pluginutils');
|
|
|
14
14
|
const debug = require('debug');
|
|
15
15
|
const kolorist = require('kolorist');
|
|
16
16
|
const apiExtractor = require('@microsoft/api-extractor');
|
|
17
|
+
const node_module = require('node:module');
|
|
17
18
|
const localPkg = require('local-pkg');
|
|
18
19
|
const compareVersions = require('compare-versions');
|
|
19
20
|
const MagicString = require('magic-string');
|
|
20
21
|
|
|
22
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
21
23
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
22
24
|
|
|
23
25
|
const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
|
|
24
26
|
const debug__default = /*#__PURE__*/_interopDefaultCompat(debug);
|
|
25
27
|
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
26
28
|
|
|
27
|
-
const
|
|
29
|
+
const _createProgram = typescript.proxyCreateProgram(ts__default, ts__default.createProgram, (ts2, options) => {
|
|
28
30
|
const { configFilePath } = options.options;
|
|
29
31
|
const vueOptions = typeof configFilePath === "string" ? languageCore.createParsedCommandLine(ts2, ts2.sys, configFilePath.replace(/\\/g, "/")).vueOptions : languageCore.resolveVueCompilerOptions({});
|
|
30
32
|
if (options.host) {
|
|
@@ -46,6 +48,34 @@ const createProgram = typescript.proxyCreateProgram(ts__default, ts__default.cre
|
|
|
46
48
|
);
|
|
47
49
|
return [vueLanguagePlugin];
|
|
48
50
|
});
|
|
51
|
+
const createProgram = (options) => {
|
|
52
|
+
const program = _createProgram(options);
|
|
53
|
+
const emit = program.emit;
|
|
54
|
+
program.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
|
|
55
|
+
if (writeFile) {
|
|
56
|
+
return emit(
|
|
57
|
+
targetSourceFile,
|
|
58
|
+
(fileName, data, writeByteOrderMark, onError, sourceFiles) => {
|
|
59
|
+
if (fileName.endsWith(".d.ts")) {
|
|
60
|
+
data = vueTsc.removeEmitGlobalTypes(data);
|
|
61
|
+
}
|
|
62
|
+
return writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
|
|
63
|
+
},
|
|
64
|
+
cancellationToken,
|
|
65
|
+
emitOnlyDtsFiles,
|
|
66
|
+
customTransformers
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return emit(
|
|
70
|
+
targetSourceFile,
|
|
71
|
+
writeFile,
|
|
72
|
+
cancellationToken,
|
|
73
|
+
emitOnlyDtsFiles,
|
|
74
|
+
customTransformers
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
return program;
|
|
78
|
+
};
|
|
49
79
|
|
|
50
80
|
const windowsSlashRE = /\\+/g;
|
|
51
81
|
function slash(p) {
|
|
@@ -170,6 +200,23 @@ function getTsConfig(tsConfigPath, readFileSync) {
|
|
|
170
200
|
Object.assign(tsConfig.compilerOptions, baseConfig.compilerOptions);
|
|
171
201
|
return tsConfig;
|
|
172
202
|
}
|
|
203
|
+
function getTsLibFolder({ root, entryRoot }) {
|
|
204
|
+
let libFolder;
|
|
205
|
+
try {
|
|
206
|
+
libFolder = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).resolve("typescript").replace(/node_modules\/typescript.*/, "node_modules/typescript");
|
|
207
|
+
} catch {
|
|
208
|
+
libFolder = resolve(root, "node_modules/typescript");
|
|
209
|
+
if (!node_fs.existsSync(libFolder)) {
|
|
210
|
+
if (root !== entryRoot) {
|
|
211
|
+
libFolder = resolve(entryRoot, "node_modules/typescript");
|
|
212
|
+
if (!node_fs.existsSync(libFolder))
|
|
213
|
+
libFolder = void 0;
|
|
214
|
+
}
|
|
215
|
+
libFolder = void 0;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return libFolder;
|
|
219
|
+
}
|
|
173
220
|
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
174
221
|
function base64Encode(number) {
|
|
175
222
|
if (number >= 0 && number < BASE64_ALPHABET.length) {
|
|
@@ -308,6 +355,9 @@ function rollupDeclarationFiles({
|
|
|
308
355
|
if (!dtsRE$1.test(fileName)) {
|
|
309
356
|
fileName += ".d.ts";
|
|
310
357
|
}
|
|
358
|
+
if (/preserve/i.test(compilerOptions.module)) {
|
|
359
|
+
compilerOptions = { ...compilerOptions, module: "ESNext" };
|
|
360
|
+
}
|
|
311
361
|
const extractorConfig = apiExtractor.ExtractorConfig.prepare({
|
|
312
362
|
configObject: {
|
|
313
363
|
...rollupConfig,
|
|
@@ -433,7 +483,7 @@ function VueResolver() {
|
|
|
433
483
|
program.emit(
|
|
434
484
|
sourceFile,
|
|
435
485
|
(path, content) => {
|
|
436
|
-
outputs.push({ path, content });
|
|
486
|
+
outputs.push({ path, content: vueTsc.removeEmitGlobalTypes(content) });
|
|
437
487
|
},
|
|
438
488
|
void 0,
|
|
439
489
|
true
|
|
@@ -524,10 +574,12 @@ function transformCode(options) {
|
|
|
524
574
|
return options.cleanVueFileName ? name.replace(/\.vue$/, "") : name;
|
|
525
575
|
};
|
|
526
576
|
let indexCount = 0;
|
|
577
|
+
let importCount = 0;
|
|
527
578
|
walkSourceFile(ast, (node, parent) => {
|
|
528
579
|
if (ts__default.isImportDeclaration(node)) {
|
|
529
580
|
if (!node.importClause) {
|
|
530
581
|
options.clearPureImport && s.remove(node.pos, node.end);
|
|
582
|
+
++importCount;
|
|
531
583
|
} else if (ts__default.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts__default.isNamedImports(node.importClause.namedBindings))) {
|
|
532
584
|
const libName = toLibName(node.moduleSpecifier.text);
|
|
533
585
|
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
@@ -546,6 +598,7 @@ function transformCode(options) {
|
|
|
546
598
|
});
|
|
547
599
|
}
|
|
548
600
|
s.remove(node.pos, node.end);
|
|
601
|
+
++importCount;
|
|
549
602
|
}
|
|
550
603
|
return false;
|
|
551
604
|
}
|
|
@@ -601,22 +654,27 @@ function transformCode(options) {
|
|
|
601
654
|
prependImports += `import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
602
655
|
`;
|
|
603
656
|
});
|
|
604
|
-
s.prepend(prependImports);
|
|
657
|
+
s.trimStart("\n").prepend(prependImports);
|
|
605
658
|
return {
|
|
606
659
|
content: s.toString(),
|
|
607
|
-
declareModules
|
|
660
|
+
declareModules,
|
|
661
|
+
diffLineCount: importMap.size && importCount < importMap.size ? importMap.size - importCount : null
|
|
608
662
|
};
|
|
609
663
|
}
|
|
610
|
-
function
|
|
664
|
+
function hasNormalExport(content) {
|
|
611
665
|
const ast = ts__default.createSourceFile("a.ts", content, ts__default.ScriptTarget.Latest);
|
|
612
666
|
let has = false;
|
|
613
667
|
walkSourceFile(ast, (node) => {
|
|
614
|
-
if (ts__default.isExportDeclaration(node)
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
668
|
+
if (ts__default.isExportDeclaration(node)) {
|
|
669
|
+
if (node.exportClause && ts__default.isNamedExports(node.exportClause)) {
|
|
670
|
+
for (const element of node.exportClause.elements) {
|
|
671
|
+
if (element.name.escapedText !== "default") {
|
|
672
|
+
has = true;
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
619
675
|
}
|
|
676
|
+
} else {
|
|
677
|
+
has = true;
|
|
620
678
|
}
|
|
621
679
|
} else if ("modifiers" in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
|
|
622
680
|
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
|
|
@@ -1023,13 +1081,28 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1023
1081
|
}
|
|
1024
1082
|
bundleDebug("emit output patch");
|
|
1025
1083
|
const currentDir = host.getCurrentDirectory();
|
|
1084
|
+
const declarationFiles = /* @__PURE__ */ new Map();
|
|
1085
|
+
const mapFiles = /* @__PURE__ */ new Map();
|
|
1086
|
+
const prependMappings = /* @__PURE__ */ new Map();
|
|
1087
|
+
for (const [filePath, content] of outputFiles.entries()) {
|
|
1088
|
+
if (filePath.endsWith(".map")) {
|
|
1089
|
+
mapFiles.set(filePath, content);
|
|
1090
|
+
} else {
|
|
1091
|
+
declarationFiles.set(filePath, content);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1026
1094
|
await runParallel(
|
|
1027
1095
|
node_os.cpus().length,
|
|
1028
|
-
Array.from(
|
|
1096
|
+
Array.from(declarationFiles.entries()),
|
|
1029
1097
|
async ([filePath, content]) => {
|
|
1030
|
-
const
|
|
1031
|
-
|
|
1032
|
-
|
|
1098
|
+
const newFilePath = resolve(
|
|
1099
|
+
outDir,
|
|
1100
|
+
node_path.relative(
|
|
1101
|
+
entryRoot,
|
|
1102
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1103
|
+
)
|
|
1104
|
+
);
|
|
1105
|
+
if (content) {
|
|
1033
1106
|
const result = transformCode({
|
|
1034
1107
|
filePath,
|
|
1035
1108
|
content,
|
|
@@ -1041,7 +1114,18 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1041
1114
|
});
|
|
1042
1115
|
content = result.content;
|
|
1043
1116
|
declareModules.push(...result.declareModules);
|
|
1117
|
+
if (result.diffLineCount) {
|
|
1118
|
+
prependMappings.set(`${newFilePath}.map`, ";".repeat(result.diffLineCount));
|
|
1119
|
+
}
|
|
1044
1120
|
}
|
|
1121
|
+
await writeOutput(newFilePath, content, outDir);
|
|
1122
|
+
}
|
|
1123
|
+
);
|
|
1124
|
+
await runParallel(
|
|
1125
|
+
node_os.cpus().length,
|
|
1126
|
+
Array.from(mapFiles.entries()),
|
|
1127
|
+
async ([filePath, content]) => {
|
|
1128
|
+
const baseDir = node_path.dirname(filePath);
|
|
1045
1129
|
filePath = resolve(
|
|
1046
1130
|
outDir,
|
|
1047
1131
|
node_path.relative(
|
|
@@ -1049,21 +1133,22 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1049
1133
|
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1050
1134
|
)
|
|
1051
1135
|
);
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
node_path.
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
} catch (e) {
|
|
1065
|
-
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${filePath}`);
|
|
1136
|
+
try {
|
|
1137
|
+
const sourceMap = JSON.parse(content);
|
|
1138
|
+
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
1139
|
+
return normalizePath(
|
|
1140
|
+
node_path.relative(
|
|
1141
|
+
node_path.dirname(filePath),
|
|
1142
|
+
resolve(currentDir, node_path.relative(publicRoot, baseDir), source)
|
|
1143
|
+
)
|
|
1144
|
+
);
|
|
1145
|
+
});
|
|
1146
|
+
if (prependMappings.has(filePath)) {
|
|
1147
|
+
sourceMap.mappings = `${prependMappings.get(filePath)}${sourceMap.mappings}`;
|
|
1066
1148
|
}
|
|
1149
|
+
content = JSON.stringify(sourceMap);
|
|
1150
|
+
} catch (e) {
|
|
1151
|
+
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${filePath}`);
|
|
1067
1152
|
}
|
|
1068
1153
|
await writeOutput(filePath, content, outDir);
|
|
1069
1154
|
}
|
|
@@ -1102,7 +1187,7 @@ ${logPrefix} ${kolorist.yellow(
|
|
|
1102
1187
|
fromPath = fullRelativeRE.test(fromPath) ? fromPath : `./${fromPath}`;
|
|
1103
1188
|
let content = "";
|
|
1104
1189
|
if (emittedFiles.has(sourceEntry)) {
|
|
1105
|
-
if (
|
|
1190
|
+
if (hasNormalExport(emittedFiles.get(sourceEntry))) {
|
|
1106
1191
|
content += `export * from '${fromPath}'
|
|
1107
1192
|
`;
|
|
1108
1193
|
}
|
|
@@ -1117,15 +1202,6 @@ export default ${libName}
|
|
|
1117
1202
|
bundleDebug("insert index");
|
|
1118
1203
|
if (rollupTypes) {
|
|
1119
1204
|
logger.info(kolorist.green(`${logPrefix} Start rollup declaration files...`));
|
|
1120
|
-
let libFolder = resolve(root, "node_modules/typescript");
|
|
1121
|
-
if (!node_fs.existsSync(libFolder)) {
|
|
1122
|
-
if (root !== entryRoot) {
|
|
1123
|
-
libFolder = resolve(entryRoot, "node_modules/typescript");
|
|
1124
|
-
if (!node_fs.existsSync(libFolder))
|
|
1125
|
-
libFolder = void 0;
|
|
1126
|
-
}
|
|
1127
|
-
libFolder = void 0;
|
|
1128
|
-
}
|
|
1129
1205
|
const rollupFiles = /* @__PURE__ */ new Set();
|
|
1130
1206
|
const compilerOptions2 = configPath ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions;
|
|
1131
1207
|
const rollup = async (path) => {
|
|
@@ -1136,7 +1212,7 @@ export default ${libName}
|
|
|
1136
1212
|
outDir,
|
|
1137
1213
|
entryPath: path,
|
|
1138
1214
|
fileName: node_path.basename(path),
|
|
1139
|
-
libFolder,
|
|
1215
|
+
libFolder: getTsLibFolder({ root, entryRoot }),
|
|
1140
1216
|
rollupConfig,
|
|
1141
1217
|
rollupOptions
|
|
1142
1218
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -17,11 +17,12 @@ import { createFilter } from '@rollup/pluginutils';
|
|
|
17
17
|
import debug from 'debug';
|
|
18
18
|
import { cyan, yellow, green } from 'kolorist';
|
|
19
19
|
import { ExtractorConfig, Extractor } from '@microsoft/api-extractor';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
20
21
|
import { getPackageInfoSync, resolveModule } from 'local-pkg';
|
|
21
22
|
import { compare } from 'compare-versions';
|
|
22
23
|
import MagicString from 'magic-string';
|
|
23
24
|
|
|
24
|
-
const
|
|
25
|
+
const _createProgram = proxyCreateProgram(ts, ts.createProgram, (ts2, options) => {
|
|
25
26
|
const { configFilePath } = options.options;
|
|
26
27
|
const vueOptions = typeof configFilePath === "string" ? createParsedCommandLine(ts2, ts2.sys, configFilePath.replace(/\\/g, "/")).vueOptions : resolveVueCompilerOptions({});
|
|
27
28
|
if (options.host) {
|
|
@@ -43,6 +44,34 @@ const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts2, options) =>
|
|
|
43
44
|
);
|
|
44
45
|
return [vueLanguagePlugin];
|
|
45
46
|
});
|
|
47
|
+
const createProgram = (options) => {
|
|
48
|
+
const program = _createProgram(options);
|
|
49
|
+
const emit = program.emit;
|
|
50
|
+
program.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
|
|
51
|
+
if (writeFile) {
|
|
52
|
+
return emit(
|
|
53
|
+
targetSourceFile,
|
|
54
|
+
(fileName, data, writeByteOrderMark, onError, sourceFiles) => {
|
|
55
|
+
if (fileName.endsWith(".d.ts")) {
|
|
56
|
+
data = removeEmitGlobalTypes(data);
|
|
57
|
+
}
|
|
58
|
+
return writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
|
|
59
|
+
},
|
|
60
|
+
cancellationToken,
|
|
61
|
+
emitOnlyDtsFiles,
|
|
62
|
+
customTransformers
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
return emit(
|
|
66
|
+
targetSourceFile,
|
|
67
|
+
writeFile,
|
|
68
|
+
cancellationToken,
|
|
69
|
+
emitOnlyDtsFiles,
|
|
70
|
+
customTransformers
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
return program;
|
|
74
|
+
};
|
|
46
75
|
|
|
47
76
|
const windowsSlashRE = /\\+/g;
|
|
48
77
|
function slash(p) {
|
|
@@ -167,6 +196,23 @@ function getTsConfig(tsConfigPath, readFileSync) {
|
|
|
167
196
|
Object.assign(tsConfig.compilerOptions, baseConfig.compilerOptions);
|
|
168
197
|
return tsConfig;
|
|
169
198
|
}
|
|
199
|
+
function getTsLibFolder({ root, entryRoot }) {
|
|
200
|
+
let libFolder;
|
|
201
|
+
try {
|
|
202
|
+
libFolder = createRequire(import.meta.url).resolve("typescript").replace(/node_modules\/typescript.*/, "node_modules/typescript");
|
|
203
|
+
} catch {
|
|
204
|
+
libFolder = resolve(root, "node_modules/typescript");
|
|
205
|
+
if (!existsSync(libFolder)) {
|
|
206
|
+
if (root !== entryRoot) {
|
|
207
|
+
libFolder = resolve(entryRoot, "node_modules/typescript");
|
|
208
|
+
if (!existsSync(libFolder))
|
|
209
|
+
libFolder = void 0;
|
|
210
|
+
}
|
|
211
|
+
libFolder = void 0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return libFolder;
|
|
215
|
+
}
|
|
170
216
|
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
171
217
|
function base64Encode(number) {
|
|
172
218
|
if (number >= 0 && number < BASE64_ALPHABET.length) {
|
|
@@ -305,6 +351,9 @@ function rollupDeclarationFiles({
|
|
|
305
351
|
if (!dtsRE$1.test(fileName)) {
|
|
306
352
|
fileName += ".d.ts";
|
|
307
353
|
}
|
|
354
|
+
if (/preserve/i.test(compilerOptions.module)) {
|
|
355
|
+
compilerOptions = { ...compilerOptions, module: "ESNext" };
|
|
356
|
+
}
|
|
308
357
|
const extractorConfig = ExtractorConfig.prepare({
|
|
309
358
|
configObject: {
|
|
310
359
|
...rollupConfig,
|
|
@@ -430,7 +479,7 @@ function VueResolver() {
|
|
|
430
479
|
program.emit(
|
|
431
480
|
sourceFile,
|
|
432
481
|
(path, content) => {
|
|
433
|
-
outputs.push({ path, content });
|
|
482
|
+
outputs.push({ path, content: removeEmitGlobalTypes(content) });
|
|
434
483
|
},
|
|
435
484
|
void 0,
|
|
436
485
|
true
|
|
@@ -521,10 +570,12 @@ function transformCode(options) {
|
|
|
521
570
|
return options.cleanVueFileName ? name.replace(/\.vue$/, "") : name;
|
|
522
571
|
};
|
|
523
572
|
let indexCount = 0;
|
|
573
|
+
let importCount = 0;
|
|
524
574
|
walkSourceFile(ast, (node, parent) => {
|
|
525
575
|
if (ts.isImportDeclaration(node)) {
|
|
526
576
|
if (!node.importClause) {
|
|
527
577
|
options.clearPureImport && s.remove(node.pos, node.end);
|
|
578
|
+
++importCount;
|
|
528
579
|
} else if (ts.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings))) {
|
|
529
580
|
const libName = toLibName(node.moduleSpecifier.text);
|
|
530
581
|
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
@@ -543,6 +594,7 @@ function transformCode(options) {
|
|
|
543
594
|
});
|
|
544
595
|
}
|
|
545
596
|
s.remove(node.pos, node.end);
|
|
597
|
+
++importCount;
|
|
546
598
|
}
|
|
547
599
|
return false;
|
|
548
600
|
}
|
|
@@ -598,22 +650,27 @@ function transformCode(options) {
|
|
|
598
650
|
prependImports += `import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
599
651
|
`;
|
|
600
652
|
});
|
|
601
|
-
s.prepend(prependImports);
|
|
653
|
+
s.trimStart("\n").prepend(prependImports);
|
|
602
654
|
return {
|
|
603
655
|
content: s.toString(),
|
|
604
|
-
declareModules
|
|
656
|
+
declareModules,
|
|
657
|
+
diffLineCount: importMap.size && importCount < importMap.size ? importMap.size - importCount : null
|
|
605
658
|
};
|
|
606
659
|
}
|
|
607
|
-
function
|
|
660
|
+
function hasNormalExport(content) {
|
|
608
661
|
const ast = ts.createSourceFile("a.ts", content, ts.ScriptTarget.Latest);
|
|
609
662
|
let has = false;
|
|
610
663
|
walkSourceFile(ast, (node) => {
|
|
611
|
-
if (ts.isExportDeclaration(node)
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
664
|
+
if (ts.isExportDeclaration(node)) {
|
|
665
|
+
if (node.exportClause && ts.isNamedExports(node.exportClause)) {
|
|
666
|
+
for (const element of node.exportClause.elements) {
|
|
667
|
+
if (element.name.escapedText !== "default") {
|
|
668
|
+
has = true;
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
616
671
|
}
|
|
672
|
+
} else {
|
|
673
|
+
has = true;
|
|
617
674
|
}
|
|
618
675
|
} else if ("modifiers" in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
|
|
619
676
|
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
|
|
@@ -1020,13 +1077,28 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1020
1077
|
}
|
|
1021
1078
|
bundleDebug("emit output patch");
|
|
1022
1079
|
const currentDir = host.getCurrentDirectory();
|
|
1080
|
+
const declarationFiles = /* @__PURE__ */ new Map();
|
|
1081
|
+
const mapFiles = /* @__PURE__ */ new Map();
|
|
1082
|
+
const prependMappings = /* @__PURE__ */ new Map();
|
|
1083
|
+
for (const [filePath, content] of outputFiles.entries()) {
|
|
1084
|
+
if (filePath.endsWith(".map")) {
|
|
1085
|
+
mapFiles.set(filePath, content);
|
|
1086
|
+
} else {
|
|
1087
|
+
declarationFiles.set(filePath, content);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1023
1090
|
await runParallel(
|
|
1024
1091
|
cpus().length,
|
|
1025
|
-
Array.from(
|
|
1092
|
+
Array.from(declarationFiles.entries()),
|
|
1026
1093
|
async ([filePath, content]) => {
|
|
1027
|
-
const
|
|
1028
|
-
|
|
1029
|
-
|
|
1094
|
+
const newFilePath = resolve(
|
|
1095
|
+
outDir,
|
|
1096
|
+
relative(
|
|
1097
|
+
entryRoot,
|
|
1098
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1099
|
+
)
|
|
1100
|
+
);
|
|
1101
|
+
if (content) {
|
|
1030
1102
|
const result = transformCode({
|
|
1031
1103
|
filePath,
|
|
1032
1104
|
content,
|
|
@@ -1038,7 +1110,18 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1038
1110
|
});
|
|
1039
1111
|
content = result.content;
|
|
1040
1112
|
declareModules.push(...result.declareModules);
|
|
1113
|
+
if (result.diffLineCount) {
|
|
1114
|
+
prependMappings.set(`${newFilePath}.map`, ";".repeat(result.diffLineCount));
|
|
1115
|
+
}
|
|
1041
1116
|
}
|
|
1117
|
+
await writeOutput(newFilePath, content, outDir);
|
|
1118
|
+
}
|
|
1119
|
+
);
|
|
1120
|
+
await runParallel(
|
|
1121
|
+
cpus().length,
|
|
1122
|
+
Array.from(mapFiles.entries()),
|
|
1123
|
+
async ([filePath, content]) => {
|
|
1124
|
+
const baseDir = dirname(filePath);
|
|
1042
1125
|
filePath = resolve(
|
|
1043
1126
|
outDir,
|
|
1044
1127
|
relative(
|
|
@@ -1046,21 +1129,22 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
1046
1129
|
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1047
1130
|
)
|
|
1048
1131
|
);
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
} catch (e) {
|
|
1062
|
-
logger.warn(`${logPrefix} ${yellow("Processing source map fail:")} ${filePath}`);
|
|
1132
|
+
try {
|
|
1133
|
+
const sourceMap = JSON.parse(content);
|
|
1134
|
+
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
1135
|
+
return normalizePath(
|
|
1136
|
+
relative(
|
|
1137
|
+
dirname(filePath),
|
|
1138
|
+
resolve(currentDir, relative(publicRoot, baseDir), source)
|
|
1139
|
+
)
|
|
1140
|
+
);
|
|
1141
|
+
});
|
|
1142
|
+
if (prependMappings.has(filePath)) {
|
|
1143
|
+
sourceMap.mappings = `${prependMappings.get(filePath)}${sourceMap.mappings}`;
|
|
1063
1144
|
}
|
|
1145
|
+
content = JSON.stringify(sourceMap);
|
|
1146
|
+
} catch (e) {
|
|
1147
|
+
logger.warn(`${logPrefix} ${yellow("Processing source map fail:")} ${filePath}`);
|
|
1064
1148
|
}
|
|
1065
1149
|
await writeOutput(filePath, content, outDir);
|
|
1066
1150
|
}
|
|
@@ -1099,7 +1183,7 @@ ${logPrefix} ${yellow(
|
|
|
1099
1183
|
fromPath = fullRelativeRE.test(fromPath) ? fromPath : `./${fromPath}`;
|
|
1100
1184
|
let content = "";
|
|
1101
1185
|
if (emittedFiles.has(sourceEntry)) {
|
|
1102
|
-
if (
|
|
1186
|
+
if (hasNormalExport(emittedFiles.get(sourceEntry))) {
|
|
1103
1187
|
content += `export * from '${fromPath}'
|
|
1104
1188
|
`;
|
|
1105
1189
|
}
|
|
@@ -1114,15 +1198,6 @@ export default ${libName}
|
|
|
1114
1198
|
bundleDebug("insert index");
|
|
1115
1199
|
if (rollupTypes) {
|
|
1116
1200
|
logger.info(green(`${logPrefix} Start rollup declaration files...`));
|
|
1117
|
-
let libFolder = resolve(root, "node_modules/typescript");
|
|
1118
|
-
if (!existsSync(libFolder)) {
|
|
1119
|
-
if (root !== entryRoot) {
|
|
1120
|
-
libFolder = resolve(entryRoot, "node_modules/typescript");
|
|
1121
|
-
if (!existsSync(libFolder))
|
|
1122
|
-
libFolder = void 0;
|
|
1123
|
-
}
|
|
1124
|
-
libFolder = void 0;
|
|
1125
|
-
}
|
|
1126
1201
|
const rollupFiles = /* @__PURE__ */ new Set();
|
|
1127
1202
|
const compilerOptions2 = configPath ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions;
|
|
1128
1203
|
const rollup = async (path) => {
|
|
@@ -1133,7 +1208,7 @@ export default ${libName}
|
|
|
1133
1208
|
outDir,
|
|
1134
1209
|
entryPath: path,
|
|
1135
1210
|
fileName: basename(path),
|
|
1136
|
-
libFolder,
|
|
1211
|
+
libFolder: getTsLibFolder({ root, entryRoot }),
|
|
1137
1212
|
rollupConfig,
|
|
1138
1213
|
rollupOptions
|
|
1139
1214
|
});
|