@ktjs/babel-plugin-ktjsx 0.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 +25 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +93 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# babel-plugin-ktjsx
|
|
2
|
+
|
|
3
|
+
`@ktjs/babel-plugin-ktjsx` is a Babel plugin that enhances compiled JSX output to better support SVG and namespace-aware element creation.
|
|
4
|
+
|
|
5
|
+
Basic usage (Babel):
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// babel.config.js
|
|
9
|
+
module.exports = {
|
|
10
|
+
plugins: [['@ktjs/babel-plugin-ktjsx', { svgTags: ['svg', 'g', 'path'] }]],
|
|
11
|
+
};
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
This plugin will transform calls like `createElement('svg', ...)` (or `h('svg', ...)`) into `createElementNS('http://www.w3.org/2000/svg', 'svg', ...)` so that SVG elements are created in the correct namespace.
|
|
15
|
+
|
|
16
|
+
Options:
|
|
17
|
+
|
|
18
|
+
- `svgTags` - array of tag names to treat as SVG (default includes common SVG tags)
|
|
19
|
+
- `namespaceURI` - SVG namespace (default `http://www.w3.org/2000/svg`)
|
|
20
|
+
- `createElementNames` - functions to consider as element factories (default `['createElement','h']`)
|
|
21
|
+
|
|
22
|
+
Notes:
|
|
23
|
+
|
|
24
|
+
- The plugin is intended to run after JSX has been transformed to `createElement`/`h` calls. If you use a custom JSX pragma, include that name in `createElementNames`.
|
|
25
|
+
- A Rollup wrapper can be created to run this plugin during bundling; prefer using Babel for precise AST-level transforms.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,KAAK,kBAAkB,GAAG,EAAE,CAAC;AAa7B,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,kBAAkB,GAAG,SAAS,CAwFvF;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
export default function babelPluginKtjsx(_, options) {
|
|
11
|
+
return {
|
|
12
|
+
name: 'babel-plugin-ktjsx',
|
|
13
|
+
visitor: {
|
|
14
|
+
JSXElement(path) {
|
|
15
|
+
const opening = path.node.openingElement;
|
|
16
|
+
if (!opening) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const oname = opening.name;
|
|
20
|
+
if (t.isJSXNamespacedName(oname)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!t.isJSXIdentifier(oname)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const tag = oname.name;
|
|
27
|
+
const isSvgRoot = isSvgTag(tag);
|
|
28
|
+
const isMathRoot = isMathTag(tag);
|
|
29
|
+
let insideSvg = false;
|
|
30
|
+
if (!isSvgRoot) {
|
|
31
|
+
/**
|
|
32
|
+
* @type {import('@babel/core').NodePath | null}
|
|
33
|
+
*/
|
|
34
|
+
const parentSvg = path.findParent((p) => {
|
|
35
|
+
if (!p.isJSXElement()) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
const popping = p.node.openingElement && p.node.openingElement.name;
|
|
39
|
+
if (!popping) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (t.isJSXIdentifier(popping)) {
|
|
43
|
+
return isSvgTag(popping.name);
|
|
44
|
+
}
|
|
45
|
+
if (t.isJSXNamespacedName(popping)) {
|
|
46
|
+
return t.isJSXIdentifier(popping.namespace) && popping.namespace.name === 'svg';
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
});
|
|
50
|
+
insideSvg = !!parentSvg;
|
|
51
|
+
}
|
|
52
|
+
let insideMath = false;
|
|
53
|
+
if (!isMathRoot) {
|
|
54
|
+
/**
|
|
55
|
+
* @type {import('@babel/core').NodePath | null}
|
|
56
|
+
*/
|
|
57
|
+
const parentMath = path.findParent((p) => {
|
|
58
|
+
if (!p.isJSXElement()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
const popping = p.node.openingElement && p.node.openingElement.name;
|
|
62
|
+
if (!popping) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (t.isJSXIdentifier(popping)) {
|
|
66
|
+
return isMathTag(popping.name);
|
|
67
|
+
}
|
|
68
|
+
if (t.isJSXNamespacedName(popping)) {
|
|
69
|
+
return t.isJSXIdentifier(popping.namespace) && popping.namespace.name === 'math';
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
});
|
|
73
|
+
insideMath = !!parentMath;
|
|
74
|
+
}
|
|
75
|
+
// If this element is neither an svg/math root nor inside one, skip.
|
|
76
|
+
const inSvgContext = isSvgRoot || insideSvg;
|
|
77
|
+
const inMathContext = isMathRoot || insideMath;
|
|
78
|
+
if (!inSvgContext && !inMathContext) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
// Add boolean attribute (SVG or MATHML) to opening element if not present
|
|
82
|
+
const attrs = opening.attributes || [];
|
|
83
|
+
const flag = inSvgContext ? SVG_ATTR_FLAG : MATHML_ATTR_FLAG;
|
|
84
|
+
const hasFlag = attrs.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === flag);
|
|
85
|
+
if (!hasFlag) {
|
|
86
|
+
attrs.push(t.jsxAttribute(t.jsxIdentifier(flag)));
|
|
87
|
+
opening.attributes = attrs;
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export { babelPluginKtjsx };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ktjs/babel-plugin-ktjsx",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Kasukabe Tsumugi",
|
|
13
|
+
"email": "futami16237@gmail.com"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/ktjs/kt.js"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"description": "Babel plugin to make JSX-generated SVG use proper namespaces.",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@babel/core": "^7.0.0",
|
|
23
|
+
"@babel/types": "^7.0.0",
|
|
24
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
25
|
+
"@types/babel__core": "^7.20.5",
|
|
26
|
+
"rollup": "^3.0.0",
|
|
27
|
+
"typescript": "^5.9.3"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@babel/core": "^7.0.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -b --force",
|
|
34
|
+
"test": "node tests/run.js"
|
|
35
|
+
}
|
|
36
|
+
}
|