rolldown-plugin-dts 0.9.6 → 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.
Files changed (2) hide show
  1. package/dist/index.js +44 -43
  2. 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] = inheritNode(stmt, node);
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 = inheritNode(stmt.declaration, node) : setStmt;
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 inheritNode(node, original.decl);
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();
@@ -608,27 +611,25 @@ function importNamespace(node, imported, source, getIdentifierIndex, prepend) {
608
611
  };
609
612
  return dep;
610
613
  }
611
- function inheritNode(oldValue, newValue) {
612
- return {
613
- ...newValue,
614
- leadingComments: oldValue.leadingComments,
615
- innerComments: oldValue.innerComments,
616
- trailingComments: oldValue.trailingComments
617
- };
618
- }
619
614
  function overwriteNode(node, newNode) {
620
- const preserve = [
621
- "leadingComments",
622
- "innerComments",
623
- "trailingComments"
624
- ];
625
- for (const key of Object.keys(node)) {
626
- if (preserve.includes(key)) continue;
627
- delete node[key];
628
- }
629
- Object.assign(node, newNode, { ...node });
615
+ for (const key of Object.keys(node)) delete node[key];
616
+ Object.assign(node, newNode);
630
617
  return node;
631
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
+ }
632
633
 
633
634
  //#endregion
634
635
  //#region src/utils/tsc.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",