@depup/vite-plugin-dts 4.5.4-depup.0
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 -0
- package/README.md +37 -0
- package/README.zh-CN.md +417 -0
- package/changes.json +34 -0
- package/dist/index.cjs +1346 -0
- package/dist/index.d.cts +244 -0
- package/dist/index.d.mts +244 -0
- package/dist/index.d.ts +244 -0
- package/dist/index.mjs +1341 -0
- package/package.json +153 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1346 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const node_path = require('node:path');
|
|
6
|
+
const node_fs = require('node:fs');
|
|
7
|
+
const promises = require('node:fs/promises');
|
|
8
|
+
const node_os = require('node:os');
|
|
9
|
+
const languageCore = require('@vue/language-core');
|
|
10
|
+
const typescript = require('@volar/typescript');
|
|
11
|
+
const ts = require('typescript');
|
|
12
|
+
const node_module = require('node:module');
|
|
13
|
+
const localPkg = require('local-pkg');
|
|
14
|
+
const pluginutils = require('@rollup/pluginutils');
|
|
15
|
+
const debug = require('debug');
|
|
16
|
+
const kolorist = require('kolorist');
|
|
17
|
+
const apiExtractor = require('@microsoft/api-extractor');
|
|
18
|
+
const compareVersions = require('compare-versions');
|
|
19
|
+
const MagicString = require('magic-string');
|
|
20
|
+
|
|
21
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
22
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
23
|
+
|
|
24
|
+
const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
|
|
25
|
+
const debug__default = /*#__PURE__*/_interopDefaultCompat(debug);
|
|
26
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
27
|
+
|
|
28
|
+
const windowsSlashRE = /\\+/g;
|
|
29
|
+
function slash(p) {
|
|
30
|
+
return p.replace(windowsSlashRE, "/");
|
|
31
|
+
}
|
|
32
|
+
function resolveConfigDir(path, configDir) {
|
|
33
|
+
return path.replace("${configDir}", configDir);
|
|
34
|
+
}
|
|
35
|
+
function normalizePath(id) {
|
|
36
|
+
return node_path.posix.normalize(slash(id));
|
|
37
|
+
}
|
|
38
|
+
function resolve(...paths) {
|
|
39
|
+
return normalizePath(node_path.resolve(...paths));
|
|
40
|
+
}
|
|
41
|
+
function isNativeObj(value) {
|
|
42
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
43
|
+
}
|
|
44
|
+
function isRegExp(value) {
|
|
45
|
+
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
46
|
+
}
|
|
47
|
+
function isPromise(value) {
|
|
48
|
+
return !!value && (typeof value === "function" || typeof value === "object") && typeof value.then === "function";
|
|
49
|
+
}
|
|
50
|
+
async function unwrapPromise(maybePromise) {
|
|
51
|
+
return isPromise(maybePromise) ? await maybePromise : maybePromise;
|
|
52
|
+
}
|
|
53
|
+
function ensureAbsolute(path, root) {
|
|
54
|
+
return normalizePath(path ? node_path.isAbsolute(path) ? path : resolve(root, path) : root);
|
|
55
|
+
}
|
|
56
|
+
function ensureArray(value) {
|
|
57
|
+
return Array.isArray(value) ? value : value ? [value] : [];
|
|
58
|
+
}
|
|
59
|
+
async function runParallel(maxConcurrency, source, iteratorFn) {
|
|
60
|
+
const ret = [];
|
|
61
|
+
const executing = [];
|
|
62
|
+
for (const item of source) {
|
|
63
|
+
const p = Promise.resolve().then(() => iteratorFn(item, source));
|
|
64
|
+
ret.push(p);
|
|
65
|
+
if (maxConcurrency <= source.length) {
|
|
66
|
+
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
|
|
67
|
+
executing.push(e);
|
|
68
|
+
if (executing.length >= maxConcurrency) {
|
|
69
|
+
await Promise.race(executing);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return Promise.all(ret);
|
|
74
|
+
}
|
|
75
|
+
const speRE = /[\\/]/;
|
|
76
|
+
function queryPublicPath(paths) {
|
|
77
|
+
if (paths.length === 0) {
|
|
78
|
+
return "";
|
|
79
|
+
} else if (paths.length === 1) {
|
|
80
|
+
return node_path.dirname(paths[0]);
|
|
81
|
+
}
|
|
82
|
+
let publicPath = node_path.normalize(node_path.dirname(paths[0])) + node_path.sep;
|
|
83
|
+
let publicUnits = publicPath.split(speRE);
|
|
84
|
+
let index = publicUnits.length - 1;
|
|
85
|
+
for (const path of paths.slice(1)) {
|
|
86
|
+
if (!index) {
|
|
87
|
+
return publicPath;
|
|
88
|
+
}
|
|
89
|
+
const dirPath = node_path.normalize(node_path.dirname(path)) + node_path.sep;
|
|
90
|
+
if (dirPath.startsWith(publicPath)) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const units = dirPath.split(speRE);
|
|
94
|
+
if (units.length < index) {
|
|
95
|
+
publicPath = dirPath;
|
|
96
|
+
publicUnits = units;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (let i = 0; i <= index; ++i) {
|
|
100
|
+
if (publicUnits[i] !== units[i]) {
|
|
101
|
+
if (!i) {
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
index = i - 1;
|
|
105
|
+
publicUnits = publicUnits.slice(0, index + 1);
|
|
106
|
+
publicPath = publicUnits.join(node_path.sep) + node_path.sep;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return publicPath.slice(0, -1);
|
|
112
|
+
}
|
|
113
|
+
function removeDirIfEmpty(dir) {
|
|
114
|
+
if (!node_fs.existsSync(dir)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let onlyHasDir = true;
|
|
118
|
+
for (const file of node_fs.readdirSync(dir)) {
|
|
119
|
+
const abs = resolve(dir, file);
|
|
120
|
+
if (node_fs.lstatSync(abs).isDirectory()) {
|
|
121
|
+
if (!removeDirIfEmpty(abs)) {
|
|
122
|
+
onlyHasDir = false;
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
onlyHasDir = false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (onlyHasDir) {
|
|
129
|
+
node_fs.rmdirSync(dir);
|
|
130
|
+
}
|
|
131
|
+
return onlyHasDir;
|
|
132
|
+
}
|
|
133
|
+
function getTsConfig(tsConfigPath, readFileSync) {
|
|
134
|
+
const baseConfig = ts__default.readConfigFile(tsConfigPath, readFileSync).config ?? {};
|
|
135
|
+
const tsConfig = {
|
|
136
|
+
...baseConfig,
|
|
137
|
+
compilerOptions: {}
|
|
138
|
+
};
|
|
139
|
+
if (tsConfig.extends) {
|
|
140
|
+
ensureArray(tsConfig.extends).forEach((configPath) => {
|
|
141
|
+
const config = getTsConfig(ensureAbsolute(configPath, node_path.dirname(tsConfigPath)), readFileSync);
|
|
142
|
+
Object.assign(tsConfig.compilerOptions, config.compilerOptions);
|
|
143
|
+
if (!tsConfig.include) {
|
|
144
|
+
tsConfig.include = config.include;
|
|
145
|
+
}
|
|
146
|
+
if (!tsConfig.exclude) {
|
|
147
|
+
tsConfig.exclude = config.exclude;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
Object.assign(tsConfig.compilerOptions, baseConfig.compilerOptions);
|
|
152
|
+
return tsConfig;
|
|
153
|
+
}
|
|
154
|
+
function getTsLibFolder() {
|
|
155
|
+
const libFolder = tryGetPackageInfo("typescript")?.rootPath;
|
|
156
|
+
return libFolder && normalizePath(libFolder);
|
|
157
|
+
}
|
|
158
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
159
|
+
function base64Encode(number) {
|
|
160
|
+
if (number >= 0 && number < BASE64_ALPHABET.length) {
|
|
161
|
+
return BASE64_ALPHABET[number];
|
|
162
|
+
}
|
|
163
|
+
throw new TypeError("Base64 integer must be between 0 and 63: " + number);
|
|
164
|
+
}
|
|
165
|
+
const VLQ_BASE_SHIFT = 5;
|
|
166
|
+
const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
|
|
167
|
+
const VLQ_BASE_MASK = VLQ_BASE - 1;
|
|
168
|
+
const VLQ_CONTINUATION_BIT = VLQ_BASE;
|
|
169
|
+
function toVLQSigned(number) {
|
|
170
|
+
return number < 0 ? (-number << 1) + 1 : (number << 1) + 0;
|
|
171
|
+
}
|
|
172
|
+
function base64VLQEncode(numbers) {
|
|
173
|
+
let encoded = "";
|
|
174
|
+
for (const number of numbers) {
|
|
175
|
+
let vlq = toVLQSigned(number);
|
|
176
|
+
let digit;
|
|
177
|
+
do {
|
|
178
|
+
digit = vlq & VLQ_BASE_MASK;
|
|
179
|
+
vlq >>>= VLQ_BASE_SHIFT;
|
|
180
|
+
if (vlq > 0) {
|
|
181
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
182
|
+
}
|
|
183
|
+
encoded += base64Encode(digit);
|
|
184
|
+
} while (vlq > 0);
|
|
185
|
+
}
|
|
186
|
+
return encoded;
|
|
187
|
+
}
|
|
188
|
+
const pkgPathCache = /* @__PURE__ */ new Map();
|
|
189
|
+
function tryGetPkgPath(beginPath) {
|
|
190
|
+
beginPath = normalizePath(beginPath);
|
|
191
|
+
if (pkgPathCache.has(beginPath)) {
|
|
192
|
+
return pkgPathCache.get(beginPath);
|
|
193
|
+
}
|
|
194
|
+
const pkgPath = resolve(beginPath, "package.json");
|
|
195
|
+
if (node_fs.existsSync(pkgPath)) {
|
|
196
|
+
pkgPathCache.set(beginPath, pkgPath);
|
|
197
|
+
return pkgPath;
|
|
198
|
+
}
|
|
199
|
+
const parentDir = normalizePath(node_path.dirname(beginPath));
|
|
200
|
+
if (!parentDir || parentDir === beginPath) {
|
|
201
|
+
pkgPathCache.set(beginPath, undefined);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
return tryGetPkgPath(parentDir);
|
|
205
|
+
}
|
|
206
|
+
function toCapitalCase(value) {
|
|
207
|
+
value = value.trim().replace(/\s+/g, "-");
|
|
208
|
+
value = value.replace(/-+(\w)/g, (_, char) => char ? char.toUpperCase() : "");
|
|
209
|
+
return (value.charAt(0).toLocaleUpperCase() + value.slice(1)).replace(
|
|
210
|
+
/[^\w]/g,
|
|
211
|
+
""
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
function findTypesPath(...pkgs) {
|
|
215
|
+
let path;
|
|
216
|
+
for (const pkg of pkgs) {
|
|
217
|
+
if (typeof pkg !== "object") continue;
|
|
218
|
+
path = pkg.types || pkg.typings || pkg.exports?.types || pkg.exports?.["."]?.types || pkg.exports?.["./"]?.types;
|
|
219
|
+
if (path) return path;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function setModuleResolution(options) {
|
|
223
|
+
if (options.moduleResolution) return;
|
|
224
|
+
const module = typeof options.module === "number" ? options.module : options.target ?? ts__default.ScriptTarget.ES5 >= 2 ? ts__default.ModuleKind.ES2015 : ts__default.ModuleKind.CommonJS;
|
|
225
|
+
let moduleResolution;
|
|
226
|
+
switch (module) {
|
|
227
|
+
case ts__default.ModuleKind.CommonJS:
|
|
228
|
+
moduleResolution = ts__default.ModuleResolutionKind.Node10;
|
|
229
|
+
break;
|
|
230
|
+
case ts__default.ModuleKind.Node16:
|
|
231
|
+
moduleResolution = ts__default.ModuleResolutionKind.Node16;
|
|
232
|
+
break;
|
|
233
|
+
case ts__default.ModuleKind.NodeNext:
|
|
234
|
+
moduleResolution = ts__default.ModuleResolutionKind.NodeNext;
|
|
235
|
+
break;
|
|
236
|
+
default:
|
|
237
|
+
moduleResolution = ts__default.version.startsWith("5") ? ts__default.ModuleResolutionKind.Bundler : ts__default.ModuleResolutionKind.Classic;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
options.moduleResolution = moduleResolution;
|
|
241
|
+
}
|
|
242
|
+
function editSourceMapDir(content, fromDir, toDir) {
|
|
243
|
+
const relativeOutDir = node_path.relative(fromDir, toDir);
|
|
244
|
+
if (relativeOutDir) {
|
|
245
|
+
try {
|
|
246
|
+
const sourceMap = JSON.parse(content);
|
|
247
|
+
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
248
|
+
return normalizePath(node_path.relative(relativeOutDir, source));
|
|
249
|
+
});
|
|
250
|
+
return JSON.stringify(sourceMap);
|
|
251
|
+
} catch (e) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g;
|
|
258
|
+
const asteriskRE = /[*]+/g;
|
|
259
|
+
function parseTsAliases(basePath, paths) {
|
|
260
|
+
const result = [];
|
|
261
|
+
for (const [pathWithAsterisk, replacements] of Object.entries(paths)) {
|
|
262
|
+
const find = new RegExp(
|
|
263
|
+
`^${pathWithAsterisk.replace(regexpSymbolRE, "\\$1").replace(asteriskRE, "(?!\\.{1,2}\\/)([^*]+)")}$`
|
|
264
|
+
);
|
|
265
|
+
let index = 1;
|
|
266
|
+
result.push({
|
|
267
|
+
find,
|
|
268
|
+
replacement: ensureAbsolute(
|
|
269
|
+
replacements[0].replace(asteriskRE, () => `$${index++}`),
|
|
270
|
+
basePath
|
|
271
|
+
)
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
const rootAsteriskImportRE = /^(?!\.{1,2}\/)([^*]+)$/;
|
|
277
|
+
function isAliasGlobal(alias) {
|
|
278
|
+
return alias.find.toString() === rootAsteriskImportRE.toString();
|
|
279
|
+
}
|
|
280
|
+
function importResolves(path) {
|
|
281
|
+
const files = [
|
|
282
|
+
// js
|
|
283
|
+
".js",
|
|
284
|
+
".jsx",
|
|
285
|
+
".mjs",
|
|
286
|
+
".cjs",
|
|
287
|
+
// ts
|
|
288
|
+
".ts",
|
|
289
|
+
".tsx",
|
|
290
|
+
".mts",
|
|
291
|
+
".cts",
|
|
292
|
+
".d.ts",
|
|
293
|
+
// json
|
|
294
|
+
".json",
|
|
295
|
+
// vue
|
|
296
|
+
".vue",
|
|
297
|
+
".vue.d.ts",
|
|
298
|
+
// svelte
|
|
299
|
+
".svelte"
|
|
300
|
+
];
|
|
301
|
+
for (const ext of files) {
|
|
302
|
+
if (node_fs.existsSync(path + ext)) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
function tryGetPackageInfo(name) {
|
|
309
|
+
if (process.versions.pnp) {
|
|
310
|
+
const targetRequire = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
311
|
+
try {
|
|
312
|
+
return localPkg.getPackageInfoSync(
|
|
313
|
+
targetRequire.resolve(`${name}/package.json`, { paths: [process.cwd()] })
|
|
314
|
+
);
|
|
315
|
+
} catch (e) {
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
return localPkg.getPackageInfoSync(name) ?? localPkg.getPackageInfoSync(name, { paths: [localPkg.resolveModule(name) || process.cwd()] });
|
|
320
|
+
} catch (e) {
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const hasVue = !!tryGetPackageInfo("vue");
|
|
325
|
+
const createProgram = !hasVue ? ts__default.createProgram : typescript.proxyCreateProgram(ts__default, ts__default.createProgram, (ts2, options) => {
|
|
326
|
+
const { configFilePath } = options.options;
|
|
327
|
+
const vueOptions = typeof configFilePath === "string" ? languageCore.createParsedCommandLine(ts2, ts2.sys, configFilePath.replace(/\\/g, "/")).vueOptions : languageCore.resolveVueCompilerOptions({});
|
|
328
|
+
const vueLanguagePlugin = languageCore.createVueLanguagePlugin(
|
|
329
|
+
ts2,
|
|
330
|
+
options.options,
|
|
331
|
+
vueOptions,
|
|
332
|
+
(id) => id
|
|
333
|
+
);
|
|
334
|
+
return [vueLanguagePlugin];
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const dtsRE$1 = /\.d\.(m|c)?tsx?$/;
|
|
338
|
+
function rollupDeclarationFiles({
|
|
339
|
+
root,
|
|
340
|
+
configPath,
|
|
341
|
+
compilerOptions,
|
|
342
|
+
outDir,
|
|
343
|
+
entryPath,
|
|
344
|
+
fileName,
|
|
345
|
+
libFolder,
|
|
346
|
+
rollupConfig = {},
|
|
347
|
+
rollupOptions = {}
|
|
348
|
+
}) {
|
|
349
|
+
const configObjectFullPath = resolve(root, "api-extractor.json");
|
|
350
|
+
if (!dtsRE$1.test(fileName)) {
|
|
351
|
+
fileName += ".d.ts";
|
|
352
|
+
}
|
|
353
|
+
if (/preserve/i.test(compilerOptions.module)) {
|
|
354
|
+
compilerOptions = { ...compilerOptions, module: "ESNext" };
|
|
355
|
+
}
|
|
356
|
+
const extractorConfig = apiExtractor.ExtractorConfig.prepare({
|
|
357
|
+
configObject: {
|
|
358
|
+
...rollupConfig,
|
|
359
|
+
projectFolder: root,
|
|
360
|
+
mainEntryPointFilePath: entryPath,
|
|
361
|
+
compiler: {
|
|
362
|
+
tsconfigFilePath: configPath,
|
|
363
|
+
overrideTsconfig: {
|
|
364
|
+
$schema: "http://json.schemastore.org/tsconfig",
|
|
365
|
+
compilerOptions
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
apiReport: {
|
|
369
|
+
enabled: false,
|
|
370
|
+
reportFileName: "<unscopedPackageName>.api.md",
|
|
371
|
+
...rollupConfig.apiReport
|
|
372
|
+
},
|
|
373
|
+
docModel: {
|
|
374
|
+
enabled: false,
|
|
375
|
+
...rollupConfig.docModel
|
|
376
|
+
},
|
|
377
|
+
dtsRollup: {
|
|
378
|
+
enabled: true,
|
|
379
|
+
publicTrimmedFilePath: resolve(outDir, fileName)
|
|
380
|
+
},
|
|
381
|
+
tsdocMetadata: {
|
|
382
|
+
enabled: false,
|
|
383
|
+
...rollupConfig.tsdocMetadata
|
|
384
|
+
},
|
|
385
|
+
messages: {
|
|
386
|
+
compilerMessageReporting: {
|
|
387
|
+
default: {
|
|
388
|
+
logLevel: "none"
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
extractorMessageReporting: {
|
|
392
|
+
default: {
|
|
393
|
+
logLevel: "none"
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
...rollupConfig.messages
|
|
397
|
+
}
|
|
398
|
+
},
|
|
399
|
+
configObjectFullPath,
|
|
400
|
+
packageJsonFullPath: tryGetPkgPath(configObjectFullPath)
|
|
401
|
+
});
|
|
402
|
+
return apiExtractor.Extractor.invoke(extractorConfig, {
|
|
403
|
+
localBuild: false,
|
|
404
|
+
showVerboseMessages: false,
|
|
405
|
+
showDiagnostics: false,
|
|
406
|
+
typescriptCompilerFolder: libFolder,
|
|
407
|
+
...rollupOptions
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const jsonRE = /\.json$/;
|
|
412
|
+
function JsonResolver() {
|
|
413
|
+
return {
|
|
414
|
+
name: "json",
|
|
415
|
+
supports(id) {
|
|
416
|
+
return jsonRE.test(id);
|
|
417
|
+
},
|
|
418
|
+
transform({ id, root, program }) {
|
|
419
|
+
const sourceFile = program.getSourceFile(id);
|
|
420
|
+
if (!sourceFile) return [];
|
|
421
|
+
return [
|
|
422
|
+
{
|
|
423
|
+
path: node_path.relative(root, `${id}.d.ts`),
|
|
424
|
+
content: `declare const _default: ${sourceFile.text};
|
|
425
|
+
|
|
426
|
+
export default _default;
|
|
427
|
+
`
|
|
428
|
+
}
|
|
429
|
+
];
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const svelteRE = /\.svelte$/;
|
|
435
|
+
let lowerVersion;
|
|
436
|
+
function querySvelteVersion() {
|
|
437
|
+
if (typeof lowerVersion === "boolean") return;
|
|
438
|
+
try {
|
|
439
|
+
const version = tryGetPackageInfo("svelte")?.version;
|
|
440
|
+
lowerVersion = version ? compareVersions.compare(version, "4.0.0", "<") : false;
|
|
441
|
+
} catch (e) {
|
|
442
|
+
lowerVersion = false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function SvelteResolver() {
|
|
446
|
+
return {
|
|
447
|
+
name: "svelte",
|
|
448
|
+
supports(id) {
|
|
449
|
+
return svelteRE.test(id);
|
|
450
|
+
},
|
|
451
|
+
transform({ id, root }) {
|
|
452
|
+
querySvelteVersion();
|
|
453
|
+
return [
|
|
454
|
+
{
|
|
455
|
+
path: node_path.relative(root, `${id}.d.ts`),
|
|
456
|
+
content: `export { ${lowerVersion ? "SvelteComponentTyped" : "SvelteComponent"} as default } from 'svelte';
|
|
457
|
+
`
|
|
458
|
+
}
|
|
459
|
+
];
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const vueRE = /\.vue$/;
|
|
465
|
+
function VueResolver() {
|
|
466
|
+
return {
|
|
467
|
+
name: "vue",
|
|
468
|
+
supports(id) {
|
|
469
|
+
return vueRE.test(id);
|
|
470
|
+
},
|
|
471
|
+
transform({ id, code, program }) {
|
|
472
|
+
const sourceFile = program.getSourceFile(id) || program.getSourceFile(id + ".ts") || program.getSourceFile(id + ".js") || program.getSourceFile(id + ".tsx") || program.getSourceFile(id + ".jsx");
|
|
473
|
+
if (!sourceFile) return [];
|
|
474
|
+
const outputs = [];
|
|
475
|
+
const { emitSkipped, diagnostics } = program.emit(
|
|
476
|
+
sourceFile,
|
|
477
|
+
(path, content) => {
|
|
478
|
+
outputs.push({ path, content });
|
|
479
|
+
},
|
|
480
|
+
undefined,
|
|
481
|
+
true
|
|
482
|
+
);
|
|
483
|
+
if (!program.getCompilerOptions().declarationMap) {
|
|
484
|
+
return {
|
|
485
|
+
outputs,
|
|
486
|
+
emitSkipped,
|
|
487
|
+
diagnostics
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
const [beforeScript] = code.split(/\s*<script.*>/);
|
|
491
|
+
const beforeLines = beforeScript.split("\n").length;
|
|
492
|
+
for (const output of outputs) {
|
|
493
|
+
if (output.path.endsWith(".map")) {
|
|
494
|
+
try {
|
|
495
|
+
const sourceMap = JSON.parse(output.content);
|
|
496
|
+
sourceMap.sources = sourceMap.sources.map(
|
|
497
|
+
(source) => source.replace(/\.vue\.ts$/, ".vue")
|
|
498
|
+
);
|
|
499
|
+
if (beforeScript && beforeScript !== code && beforeLines) {
|
|
500
|
+
sourceMap.mappings = `${base64VLQEncode([0, 0, beforeLines, 0])};${sourceMap.mappings}`;
|
|
501
|
+
}
|
|
502
|
+
output.content = JSON.stringify(sourceMap);
|
|
503
|
+
} catch (e) {
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return {
|
|
508
|
+
outputs,
|
|
509
|
+
emitSkipped,
|
|
510
|
+
diagnostics
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function parseResolvers(resolvers) {
|
|
517
|
+
const nameMap = /* @__PURE__ */ new Map();
|
|
518
|
+
for (const resolver of resolvers) {
|
|
519
|
+
resolver.name && nameMap.set(resolver.name, resolver);
|
|
520
|
+
}
|
|
521
|
+
return Array.from(nameMap.values());
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
525
|
+
function normalizeGlob(path) {
|
|
526
|
+
if (/[\\/]$/.test(path)) {
|
|
527
|
+
return path + "**";
|
|
528
|
+
} else if (!globSuffixRE.test(path.split(/[\\/]/).pop())) {
|
|
529
|
+
return path + "/**";
|
|
530
|
+
}
|
|
531
|
+
return path;
|
|
532
|
+
}
|
|
533
|
+
function walkSourceFile(sourceFile, callback) {
|
|
534
|
+
function walkNode(node, parent, callback2) {
|
|
535
|
+
if (callback2(node, parent) !== false) {
|
|
536
|
+
node.forEachChild((child) => walkNode(child, node, callback2));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
sourceFile.forEachChild((child) => walkNode(child, sourceFile, callback));
|
|
540
|
+
}
|
|
541
|
+
function isAliasMatch(alias, importer) {
|
|
542
|
+
if (isRegExp(alias.find)) return alias.find.test(importer);
|
|
543
|
+
if (importer.length < alias.find.length) return false;
|
|
544
|
+
if (importer === alias.find) return true;
|
|
545
|
+
return importer.indexOf(alias.find) === 0 && (alias.find.endsWith("/") || importer.substring(alias.find.length)[0] === "/");
|
|
546
|
+
}
|
|
547
|
+
function transformAlias(importer, dir, aliases, aliasesExclude) {
|
|
548
|
+
if (aliases?.length && !aliasesExclude.some((e) => isRegExp(e) ? e.test(importer) : String(e) === importer)) {
|
|
549
|
+
const matchedAlias = aliases.find((alias) => isAliasMatch(alias, importer));
|
|
550
|
+
if (matchedAlias) {
|
|
551
|
+
const replacement = node_path.isAbsolute(matchedAlias.replacement) ? normalizePath(node_path.relative(dir, matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
552
|
+
const endsWithSlash = typeof matchedAlias.find === "string" ? matchedAlias.find.endsWith("/") : importer.match(matchedAlias.find)[0].endsWith("/");
|
|
553
|
+
const truthPath = importer.replace(
|
|
554
|
+
matchedAlias.find,
|
|
555
|
+
replacement + (endsWithSlash ? "/" : "")
|
|
556
|
+
);
|
|
557
|
+
const absolutePath = node_path.resolve(dir, truthPath);
|
|
558
|
+
const normalizedPath = normalizePath(node_path.relative(dir, absolutePath));
|
|
559
|
+
const resultPath = normalizedPath.startsWith(".") ? normalizedPath : `./${normalizedPath}`;
|
|
560
|
+
if (!isAliasGlobal(matchedAlias)) return resultPath;
|
|
561
|
+
if (importResolves(absolutePath)) return resultPath;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return importer;
|
|
565
|
+
}
|
|
566
|
+
const vlsRE = /^_?__VLS_/;
|
|
567
|
+
function isVLSNode(node) {
|
|
568
|
+
if (ts__default.isVariableStatement(node)) {
|
|
569
|
+
return node.declarationList.declarations.some(
|
|
570
|
+
(d) => ts__default.isIdentifier(d.name) && vlsRE.test(`${d.name.escapedText}`)
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
if (ts__default.isTypeAliasDeclaration(node)) {
|
|
574
|
+
return vlsRE.test(`${node.name.escapedText}`);
|
|
575
|
+
}
|
|
576
|
+
if (ts__default.isFunctionDeclaration(node)) {
|
|
577
|
+
return !!node.name && vlsRE.test(`${node.name.escapedText}`);
|
|
578
|
+
}
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
function transformCode(options) {
|
|
582
|
+
const s = new MagicString__default(options.content);
|
|
583
|
+
const ast = ts__default.createSourceFile("a.ts", options.content, ts__default.ScriptTarget.Latest);
|
|
584
|
+
const dir = node_path.dirname(options.filePath);
|
|
585
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
586
|
+
const usedDefault = /* @__PURE__ */ new Map();
|
|
587
|
+
const declareModules = [];
|
|
588
|
+
const toLibName = (origin) => {
|
|
589
|
+
const name = transformAlias(origin, dir, options.aliases, options.aliasesExclude);
|
|
590
|
+
return options.cleanVueFileName ? name.replace(/\.vue$/, "") : name;
|
|
591
|
+
};
|
|
592
|
+
let indexCount = 0;
|
|
593
|
+
let importCount = 0;
|
|
594
|
+
walkSourceFile(ast, (node, parent) => {
|
|
595
|
+
if (ts__default.isImportDeclaration(node)) {
|
|
596
|
+
if (!node.importClause) {
|
|
597
|
+
options.clearPureImport && s.remove(node.pos, node.end);
|
|
598
|
+
++importCount;
|
|
599
|
+
} else if (ts__default.isStringLiteral(node.moduleSpecifier) && (node.importClause.name || node.importClause.namedBindings && ts__default.isNamedImports(node.importClause.namedBindings))) {
|
|
600
|
+
const libName = toLibName(node.moduleSpecifier.text);
|
|
601
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
602
|
+
if (node.importClause.name && !usedDefault.has(libName)) {
|
|
603
|
+
const usedType = node.importClause.name.escapedText;
|
|
604
|
+
usedDefault.set(libName, usedType);
|
|
605
|
+
importSet.add(`default as ${usedType}`);
|
|
606
|
+
}
|
|
607
|
+
if (node.importClause.namedBindings && ts__default.isNamedImports(node.importClause.namedBindings)) {
|
|
608
|
+
node.importClause.namedBindings.elements.forEach((element) => {
|
|
609
|
+
if (element.propertyName) {
|
|
610
|
+
importSet.add(`${element.propertyName.getText(ast)} as ${element.name.escapedText}`);
|
|
611
|
+
} else {
|
|
612
|
+
importSet.add(element.name.escapedText);
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
s.remove(node.pos, node.end);
|
|
617
|
+
++importCount;
|
|
618
|
+
}
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
if (ts__default.isImportTypeNode(node) && node.qualifier && ts__default.isLiteralTypeNode(node.argument) && ts__default.isIdentifier(node.qualifier) && ts__default.isStringLiteral(node.argument.literal)) {
|
|
622
|
+
const libName = toLibName(node.argument.literal.text);
|
|
623
|
+
if (!options.staticImport) {
|
|
624
|
+
s.update(node.argument.literal.pos, node.argument.literal.end, `'${libName}'`);
|
|
625
|
+
return !!node.typeArguments;
|
|
626
|
+
}
|
|
627
|
+
const importSet = importMap.get(libName) ?? importMap.set(libName, /* @__PURE__ */ new Set()).get(libName);
|
|
628
|
+
let usedType = node.qualifier.escapedText;
|
|
629
|
+
if (usedType === "default") {
|
|
630
|
+
usedType = usedDefault.get(libName) ?? usedDefault.set(libName, `__DTS_DEFAULT_${indexCount++}__`).get(libName);
|
|
631
|
+
importSet.add(`default as ${usedType}`);
|
|
632
|
+
s.update(node.qualifier.pos, node.qualifier.end, usedType);
|
|
633
|
+
} else {
|
|
634
|
+
importSet.add(usedType);
|
|
635
|
+
}
|
|
636
|
+
if (ts__default.isImportTypeNode(parent) && parent.typeArguments && parent.typeArguments[0] === node) {
|
|
637
|
+
s.remove(node.pos, node.argument.end + 2);
|
|
638
|
+
} else {
|
|
639
|
+
s.update(node.pos, node.argument.end + 2, " ");
|
|
640
|
+
}
|
|
641
|
+
return !!node.typeArguments;
|
|
642
|
+
}
|
|
643
|
+
if (ts__default.isCallExpression(node) && node.expression.kind === ts__default.SyntaxKind.ImportKeyword && ts__default.isStringLiteral(node.arguments[0])) {
|
|
644
|
+
s.update(
|
|
645
|
+
node.arguments[0].pos,
|
|
646
|
+
node.arguments[0].end,
|
|
647
|
+
`'${toLibName(node.arguments[0].text)}'`
|
|
648
|
+
);
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
if (ts__default.isExportDeclaration(node) && node.moduleSpecifier && ts__default.isStringLiteral(node.moduleSpecifier)) {
|
|
652
|
+
s.update(
|
|
653
|
+
node.moduleSpecifier.pos,
|
|
654
|
+
node.moduleSpecifier.end,
|
|
655
|
+
` '${toLibName(node.moduleSpecifier.text)}'`
|
|
656
|
+
);
|
|
657
|
+
return false;
|
|
658
|
+
}
|
|
659
|
+
if (ts__default.isModuleDeclaration(node) && node.body && ts__default.isModuleBlock(node.body)) {
|
|
660
|
+
if (ts__default.isIdentifier(node.name) && node.name.escapedText === "global" && node.body.statements.some(isVLSNode)) {
|
|
661
|
+
s.remove(node.pos, node.end);
|
|
662
|
+
} else if (ts__default.isStringLiteral(node.name)) {
|
|
663
|
+
const libName = toLibName(node.name.text);
|
|
664
|
+
if (libName !== node.name.text) {
|
|
665
|
+
s.update(node.name.pos, node.name.end, ` '${libName}'`);
|
|
666
|
+
}
|
|
667
|
+
if (!libName.startsWith(".") && node.modifiers?.[0] && node.modifiers[0].kind === ts__default.SyntaxKind.DeclareKeyword && !node.body.statements.some(
|
|
668
|
+
(s2) => ts__default.isExportAssignment(s2) || ts__default.isExportDeclaration(s2) || ts__default.isImportDeclaration(s2)
|
|
669
|
+
)) {
|
|
670
|
+
declareModules.push(s.slice(node.pos, node.end + 1));
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
let prependImports = "";
|
|
677
|
+
importMap.forEach((importSet, libName) => {
|
|
678
|
+
prependImports += `import { ${Array.from(importSet).join(", ")} } from '${libName}';
|
|
679
|
+
`;
|
|
680
|
+
});
|
|
681
|
+
s.trimStart("\n").prepend(prependImports);
|
|
682
|
+
return {
|
|
683
|
+
content: s.toString(),
|
|
684
|
+
declareModules,
|
|
685
|
+
diffLineCount: importMap.size && importCount < importMap.size ? importMap.size - importCount : null
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
function hasNormalExport(content) {
|
|
689
|
+
const ast = ts__default.createSourceFile("a.ts", content, ts__default.ScriptTarget.Latest);
|
|
690
|
+
let has = false;
|
|
691
|
+
walkSourceFile(ast, (node) => {
|
|
692
|
+
if (ts__default.isExportDeclaration(node)) {
|
|
693
|
+
if (node.exportClause && ts__default.isNamedExports(node.exportClause)) {
|
|
694
|
+
for (const element of node.exportClause.elements) {
|
|
695
|
+
if (element.name.getText(ast) !== "default") {
|
|
696
|
+
has = true;
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
} else {
|
|
701
|
+
has = true;
|
|
702
|
+
}
|
|
703
|
+
} else if ("modifiers" in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
|
|
704
|
+
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
|
|
705
|
+
if (node.modifiers[i].kind === ts__default.SyntaxKind.ExportKeyword && node.modifiers[i + 1]?.kind !== ts__default.SyntaxKind.DefaultKeyword) {
|
|
706
|
+
has = true;
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
return false;
|
|
712
|
+
});
|
|
713
|
+
return has;
|
|
714
|
+
}
|
|
715
|
+
function hasExportDefault(content) {
|
|
716
|
+
const ast = ts__default.createSourceFile("a.ts", content, ts__default.ScriptTarget.Latest);
|
|
717
|
+
let has = false;
|
|
718
|
+
walkSourceFile(ast, (node) => {
|
|
719
|
+
if (ts__default.isExportAssignment(node)) {
|
|
720
|
+
has = true;
|
|
721
|
+
} else if (ts__default.isExportDeclaration(node) && node.exportClause && ts__default.isNamedExports(node.exportClause)) {
|
|
722
|
+
for (const element of node.exportClause.elements) {
|
|
723
|
+
if (element.name.getText(ast) === "default") {
|
|
724
|
+
has = true;
|
|
725
|
+
break;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
} else if ("modifiers" in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
|
|
729
|
+
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
|
|
730
|
+
if (node.modifiers[i].kind === ts__default.SyntaxKind.ExportKeyword && node.modifiers[i + 1]?.kind === ts__default.SyntaxKind.DefaultKeyword) {
|
|
731
|
+
has = true;
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return false;
|
|
737
|
+
});
|
|
738
|
+
return has;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const jsRE = /\.(m|c)?jsx?$/;
|
|
742
|
+
const tsRE = /\.(m|c)?tsx?$/;
|
|
743
|
+
const dtsRE = /\.d\.(m|c)?tsx?$/;
|
|
744
|
+
const tjsRE = /\.(m|c)?(t|j)sx?$/;
|
|
745
|
+
const mtjsRE = /\.m(t|j)sx?$/;
|
|
746
|
+
const ctjsRE = /\.c(t|j)sx?$/;
|
|
747
|
+
const fullRelativeRE = /^\.\.?\//;
|
|
748
|
+
const defaultIndex = "index.d.ts";
|
|
749
|
+
const pluginName = "vite:dts";
|
|
750
|
+
const logPrefix = kolorist.cyan(`[${pluginName}]`);
|
|
751
|
+
const bundleDebug = debug__default("vite-plugin-dts:bundle");
|
|
752
|
+
const fixedCompilerOptions = {
|
|
753
|
+
noEmit: false,
|
|
754
|
+
declaration: true,
|
|
755
|
+
emitDeclarationOnly: true,
|
|
756
|
+
checkJs: false,
|
|
757
|
+
skipLibCheck: true,
|
|
758
|
+
preserveSymlinks: false,
|
|
759
|
+
noEmitOnError: undefined,
|
|
760
|
+
target: ts__default.ScriptTarget.ESNext
|
|
761
|
+
};
|
|
762
|
+
const noop = () => {
|
|
763
|
+
};
|
|
764
|
+
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
765
|
+
const tsToDts = (path) => `${path.replace(tsRE, "")}.d.ts`;
|
|
766
|
+
function dtsPlugin(options = {}) {
|
|
767
|
+
const {
|
|
768
|
+
tsconfigPath,
|
|
769
|
+
logLevel,
|
|
770
|
+
staticImport = false,
|
|
771
|
+
clearPureImport = true,
|
|
772
|
+
cleanVueFileName = false,
|
|
773
|
+
insertTypesEntry = false,
|
|
774
|
+
rollupTypes = false,
|
|
775
|
+
pathsToAliases = true,
|
|
776
|
+
aliasesExclude = [],
|
|
777
|
+
rollupOptions = {},
|
|
778
|
+
copyDtsFiles = false,
|
|
779
|
+
declarationOnly = false,
|
|
780
|
+
strictOutput = true,
|
|
781
|
+
afterDiagnostic = noop,
|
|
782
|
+
beforeWriteFile = noop,
|
|
783
|
+
afterRollup = noop,
|
|
784
|
+
afterBuild = noop
|
|
785
|
+
} = options;
|
|
786
|
+
let root = ensureAbsolute(options.root ?? "", process.cwd());
|
|
787
|
+
let publicRoot = "";
|
|
788
|
+
let entryRoot = options.entryRoot ?? "";
|
|
789
|
+
let configPath;
|
|
790
|
+
let compilerOptions;
|
|
791
|
+
let rawCompilerOptions;
|
|
792
|
+
let outDirs;
|
|
793
|
+
let entries;
|
|
794
|
+
let include;
|
|
795
|
+
let exclude;
|
|
796
|
+
let aliases;
|
|
797
|
+
let libName;
|
|
798
|
+
let indexName;
|
|
799
|
+
let logger;
|
|
800
|
+
let host;
|
|
801
|
+
let program;
|
|
802
|
+
let filter;
|
|
803
|
+
let rootNames = [];
|
|
804
|
+
let rebuildProgram;
|
|
805
|
+
let bundled = false;
|
|
806
|
+
let timeRecord = 0;
|
|
807
|
+
const resolvers = parseResolvers([
|
|
808
|
+
JsonResolver(),
|
|
809
|
+
VueResolver(),
|
|
810
|
+
SvelteResolver(),
|
|
811
|
+
...options.resolvers || []
|
|
812
|
+
]);
|
|
813
|
+
const rootFiles = /* @__PURE__ */ new Set();
|
|
814
|
+
const outputFiles = /* @__PURE__ */ new Map();
|
|
815
|
+
const transformedFiles = /* @__PURE__ */ new Set();
|
|
816
|
+
const diagnostics = [];
|
|
817
|
+
const setOutputFile = (path, content) => {
|
|
818
|
+
outputFiles.set(path, content);
|
|
819
|
+
};
|
|
820
|
+
const rollupConfig = { ...options.rollupConfig || {} };
|
|
821
|
+
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [];
|
|
822
|
+
const cleanPath = (path, emittedFiles) => {
|
|
823
|
+
const newPath = path.replace(".vue.d.ts", ".d.ts");
|
|
824
|
+
return !emittedFiles.has(newPath) && cleanVueFileName ? newPath : path;
|
|
825
|
+
};
|
|
826
|
+
return {
|
|
827
|
+
name: pluginName,
|
|
828
|
+
apply: "build",
|
|
829
|
+
enforce: "pre",
|
|
830
|
+
config(config) {
|
|
831
|
+
const aliasOptions = config?.resolve?.alias ?? [];
|
|
832
|
+
if (isNativeObj(aliasOptions)) {
|
|
833
|
+
aliases = Object.entries(aliasOptions).map(([key, value]) => {
|
|
834
|
+
return { find: key, replacement: value };
|
|
835
|
+
});
|
|
836
|
+
} else {
|
|
837
|
+
aliases = ensureArray(aliasOptions).map((alias) => ({ ...alias }));
|
|
838
|
+
}
|
|
839
|
+
if (aliasesExclude.length > 0) {
|
|
840
|
+
aliases = aliases.filter(
|
|
841
|
+
({ find }) => !aliasesExclude.some(
|
|
842
|
+
(aliasExclude) => aliasExclude && (isRegExp(find) ? find.toString() === aliasExclude.toString() : isRegExp(aliasExclude) ? find.match(aliasExclude)?.[0] : find === aliasExclude)
|
|
843
|
+
)
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
for (const alias of aliases) {
|
|
847
|
+
alias.replacement = resolve(alias.replacement);
|
|
848
|
+
}
|
|
849
|
+
},
|
|
850
|
+
async configResolved(config) {
|
|
851
|
+
logger = logLevel ? (await import('vite')).createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
852
|
+
root = ensureAbsolute(options.root ?? "", config.root);
|
|
853
|
+
if (config.build.lib) {
|
|
854
|
+
const input = typeof config.build.lib.entry === "string" ? [config.build.lib.entry] : config.build.lib.entry;
|
|
855
|
+
if (Array.isArray(input)) {
|
|
856
|
+
entries = input.reduce(
|
|
857
|
+
(prev, current) => {
|
|
858
|
+
prev[node_path.basename(current)] = current;
|
|
859
|
+
return prev;
|
|
860
|
+
},
|
|
861
|
+
{}
|
|
862
|
+
);
|
|
863
|
+
} else {
|
|
864
|
+
entries = { ...input };
|
|
865
|
+
}
|
|
866
|
+
const filename = config.build.lib.fileName ?? defaultIndex;
|
|
867
|
+
const entry = typeof config.build.lib.entry === "string" ? config.build.lib.entry : Object.keys(config.build.lib.entry)[0];
|
|
868
|
+
libName = config.build.lib.name || "_default";
|
|
869
|
+
indexName = typeof filename === "string" ? filename : filename("es", entry);
|
|
870
|
+
if (!dtsRE.test(indexName)) {
|
|
871
|
+
indexName = `${indexName.replace(tjsRE, "")}.d.${extPrefix(indexName)}ts`;
|
|
872
|
+
}
|
|
873
|
+
} else {
|
|
874
|
+
logger.warn(
|
|
875
|
+
`
|
|
876
|
+
${logPrefix} ${kolorist.yellow(
|
|
877
|
+
"You are building a library that may not need to generate declaration files."
|
|
878
|
+
)}
|
|
879
|
+
`
|
|
880
|
+
);
|
|
881
|
+
libName = "_default";
|
|
882
|
+
indexName = defaultIndex;
|
|
883
|
+
}
|
|
884
|
+
if (!options.outDir) {
|
|
885
|
+
outDirs = [ensureAbsolute(config.build.outDir, root)];
|
|
886
|
+
}
|
|
887
|
+
bundleDebug("parse vite config");
|
|
888
|
+
},
|
|
889
|
+
options(options2) {
|
|
890
|
+
if (entries) return;
|
|
891
|
+
const input = typeof options2.input === "string" ? [options2.input] : options2.input;
|
|
892
|
+
if (Array.isArray(input)) {
|
|
893
|
+
entries = input.reduce(
|
|
894
|
+
(prev, current) => {
|
|
895
|
+
prev[node_path.basename(current)] = current;
|
|
896
|
+
return prev;
|
|
897
|
+
},
|
|
898
|
+
{}
|
|
899
|
+
);
|
|
900
|
+
} else {
|
|
901
|
+
entries = { ...input };
|
|
902
|
+
}
|
|
903
|
+
logger = logger || console;
|
|
904
|
+
aliases = aliases || [];
|
|
905
|
+
libName = "_default";
|
|
906
|
+
indexName = defaultIndex;
|
|
907
|
+
bundleDebug("parse options");
|
|
908
|
+
},
|
|
909
|
+
async buildStart() {
|
|
910
|
+
if (program) return;
|
|
911
|
+
bundleDebug("begin buildStart");
|
|
912
|
+
timeRecord = 0;
|
|
913
|
+
const startTime = Date.now();
|
|
914
|
+
configPath = tsconfigPath ? ensureAbsolute(tsconfigPath, root) : ts__default.findConfigFile(root, ts__default.sys.fileExists);
|
|
915
|
+
const content = configPath ? languageCore.createParsedCommandLine(ts__default, ts__default.sys, configPath) : undefined;
|
|
916
|
+
compilerOptions = {
|
|
917
|
+
...content?.options || {},
|
|
918
|
+
...options.compilerOptions || {},
|
|
919
|
+
...fixedCompilerOptions,
|
|
920
|
+
outDir: ".",
|
|
921
|
+
declarationDir: "."
|
|
922
|
+
};
|
|
923
|
+
rawCompilerOptions = content?.raw.compilerOptions || {};
|
|
924
|
+
if (content?.fileNames.find((name) => name.endsWith(".vue"))) {
|
|
925
|
+
setModuleResolution(compilerOptions);
|
|
926
|
+
}
|
|
927
|
+
if (!outDirs) {
|
|
928
|
+
outDirs = options.outDir ? ensureArray(options.outDir).map((d) => ensureAbsolute(d, root)) : [
|
|
929
|
+
ensureAbsolute(
|
|
930
|
+
content?.raw.compilerOptions?.outDir ? resolveConfigDir(content.raw.compilerOptions.outDir, root) : "dist",
|
|
931
|
+
root
|
|
932
|
+
)
|
|
933
|
+
];
|
|
934
|
+
}
|
|
935
|
+
const {
|
|
936
|
+
// Here we are using the default value to set the `baseUrl` to the current directory if no value exists. This is
|
|
937
|
+
// the same behavior as the TS Compiler. See TS source:
|
|
938
|
+
// https://github.com/microsoft/TypeScript/blob/3386e943215613c40f68ba0b108cda1ddb7faee1/src/compiler/utilities.ts#L6493-L6501
|
|
939
|
+
baseUrl = compilerOptions.paths ? process.cwd() : undefined,
|
|
940
|
+
paths
|
|
941
|
+
} = compilerOptions;
|
|
942
|
+
if (pathsToAliases && baseUrl && paths) {
|
|
943
|
+
aliases.push(
|
|
944
|
+
...parseTsAliases(
|
|
945
|
+
ensureAbsolute(
|
|
946
|
+
resolveConfigDir(baseUrl, root),
|
|
947
|
+
configPath ? node_path.dirname(configPath) : root
|
|
948
|
+
),
|
|
949
|
+
paths
|
|
950
|
+
)
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
const computeGlobs = (rootGlobs, tsGlobs, defaultGlob) => {
|
|
954
|
+
if (rootGlobs?.length) {
|
|
955
|
+
return ensureArray(rootGlobs).map(
|
|
956
|
+
(glob) => normalizeGlob(ensureAbsolute(resolveConfigDir(glob, root), root))
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
return ensureArray(tsGlobs?.length ? tsGlobs : defaultGlob).map(
|
|
960
|
+
(glob) => normalizeGlob(
|
|
961
|
+
ensureAbsolute(resolveConfigDir(glob, root), configPath ? node_path.dirname(configPath) : root)
|
|
962
|
+
)
|
|
963
|
+
);
|
|
964
|
+
};
|
|
965
|
+
include = computeGlobs(
|
|
966
|
+
options.include,
|
|
967
|
+
[...ensureArray(content?.raw.include ?? []), ...ensureArray(content?.raw.files ?? [])],
|
|
968
|
+
"**/*"
|
|
969
|
+
);
|
|
970
|
+
exclude = computeGlobs(options.exclude, content?.raw.exclude, "node_modules/**");
|
|
971
|
+
filter = pluginutils.createFilter(include, exclude);
|
|
972
|
+
rootNames = [
|
|
973
|
+
...new Set(
|
|
974
|
+
Object.values(entries).map((entry) => ensureAbsolute(entry, root)).concat(content?.fileNames.filter(filter) || []).map(normalizePath)
|
|
975
|
+
)
|
|
976
|
+
];
|
|
977
|
+
host = ts__default.createCompilerHost(compilerOptions);
|
|
978
|
+
program = (rebuildProgram = () => createProgram({
|
|
979
|
+
host,
|
|
980
|
+
rootNames,
|
|
981
|
+
options: compilerOptions,
|
|
982
|
+
projectReferences: content?.projectReferences
|
|
983
|
+
}))();
|
|
984
|
+
libName = toCapitalCase(libName || "_default");
|
|
985
|
+
indexName = indexName || defaultIndex;
|
|
986
|
+
const maybeEmitted = (sourceFile) => {
|
|
987
|
+
return !(compilerOptions.noEmitForJsFiles && jsRE.test(sourceFile.fileName)) && !sourceFile.isDeclarationFile && !program.isSourceFileFromExternalLibrary(sourceFile);
|
|
988
|
+
};
|
|
989
|
+
publicRoot = compilerOptions.rootDir ? ensureAbsolute(resolveConfigDir(compilerOptions.rootDir, root), root) : compilerOptions.composite && compilerOptions.configFilePath ? node_path.dirname(compilerOptions.configFilePath) : queryPublicPath(
|
|
990
|
+
program.getSourceFiles().filter(maybeEmitted).map((sourceFile) => sourceFile.fileName)
|
|
991
|
+
);
|
|
992
|
+
publicRoot = normalizePath(publicRoot);
|
|
993
|
+
entryRoot = entryRoot || publicRoot;
|
|
994
|
+
entryRoot = ensureAbsolute(entryRoot, root);
|
|
995
|
+
diagnostics.push(
|
|
996
|
+
...program.getDeclarationDiagnostics(),
|
|
997
|
+
...program.getSemanticDiagnostics(),
|
|
998
|
+
...program.getSyntacticDiagnostics()
|
|
999
|
+
);
|
|
1000
|
+
for (const file of rootNames) {
|
|
1001
|
+
this.addWatchFile(file);
|
|
1002
|
+
rootFiles.add(file);
|
|
1003
|
+
}
|
|
1004
|
+
bundleDebug("create ts program");
|
|
1005
|
+
timeRecord += Date.now() - startTime;
|
|
1006
|
+
},
|
|
1007
|
+
async transform(code, id) {
|
|
1008
|
+
let resolver;
|
|
1009
|
+
id = normalizePath(id).split("?")[0];
|
|
1010
|
+
if (!host || !program || !filter(id) || !(resolver = resolvers.find((r) => r.supports(id))) && !tjsRE.test(id) || transformedFiles.has(id)) {
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
const startTime = Date.now();
|
|
1014
|
+
const outDir = outDirs[0];
|
|
1015
|
+
rootFiles.delete(id);
|
|
1016
|
+
transformedFiles.add(id);
|
|
1017
|
+
if (resolver) {
|
|
1018
|
+
const result = await resolver.transform({
|
|
1019
|
+
id,
|
|
1020
|
+
code,
|
|
1021
|
+
root: publicRoot,
|
|
1022
|
+
outDir,
|
|
1023
|
+
host,
|
|
1024
|
+
program
|
|
1025
|
+
});
|
|
1026
|
+
let output;
|
|
1027
|
+
if (Array.isArray(result)) {
|
|
1028
|
+
output = result;
|
|
1029
|
+
} else {
|
|
1030
|
+
output = result.outputs;
|
|
1031
|
+
if (result.emitSkipped && result.diagnostics?.length) {
|
|
1032
|
+
diagnostics.push(...result.diagnostics);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
for (const { path, content } of output) {
|
|
1036
|
+
setOutputFile(
|
|
1037
|
+
resolve(publicRoot, node_path.relative(outDir, ensureAbsolute(path, outDir))),
|
|
1038
|
+
content
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
} else {
|
|
1042
|
+
const sourceFile = program.getSourceFile(id);
|
|
1043
|
+
if (sourceFile) {
|
|
1044
|
+
const result = program.emit(
|
|
1045
|
+
sourceFile,
|
|
1046
|
+
(name, text) => {
|
|
1047
|
+
setOutputFile(
|
|
1048
|
+
resolve(publicRoot, node_path.relative(outDir, ensureAbsolute(name, outDir))),
|
|
1049
|
+
text
|
|
1050
|
+
);
|
|
1051
|
+
},
|
|
1052
|
+
undefined,
|
|
1053
|
+
true
|
|
1054
|
+
);
|
|
1055
|
+
if (result.emitSkipped && result.diagnostics.length) {
|
|
1056
|
+
diagnostics.push(...result.diagnostics);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
const dtsId = id.replace(tjsRE, "") + ".d.ts";
|
|
1061
|
+
const dtsSourceFile = program.getSourceFile(dtsId);
|
|
1062
|
+
dtsSourceFile && filter(dtsSourceFile.fileName) && setOutputFile(normalizePath(dtsSourceFile.fileName), dtsSourceFile.getFullText());
|
|
1063
|
+
timeRecord += Date.now() - startTime;
|
|
1064
|
+
},
|
|
1065
|
+
watchChange(id) {
|
|
1066
|
+
id = normalizePath(id);
|
|
1067
|
+
if (!host || !program || !filter(id) || !resolvers.find((r) => r.supports(id)) && !tjsRE.test(id)) {
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
id = id.split("?")[0];
|
|
1071
|
+
const sourceFile = host.getSourceFile(id, ts__default.ScriptTarget.ESNext);
|
|
1072
|
+
if (sourceFile) {
|
|
1073
|
+
for (const file of rootNames) {
|
|
1074
|
+
rootFiles.add(file);
|
|
1075
|
+
}
|
|
1076
|
+
rootFiles.add(normalizePath(sourceFile.fileName));
|
|
1077
|
+
bundled = false;
|
|
1078
|
+
timeRecord = 0;
|
|
1079
|
+
program = rebuildProgram();
|
|
1080
|
+
}
|
|
1081
|
+
},
|
|
1082
|
+
async writeBundle() {
|
|
1083
|
+
transformedFiles.clear();
|
|
1084
|
+
if (!host || !program || bundled) return;
|
|
1085
|
+
bundled = true;
|
|
1086
|
+
bundleDebug("begin writeBundle");
|
|
1087
|
+
logger.info(kolorist.green(`
|
|
1088
|
+
${logPrefix} Start generate declaration files...`));
|
|
1089
|
+
const startTime = Date.now();
|
|
1090
|
+
if (diagnostics?.length) {
|
|
1091
|
+
logger.error(ts__default.formatDiagnosticsWithColorAndContext(diagnostics, host));
|
|
1092
|
+
}
|
|
1093
|
+
if (typeof afterDiagnostic === "function") {
|
|
1094
|
+
await unwrapPromise(afterDiagnostic(diagnostics));
|
|
1095
|
+
}
|
|
1096
|
+
const outDir = outDirs[0];
|
|
1097
|
+
const emittedFiles = /* @__PURE__ */ new Map();
|
|
1098
|
+
const declareModules = [];
|
|
1099
|
+
const writeOutput = async (path, content, outDir2, record = true) => {
|
|
1100
|
+
if (typeof beforeWriteFile === "function") {
|
|
1101
|
+
const result = await unwrapPromise(beforeWriteFile(path, content));
|
|
1102
|
+
if (result === false) return;
|
|
1103
|
+
if (result) {
|
|
1104
|
+
path = result.filePath || path;
|
|
1105
|
+
content = result.content ?? content;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
path = normalizePath(path);
|
|
1109
|
+
const dir = normalizePath(node_path.dirname(path));
|
|
1110
|
+
if (strictOutput && !dir.startsWith(normalizePath(outDir2))) {
|
|
1111
|
+
logger.warn(`${logPrefix} ${kolorist.yellow("Outside emitted:")} ${path}`);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
if (!node_fs.existsSync(dir)) {
|
|
1115
|
+
await promises.mkdir(dir, { recursive: true });
|
|
1116
|
+
}
|
|
1117
|
+
await promises.writeFile(path, content, "utf-8");
|
|
1118
|
+
record && emittedFiles.set(path, content);
|
|
1119
|
+
};
|
|
1120
|
+
const sourceFiles = program.getSourceFiles();
|
|
1121
|
+
for (const sourceFile of sourceFiles) {
|
|
1122
|
+
if (!filter(sourceFile.fileName)) continue;
|
|
1123
|
+
if (copyDtsFiles && dtsRE.test(sourceFile.fileName)) {
|
|
1124
|
+
setOutputFile(normalizePath(sourceFile.fileName), sourceFile.getFullText());
|
|
1125
|
+
}
|
|
1126
|
+
if (rootFiles.has(sourceFile.fileName)) {
|
|
1127
|
+
program.emit(
|
|
1128
|
+
sourceFile,
|
|
1129
|
+
(name, text) => {
|
|
1130
|
+
setOutputFile(
|
|
1131
|
+
resolve(publicRoot, node_path.relative(outDir, ensureAbsolute(name, outDir))),
|
|
1132
|
+
text
|
|
1133
|
+
);
|
|
1134
|
+
},
|
|
1135
|
+
undefined,
|
|
1136
|
+
true
|
|
1137
|
+
);
|
|
1138
|
+
rootFiles.delete(sourceFile.fileName);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
bundleDebug("emit output patch");
|
|
1142
|
+
const currentDir = host.getCurrentDirectory();
|
|
1143
|
+
const declarationFiles = /* @__PURE__ */ new Map();
|
|
1144
|
+
const mapFiles = /* @__PURE__ */ new Map();
|
|
1145
|
+
const prependMappings = /* @__PURE__ */ new Map();
|
|
1146
|
+
for (const [filePath, content] of outputFiles.entries()) {
|
|
1147
|
+
if (filePath.endsWith(".map")) {
|
|
1148
|
+
mapFiles.set(filePath, content);
|
|
1149
|
+
} else {
|
|
1150
|
+
declarationFiles.set(filePath, content);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
await runParallel(
|
|
1154
|
+
node_os.cpus().length,
|
|
1155
|
+
Array.from(declarationFiles.entries()),
|
|
1156
|
+
async ([filePath, content]) => {
|
|
1157
|
+
const newFilePath = resolve(
|
|
1158
|
+
outDir,
|
|
1159
|
+
node_path.relative(
|
|
1160
|
+
entryRoot,
|
|
1161
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1162
|
+
)
|
|
1163
|
+
);
|
|
1164
|
+
if (content) {
|
|
1165
|
+
const result = transformCode({
|
|
1166
|
+
filePath,
|
|
1167
|
+
content,
|
|
1168
|
+
aliases,
|
|
1169
|
+
aliasesExclude,
|
|
1170
|
+
staticImport,
|
|
1171
|
+
clearPureImport,
|
|
1172
|
+
cleanVueFileName
|
|
1173
|
+
});
|
|
1174
|
+
content = result.content;
|
|
1175
|
+
declareModules.push(...result.declareModules);
|
|
1176
|
+
if (result.diffLineCount) {
|
|
1177
|
+
prependMappings.set(`${newFilePath}.map`, ";".repeat(result.diffLineCount));
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
await writeOutput(newFilePath, content, outDir);
|
|
1181
|
+
}
|
|
1182
|
+
);
|
|
1183
|
+
await runParallel(
|
|
1184
|
+
node_os.cpus().length,
|
|
1185
|
+
Array.from(mapFiles.entries()),
|
|
1186
|
+
async ([filePath, content]) => {
|
|
1187
|
+
const baseDir = node_path.dirname(filePath);
|
|
1188
|
+
filePath = resolve(
|
|
1189
|
+
outDir,
|
|
1190
|
+
node_path.relative(
|
|
1191
|
+
entryRoot,
|
|
1192
|
+
cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath
|
|
1193
|
+
)
|
|
1194
|
+
);
|
|
1195
|
+
try {
|
|
1196
|
+
const sourceMap = JSON.parse(content);
|
|
1197
|
+
sourceMap.sources = sourceMap.sources.map((source) => {
|
|
1198
|
+
return normalizePath(
|
|
1199
|
+
node_path.relative(
|
|
1200
|
+
node_path.dirname(filePath),
|
|
1201
|
+
resolve(currentDir, node_path.relative(publicRoot, baseDir), source)
|
|
1202
|
+
)
|
|
1203
|
+
);
|
|
1204
|
+
});
|
|
1205
|
+
if (prependMappings.has(filePath)) {
|
|
1206
|
+
sourceMap.mappings = `${prependMappings.get(filePath)}${sourceMap.mappings}`;
|
|
1207
|
+
}
|
|
1208
|
+
content = JSON.stringify(sourceMap);
|
|
1209
|
+
} catch (e) {
|
|
1210
|
+
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${filePath}`);
|
|
1211
|
+
}
|
|
1212
|
+
await writeOutput(filePath, content, outDir);
|
|
1213
|
+
}
|
|
1214
|
+
);
|
|
1215
|
+
bundleDebug("write output");
|
|
1216
|
+
if (insertTypesEntry || rollupTypes) {
|
|
1217
|
+
const pkgPath = tryGetPkgPath(root);
|
|
1218
|
+
let pkg;
|
|
1219
|
+
try {
|
|
1220
|
+
pkg = pkgPath && node_fs.existsSync(pkgPath) ? JSON.parse(await promises.readFile(pkgPath, "utf-8")) : {};
|
|
1221
|
+
} catch (e) {
|
|
1222
|
+
}
|
|
1223
|
+
const entryNames = Object.keys(entries);
|
|
1224
|
+
const types = findTypesPath(pkg.publishConfig, pkg);
|
|
1225
|
+
const multiple = entryNames.length > 1;
|
|
1226
|
+
let typesPath = cleanPath(
|
|
1227
|
+
types ? resolve(root, types) : resolve(outDir, indexName),
|
|
1228
|
+
emittedFiles
|
|
1229
|
+
);
|
|
1230
|
+
if (!multiple && !dtsRE.test(typesPath)) {
|
|
1231
|
+
logger.warn(
|
|
1232
|
+
`
|
|
1233
|
+
${logPrefix} ${kolorist.yellow(
|
|
1234
|
+
"The resolved path of type entry is not ending with '.d.ts'."
|
|
1235
|
+
)}
|
|
1236
|
+
`
|
|
1237
|
+
);
|
|
1238
|
+
typesPath = `${typesPath.replace(tjsRE, "")}.d.${extPrefix(typesPath)}ts`;
|
|
1239
|
+
}
|
|
1240
|
+
for (const name of entryNames) {
|
|
1241
|
+
const entryDtsPath = multiple ? cleanPath(resolve(outDir, tsToDts(name)), emittedFiles) : typesPath;
|
|
1242
|
+
if (node_fs.existsSync(entryDtsPath)) continue;
|
|
1243
|
+
const sourceEntry = normalizePath(
|
|
1244
|
+
cleanPath(resolve(outDir, node_path.relative(entryRoot, tsToDts(entries[name]))), emittedFiles)
|
|
1245
|
+
);
|
|
1246
|
+
let fromPath = normalizePath(node_path.relative(node_path.dirname(entryDtsPath), sourceEntry));
|
|
1247
|
+
fromPath = fromPath.replace(dtsRE, "");
|
|
1248
|
+
fromPath = fullRelativeRE.test(fromPath) ? fromPath : `./${fromPath}`;
|
|
1249
|
+
let content = "export {}\n";
|
|
1250
|
+
if (emittedFiles.has(sourceEntry)) {
|
|
1251
|
+
if (hasNormalExport(emittedFiles.get(sourceEntry))) {
|
|
1252
|
+
content = `export * from '${fromPath}'
|
|
1253
|
+
${content}`;
|
|
1254
|
+
}
|
|
1255
|
+
if (hasExportDefault(emittedFiles.get(sourceEntry))) {
|
|
1256
|
+
content += `import ${libName} from '${fromPath}'
|
|
1257
|
+
export default ${libName}
|
|
1258
|
+
${content}`;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
await writeOutput(cleanPath(entryDtsPath, emittedFiles), content, outDir);
|
|
1262
|
+
}
|
|
1263
|
+
bundleDebug("insert index");
|
|
1264
|
+
if (rollupTypes) {
|
|
1265
|
+
logger.info(kolorist.green(`${logPrefix} Start rollup declaration files...`));
|
|
1266
|
+
const rollupFiles = /* @__PURE__ */ new Set();
|
|
1267
|
+
const compilerOptions2 = configPath ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions;
|
|
1268
|
+
const rollup = async (path) => {
|
|
1269
|
+
const result = rollupDeclarationFiles({
|
|
1270
|
+
root: publicRoot,
|
|
1271
|
+
configPath,
|
|
1272
|
+
compilerOptions: compilerOptions2,
|
|
1273
|
+
outDir,
|
|
1274
|
+
entryPath: path,
|
|
1275
|
+
fileName: node_path.basename(path),
|
|
1276
|
+
libFolder: getTsLibFolder(),
|
|
1277
|
+
rollupConfig,
|
|
1278
|
+
rollupOptions
|
|
1279
|
+
});
|
|
1280
|
+
emittedFiles.delete(path);
|
|
1281
|
+
rollupFiles.add(path);
|
|
1282
|
+
if (typeof afterRollup === "function") {
|
|
1283
|
+
await unwrapPromise(afterRollup(result));
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
if (multiple) {
|
|
1287
|
+
await runParallel(node_os.cpus().length, entryNames, async (name) => {
|
|
1288
|
+
await rollup(cleanPath(resolve(outDir, tsToDts(name)), emittedFiles));
|
|
1289
|
+
});
|
|
1290
|
+
} else {
|
|
1291
|
+
await rollup(typesPath);
|
|
1292
|
+
}
|
|
1293
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles.keys()), (f) => promises.unlink(f));
|
|
1294
|
+
removeDirIfEmpty(outDir);
|
|
1295
|
+
emittedFiles.clear();
|
|
1296
|
+
const declared = declareModules.join("\n");
|
|
1297
|
+
await runParallel(node_os.cpus().length, [...rollupFiles], async (filePath) => {
|
|
1298
|
+
await writeOutput(
|
|
1299
|
+
filePath,
|
|
1300
|
+
await promises.readFile(filePath, "utf-8") + (declared ? `
|
|
1301
|
+
${declared}` : ""),
|
|
1302
|
+
node_path.dirname(filePath)
|
|
1303
|
+
);
|
|
1304
|
+
});
|
|
1305
|
+
bundleDebug("rollup output");
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
if (outDirs.length > 1) {
|
|
1309
|
+
const extraOutDirs = outDirs.slice(1);
|
|
1310
|
+
await runParallel(node_os.cpus().length, Array.from(emittedFiles), async ([wroteFile, content]) => {
|
|
1311
|
+
const relativePath = node_path.relative(outDir, wroteFile);
|
|
1312
|
+
await Promise.all(
|
|
1313
|
+
extraOutDirs.map(async (targetOutDir) => {
|
|
1314
|
+
const path = resolve(targetOutDir, relativePath);
|
|
1315
|
+
if (wroteFile.endsWith(".map")) {
|
|
1316
|
+
if (!editSourceMapDir(content, outDir, targetOutDir)) {
|
|
1317
|
+
logger.warn(`${logPrefix} ${kolorist.yellow("Processing source map fail:")} ${path}`);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
await writeOutput(path, content, targetOutDir, false);
|
|
1321
|
+
})
|
|
1322
|
+
);
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
diagnostics.length = 0;
|
|
1326
|
+
if (typeof afterBuild === "function") {
|
|
1327
|
+
await unwrapPromise(afterBuild(emittedFiles));
|
|
1328
|
+
}
|
|
1329
|
+
bundleDebug("finish");
|
|
1330
|
+
logger.info(
|
|
1331
|
+
kolorist.green(`${logPrefix} Declaration files built in ${timeRecord + Date.now() - startTime}ms.
|
|
1332
|
+
`)
|
|
1333
|
+
);
|
|
1334
|
+
},
|
|
1335
|
+
generateBundle(_, bundle) {
|
|
1336
|
+
if (declarationOnly) {
|
|
1337
|
+
for (const id of Object.keys(bundle)) {
|
|
1338
|
+
delete bundle[id];
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
exports.default = dtsPlugin;
|
|
1346
|
+
exports.editSourceMapDir = editSourceMapDir;
|