@ktjs/babel-plugin-ktjsx 0.0.4 → 0.2.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/README.md +40 -13
- package/dist/babel-plugin-ktjsx/src/index.d.ts +4 -0
- package/dist/babel-plugin-ktjsx/src/index.d.ts.map +1 -0
- package/dist/babel-plugin-ktjsx/src/transform.d.ts +3 -0
- package/dist/babel-plugin-ktjsx/src/transform.d.ts.map +1 -0
- package/dist/index.d.ts +1 -6
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/transformer/src/if-else.d.ts +4 -0
- package/dist/transformer/src/if-else.d.ts.map +1 -0
- package/dist/transformer/src/index.d.ts +4 -0
- package/dist/transformer/src/index.d.ts.map +1 -0
- package/dist/transformer/src/k-for.d.ts +5 -0
- package/dist/transformer/src/k-for.d.ts.map +1 -0
- package/dist/transformer/src/plugin.d.ts +4 -0
- package/dist/transformer/src/plugin.d.ts.map +1 -0
- package/dist/transformer/src/svg-mathml-flag.d.ts +4 -0
- package/dist/transformer/src/svg-mathml-flag.d.ts.map +1 -0
- package/dist/transformer/src/transform.d.ts +8 -0
- package/dist/transformer/src/transform.d.ts.map +1 -0
- package/package.json +9 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -96
package/README.md
CHANGED
|
@@ -1,25 +1,52 @@
|
|
|
1
|
-
# babel-plugin-ktjsx
|
|
1
|
+
# @ktjs/babel-plugin-ktjsx
|
|
2
2
|
|
|
3
|
-
`@ktjs/babel-plugin-ktjsx` is
|
|
3
|
+
`@ktjs/babel-plugin-ktjsx` is the Babel entry for KT.js JSX transforms, re-exported from `@ktjs/transformer`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Current behavior:
|
|
6
|
+
|
|
7
|
+
- add KT.js internal SVG / MathML flags on JSX elements in SVG / MathML context
|
|
8
|
+
- validate directive combinations (`k-if` + `k-else`, and `k-for` mixed with conditional directives)
|
|
9
|
+
- transform `k-for` into `source.map(...)` and remove `k-for` / `k-key` attributes
|
|
10
|
+
- normalize sibling `k-if` / `k-else-if` / `k-else` chains into explicit `k-if` conditions
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add -D @ktjs/babel-plugin-ktjsx @babel/core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage (ESM Babel config)
|
|
6
19
|
|
|
7
20
|
```js
|
|
8
|
-
// babel.config.
|
|
21
|
+
// babel.config.mjs
|
|
22
|
+
import ktjsx from '@ktjs/babel-plugin-ktjsx';
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
plugins: [ktjsx],
|
|
26
|
+
};
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage (CommonJS Babel config)
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// babel.config.cjs
|
|
33
|
+
const { default: ktjsx } = require('@ktjs/babel-plugin-ktjsx');
|
|
34
|
+
|
|
9
35
|
module.exports = {
|
|
10
|
-
plugins: [
|
|
36
|
+
plugins: [ktjsx],
|
|
11
37
|
};
|
|
12
38
|
```
|
|
13
39
|
|
|
14
|
-
|
|
40
|
+
## Named exports
|
|
15
41
|
|
|
16
|
-
|
|
42
|
+
```ts
|
|
43
|
+
import { transformerKTjsx, transformWithKTjsx } from '@ktjs/babel-plugin-ktjsx';
|
|
44
|
+
```
|
|
17
45
|
|
|
18
|
-
- `
|
|
19
|
-
- `
|
|
20
|
-
- `createElementNames` - functions to consider as element factories (default `['createElement','h']`)
|
|
46
|
+
- `transformerKTjsx`: Babel plugin factory (same as default export)
|
|
47
|
+
- `transformWithKTjsx`: programmatic transform helper
|
|
21
48
|
|
|
22
|
-
Notes
|
|
49
|
+
## Notes
|
|
23
50
|
|
|
24
|
-
-
|
|
25
|
-
-
|
|
51
|
+
- This plugin works on JSX AST directly (not on `createElement(...)` output).
|
|
52
|
+
- There are no custom plugin options yet.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../src/transform.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Transform `<svg ...>` and `<math ...>` JSX elements to include a special attribute flag
|
|
4
|
-
*/
|
|
5
|
-
export default function babelKTjsx(): PluginObj;
|
|
6
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export { KTjsxTransformOptions, default, transformWithKTjsx, transformerKTjsx } from '@ktjs/transformer';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"if-else.d.ts","sourceRoot":"","sources":["../../../../transformer/src/if-else.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAiFlC,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,QA8DtE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../transformer/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type NodePath } from '@babel/core';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
export declare function validateDirectiveCombinations(path: NodePath<t.JSXElement>): void;
|
|
4
|
+
export declare function transformKFor(path: NodePath<t.JSXElement>): boolean;
|
|
5
|
+
//# sourceMappingURL=k-for.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"k-for.d.ts","sourceRoot":"","sources":["../../../../transformer/src/k-for.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAWlC,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,QAkBzE;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,OAAO,CAqCnE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../../transformer/src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAK7C,wBAAgB,gBAAgB,IAAI,SAAS,CAgB5C;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svg-mathml-flag.d.ts","sourceRoot":"","sources":["../../../../transformer/src/svg-mathml-flag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAUlC,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,QAiFrE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type TransformOptions } from '@babel/core';
|
|
2
|
+
type ReservedTransformOptions = 'ast' | 'babelrc' | 'configFile' | 'parserOpts' | 'plugins' | 'sourceMaps';
|
|
3
|
+
export type KTjsxTransformOptions = Omit<TransformOptions, ReservedTransformOptions> & {
|
|
4
|
+
parserPlugins?: NonNullable<TransformOptions['parserOpts']>['plugins'];
|
|
5
|
+
};
|
|
6
|
+
export declare function transformWithKTjsx(code: string, options?: KTjsxTransformOptions): Promise<import("@babel/core").BabelFileResult>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../../transformer/src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAKpE,KAAK,wBAAwB,GACzB,KAAK,GACL,SAAS,GACT,YAAY,GACZ,YAAY,GACZ,SAAS,GACT,YAAY,CAAC;AAEjB,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,gBAAgB,EAAE,wBAAwB,CAAC,GAAG;IACrF,aAAa,CAAC,EAAE,WAAW,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;CACxE,CAAC;AAEF,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,kDAezF"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ktjs/babel-plugin-ktjsx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "dist/index.
|
|
6
|
-
"module": "dist/index.
|
|
5
|
+
"main": "dist/index.mjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.
|
|
12
|
-
"default": "./dist/index.
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"description": "Babel plugin to make JSX-generated SVG use proper namespaces.",
|
|
28
|
+
"description_zh": "用于让 JSX 生成的 SVG 使用正确命名空间的 Babel 插件。",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@ktjs/transformer": "^*"
|
|
31
|
+
},
|
|
28
32
|
"devDependencies": {
|
|
29
33
|
"@babel/core": "^7.0.0",
|
|
30
34
|
"@babel/types": "^7.0.0",
|
|
@@ -35,9 +39,5 @@
|
|
|
35
39
|
},
|
|
36
40
|
"peerDependencies": {
|
|
37
41
|
"@babel/core": "^7.0.0"
|
|
38
|
-
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"build": "node build.js",
|
|
41
|
-
"test": "node tests/run.js"
|
|
42
42
|
}
|
|
43
43
|
}
|
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
|
-
}
|