@elenajs/plugin-cem-define 0.0.2
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/package.json +25 -0
- package/src/index.js +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ariel Salminen https://arielsalminen.com
|
|
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/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elenajs/plugin-cem-define",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "CEM analyzer plugin that extracts tagName from Elena() mixin calls.",
|
|
5
|
+
"author": "Elena <hi@elenajs.com>",
|
|
6
|
+
"homepage": "https://elenajs.com/",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">= 22"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@custom-elements-manifest/analyzer": ">=0.10.0"
|
|
23
|
+
},
|
|
24
|
+
"gitHead": "0dc35402bc75dbe3bb71732f970548ca73e3cd1b"
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CEM analyzer plugin that reads `tagName` from the options object passed to
|
|
3
|
+
* `Elena(superClass, { tagName: "...", ... })` and registers the element in the manifest.
|
|
4
|
+
*
|
|
5
|
+
* Supports both inline objects and variable references:
|
|
6
|
+
* - `Elena(HTMLElement, { tagName: "elena-button", ... })`
|
|
7
|
+
* - `const options = { tagName: "elena-button", ... }; Elena(HTMLElement, options)`
|
|
8
|
+
*
|
|
9
|
+
* @returns {import("@custom-elements-manifest/analyzer").Plugin}
|
|
10
|
+
*/
|
|
11
|
+
export function elenaDefinePlugin() {
|
|
12
|
+
/** @type {Map<string, string>} variable name → tag name */
|
|
13
|
+
const optionsMap = new Map();
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
name: "define-element",
|
|
17
|
+
|
|
18
|
+
analyzePhase({ ts, node, moduleDoc }) {
|
|
19
|
+
// Collect variable declarations: const options = { tagName: "...", ... }
|
|
20
|
+
if (
|
|
21
|
+
ts.isVariableDeclaration(node) &&
|
|
22
|
+
node.initializer &&
|
|
23
|
+
ts.isObjectLiteralExpression(node.initializer)
|
|
24
|
+
) {
|
|
25
|
+
const varName = node.name.getText();
|
|
26
|
+
const tagNameProp = node.initializer.properties.find(
|
|
27
|
+
p => ts.isPropertyAssignment(p) && p.name.getText() === "tagName"
|
|
28
|
+
);
|
|
29
|
+
if (tagNameProp && ts.isStringLiteral(tagNameProp.initializer)) {
|
|
30
|
+
optionsMap.set(varName, tagNameProp.initializer.text);
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!ts.isClassDeclaration(node)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const className = node.name?.getText();
|
|
40
|
+
if (!className) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const heritageClause = node.heritageClauses?.[0];
|
|
45
|
+
if (!heritageClause) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const type of heritageClause.types) {
|
|
50
|
+
const expr = type.expression;
|
|
51
|
+
if (!ts.isCallExpression(expr) || expr.expression.getText() !== "Elena") {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const optionsArg = expr.arguments[1];
|
|
56
|
+
if (!optionsArg) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let tagName;
|
|
61
|
+
|
|
62
|
+
if (ts.isObjectLiteralExpression(optionsArg)) {
|
|
63
|
+
// Inline: Elena(HTMLElement, { tagName: "...", ... })
|
|
64
|
+
const tagNameProp = optionsArg.properties.find(
|
|
65
|
+
p => ts.isPropertyAssignment(p) && p.name.getText() === "tagName"
|
|
66
|
+
);
|
|
67
|
+
if (tagNameProp && ts.isStringLiteral(tagNameProp.initializer)) {
|
|
68
|
+
tagName = tagNameProp.initializer.text;
|
|
69
|
+
}
|
|
70
|
+
} else if (ts.isIdentifier(optionsArg)) {
|
|
71
|
+
// Variable reference: Elena(HTMLElement, options)
|
|
72
|
+
tagName = optionsMap.get(optionsArg.getText());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!tagName) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const declaration = moduleDoc.declarations?.find(d => d.name === className);
|
|
80
|
+
if (declaration) {
|
|
81
|
+
declaration.tagName = tagName;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
moduleDoc.exports ??= [];
|
|
85
|
+
moduleDoc.exports.push({
|
|
86
|
+
kind: "custom-element-definition",
|
|
87
|
+
name: tagName,
|
|
88
|
+
declaration: { name: className, module: moduleDoc.path },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|