rolldown-plugin-dts 0.11.1 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +219 -208
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -248,17 +248,6 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
248
248
|
let identifierIdx = 0;
|
|
249
249
|
const symbolMap = new Map();
|
|
250
250
|
const commentsMap = new Map();
|
|
251
|
-
function getIdentifierIndex() {
|
|
252
|
-
return identifierIdx++;
|
|
253
|
-
}
|
|
254
|
-
function registerSymbol(info) {
|
|
255
|
-
const symbolId = symbolIdx++;
|
|
256
|
-
symbolMap.set(symbolId, info);
|
|
257
|
-
return symbolId;
|
|
258
|
-
}
|
|
259
|
-
function getSymbol(symbolId) {
|
|
260
|
-
return symbolMap.get(symbolId);
|
|
261
|
-
}
|
|
262
251
|
return {
|
|
263
252
|
name: "rolldown-plugin-dts:fake-js",
|
|
264
253
|
outputOptions(options) {
|
|
@@ -276,144 +265,9 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
276
265
|
},
|
|
277
266
|
transform: {
|
|
278
267
|
filter: { id: RE_DTS },
|
|
279
|
-
handler
|
|
280
|
-
const file = parse(code, {
|
|
281
|
-
plugins: [["typescript", { dts: true }]],
|
|
282
|
-
sourceType: "module"
|
|
283
|
-
});
|
|
284
|
-
const { program, comments } = file;
|
|
285
|
-
if (comments) {
|
|
286
|
-
const directives = collectReferenceDirectives(comments);
|
|
287
|
-
commentsMap.set(id, directives);
|
|
288
|
-
}
|
|
289
|
-
const prependStmts = [];
|
|
290
|
-
const appendStmts = [];
|
|
291
|
-
const prepend = (stmt) => prependStmts.push(stmt);
|
|
292
|
-
for (const [i, stmt] of program.body.entries()) {
|
|
293
|
-
const setStmt = (node) => program.body[i] = node;
|
|
294
|
-
if (rewriteImportExport(stmt, setStmt)) continue;
|
|
295
|
-
const sideEffect = stmt.type === "TSModuleDeclaration" && stmt.kind !== "namespace";
|
|
296
|
-
const isDefaultExport = stmt.type === "ExportDefaultDeclaration";
|
|
297
|
-
const isDecl = isTypeOf(stmt, ["ExportNamedDeclaration", "ExportDefaultDeclaration"]) && stmt.declaration;
|
|
298
|
-
const decl = isDecl ? stmt.declaration : stmt;
|
|
299
|
-
const setDecl = isDecl ? (node) => stmt.declaration = node : setStmt;
|
|
300
|
-
if (decl.type === "VariableDeclaration" && decl.declarations.length !== 1) throw new Error("Only one declaration is supported");
|
|
301
|
-
if (decl.type !== "TSDeclareFunction" && !isDeclarationType(decl)) continue;
|
|
302
|
-
if (isTypeOf(decl, [
|
|
303
|
-
"TSEnumDeclaration",
|
|
304
|
-
"ClassDeclaration",
|
|
305
|
-
"FunctionDeclaration",
|
|
306
|
-
"TSDeclareFunction",
|
|
307
|
-
"TSModuleDeclaration",
|
|
308
|
-
"VariableDeclaration"
|
|
309
|
-
])) decl.declare = true;
|
|
310
|
-
let binding = decl.type === "VariableDeclaration" ? decl.declarations[0].id : "id" in decl ? decl.id : null;
|
|
311
|
-
if (!binding) {
|
|
312
|
-
binding = t.identifier("export_default");
|
|
313
|
-
decl.id = binding;
|
|
314
|
-
}
|
|
315
|
-
binding = sideEffect ? t.identifier(`_${identifierIdx++}`) : binding;
|
|
316
|
-
const deps = collectDependencies(decl, getIdentifierIndex, prepend);
|
|
317
|
-
const elements = [
|
|
318
|
-
t.numericLiteral(0),
|
|
319
|
-
...deps.map((dep) => t.arrowFunctionExpression([], dep)),
|
|
320
|
-
...sideEffect ? [t.callExpression(t.identifier("sideEffect"), [])] : []
|
|
321
|
-
];
|
|
322
|
-
const runtime = t.arrayExpression(elements);
|
|
323
|
-
if (decl !== stmt) {
|
|
324
|
-
decl.innerComments = stmt.innerComments;
|
|
325
|
-
decl.leadingComments = stmt.leadingComments;
|
|
326
|
-
decl.trailingComments = stmt.trailingComments;
|
|
327
|
-
}
|
|
328
|
-
const symbolId = registerSymbol({
|
|
329
|
-
decl,
|
|
330
|
-
deps,
|
|
331
|
-
binding
|
|
332
|
-
});
|
|
333
|
-
elements[0] = t.numericLiteral(symbolId);
|
|
334
|
-
const runtimeAssignment = {
|
|
335
|
-
type: "VariableDeclaration",
|
|
336
|
-
kind: "var",
|
|
337
|
-
declarations: [{
|
|
338
|
-
type: "VariableDeclarator",
|
|
339
|
-
id: {
|
|
340
|
-
...binding,
|
|
341
|
-
typeAnnotation: null
|
|
342
|
-
},
|
|
343
|
-
init: runtime
|
|
344
|
-
}]
|
|
345
|
-
};
|
|
346
|
-
if (isDefaultExport) {
|
|
347
|
-
appendStmts.push(t.exportNamedDeclaration(null, [t.exportSpecifier(binding, t.identifier("default"))]));
|
|
348
|
-
setStmt(runtimeAssignment);
|
|
349
|
-
} else setDecl(runtimeAssignment);
|
|
350
|
-
}
|
|
351
|
-
program.body = [
|
|
352
|
-
...prependStmts,
|
|
353
|
-
...program.body,
|
|
354
|
-
...appendStmts
|
|
355
|
-
];
|
|
356
|
-
const result = generate(file, {
|
|
357
|
-
comments: true,
|
|
358
|
-
sourceMaps: sourcemap,
|
|
359
|
-
sourceFileName: id
|
|
360
|
-
});
|
|
361
|
-
return result;
|
|
362
|
-
}
|
|
363
|
-
},
|
|
364
|
-
renderChunk(code, chunk) {
|
|
365
|
-
if (!RE_DTS.test(chunk.fileName)) return;
|
|
366
|
-
const file = parse(code, { sourceType: "module" });
|
|
367
|
-
const { program } = file;
|
|
368
|
-
program.body = patchTsNamespace(program.body);
|
|
369
|
-
program.body = program.body.map((node) => {
|
|
370
|
-
if (patchImportSource(node)) return node;
|
|
371
|
-
if (node.type !== "VariableDeclaration" || node.declarations.length !== 1) return node;
|
|
372
|
-
const [decl] = node.declarations;
|
|
373
|
-
if (decl.init?.type !== "ArrayExpression" || !decl.init.elements[0]) return null;
|
|
374
|
-
const [symbolIdNode, ...depsFns] = decl.init.elements;
|
|
375
|
-
if (symbolIdNode?.type !== "NumericLiteral") return null;
|
|
376
|
-
const symbolId = symbolIdNode.value;
|
|
377
|
-
const original = getSymbol(symbolId);
|
|
378
|
-
const transformedBinding = {
|
|
379
|
-
...decl.id,
|
|
380
|
-
typeAnnotation: original.binding.typeAnnotation
|
|
381
|
-
};
|
|
382
|
-
overwriteNode(original.binding, transformedBinding);
|
|
383
|
-
const transformedDeps = depsFns.filter((node$1) => node$1?.type === "ArrowFunctionExpression").map((node$1) => node$1.body);
|
|
384
|
-
if (original.deps.length) for (let i = 0; i < original.deps.length; i++) {
|
|
385
|
-
const originalDep = original.deps[i];
|
|
386
|
-
if (originalDep.replace) originalDep.replace(transformedDeps[i]);
|
|
387
|
-
else Object.assign(originalDep, transformedDeps[i]);
|
|
388
|
-
}
|
|
389
|
-
return inheritNodeComments(node, original.decl);
|
|
390
|
-
}).filter((node) => !!node);
|
|
391
|
-
if (program.body.length === 0) return "export { };";
|
|
392
|
-
const comments = new Set();
|
|
393
|
-
const commentsValue = new Set();
|
|
394
|
-
for (const id of chunk.moduleIds) {
|
|
395
|
-
const preserveComments = commentsMap.get(id);
|
|
396
|
-
if (preserveComments) {
|
|
397
|
-
preserveComments.forEach((c) => {
|
|
398
|
-
const id$1 = c.type + c.value;
|
|
399
|
-
if (commentsValue.has(id$1)) return;
|
|
400
|
-
commentsValue.add(id$1);
|
|
401
|
-
comments.add(c);
|
|
402
|
-
});
|
|
403
|
-
commentsMap.delete(id);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
if (comments.size) {
|
|
407
|
-
program.body[0].leadingComments ||= [];
|
|
408
|
-
program.body[0].leadingComments.unshift(...comments);
|
|
409
|
-
}
|
|
410
|
-
const result = generate(file, {
|
|
411
|
-
comments: true,
|
|
412
|
-
sourceMaps: sourcemap,
|
|
413
|
-
sourceFileName: chunk.fileName
|
|
414
|
-
});
|
|
415
|
-
return result;
|
|
268
|
+
handler: transform
|
|
416
269
|
},
|
|
270
|
+
renderChunk,
|
|
417
271
|
generateBundle(options, bundle) {
|
|
418
272
|
for (const chunk of Object.values(bundle)) {
|
|
419
273
|
if (chunk.type !== "asset" || !RE_DTS_MAP.test(chunk.fileName) || typeof chunk.source !== "string") continue;
|
|
@@ -425,48 +279,221 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
425
279
|
}
|
|
426
280
|
}
|
|
427
281
|
};
|
|
282
|
+
function transform(code, id) {
|
|
283
|
+
const file = parse(code, {
|
|
284
|
+
plugins: [["typescript", { dts: true }]],
|
|
285
|
+
sourceType: "module"
|
|
286
|
+
});
|
|
287
|
+
const { program, comments } = file;
|
|
288
|
+
if (comments) {
|
|
289
|
+
const directives = collectReferenceDirectives(comments);
|
|
290
|
+
commentsMap.set(id, directives);
|
|
291
|
+
}
|
|
292
|
+
const appendStmts = [];
|
|
293
|
+
const namespaceStmts = new Map();
|
|
294
|
+
for (const [i, stmt] of program.body.entries()) {
|
|
295
|
+
const setStmt = (node) => program.body[i] = node;
|
|
296
|
+
if (rewriteImportExport(stmt, setStmt)) continue;
|
|
297
|
+
const sideEffect = stmt.type === "TSModuleDeclaration" && stmt.kind !== "namespace";
|
|
298
|
+
const isDefaultExport = stmt.type === "ExportDefaultDeclaration";
|
|
299
|
+
const isDecl = isTypeOf(stmt, ["ExportNamedDeclaration", "ExportDefaultDeclaration"]) && stmt.declaration;
|
|
300
|
+
const decl = isDecl ? stmt.declaration : stmt;
|
|
301
|
+
const setDecl = isDecl ? (node) => stmt.declaration = node : setStmt;
|
|
302
|
+
if (decl.type === "VariableDeclaration" && decl.declarations.length !== 1) throw new Error("Only one declaration is supported");
|
|
303
|
+
if (decl.type !== "TSDeclareFunction" && !isDeclarationType(decl)) continue;
|
|
304
|
+
if (isTypeOf(decl, [
|
|
305
|
+
"TSEnumDeclaration",
|
|
306
|
+
"ClassDeclaration",
|
|
307
|
+
"FunctionDeclaration",
|
|
308
|
+
"TSDeclareFunction",
|
|
309
|
+
"TSModuleDeclaration",
|
|
310
|
+
"VariableDeclaration"
|
|
311
|
+
])) decl.declare = true;
|
|
312
|
+
let binding = decl.type === "VariableDeclaration" ? decl.declarations[0].id : "id" in decl ? decl.id : null;
|
|
313
|
+
if (!binding) {
|
|
314
|
+
binding = t.identifier("export_default");
|
|
315
|
+
decl.id = binding;
|
|
316
|
+
}
|
|
317
|
+
binding = sideEffect ? t.identifier(`_${identifierIdx++}`) : binding;
|
|
318
|
+
const deps = collectDependencies(decl, namespaceStmts);
|
|
319
|
+
const elements = [
|
|
320
|
+
t.numericLiteral(0),
|
|
321
|
+
...deps.map((dep) => t.arrowFunctionExpression([], dep)),
|
|
322
|
+
...sideEffect ? [t.callExpression(t.identifier("sideEffect"), [])] : []
|
|
323
|
+
];
|
|
324
|
+
const runtime = t.arrayExpression(elements);
|
|
325
|
+
if (decl !== stmt) {
|
|
326
|
+
decl.innerComments = stmt.innerComments;
|
|
327
|
+
decl.leadingComments = stmt.leadingComments;
|
|
328
|
+
decl.trailingComments = stmt.trailingComments;
|
|
329
|
+
}
|
|
330
|
+
const symbolId = registerSymbol({
|
|
331
|
+
decl,
|
|
332
|
+
deps,
|
|
333
|
+
binding
|
|
334
|
+
});
|
|
335
|
+
elements[0] = t.numericLiteral(symbolId);
|
|
336
|
+
const runtimeAssignment = {
|
|
337
|
+
type: "VariableDeclaration",
|
|
338
|
+
kind: "var",
|
|
339
|
+
declarations: [{
|
|
340
|
+
type: "VariableDeclarator",
|
|
341
|
+
id: {
|
|
342
|
+
...binding,
|
|
343
|
+
typeAnnotation: null
|
|
344
|
+
},
|
|
345
|
+
init: runtime
|
|
346
|
+
}]
|
|
347
|
+
};
|
|
348
|
+
if (isDefaultExport) {
|
|
349
|
+
appendStmts.push(t.exportNamedDeclaration(null, [t.exportSpecifier(binding, t.identifier("default"))]));
|
|
350
|
+
setStmt(runtimeAssignment);
|
|
351
|
+
} else setDecl(runtimeAssignment);
|
|
352
|
+
}
|
|
353
|
+
program.body = [
|
|
354
|
+
...Array.from(namespaceStmts.values()).map(({ stmt }) => stmt),
|
|
355
|
+
...program.body,
|
|
356
|
+
...appendStmts
|
|
357
|
+
];
|
|
358
|
+
const result = generate(file, {
|
|
359
|
+
comments: true,
|
|
360
|
+
sourceMaps: sourcemap,
|
|
361
|
+
sourceFileName: id
|
|
362
|
+
});
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
function renderChunk(code, chunk) {
|
|
366
|
+
if (!RE_DTS.test(chunk.fileName)) return;
|
|
367
|
+
const file = parse(code, { sourceType: "module" });
|
|
368
|
+
const { program } = file;
|
|
369
|
+
program.body = patchTsNamespace(program.body);
|
|
370
|
+
program.body = program.body.map((node) => {
|
|
371
|
+
if (patchImportSource(node)) return node;
|
|
372
|
+
if (node.type !== "VariableDeclaration" || node.declarations.length !== 1) return node;
|
|
373
|
+
const [decl] = node.declarations;
|
|
374
|
+
if (decl.init?.type !== "ArrayExpression" || !decl.init.elements[0]) return null;
|
|
375
|
+
const [symbolIdNode, ...depsFns] = decl.init.elements;
|
|
376
|
+
if (symbolIdNode?.type !== "NumericLiteral") return null;
|
|
377
|
+
const symbolId = symbolIdNode.value;
|
|
378
|
+
const original = getSymbol(symbolId);
|
|
379
|
+
const transformedBinding = {
|
|
380
|
+
...decl.id,
|
|
381
|
+
typeAnnotation: original.binding.typeAnnotation
|
|
382
|
+
};
|
|
383
|
+
overwriteNode(original.binding, transformedBinding);
|
|
384
|
+
const transformedDeps = depsFns.filter((node$1) => node$1?.type === "ArrowFunctionExpression").map((node$1) => node$1.body);
|
|
385
|
+
if (original.deps.length) for (let i = 0; i < original.deps.length; i++) {
|
|
386
|
+
const originalDep = original.deps[i];
|
|
387
|
+
if (originalDep.replace) originalDep.replace(transformedDeps[i]);
|
|
388
|
+
else Object.assign(originalDep, transformedDeps[i]);
|
|
389
|
+
}
|
|
390
|
+
return inheritNodeComments(node, original.decl);
|
|
391
|
+
}).filter((node) => !!node);
|
|
392
|
+
if (program.body.length === 0) return "export { };";
|
|
393
|
+
const comments = new Set();
|
|
394
|
+
const commentsValue = new Set();
|
|
395
|
+
for (const id of chunk.moduleIds) {
|
|
396
|
+
const preserveComments = commentsMap.get(id);
|
|
397
|
+
if (preserveComments) {
|
|
398
|
+
preserveComments.forEach((c) => {
|
|
399
|
+
const id$1 = c.type + c.value;
|
|
400
|
+
if (commentsValue.has(id$1)) return;
|
|
401
|
+
commentsValue.add(id$1);
|
|
402
|
+
comments.add(c);
|
|
403
|
+
});
|
|
404
|
+
commentsMap.delete(id);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (comments.size) {
|
|
408
|
+
program.body[0].leadingComments ||= [];
|
|
409
|
+
program.body[0].leadingComments.unshift(...comments);
|
|
410
|
+
}
|
|
411
|
+
const result = generate(file, {
|
|
412
|
+
comments: true,
|
|
413
|
+
sourceMaps: sourcemap,
|
|
414
|
+
sourceFileName: chunk.fileName
|
|
415
|
+
});
|
|
416
|
+
return result;
|
|
417
|
+
}
|
|
418
|
+
function getIdentifierIndex() {
|
|
419
|
+
return identifierIdx++;
|
|
420
|
+
}
|
|
421
|
+
function registerSymbol(info) {
|
|
422
|
+
const symbolId = symbolIdx++;
|
|
423
|
+
symbolMap.set(symbolId, info);
|
|
424
|
+
return symbolId;
|
|
425
|
+
}
|
|
426
|
+
function getSymbol(symbolId) {
|
|
427
|
+
return symbolMap.get(symbolId);
|
|
428
|
+
}
|
|
429
|
+
function collectDependencies(node, namespaceStmts) {
|
|
430
|
+
const deps = new Set();
|
|
431
|
+
const seen = new Set();
|
|
432
|
+
walk(node, { leave(node$1) {
|
|
433
|
+
if (node$1.type === "ExportNamedDeclaration") {
|
|
434
|
+
for (const specifier of node$1.specifiers) if (specifier.type === "ExportSpecifier") addDependency(specifier.local);
|
|
435
|
+
} else if (node$1.type === "TSInterfaceDeclaration" && node$1.extends) for (const heritage of node$1.extends || []) addDependency(TSEntityNameToRuntime(heritage.expression));
|
|
436
|
+
else if (node$1.type === "ClassDeclaration") {
|
|
437
|
+
if (node$1.superClass) addDependency(node$1.superClass);
|
|
438
|
+
if (node$1.implements) for (const implement of node$1.implements) addDependency(TSEntityNameToRuntime(implement.expression));
|
|
439
|
+
} else if (isTypeOf(node$1, [
|
|
440
|
+
"ObjectMethod",
|
|
441
|
+
"ObjectProperty",
|
|
442
|
+
"ClassProperty",
|
|
443
|
+
"TSPropertySignature",
|
|
444
|
+
"TSDeclareMethod"
|
|
445
|
+
])) {
|
|
446
|
+
if (node$1.computed && isReferenceId(node$1.key)) addDependency(node$1.key);
|
|
447
|
+
if ("value" in node$1 && isReferenceId(node$1.value)) addDependency(node$1.value);
|
|
448
|
+
} else if (node$1.type === "TSTypeReference") addDependency(TSEntityNameToRuntime(node$1.typeName));
|
|
449
|
+
else if (node$1.type === "TSTypeQuery") {
|
|
450
|
+
if (seen.has(node$1.exprName)) return;
|
|
451
|
+
if (node$1.exprName.type !== "TSImportType") addDependency(TSEntityNameToRuntime(node$1.exprName));
|
|
452
|
+
} else if (node$1.type === "TSImportType") {
|
|
453
|
+
seen.add(node$1);
|
|
454
|
+
const source = node$1.argument;
|
|
455
|
+
const imported = node$1.qualifier;
|
|
456
|
+
const dep = importNamespace(node$1, imported, source, namespaceStmts);
|
|
457
|
+
addDependency(dep);
|
|
458
|
+
}
|
|
459
|
+
} });
|
|
460
|
+
return Array.from(deps);
|
|
461
|
+
function addDependency(node$1) {
|
|
462
|
+
if (node$1.type === "Identifier" && node$1.name === "this") return;
|
|
463
|
+
deps.add(node$1);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
function importNamespace(node, imported, source, namespaceStmts) {
|
|
467
|
+
const sourceText = source.value.replaceAll(/\W/g, "_");
|
|
468
|
+
let local = t.identifier(`${sourceText}${getIdentifierIndex()}`);
|
|
469
|
+
if (namespaceStmts.has(source.value)) local = namespaceStmts.get(source.value).local;
|
|
470
|
+
else namespaceStmts.set(source.value, {
|
|
471
|
+
stmt: t.importDeclaration([t.importNamespaceSpecifier(local)], source),
|
|
472
|
+
local
|
|
473
|
+
});
|
|
474
|
+
if (imported) {
|
|
475
|
+
const importedLeft = getIdFromTSEntityName(imported);
|
|
476
|
+
overwriteNode(importedLeft, t.tsQualifiedName(local, { ...importedLeft }));
|
|
477
|
+
local = imported;
|
|
478
|
+
}
|
|
479
|
+
let replacement = node;
|
|
480
|
+
if (node.typeParameters) {
|
|
481
|
+
overwriteNode(node, t.tsTypeReference(local, node.typeParameters));
|
|
482
|
+
replacement = local;
|
|
483
|
+
} else overwriteNode(node, local);
|
|
484
|
+
const dep = {
|
|
485
|
+
...TSEntityNameToRuntime(local),
|
|
486
|
+
replace(newNode) {
|
|
487
|
+
overwriteNode(replacement, newNode);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
return dep;
|
|
491
|
+
}
|
|
428
492
|
}
|
|
429
493
|
const REFERENCE_RE = /\/\s*<reference\s+(?:path|types)=/;
|
|
430
494
|
function collectReferenceDirectives(comment, negative = false) {
|
|
431
495
|
return comment.filter((c) => REFERENCE_RE.test(c.value) !== negative);
|
|
432
496
|
}
|
|
433
|
-
function collectDependencies(node, getIdentifierIndex, prepend) {
|
|
434
|
-
const deps = new Set();
|
|
435
|
-
const seen = new Set();
|
|
436
|
-
walk(node, { leave(node$1) {
|
|
437
|
-
if (node$1.type === "ExportNamedDeclaration") {
|
|
438
|
-
for (const specifier of node$1.specifiers) if (specifier.type === "ExportSpecifier") addDependency(specifier.local);
|
|
439
|
-
} else if (node$1.type === "TSInterfaceDeclaration" && node$1.extends) for (const heritage of node$1.extends || []) addDependency(TSEntityNameToRuntime(heritage.expression));
|
|
440
|
-
else if (node$1.type === "ClassDeclaration") {
|
|
441
|
-
if (node$1.superClass) addDependency(node$1.superClass);
|
|
442
|
-
if (node$1.implements) for (const implement of node$1.implements) addDependency(TSEntityNameToRuntime(implement.expression));
|
|
443
|
-
} else if (isTypeOf(node$1, [
|
|
444
|
-
"ObjectMethod",
|
|
445
|
-
"ObjectProperty",
|
|
446
|
-
"ClassProperty",
|
|
447
|
-
"TSPropertySignature",
|
|
448
|
-
"TSDeclareMethod"
|
|
449
|
-
])) {
|
|
450
|
-
if (node$1.computed && isReferenceId(node$1.key)) addDependency(node$1.key);
|
|
451
|
-
if ("value" in node$1 && isReferenceId(node$1.value)) addDependency(node$1.value);
|
|
452
|
-
} else if (node$1.type === "TSTypeReference") addDependency(TSEntityNameToRuntime(node$1.typeName));
|
|
453
|
-
else if (node$1.type === "TSTypeQuery") {
|
|
454
|
-
if (seen.has(node$1.exprName)) return;
|
|
455
|
-
if (node$1.exprName.type !== "TSImportType") addDependency(TSEntityNameToRuntime(node$1.exprName));
|
|
456
|
-
} else if (node$1.type === "TSImportType") {
|
|
457
|
-
seen.add(node$1);
|
|
458
|
-
const source = node$1.argument;
|
|
459
|
-
const imported = node$1.qualifier;
|
|
460
|
-
const dep = importNamespace(node$1, imported, source, getIdentifierIndex, prepend);
|
|
461
|
-
addDependency(dep);
|
|
462
|
-
}
|
|
463
|
-
} });
|
|
464
|
-
return Array.from(deps);
|
|
465
|
-
function addDependency(node$1) {
|
|
466
|
-
if (node$1.type === "Identifier" && node$1.name === "this") return;
|
|
467
|
-
deps.add(node$1);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
497
|
function TSEntityNameToRuntime(node) {
|
|
471
498
|
if (node.type === "Identifier") return node;
|
|
472
499
|
const left = TSEntityNameToRuntime(node.left);
|
|
@@ -571,28 +598,6 @@ function rewriteImportExport(node, set) {
|
|
|
571
598
|
}
|
|
572
599
|
return false;
|
|
573
600
|
}
|
|
574
|
-
function importNamespace(node, imported, source, getIdentifierIndex, prepend) {
|
|
575
|
-
const sourceText = source.value.replaceAll(/\W/g, "_");
|
|
576
|
-
let local = t.identifier(`${sourceText}${getIdentifierIndex()}`);
|
|
577
|
-
prepend(t.importDeclaration([t.importNamespaceSpecifier(local)], source));
|
|
578
|
-
if (imported) {
|
|
579
|
-
const importedLeft = getIdFromTSEntityName(imported);
|
|
580
|
-
overwriteNode(importedLeft, t.tsQualifiedName(local, { ...importedLeft }));
|
|
581
|
-
local = imported;
|
|
582
|
-
}
|
|
583
|
-
let replacement = node;
|
|
584
|
-
if (node.typeParameters) {
|
|
585
|
-
overwriteNode(node, t.tsTypeReference(local, node.typeParameters));
|
|
586
|
-
replacement = local;
|
|
587
|
-
} else overwriteNode(node, local);
|
|
588
|
-
const dep = {
|
|
589
|
-
...TSEntityNameToRuntime(local),
|
|
590
|
-
replace(newNode) {
|
|
591
|
-
overwriteNode(replacement, newNode);
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
return dep;
|
|
595
|
-
}
|
|
596
601
|
function overwriteNode(node, newNode) {
|
|
597
602
|
for (const key of Object.keys(node)) delete node[key];
|
|
598
603
|
Object.assign(node, newNode);
|
|
@@ -761,7 +766,10 @@ function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emit
|
|
|
761
766
|
};
|
|
762
767
|
},
|
|
763
768
|
resolveId(id) {
|
|
764
|
-
if (dtsMap.has(id))
|
|
769
|
+
if (dtsMap.has(id)) {
|
|
770
|
+
debug$1("resolve dts id %s", id);
|
|
771
|
+
return { id };
|
|
772
|
+
}
|
|
765
773
|
},
|
|
766
774
|
transform: {
|
|
767
775
|
order: "pre",
|
|
@@ -778,6 +786,7 @@ function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emit
|
|
|
778
786
|
id,
|
|
779
787
|
isEntry
|
|
780
788
|
});
|
|
789
|
+
debug$1("register dts source: %s", id);
|
|
781
790
|
if (isEntry) {
|
|
782
791
|
const name = inputAliasMap.get(id);
|
|
783
792
|
this.emitFile({
|
|
@@ -799,6 +808,7 @@ function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emit
|
|
|
799
808
|
const { code, id, isEntry } = dtsMap.get(dtsId);
|
|
800
809
|
let dtsCode;
|
|
801
810
|
let map;
|
|
811
|
+
debug$1("generate dts %s from %s", dtsId, id);
|
|
802
812
|
if (isolatedDeclarations) {
|
|
803
813
|
const result = isolatedDeclaration(id, code, isolatedDeclarations);
|
|
804
814
|
if (result.errors.length) {
|
|
@@ -860,6 +870,7 @@ function createDtsResolvePlugin({ tsconfig, resolve }) {
|
|
|
860
870
|
moduleSideEffects: false
|
|
861
871
|
};
|
|
862
872
|
let resolution = resolver(id, importer);
|
|
873
|
+
resolution &&= path.normalize(resolution);
|
|
863
874
|
if (!resolution || !RE_TS.test(resolution)) {
|
|
864
875
|
const result = await this.resolve(id, importer, options);
|
|
865
876
|
if (!result || !RE_TS.test(result.id)) return external;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-plugin-dts",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"description": "A Rolldown plugin to bundle dts files",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,39 +38,39 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@babel/generator": "^7.27.1",
|
|
41
|
-
"@babel/parser": "^7.27.
|
|
41
|
+
"@babel/parser": "^7.27.2",
|
|
42
42
|
"@babel/types": "^7.27.1",
|
|
43
43
|
"ast-kit": "^1.4.3",
|
|
44
44
|
"debug": "^4.4.0",
|
|
45
|
-
"dts-resolver": "^1.1.
|
|
45
|
+
"dts-resolver": "^1.1.2",
|
|
46
46
|
"get-tsconfig": "^4.10.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@sxzz/eslint-config": "^
|
|
49
|
+
"@sxzz/eslint-config": "^7.0.0",
|
|
50
50
|
"@sxzz/prettier-config": "^2.2.1",
|
|
51
51
|
"@sxzz/test-utils": "^0.5.6",
|
|
52
52
|
"@types/babel__generator": "^7.27.0",
|
|
53
53
|
"@types/debug": "^4.1.12",
|
|
54
54
|
"@types/diff": "^7.0.2",
|
|
55
|
-
"@types/node": "^22.15.
|
|
55
|
+
"@types/node": "^22.15.17",
|
|
56
56
|
"bumpp": "^10.1.0",
|
|
57
57
|
"diff": "^7.0.0",
|
|
58
|
-
"eslint": "^9.
|
|
58
|
+
"eslint": "^9.26.0",
|
|
59
59
|
"estree-walker": "^3.0.3",
|
|
60
60
|
"prettier": "^3.5.3",
|
|
61
|
-
"rolldown": "
|
|
61
|
+
"rolldown": "nightly",
|
|
62
62
|
"rollup-plugin-dts": "^6.2.1",
|
|
63
63
|
"tinyglobby": "^0.2.13",
|
|
64
|
-
"tsdown": "^0.
|
|
64
|
+
"tsdown": "^0.11.1",
|
|
65
65
|
"tsx": "^4.19.4",
|
|
66
66
|
"typescript": "^5.8.3",
|
|
67
|
-
"vitest": "^3.1.
|
|
67
|
+
"vitest": "^3.1.3"
|
|
68
68
|
},
|
|
69
69
|
"engines": {
|
|
70
70
|
"node": ">=20.18.0"
|
|
71
71
|
},
|
|
72
72
|
"resolutions": {
|
|
73
|
-
"rolldown": "
|
|
73
|
+
"rolldown": "nightly",
|
|
74
74
|
"rolldown-plugin-dts": "workspace:*"
|
|
75
75
|
},
|
|
76
76
|
"prettier": "@sxzz/prettier-config",
|