rolldown-plugin-dts 0.9.5 → 0.9.7
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 +47 -45
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -310,13 +310,13 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
310
310
|
const appendStmts = [];
|
|
311
311
|
const prepend = (stmt) => prependStmts.push(stmt);
|
|
312
312
|
for (const [i, stmt] of program.body.entries()) {
|
|
313
|
-
const setStmt = (node) => program.body[i] =
|
|
313
|
+
const setStmt = (node) => program.body[i] = node;
|
|
314
314
|
if (rewriteImportExport(stmt, setStmt)) continue;
|
|
315
315
|
const sideEffect = stmt.type === "TSModuleDeclaration" && stmt.kind !== "namespace";
|
|
316
316
|
const isDefaultExport = stmt.type === "ExportDefaultDeclaration";
|
|
317
317
|
const isDecl = isTypeOf(stmt, ["ExportNamedDeclaration", "ExportDefaultDeclaration"]) && stmt.declaration;
|
|
318
318
|
const decl = isDecl ? stmt.declaration : stmt;
|
|
319
|
-
const setDecl = isDecl ? (node) => stmt.declaration =
|
|
319
|
+
const setDecl = isDecl ? (node) => stmt.declaration = node : setStmt;
|
|
320
320
|
if (decl.type === "VariableDeclaration" && decl.declarations.length !== 1) throw new Error("Only one declaration is supported");
|
|
321
321
|
if (decl.type !== "TSDeclareFunction" && !isDeclarationType(decl)) continue;
|
|
322
322
|
if (isTypeOf(decl, [
|
|
@@ -340,6 +340,11 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
340
340
|
...sideEffect ? [t.callExpression(t.identifier("sideEffect"), [])] : []
|
|
341
341
|
];
|
|
342
342
|
const runtime = t.arrayExpression(elements);
|
|
343
|
+
if (decl !== stmt) {
|
|
344
|
+
decl.innerComments = stmt.innerComments;
|
|
345
|
+
decl.leadingComments = stmt.leadingComments;
|
|
346
|
+
decl.trailingComments = stmt.trailingComments;
|
|
347
|
+
}
|
|
343
348
|
const symbolId = registerSymbol({
|
|
344
349
|
decl,
|
|
345
350
|
deps,
|
|
@@ -380,26 +385,6 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
380
385
|
if (!RE_DTS.test(chunk.fileName)) return;
|
|
381
386
|
const file = parse(code, { sourceType: "module" });
|
|
382
387
|
const { program } = file;
|
|
383
|
-
if (program.body.length) {
|
|
384
|
-
const comments = new Set();
|
|
385
|
-
const commentsValue = new Set();
|
|
386
|
-
for (const id of chunk.moduleIds) {
|
|
387
|
-
const preserveComments = commentsMap.get(id);
|
|
388
|
-
if (preserveComments) {
|
|
389
|
-
preserveComments.forEach((c) => {
|
|
390
|
-
const id$1 = c.type + c.value;
|
|
391
|
-
if (commentsValue.has(id$1)) return;
|
|
392
|
-
commentsValue.add(id$1);
|
|
393
|
-
comments.add(c);
|
|
394
|
-
});
|
|
395
|
-
commentsMap.delete(id);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
if (comments.size) {
|
|
399
|
-
program.body[0].leadingComments ||= [];
|
|
400
|
-
program.body[0].leadingComments.push(...comments);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
388
|
program.body = patchTsNamespace(program.body);
|
|
404
389
|
program.body = program.body.map((node) => {
|
|
405
390
|
if (patchImportSource(node)) return node;
|
|
@@ -421,9 +406,27 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
421
406
|
if (originalDep.replace) originalDep.replace(transformedDeps[i]);
|
|
422
407
|
else Object.assign(originalDep, transformedDeps[i]);
|
|
423
408
|
}
|
|
424
|
-
return
|
|
409
|
+
return inheritNodeComments(node, original.decl);
|
|
425
410
|
}).filter((node) => !!node);
|
|
426
411
|
if (program.body.length === 0) return "export { };";
|
|
412
|
+
const comments = new Set();
|
|
413
|
+
const commentsValue = new Set();
|
|
414
|
+
for (const id of chunk.moduleIds) {
|
|
415
|
+
const preserveComments = commentsMap.get(id);
|
|
416
|
+
if (preserveComments) {
|
|
417
|
+
preserveComments.forEach((c) => {
|
|
418
|
+
const id$1 = c.type + c.value;
|
|
419
|
+
if (commentsValue.has(id$1)) return;
|
|
420
|
+
commentsValue.add(id$1);
|
|
421
|
+
comments.add(c);
|
|
422
|
+
});
|
|
423
|
+
commentsMap.delete(id);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (comments.size) {
|
|
427
|
+
program.body[0].leadingComments ||= [];
|
|
428
|
+
program.body[0].leadingComments.unshift(...comments);
|
|
429
|
+
}
|
|
427
430
|
const result = generate(file, {
|
|
428
431
|
comments: true,
|
|
429
432
|
sourceMaps: sourcemap,
|
|
@@ -442,8 +445,8 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
|
|
|
442
445
|
};
|
|
443
446
|
}
|
|
444
447
|
const REFERENCE_RE = /\/\s*<reference\s+(?:path|types)=/;
|
|
445
|
-
function collectReferenceDirectives(comment) {
|
|
446
|
-
return comment.filter((c) => REFERENCE_RE.test(c.value));
|
|
448
|
+
function collectReferenceDirectives(comment, negative = false) {
|
|
449
|
+
return comment.filter((c) => REFERENCE_RE.test(c.value) !== negative);
|
|
447
450
|
}
|
|
448
451
|
function collectDependencies(node, getIdentifierIndex, prepend) {
|
|
449
452
|
const deps = new Set();
|
|
@@ -587,7 +590,8 @@ function rewriteImportExport(node, set) {
|
|
|
587
590
|
return false;
|
|
588
591
|
}
|
|
589
592
|
function importNamespace(node, imported, source, getIdentifierIndex, prepend) {
|
|
590
|
-
|
|
593
|
+
const sourceText = source.value.replaceAll(/\W/g, "_");
|
|
594
|
+
let local = t.identifier(`${sourceText}${getIdentifierIndex()}`);
|
|
591
595
|
prepend(t.importDeclaration([t.importNamespaceSpecifier(local)], source));
|
|
592
596
|
if (imported) {
|
|
593
597
|
const importedLeft = getIdFromTSEntityName(imported);
|
|
@@ -607,27 +611,25 @@ function importNamespace(node, imported, source, getIdentifierIndex, prepend) {
|
|
|
607
611
|
};
|
|
608
612
|
return dep;
|
|
609
613
|
}
|
|
610
|
-
function inheritNode(oldValue, newValue) {
|
|
611
|
-
return {
|
|
612
|
-
...newValue,
|
|
613
|
-
leadingComments: oldValue.leadingComments,
|
|
614
|
-
innerComments: oldValue.innerComments,
|
|
615
|
-
trailingComments: oldValue.trailingComments
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
614
|
function overwriteNode(node, newNode) {
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
"innerComments",
|
|
622
|
-
"trailingComments"
|
|
623
|
-
];
|
|
624
|
-
for (const key of Object.keys(node)) {
|
|
625
|
-
if (preserve.includes(key)) continue;
|
|
626
|
-
delete node[key];
|
|
627
|
-
}
|
|
628
|
-
Object.assign(node, newNode, { ...node });
|
|
615
|
+
for (const key of Object.keys(node)) delete node[key];
|
|
616
|
+
Object.assign(node, newNode);
|
|
629
617
|
return node;
|
|
630
618
|
}
|
|
619
|
+
function inheritNodeComments(oldNode, newNode) {
|
|
620
|
+
newNode.leadingComments ||= [];
|
|
621
|
+
newNode.trailingComments ||= [];
|
|
622
|
+
const leadingComments = filterHashtag(oldNode.leadingComments);
|
|
623
|
+
if (leadingComments) newNode.leadingComments.unshift(...leadingComments);
|
|
624
|
+
const trailingComments = filterHashtag(oldNode.trailingComments);
|
|
625
|
+
if (trailingComments) newNode.trailingComments.unshift(...trailingComments);
|
|
626
|
+
newNode.leadingComments = collectReferenceDirectives(newNode.leadingComments, true);
|
|
627
|
+
newNode.trailingComments = collectReferenceDirectives(newNode.trailingComments || [], true);
|
|
628
|
+
return newNode;
|
|
629
|
+
function filterHashtag(comments) {
|
|
630
|
+
return comments?.filter((comment) => comment.value.startsWith("#"));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
631
633
|
|
|
632
634
|
//#endregion
|
|
633
635
|
//#region src/utils/tsc.ts
|
|
@@ -891,7 +893,7 @@ function createDtsResolvePlugin({ tsconfig, resolve, resolvePaths }) {
|
|
|
891
893
|
...resolution,
|
|
892
894
|
meta
|
|
893
895
|
};
|
|
894
|
-
if (resolvePaths && (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id))) return resolveDependency(
|
|
896
|
+
if (resolvePaths && (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id))) return resolveDependency(id, importer);
|
|
895
897
|
if (!resolution || resolution.external) return resolution;
|
|
896
898
|
if (RE_JS.test(resolution.id)) {
|
|
897
899
|
resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, options);
|