rolldown-plugin-access-privates 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 +2 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +124 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
- package/src/index.ts +143 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Martin Elend
|
|
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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type GeneralHookFilter, type Plugin } from "rolldown";
|
|
2
|
+
import { type ESTree } from "rolldown/utils";
|
|
3
|
+
/**
|
|
4
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
5
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
6
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
7
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
8
|
+
* @param options - Options for the plugin.
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export default function AccessPrivates({ exports, classMembers, suffix, idFilter }?: {
|
|
12
|
+
/**
|
|
13
|
+
* Whether to export all top-level variables, functions, and classes in the module.
|
|
14
|
+
* Can be an array of "variable", "function", and "class", or a function that will be called with the module ID and AST node of each variable, function, or class declaration
|
|
15
|
+
* @default true
|
|
16
|
+
*/
|
|
17
|
+
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Which class members to generate accessors for.
|
|
20
|
+
* Can be an array of "method", "get", "set", and "property", or a function that will be called with the module ID and AST node of each method or property definition.
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The suffix to use for the generated accessors. Defaults to "Private". For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
26
|
+
* @default "Private"
|
|
27
|
+
*/
|
|
28
|
+
suffix?: string | ((name: string) => string) | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Filter for which modules the plugin should apply.
|
|
31
|
+
* Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.
|
|
32
|
+
* The filter will be applied to the module ID.
|
|
33
|
+
* @default /\.[jt]sx?$/
|
|
34
|
+
*/
|
|
35
|
+
idFilter?: GeneralHookFilter | undefined;
|
|
36
|
+
}): Plugin;
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAAW,KAAK,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAS1E;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EACrC,OAAc,EACd,YAAmB,EACnB,MAAkB,EAClB,QAAuB,EACxB,GAAE;IACD;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,mBAAmB,GAAG,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,KAAK,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;IACxK;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,UAAU,CAAC,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;IACzK;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAC;IACzD;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;CACrC,GAAG,MAAM,CA6Fd"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {} from "rolldown";
|
|
2
|
+
import { withMagicString } from "rolldown-string";
|
|
3
|
+
import { Visitor } from "rolldown/utils";
|
|
4
|
+
/** Map exports types to their corresponding AST node types */
|
|
5
|
+
const exportsToType = {
|
|
6
|
+
variable: "VariableDeclaration",
|
|
7
|
+
function: "FunctionDeclaration",
|
|
8
|
+
class: "ClassDeclaration",
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
12
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
13
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
14
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
15
|
+
* @param options - Options for the plugin.
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export default function AccessPrivates({ exports = true, classMembers = true, suffix = "Private", idFilter = /\.[jt]sx?$/, } = {}) {
|
|
19
|
+
// Calculate which code path can be optimized.
|
|
20
|
+
const canNotExport = exports === false || (Array.isArray(exports) && exports.length === 0);
|
|
21
|
+
const canNotPropertyDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes("property")));
|
|
22
|
+
const canNotMethodDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some(m => m === "method" || m === "get" || m === "set")));
|
|
23
|
+
// Transform filters in to a unified format
|
|
24
|
+
if (typeof exports === "boolean") {
|
|
25
|
+
const shouldExport = exports;
|
|
26
|
+
exports = () => shouldExport;
|
|
27
|
+
}
|
|
28
|
+
else if (Array.isArray(exports)) {
|
|
29
|
+
const mappedExports = exports.map(e => exportsToType[e]);
|
|
30
|
+
exports = (_id, astNode) => mappedExports.includes(astNode.type);
|
|
31
|
+
}
|
|
32
|
+
if (typeof classMembers === "boolean") {
|
|
33
|
+
const shouldGenerate = classMembers;
|
|
34
|
+
classMembers = () => shouldGenerate;
|
|
35
|
+
}
|
|
36
|
+
else if (Array.isArray(classMembers)) {
|
|
37
|
+
const members = classMembers;
|
|
38
|
+
classMembers = (_id, astNode) => {
|
|
39
|
+
if (astNode.type === "PropertyDefinition")
|
|
40
|
+
return members.includes("property");
|
|
41
|
+
if (astNode.type === "MethodDefinition") {
|
|
42
|
+
if (astNode.kind === "method")
|
|
43
|
+
return members.includes("method");
|
|
44
|
+
if (astNode.kind === "get")
|
|
45
|
+
return members.includes("get");
|
|
46
|
+
if (astNode.kind === "set")
|
|
47
|
+
return members.includes("set");
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (typeof suffix === "string") {
|
|
53
|
+
const suffixString = suffix;
|
|
54
|
+
suffix = (name) => name + suffixString;
|
|
55
|
+
}
|
|
56
|
+
// Definition of the Plugin
|
|
57
|
+
return {
|
|
58
|
+
name: "rolldown-plugin-access-privates",
|
|
59
|
+
transform: {
|
|
60
|
+
// Filters to optimize the plugin by skipping modules that don't need to be transformed.
|
|
61
|
+
filter: {
|
|
62
|
+
id: idFilter,
|
|
63
|
+
// if there is no need to add exports, we only need to process modules that have private class members.
|
|
64
|
+
...(canNotExport ? { code: "#" } : {}),
|
|
65
|
+
},
|
|
66
|
+
// Function implementing the transformation logic.
|
|
67
|
+
handler: withMagicString(function (code, id, meta) {
|
|
68
|
+
// If ast is provided in meta, use it. Otherwise, parse the code with the appropriate language based on the file extension.
|
|
69
|
+
let ast = meta.ast !== undefined ? meta.ast : this.parse(code.original, { lang: id.endsWith('.tsx') ? 'tsx' : id.endsWith('.ts') ? 'ts' : id.endsWith('.jsx') ? 'jsx' : 'js' });
|
|
70
|
+
const visitors = {};
|
|
71
|
+
// only add the visitors needed.
|
|
72
|
+
if (!canNotExport) {
|
|
73
|
+
// Visitor to add export keyword to top-level variable, function, and class declarations.
|
|
74
|
+
visitors.Program = function (node) {
|
|
75
|
+
for (const child of node.body) {
|
|
76
|
+
if (child.type !== "VariableDeclaration" && child.type !== "FunctionDeclaration" && child.type !== "ClassDeclaration" || child.declare)
|
|
77
|
+
continue;
|
|
78
|
+
if (!exports(id, child))
|
|
79
|
+
continue;
|
|
80
|
+
code.appendLeft(child.start, "export ");
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!canNotPropertyDefinition) {
|
|
85
|
+
// Visitor to add getter and setter for private fields defined with PropertyDefinition.
|
|
86
|
+
visitors.PropertyDefinition = function (node) {
|
|
87
|
+
if (node.type !== "PropertyDefinition" || node.key.type !== "PrivateIdentifier" || node.declare || node.override)
|
|
88
|
+
return;
|
|
89
|
+
if (!classMembers(id, node))
|
|
90
|
+
return;
|
|
91
|
+
const name = suffix(node.key.name);
|
|
92
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
93
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (!canNotMethodDefinition) {
|
|
97
|
+
// Visitor to add getter and setter for private fields defined with MethodDefinition.
|
|
98
|
+
visitors.MethodDefinition = function (node) {
|
|
99
|
+
if (node.type !== "MethodDefinition" || node.key.type !== "PrivateIdentifier" || node.override)
|
|
100
|
+
return;
|
|
101
|
+
if (!classMembers(id, node))
|
|
102
|
+
return;
|
|
103
|
+
const name = suffix(node.key.name);
|
|
104
|
+
switch (node.kind) {
|
|
105
|
+
case "method":
|
|
106
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
107
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
108
|
+
break;
|
|
109
|
+
case "get":
|
|
110
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
111
|
+
break;
|
|
112
|
+
case "set":
|
|
113
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
// Walk the AST with the defined visitors to apply the transformations.
|
|
119
|
+
new Visitor(visitors).visit(ast);
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAmC,MAAM,gBAAgB,CAAC;AAE1E,8DAA8D;AAC9D,MAAM,aAAa,GAAG;IACpB,QAAQ,EAAE,qBAAqB;IAC/B,QAAQ,EAAE,qBAAqB;IAC/B,KAAK,EAAE,kBAAkB;CACjB,CAAC;AAEX;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EACrC,OAAO,GAAG,IAAI,EACd,YAAY,GAAG,IAAI,EACnB,MAAM,GAAG,SAAS,EAClB,QAAQ,GAAG,YAAY,GACxB,GAyBG,EAAE;IACJ,8CAA8C;IAC9C,MAAM,YAAY,GAAG,OAAO,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IAC3F,MAAM,wBAAwB,GAAG,YAAY,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,KA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,EAAE,KAAK,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,IAAI,sBAAsB,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC;gCAClH,MAAM;4BACR,KAAK,KAAK;gCACR,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,IAAI,oBAAoB,IAAI,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,CAAC;gCACxH,MAAM;wBACV,CAAC;oBACH,CAAC,CAAC;gBACJ,CAAC;gBACD,uEAAuE;gBACvE,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnC,CAAC,CAAC;SACH;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rolldown-plugin-access-privates",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rolldown Plugin to add accessors for class Private Fields",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/IIIMADDINIII/rolldown-plugin-access-privates"
|
|
20
|
+
},
|
|
21
|
+
"author": "IIIMADDINIII <martin@elend.name>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"private",
|
|
25
|
+
"class",
|
|
26
|
+
"fields",
|
|
27
|
+
"accessors",
|
|
28
|
+
"rolldown",
|
|
29
|
+
"rolldown-plugin",
|
|
30
|
+
"testing"
|
|
31
|
+
],
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/IIIMADDINIII/rolldown-plugin-access-privates/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/IIIMADDINIII/rolldown-plugin-access-privates#readme",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@typescript/native-preview": "^7.0.0-dev.20260324.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"rolldown": ">=1.0.0-rc.12"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"vite": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"rolldown-string": "^0.3.0",
|
|
49
|
+
"tslib": ">=2.8.1"
|
|
50
|
+
},
|
|
51
|
+
"compatiblePackages": {
|
|
52
|
+
"schemaVersion": 1,
|
|
53
|
+
"rollup": {
|
|
54
|
+
"type": "incompatible",
|
|
55
|
+
"reason": "Uses Rolldown-specific APIs"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { type GeneralHookFilter, type Plugin } from "rolldown";
|
|
2
|
+
import { withMagicString } from "rolldown-string";
|
|
3
|
+
import { Visitor, type ESTree, type VisitorObject } from "rolldown/utils";
|
|
4
|
+
|
|
5
|
+
/** Map exports types to their corresponding AST node types */
|
|
6
|
+
const exportsToType = {
|
|
7
|
+
variable: "VariableDeclaration",
|
|
8
|
+
function: "FunctionDeclaration",
|
|
9
|
+
class: "ClassDeclaration",
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
14
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
15
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
16
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
17
|
+
* @param options - Options for the plugin.
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export default function AccessPrivates({
|
|
21
|
+
exports = true,
|
|
22
|
+
classMembers = true,
|
|
23
|
+
suffix = "Private",
|
|
24
|
+
idFilter = /\.[jt]sx?$/,
|
|
25
|
+
}: {
|
|
26
|
+
/**
|
|
27
|
+
* Whether to export all top-level variables, functions, and classes in the module.
|
|
28
|
+
* Can be an array of "variable", "function", and "class", or a function that will be called with the module ID and AST node of each variable, function, or class declaration
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Which class members to generate accessors for.
|
|
34
|
+
* Can be an array of "method", "get", "set", and "property", or a function that will be called with the module ID and AST node of each method or property definition.
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The suffix to use for the generated accessors. Defaults to "Private". For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
40
|
+
* @default "Private"
|
|
41
|
+
*/
|
|
42
|
+
suffix?: string | ((name: string) => string) | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Filter for which modules the plugin should apply.
|
|
45
|
+
* Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.
|
|
46
|
+
* The filter will be applied to the module ID.
|
|
47
|
+
* @default /\.[jt]sx?$/
|
|
48
|
+
*/
|
|
49
|
+
idFilter?: GeneralHookFilter | undefined;
|
|
50
|
+
} = {}): Plugin {
|
|
51
|
+
// Calculate which code path can be optimized.
|
|
52
|
+
const canNotExport = exports === false || (Array.isArray(exports) && exports.length === 0);
|
|
53
|
+
const canNotPropertyDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes("property")));
|
|
54
|
+
const canNotMethodDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some(m => m === "method" || m === "get" || m === "set")));
|
|
55
|
+
// Transform filters in to a unified format
|
|
56
|
+
if (typeof exports === "boolean") {
|
|
57
|
+
const shouldExport = exports;
|
|
58
|
+
exports = () => shouldExport;
|
|
59
|
+
} else if (Array.isArray(exports)) {
|
|
60
|
+
const mappedExports: ESTree.Node["type"][] = exports.map(e => exportsToType[e]);
|
|
61
|
+
exports = (_id, astNode) => mappedExports.includes(astNode.type);
|
|
62
|
+
}
|
|
63
|
+
if (typeof classMembers === "boolean") {
|
|
64
|
+
const shouldGenerate = classMembers;
|
|
65
|
+
classMembers = () => shouldGenerate;
|
|
66
|
+
} else if (Array.isArray(classMembers)) {
|
|
67
|
+
const members = classMembers;
|
|
68
|
+
classMembers = (_id, astNode) => {
|
|
69
|
+
if (astNode.type === "PropertyDefinition") return members.includes("property");
|
|
70
|
+
if (astNode.type === "MethodDefinition") {
|
|
71
|
+
if (astNode.kind === "method") return members.includes("method");
|
|
72
|
+
if (astNode.kind === "get") return members.includes("get");
|
|
73
|
+
if (astNode.kind === "set") return members.includes("set");
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (typeof suffix === "string") {
|
|
79
|
+
const suffixString = suffix;
|
|
80
|
+
suffix = (name) => name + suffixString;
|
|
81
|
+
}
|
|
82
|
+
// Definition of the Plugin
|
|
83
|
+
return {
|
|
84
|
+
name: "rolldown-plugin-access-privates",
|
|
85
|
+
transform: {
|
|
86
|
+
// Filters to optimize the plugin by skipping modules that don't need to be transformed.
|
|
87
|
+
filter: {
|
|
88
|
+
id: idFilter,
|
|
89
|
+
// if there is no need to add exports, we only need to process modules that have private class members.
|
|
90
|
+
...(canNotExport ? { code: "#" } : {}),
|
|
91
|
+
},
|
|
92
|
+
// Function implementing the transformation logic.
|
|
93
|
+
handler: withMagicString(function (code, id, meta) {
|
|
94
|
+
// If ast is provided in meta, use it. Otherwise, parse the code with the appropriate language based on the file extension.
|
|
95
|
+
let ast = meta.ast !== undefined ? meta.ast : this.parse(code.original, { lang: id.endsWith('.tsx') ? 'tsx' : id.endsWith('.ts') ? 'ts' : id.endsWith('.jsx') ? 'jsx' : 'js' });
|
|
96
|
+
const visitors: VisitorObject = {};
|
|
97
|
+
// only add the visitors needed.
|
|
98
|
+
if (!canNotExport) {
|
|
99
|
+
// Visitor to add export keyword to top-level variable, function, and class declarations.
|
|
100
|
+
visitors.Program = function (node) {
|
|
101
|
+
for (const child of node.body) {
|
|
102
|
+
if (child.type !== "VariableDeclaration" && child.type !== "FunctionDeclaration" && child.type !== "ClassDeclaration" || child.declare) continue;
|
|
103
|
+
if (!exports(id, child)) continue;
|
|
104
|
+
code.appendLeft(child.start, "export ");
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!canNotPropertyDefinition) {
|
|
109
|
+
// Visitor to add getter and setter for private fields defined with PropertyDefinition.
|
|
110
|
+
visitors.PropertyDefinition = function (node) {
|
|
111
|
+
if (node.type !== "PropertyDefinition" || node.key.type !== "PrivateIdentifier" || node.declare || node.override) return;
|
|
112
|
+
if (!classMembers(id, node)) return;
|
|
113
|
+
const name = suffix(node.key.name);
|
|
114
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
115
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (!canNotMethodDefinition) {
|
|
119
|
+
// Visitor to add getter and setter for private fields defined with MethodDefinition.
|
|
120
|
+
visitors.MethodDefinition = function (node) {
|
|
121
|
+
if (node.type !== "MethodDefinition" || node.key.type !== "PrivateIdentifier" || node.override) return;
|
|
122
|
+
if (!classMembers(id, node)) return;
|
|
123
|
+
const name = suffix(node.key.name);
|
|
124
|
+
switch (node.kind) {
|
|
125
|
+
case "method":
|
|
126
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
127
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
128
|
+
break;
|
|
129
|
+
case "get":
|
|
130
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
131
|
+
break;
|
|
132
|
+
case "set":
|
|
133
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// Walk the AST with the defined visitors to apply the transformations.
|
|
139
|
+
new Visitor(visitors).visit(ast);
|
|
140
|
+
}),
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|