@ktjs/babel-plugin-ktjsx 0.0.3 → 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/dist/index.d.ts +6 -6
- package/dist/index.mjs +14986 -0
- package/package.json +15 -8
- package/LICENSE +0 -21
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -96
package/package.json
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ktjs/babel-plugin-ktjsx",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "dist/index.
|
|
6
|
-
"module": "dist/index.
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
8
15
|
"files": [
|
|
9
16
|
"dist"
|
|
10
17
|
],
|
|
@@ -18,6 +25,10 @@
|
|
|
18
25
|
},
|
|
19
26
|
"license": "MIT",
|
|
20
27
|
"description": "Babel plugin to make JSX-generated SVG use proper namespaces.",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "node build.js",
|
|
30
|
+
"test": "node tests/run.js"
|
|
31
|
+
},
|
|
21
32
|
"devDependencies": {
|
|
22
33
|
"@babel/core": "^7.0.0",
|
|
23
34
|
"@babel/types": "^7.0.0",
|
|
@@ -28,9 +39,5 @@
|
|
|
28
39
|
},
|
|
29
40
|
"peerDependencies": {
|
|
30
41
|
"@babel/core": "^7.0.0"
|
|
31
|
-
},
|
|
32
|
-
"scripts": {
|
|
33
|
-
"build": "node build.js",
|
|
34
|
-
"test": "node tests/run.js"
|
|
35
42
|
}
|
|
36
|
-
}
|
|
43
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 kasukabe tsumugi
|
|
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/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAiBxC;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,IAAI,SAAS,CAwF9C"}
|
package/dist/index.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import * as t from '@babel/types';
|
|
2
|
-
const SVG_ATTR_FLAG = '__kt_svg__';
|
|
3
|
-
const MATHML_ATTR_FLAG = '__kt_mathml__';
|
|
4
|
-
function isSvgTag(tag) {
|
|
5
|
-
return tag === 'svg' || (typeof tag === 'string' && tag.startsWith('svg:'));
|
|
6
|
-
}
|
|
7
|
-
function isMathTag(tag) {
|
|
8
|
-
return tag === 'math' || (typeof tag === 'string' && tag.startsWith('math:'));
|
|
9
|
-
}
|
|
10
|
-
//_: any, options: KTJSXPluginOptions
|
|
11
|
-
/**
|
|
12
|
-
* Transform `<svg ...>` and `<math ...>` JSX elements to include a special attribute flag
|
|
13
|
-
*/
|
|
14
|
-
export default function babelKTjsx() {
|
|
15
|
-
return {
|
|
16
|
-
name: 'babel-plugin-ktjsx',
|
|
17
|
-
visitor: {
|
|
18
|
-
JSXElement(path) {
|
|
19
|
-
const opening = path.node.openingElement;
|
|
20
|
-
if (!opening) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const oname = opening.name;
|
|
24
|
-
if (t.isJSXNamespacedName(oname)) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (!t.isJSXIdentifier(oname)) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const tag = oname.name;
|
|
31
|
-
const isSvgRoot = isSvgTag(tag);
|
|
32
|
-
const isMathRoot = isMathTag(tag);
|
|
33
|
-
let insideSvg = false;
|
|
34
|
-
if (!isSvgRoot) {
|
|
35
|
-
/**
|
|
36
|
-
* @type {import('@babel/core').NodePath | null}
|
|
37
|
-
*/
|
|
38
|
-
const parentSvg = path.findParent((p) => {
|
|
39
|
-
if (!p.isJSXElement()) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
const popping = p.node.openingElement && p.node.openingElement.name;
|
|
43
|
-
if (!popping) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
if (t.isJSXIdentifier(popping)) {
|
|
47
|
-
return isSvgTag(popping.name);
|
|
48
|
-
}
|
|
49
|
-
if (t.isJSXNamespacedName(popping)) {
|
|
50
|
-
return t.isJSXIdentifier(popping.namespace) && popping.namespace.name === 'svg';
|
|
51
|
-
}
|
|
52
|
-
return false;
|
|
53
|
-
});
|
|
54
|
-
insideSvg = !!parentSvg;
|
|
55
|
-
}
|
|
56
|
-
let insideMath = false;
|
|
57
|
-
if (!isMathRoot) {
|
|
58
|
-
/**
|
|
59
|
-
* @type {import('@babel/core').NodePath | null}
|
|
60
|
-
*/
|
|
61
|
-
const parentMath = path.findParent((p) => {
|
|
62
|
-
if (!p.isJSXElement()) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
const popping = p.node.openingElement && p.node.openingElement.name;
|
|
66
|
-
if (!popping) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
if (t.isJSXIdentifier(popping)) {
|
|
70
|
-
return isMathTag(popping.name);
|
|
71
|
-
}
|
|
72
|
-
if (t.isJSXNamespacedName(popping)) {
|
|
73
|
-
return t.isJSXIdentifier(popping.namespace) && popping.namespace.name === 'math';
|
|
74
|
-
}
|
|
75
|
-
return false;
|
|
76
|
-
});
|
|
77
|
-
insideMath = !!parentMath;
|
|
78
|
-
}
|
|
79
|
-
// If this element is neither an svg/math root nor inside one, skip.
|
|
80
|
-
const inSvgContext = isSvgRoot || insideSvg;
|
|
81
|
-
const inMathContext = isMathRoot || insideMath;
|
|
82
|
-
if (!inSvgContext && !inMathContext) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
// Add boolean attribute (SVG or MATHML) to opening element if not present
|
|
86
|
-
const attrs = opening.attributes || [];
|
|
87
|
-
const flag = inSvgContext ? SVG_ATTR_FLAG : MATHML_ATTR_FLAG;
|
|
88
|
-
const hasFlag = attrs.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === flag);
|
|
89
|
-
if (!hasFlag) {
|
|
90
|
-
attrs.push(t.jsxAttribute(t.jsxIdentifier(flag)));
|
|
91
|
-
opening.attributes = attrs;
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
}
|