rolldown-plugin-dts-snapshot 0.1.0 → 0.2.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/README.md CHANGED
@@ -12,6 +12,19 @@ DTS snapshot plugin for Rolldown
12
12
  npm i rolldown-plugin-dts-snapshot
13
13
  ```
14
14
 
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { defineConfig } from 'rolldown'
19
+ import { dts } from 'rolldown-plugin-dts'
20
+ import { DtsSnapshot } from 'rolldown-plugin-dts-snapshot'
21
+
22
+ export default defineConfig({
23
+ input: 'src/index.ts',
24
+ plugins: [dts(), DtsSnapshot()],
25
+ })
26
+ ```
27
+
15
28
  ## Sponsors
16
29
 
17
30
  <p align="center">
package/dist/api.d.mts CHANGED
@@ -1,4 +1,8 @@
1
1
  //#region src/api.d.ts
2
- declare function snapshot(code: string, fileName?: string): Record<string, string>;
2
+ declare function snapshot(code: string, fileName?: string, {
3
+ applyExportRename
4
+ }?: {
5
+ applyExportRename?: boolean;
6
+ }): Record<string, string>;
3
7
  //#endregion
4
8
  export { snapshot };
package/dist/api.mjs CHANGED
@@ -6,13 +6,13 @@ import { parseSync } from "rolldown/experimental";
6
6
  //#region src/api.ts
7
7
  const multilineCommentsRE = /\/\*.*?\*\//gs;
8
8
  const singlelineCommentsRE = /\/\/.*$/gm;
9
- function snapshot(code, fileName = "dummy.d.ts") {
9
+ function snapshot(code, fileName = "dummy.d.ts", { applyExportRename = true } = {}) {
10
10
  code = code.replaceAll(multilineCommentsRE, "").replaceAll(singlelineCommentsRE, "");
11
11
  const s = new MagicString(code);
12
12
  const slice = (node) => s.slice(node.start, node.end);
13
13
  const { program } = parseSync(fileName, code);
14
14
  walk(program, { enter(node, parent, key) {
15
- if (key === "params" && node.type === "Identifier") {
15
+ if (key === "params" && (node.type === "Identifier" || node.type === "ObjectPattern" || node.type === "ArrayPattern")) {
16
16
  const end = node.typeAnnotation?.start ?? node.end;
17
17
  s.overwrite(node.start, end, "_");
18
18
  }
@@ -36,10 +36,12 @@ function snapshot(code, fileName = "dummy.d.ts") {
36
36
  else if ("id" in decl && decl.id) register(nodeToString(decl.id), decl);
37
37
  else if (decl.type === "ExportDefaultDeclaration" && "id" in decl.declaration && decl.declaration.id) register("default", decl.declaration);
38
38
  }
39
- for (const stmt of program.body) if (stmt.type === "ExportNamedDeclaration" && stmt.declaration === null && stmt.specifiers.length) for (const specifier of stmt.specifiers) {
40
- const exported = nodeToString(specifier.exported);
41
- const local = nodeToString(specifier.local);
42
- if (local !== exported) result[exported] = result[local];
39
+ if (applyExportRename) {
40
+ for (const stmt of program.body) if (stmt.type === "ExportNamedDeclaration" && stmt.declaration === null && stmt.specifiers.length) for (const specifier of stmt.specifiers) {
41
+ const exported = nodeToString(specifier.exported);
42
+ const local = nodeToString(specifier.local);
43
+ if (local !== exported) result[exported] = result[local];
44
+ }
43
45
  }
44
46
  return result;
45
47
  function nodeToString(node) {
package/dist/index.d.mts CHANGED
@@ -17,6 +17,11 @@ interface Options {
17
17
  */
18
18
  saveTo?: string;
19
19
  }
20
- declare function DtsSnapshot(options?: Options): Plugin;
20
+ declare function DtsSnapshot({
21
+ include,
22
+ exclude,
23
+ excludeNonExport,
24
+ saveTo
25
+ }?: Options): Plugin;
21
26
  //#endregion
22
27
  export { DtsSnapshot, Options };
package/dist/index.mjs CHANGED
@@ -4,8 +4,7 @@ import { createFilter } from "unplugin-utils";
4
4
 
5
5
  //#region src/index.ts
6
6
  const RE_DTS = /\.d\.[cm]?ts$/;
7
- function DtsSnapshot(options = {}) {
8
- const { include = RE_DTS, exclude, excludeNonExport = true, saveTo = "dts.snapshot.json" } = options;
7
+ function DtsSnapshot({ include = RE_DTS, exclude, excludeNonExport = true, saveTo = "dts.snapshot.json" } = {}) {
9
8
  const filter = createFilter(include, exclude);
10
9
  return {
11
10
  name: "rolldown-plugin-dts-snapshot",
@@ -15,7 +14,7 @@ function DtsSnapshot(options = {}) {
15
14
  const result = Object.create(null);
16
15
  for (const chunk of Object.values(bundle)) {
17
16
  if (chunk.type === "asset" || !filter(chunk.fileName)) continue;
18
- const map = result[chunk.fileName] = snapshot(chunk.code, chunk.fileName);
17
+ const map = result[chunk.preliminaryFileName] = snapshot(chunk.code, chunk.fileName, { applyExportRename: chunk.isEntry });
19
18
  if (chunk.isEntry) {
20
19
  if (excludeNonExport) {
21
20
  for (const key of Object.keys(map)) if (key !== "#exports" && !chunk.exports.includes(key)) delete map[key];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts-snapshot",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "description": "DTS snapshot plugin for Rolldown",
6
6
  "author": "Kevin Deng <sxzz@sxzz.moe>",
7
7
  "license": "MIT",