rolldown-plugin-dts-snapshot 0.1.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 +34 -0
- package/dist/api.d.mts +4 -0
- package/dist/api.mjs +65 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.mjs +33 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025-PRESENT Kevin Deng (https://github.com/sxzz)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# rolldown-plugin-dts-snapshot
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![Unit Test][unit-test-src]][unit-test-href]
|
|
6
|
+
|
|
7
|
+
DTS snapshot plugin for Rolldown
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i rolldown-plugin-dts-snapshot
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Sponsors
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
|
|
19
|
+
<img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
|
|
20
|
+
</a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
[MIT](./LICENSE) License © 2025-PRESENT [Kevin Deng](https://github.com/sxzz)
|
|
26
|
+
|
|
27
|
+
<!-- Badges -->
|
|
28
|
+
|
|
29
|
+
[npm-version-src]: https://img.shields.io/npm/v/rolldown-plugin-dts-snapshot.svg
|
|
30
|
+
[npm-version-href]: https://npmjs.com/package/rolldown-plugin-dts-snapshot
|
|
31
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/rolldown-plugin-dts-snapshot
|
|
32
|
+
[npm-downloads-href]: https://www.npmcharts.com/compare/rolldown-plugin-dts-snapshot?interval=30
|
|
33
|
+
[unit-test-src]: https://github.com/sxzz/rolldown-plugin-dts-snapshot/actions/workflows/unit-test.yml/badge.svg
|
|
34
|
+
[unit-test-href]: https://github.com/sxzz/rolldown-plugin-dts-snapshot/actions/workflows/unit-test.yml
|
package/dist/api.d.mts
ADDED
package/dist/api.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import dprint from "dprint-node";
|
|
2
|
+
import { walk } from "estree-walker";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { parseSync } from "rolldown/experimental";
|
|
5
|
+
|
|
6
|
+
//#region src/api.ts
|
|
7
|
+
const multilineCommentsRE = /\/\*.*?\*\//gs;
|
|
8
|
+
const singlelineCommentsRE = /\/\/.*$/gm;
|
|
9
|
+
function snapshot(code, fileName = "dummy.d.ts") {
|
|
10
|
+
code = code.replaceAll(multilineCommentsRE, "").replaceAll(singlelineCommentsRE, "");
|
|
11
|
+
const s = new MagicString(code);
|
|
12
|
+
const slice = (node) => s.slice(node.start, node.end);
|
|
13
|
+
const { program } = parseSync(fileName, code);
|
|
14
|
+
walk(program, { enter(node, parent, key) {
|
|
15
|
+
if (key === "params" && node.type === "Identifier") {
|
|
16
|
+
const end = node.typeAnnotation?.start ?? node.end;
|
|
17
|
+
s.overwrite(node.start, end, "_");
|
|
18
|
+
}
|
|
19
|
+
} });
|
|
20
|
+
const result = Object.create(null);
|
|
21
|
+
for (const stmt of program.body) {
|
|
22
|
+
let decl;
|
|
23
|
+
if ((stmt.type === "ExportNamedDeclaration" || stmt.type === "ExportDefaultDeclaration") && stmt.declaration) decl = stmt.declaration;
|
|
24
|
+
else decl = stmt;
|
|
25
|
+
const register = (symbol, node) => {
|
|
26
|
+
let code$1;
|
|
27
|
+
if (node.type === "VariableDeclarator") {
|
|
28
|
+
const typeAnnotation = node.id.typeAnnotation?.typeAnnotation;
|
|
29
|
+
if (typeAnnotation) code$1 = s.slice(typeAnnotation.start, node.end);
|
|
30
|
+
else if (node.init) code$1 = slice(node.init);
|
|
31
|
+
}
|
|
32
|
+
code$1 ||= slice(node);
|
|
33
|
+
result[symbol] = format(code$1);
|
|
34
|
+
};
|
|
35
|
+
if (decl.type === "VariableDeclaration") for (const node of decl.declarations) register(nodeToString(node.id), node);
|
|
36
|
+
else if ("id" in decl && decl.id) register(nodeToString(decl.id), decl);
|
|
37
|
+
else if (decl.type === "ExportDefaultDeclaration" && "id" in decl.declaration && decl.declaration.id) register("default", decl.declaration);
|
|
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];
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
function nodeToString(node) {
|
|
46
|
+
return node.type === "Identifier" ? node.name : node.type === "Literal" ? node.value : slice(node);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function format(code) {
|
|
50
|
+
return dprint.format("dummy.d.ts", code, {
|
|
51
|
+
lineWidth: 1e8,
|
|
52
|
+
indentWidth: 1,
|
|
53
|
+
semiColons: "asi",
|
|
54
|
+
preferSingleLine: true,
|
|
55
|
+
"arrowFunction.useParentheses": "force",
|
|
56
|
+
quoteStyle: "alwaysSingle",
|
|
57
|
+
singleBodyPosition: "sameLine",
|
|
58
|
+
bracePosition: "sameLine",
|
|
59
|
+
operatorPosition: "sameLine",
|
|
60
|
+
preferHanging: true
|
|
61
|
+
}).trim().replaceAll("\n\n", "\n");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { snapshot };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FilterPattern } from "unplugin-utils";
|
|
2
|
+
import { Plugin } from "rolldown";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* @default /\.d\.[cm]?ts$/
|
|
8
|
+
*/
|
|
9
|
+
include?: FilterPattern;
|
|
10
|
+
exclude?: FilterPattern;
|
|
11
|
+
/**
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
excludeNonExport?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* @default '[cwd]/dts.snapshot.json'
|
|
17
|
+
*/
|
|
18
|
+
saveTo?: string;
|
|
19
|
+
}
|
|
20
|
+
declare function DtsSnapshot(options?: Options): Plugin;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { DtsSnapshot, Options };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { snapshot } from "./api.mjs";
|
|
2
|
+
import { writeFile } from "node:fs/promises";
|
|
3
|
+
import { createFilter } from "unplugin-utils";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const RE_DTS = /\.d\.[cm]?ts$/;
|
|
7
|
+
function DtsSnapshot(options = {}) {
|
|
8
|
+
const { include = RE_DTS, exclude, excludeNonExport = true, saveTo = "dts.snapshot.json" } = options;
|
|
9
|
+
const filter = createFilter(include, exclude);
|
|
10
|
+
return {
|
|
11
|
+
name: "rolldown-plugin-dts-snapshot",
|
|
12
|
+
generateBundle: {
|
|
13
|
+
order: "post",
|
|
14
|
+
async handler(_, bundle) {
|
|
15
|
+
const result = Object.create(null);
|
|
16
|
+
for (const chunk of Object.values(bundle)) {
|
|
17
|
+
if (chunk.type === "asset" || !filter(chunk.fileName)) continue;
|
|
18
|
+
const map = result[chunk.fileName] = snapshot(chunk.code, chunk.fileName);
|
|
19
|
+
if (chunk.isEntry) {
|
|
20
|
+
if (excludeNonExport) {
|
|
21
|
+
for (const key of Object.keys(map)) if (key !== "#exports" && !chunk.exports.includes(key)) delete map[key];
|
|
22
|
+
}
|
|
23
|
+
map["#exports"] = chunk.exports;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
await writeFile(saveTo, `${JSON.stringify(result, null, 2)}\n`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { DtsSnapshot };
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rolldown-plugin-dts-snapshot",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "DTS snapshot plugin for Rolldown",
|
|
6
|
+
"author": "Kevin Deng <sxzz@sxzz.moe>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://github.com/sponsors/sxzz",
|
|
9
|
+
"homepage": "https://github.com/sxzz/rolldown-plugin-dts-snapshot#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/sxzz/rolldown-plugin-dts-snapshot.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/sxzz/rolldown-plugin-dts-snapshot/issues"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./dist/index.mjs",
|
|
19
|
+
"./api": "./dist/api.mjs",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.mjs",
|
|
23
|
+
"module": "./dist/index.mjs",
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.19.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"rolldown": "^1.0.0-beta.56"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"dprint-node": "^1.0.8",
|
|
39
|
+
"estree-walker": "^3.0.3",
|
|
40
|
+
"magic-string": "^0.30.21",
|
|
41
|
+
"unplugin-utils": "^0.3.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@oxc-project/types": "^0.105.0",
|
|
45
|
+
"@sxzz/eslint-config": "^7.4.3",
|
|
46
|
+
"@sxzz/prettier-config": "^2.2.6",
|
|
47
|
+
"@types/node": "^25.0.3",
|
|
48
|
+
"@typescript/native-preview": "7.0.0-dev.20251221.1",
|
|
49
|
+
"bumpp": "^10.3.2",
|
|
50
|
+
"eslint": "^9.39.2",
|
|
51
|
+
"prettier": "^3.7.4",
|
|
52
|
+
"rolldown": "^1.0.0-beta.56",
|
|
53
|
+
"tsdown": "^0.18.2",
|
|
54
|
+
"typescript": "^5.9.3",
|
|
55
|
+
"vitest": "^4.0.16"
|
|
56
|
+
},
|
|
57
|
+
"prettier": "@sxzz/prettier-config",
|
|
58
|
+
"scripts": {
|
|
59
|
+
"lint": "eslint --cache .",
|
|
60
|
+
"lint:fix": "pnpm run lint --fix",
|
|
61
|
+
"build": "tsdown",
|
|
62
|
+
"dev": "tsdown --watch",
|
|
63
|
+
"test": "vitest",
|
|
64
|
+
"typecheck": "tsgo --noEmit",
|
|
65
|
+
"format": "prettier --cache --write .",
|
|
66
|
+
"release": "bumpp"
|
|
67
|
+
}
|
|
68
|
+
}
|