babel-plugin-vona-bean-module 1.0.1
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 +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +86 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Vona
|
|
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 @@
|
|
|
1
|
+
# babel-plugin-vona-bean-module
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PluginPass } from '@babel/core';
|
|
2
|
+
import type { Visitor } from '@babel/traverse';
|
|
3
|
+
export interface PluginZovaBeanModuleOptions {
|
|
4
|
+
brandName: 'zova' | 'vona';
|
|
5
|
+
}
|
|
6
|
+
export default function (_plugin: any, options: PluginZovaBeanModuleOptions): {
|
|
7
|
+
visitor: Visitor<PluginPass>;
|
|
8
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { /* template, */ types as t } from '@babel/core';
|
|
2
|
+
import { parseInfoFromPath } from '@cabloy/module-info';
|
|
3
|
+
export default function (_plugin, options) {
|
|
4
|
+
const visitor = {
|
|
5
|
+
Program(path, state) {
|
|
6
|
+
const beanInfo = __parseBeanInfo(state);
|
|
7
|
+
if (!beanInfo)
|
|
8
|
+
return;
|
|
9
|
+
// context
|
|
10
|
+
const context = {
|
|
11
|
+
needBeanInfo: false,
|
|
12
|
+
};
|
|
13
|
+
// traverse
|
|
14
|
+
path.traverse(createVisitor(context, beanInfo));
|
|
15
|
+
// insertImport
|
|
16
|
+
if (context.needBeanInfo) {
|
|
17
|
+
insertImport(path, options);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
return { visitor };
|
|
22
|
+
}
|
|
23
|
+
function createVisitor(context, beanInfo) {
|
|
24
|
+
return {
|
|
25
|
+
ExpressionStatement(path) {
|
|
26
|
+
if (!t.isAssignmentExpression(path.node.expression))
|
|
27
|
+
return;
|
|
28
|
+
const right = path.node.expression.right;
|
|
29
|
+
if (!t.isCallExpression(right))
|
|
30
|
+
return;
|
|
31
|
+
if (right.callee.name !== '__decorate')
|
|
32
|
+
return;
|
|
33
|
+
if (right.arguments.length !== 2)
|
|
34
|
+
return;
|
|
35
|
+
const decorators = right.arguments[0];
|
|
36
|
+
if (!t.isArrayExpression(decorators))
|
|
37
|
+
return;
|
|
38
|
+
const decoratorNode = __createDecoratorNode(beanInfo);
|
|
39
|
+
decorators.elements.push(decoratorNode);
|
|
40
|
+
context.needBeanInfo = true;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function insertImport(path, options) {
|
|
45
|
+
const nodeImport = t.importDeclaration([t.importSpecifier(t.identifier('__z_BeanInfo'), t.stringLiteral('BeanInfo'))], t.stringLiteral(options.brandName));
|
|
46
|
+
path.get('body')[0].insertBefore(nodeImport);
|
|
47
|
+
}
|
|
48
|
+
function __parseBeanInfo(state) {
|
|
49
|
+
const sourceFileName = state.file.opts.sourceFileName || state.file.opts.filename || '';
|
|
50
|
+
const moduleName = __getModuleName(sourceFileName);
|
|
51
|
+
if (!moduleName)
|
|
52
|
+
return;
|
|
53
|
+
return { module: moduleName };
|
|
54
|
+
}
|
|
55
|
+
function __getModuleName(sourceFileName) {
|
|
56
|
+
if (!__checkIfValid(sourceFileName))
|
|
57
|
+
return;
|
|
58
|
+
const moduleInfo = parseInfoFromPath(sourceFileName);
|
|
59
|
+
if (!moduleInfo)
|
|
60
|
+
return;
|
|
61
|
+
return moduleInfo.relativeName;
|
|
62
|
+
}
|
|
63
|
+
function __createDecoratorNode(beanInfo) {
|
|
64
|
+
const propertyNodeModule = t.objectProperty(t.identifier('module'), t.stringLiteral(beanInfo.module));
|
|
65
|
+
const objectExpression = t.objectExpression([propertyNodeModule]);
|
|
66
|
+
const callExpression = t.callExpression(t.identifier('__z_BeanInfo'), [objectExpression]);
|
|
67
|
+
return callExpression;
|
|
68
|
+
}
|
|
69
|
+
// function __applyDecorator(expression: t.CallExpression, moduleName: string) {
|
|
70
|
+
// const propertyNode = t.objectProperty(t.identifier('module'), t.stringLiteral(moduleName));
|
|
71
|
+
// const args = expression.arguments;
|
|
72
|
+
// if (args.length === 0) {
|
|
73
|
+
// const objectExpression = t.objectExpression([propertyNode]);
|
|
74
|
+
// args.push(objectExpression);
|
|
75
|
+
// } else {
|
|
76
|
+
// const objectExpression = args[0] as t.ObjectExpression;
|
|
77
|
+
// objectExpression.properties.push(propertyNode);
|
|
78
|
+
// }
|
|
79
|
+
// }
|
|
80
|
+
function __checkIfValid(fileName) {
|
|
81
|
+
if (!fileName)
|
|
82
|
+
return false;
|
|
83
|
+
return !['src/boot/app/', '.zova/app/', 'src/backend/', '.vona/'].some(item => {
|
|
84
|
+
return fileName.includes(item);
|
|
85
|
+
});
|
|
86
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "babel-plugin-vona-bean-module",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"description": "babel-plugin-vona-bean-module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"author": "zhennann",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"framework",
|
|
13
|
+
"vona"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": [
|
|
18
|
+
"./src/index.ts",
|
|
19
|
+
"./dist/index.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@babel/core": "^7.28.0",
|
|
30
|
+
"@babel/traverse": "^7.28.0",
|
|
31
|
+
"@cabloy/module-info": "^1.3.31",
|
|
32
|
+
"@cabloy/word-utils": "^2.0.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"clean": "rimraf dist tsconfig.tsbuildinfo",
|
|
37
|
+
"tsc:publish": "npm run clean && tsc"
|
|
38
|
+
}
|
|
39
|
+
}
|