@arcmantle/lit-jsx 1.0.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/README.md +768 -0
- package/dist/compiler/attribute-processor.d.ts +128 -0
- package/dist/compiler/attribute-processor.d.ts.map +1 -0
- package/dist/compiler/attribute-processor.js +380 -0
- package/dist/compiler/attribute-processor.js.map +1 -0
- package/dist/compiler/babel-preset.d.ts +6 -0
- package/dist/compiler/babel-preset.d.ts.map +1 -0
- package/dist/compiler/babel-preset.js +27 -0
- package/dist/compiler/babel-preset.js.map +1 -0
- package/dist/compiler/builder.d.ts +22 -0
- package/dist/compiler/builder.d.ts.map +1 -0
- package/dist/compiler/builder.js +62 -0
- package/dist/compiler/builder.js.map +1 -0
- package/dist/compiler/compiler-utils.d.ts +81 -0
- package/dist/compiler/compiler-utils.d.ts.map +1 -0
- package/dist/compiler/compiler-utils.js +410 -0
- package/dist/compiler/compiler-utils.js.map +1 -0
- package/dist/compiler/config.d.ts +77 -0
- package/dist/compiler/config.d.ts.map +1 -0
- package/dist/compiler/config.js +78 -0
- package/dist/compiler/config.js.map +1 -0
- package/dist/compiler/postprocess.d.ts +5 -0
- package/dist/compiler/postprocess.d.ts.map +1 -0
- package/dist/compiler/postprocess.js +3 -0
- package/dist/compiler/postprocess.js.map +1 -0
- package/dist/compiler/preprocess.d.ts +5 -0
- package/dist/compiler/preprocess.d.ts.map +1 -0
- package/dist/compiler/preprocess.js +28 -0
- package/dist/compiler/preprocess.js.map +1 -0
- package/dist/compiler/transform-jsx.d.ts +5 -0
- package/dist/compiler/transform-jsx.d.ts.map +1 -0
- package/dist/compiler/transform-jsx.js +25 -0
- package/dist/compiler/transform-jsx.js.map +1 -0
- package/dist/compiler/transpiler.d.ts +48 -0
- package/dist/compiler/transpiler.d.ts.map +1 -0
- package/dist/compiler/transpiler.js +463 -0
- package/dist/compiler/transpiler.js.map +1 -0
- package/dist/compiler/vite-plugin.d.ts +38 -0
- package/dist/compiler/vite-plugin.d.ts.map +1 -0
- package/dist/compiler/vite-plugin.js +96 -0
- package/dist/compiler/vite-plugin.js.map +1 -0
- package/dist/runtime/choose-component.d.ts +39 -0
- package/dist/runtime/choose-component.d.ts.map +1 -0
- package/dist/runtime/choose-component.js +40 -0
- package/dist/runtime/choose-component.js.map +1 -0
- package/dist/runtime/compiler-ctors.d.ts +21 -0
- package/dist/runtime/compiler-ctors.d.ts.map +1 -0
- package/dist/runtime/compiler-ctors.js +21 -0
- package/dist/runtime/compiler-ctors.js.map +1 -0
- package/dist/runtime/for-component.d.ts +25 -0
- package/dist/runtime/for-component.d.ts.map +1 -0
- package/dist/runtime/for-component.js +35 -0
- package/dist/runtime/for-component.js.map +1 -0
- package/dist/runtime/literal-map.d.ts +22 -0
- package/dist/runtime/literal-map.d.ts.map +1 -0
- package/dist/runtime/literal-map.js +29 -0
- package/dist/runtime/literal-map.js.map +1 -0
- package/dist/runtime/rest-directive.d.ts +28 -0
- package/dist/runtime/rest-directive.d.ts.map +1 -0
- package/dist/runtime/rest-directive.js +49 -0
- package/dist/runtime/rest-directive.js.map +1 -0
- package/dist/runtime/show-component.d.ts +33 -0
- package/dist/runtime/show-component.d.ts.map +1 -0
- package/dist/runtime/show-component.js +30 -0
- package/dist/runtime/show-component.js.map +1 -0
- package/dist/runtime/tagged-template.d.ts +12 -0
- package/dist/runtime/tagged-template.d.ts.map +1 -0
- package/dist/runtime/tagged-template.js +12 -0
- package/dist/runtime/tagged-template.js.map +1 -0
- package/dist/runtime/type-helpers.d.ts +80 -0
- package/dist/runtime/type-helpers.d.ts.map +1 -0
- package/dist/runtime/type-helpers.js +85 -0
- package/dist/runtime/type-helpers.js.map +1 -0
- package/dist/shared/jsx-types.d.ts +2139 -0
- package/dist/shared/jsx-types.d.ts.map +1 -0
- package/dist/shared/jsx-types.js +2 -0
- package/dist/shared/jsx-types.js.map +1 -0
- package/dist/shared/jsx-utils.d.ts +30 -0
- package/dist/shared/jsx-utils.d.ts.map +1 -0
- package/dist/shared/jsx-utils.js +58 -0
- package/dist/shared/jsx-utils.js.map +1 -0
- package/dist/shared/mathml-tags.d.ts +12 -0
- package/dist/shared/mathml-tags.d.ts.map +1 -0
- package/dist/shared/mathml-tags.js +215 -0
- package/dist/shared/mathml-tags.js.map +1 -0
- package/dist/shared/svg-tags.d.ts +13 -0
- package/dist/shared/svg-tags.d.ts.map +1 -0
- package/dist/shared/svg-tags.js +95 -0
- package/dist/shared/svg-tags.js.map +1 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +30 -0
- package/dist/utils.js.map +1 -0
- package/package.json +52 -0
- package/src/compiler/attribute-processor.ts +579 -0
- package/src/compiler/babel-preset.ts +34 -0
- package/src/compiler/builder.ts +86 -0
- package/src/compiler/compiler-utils.ts +789 -0
- package/src/compiler/config.ts +77 -0
- package/src/compiler/postprocess.ts +7 -0
- package/src/compiler/preprocess.ts +40 -0
- package/src/compiler/transform-jsx.ts +36 -0
- package/src/compiler/transpiler.ts +644 -0
- package/src/compiler/vite-plugin.ts +114 -0
- package/src/external.d.ts +9 -0
- package/src/runtime/choose-component.ts +53 -0
- package/src/runtime/compiler-ctors.ts +28 -0
- package/src/runtime/for-component.ts +54 -0
- package/src/runtime/literal-map.ts +37 -0
- package/src/runtime/rest-directive.ts +66 -0
- package/src/runtime/show-component.ts +48 -0
- package/src/runtime/tagged-template.ts +11 -0
- package/src/runtime/type-helpers.ts +91 -0
- package/src/shared/jsx-types.ts +2556 -0
- package/src/shared/jsx-utils.ts +85 -0
- package/src/shared/mathml-tags.ts +235 -0
- package/src/shared/svg-tags.ts +103 -0
- package/src/tsconfig.json +4 -0
- package/src/utils.ts +30 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//
|
|
2
|
+
export const COMPONENT_LITERAL_PREFIX = '__$';
|
|
3
|
+
export const COMPONENT_POSTFIX = '.tag';
|
|
4
|
+
export const WHITESPACE_TAGS: string[] = [ 'pre', 'textarea' ];
|
|
5
|
+
export const SPECIAL_TAGS: string[] = [];
|
|
6
|
+
export const ATTR_NAMES = {
|
|
7
|
+
REF: 'ref',
|
|
8
|
+
CLASS_LIST: 'classList',
|
|
9
|
+
STYLE_LIST: 'styleList',
|
|
10
|
+
DIRECTIVE: 'directive',
|
|
11
|
+
EVENT_PREFIX: 'on-',
|
|
12
|
+
} as const;
|
|
13
|
+
export const ATTR_BIND_OBJ_NAME = 'as';
|
|
14
|
+
export const ATTR_VALUES = {
|
|
15
|
+
BOOL: 'bool',
|
|
16
|
+
PROP: 'prop',
|
|
17
|
+
} as const;
|
|
18
|
+
export const VARIABLES = {
|
|
19
|
+
HTML: 'html',
|
|
20
|
+
HTML_STATIC: 'htmlStatic',
|
|
21
|
+
SVG: 'svg',
|
|
22
|
+
SVG_STATIC: 'svgStatic',
|
|
23
|
+
MATHML: 'mathml',
|
|
24
|
+
MATHML_STATIC: 'mathmlStatic',
|
|
25
|
+
UNSAFE_STATIC: 'unsafeStatic',
|
|
26
|
+
CLASS_MAP: 'classMap',
|
|
27
|
+
STYLE_MAP: 'styleMap',
|
|
28
|
+
REF: 'ref',
|
|
29
|
+
REST: '__$rest',
|
|
30
|
+
LITERAL_MAP: '__$literalMap',
|
|
31
|
+
TAGGED_TEMPLATE_UTIL: '__$t',
|
|
32
|
+
BOOLEAN_PART: 'BooleanPart',
|
|
33
|
+
ATTRIBUTE_PART: 'AttributePart',
|
|
34
|
+
PROPERTY_PART: 'PropertyPart',
|
|
35
|
+
ELEMENT_PART: 'ElementPart',
|
|
36
|
+
EVENT_PART: 'EventPart',
|
|
37
|
+
CHILD_PART: 'ChildPart',
|
|
38
|
+
} as const;
|
|
39
|
+
export const SOURCES = {
|
|
40
|
+
HTML: 'lit-html',
|
|
41
|
+
HTML_ALT: 'lit',
|
|
42
|
+
HTML_STATIC: 'lit-html/static.js',
|
|
43
|
+
HTML_STATIC_ALT: 'lit/static-html.js',
|
|
44
|
+
SVG: 'lit-html/directives/svg.js',
|
|
45
|
+
SVG_ALT: 'lit/directives/svg.js',
|
|
46
|
+
MATHML: 'lit-html/directives/mathml.js',
|
|
47
|
+
MATHML_ALT: 'lit/directives/mathml.js',
|
|
48
|
+
SVG_STATIC: 'lit-html/static.js',
|
|
49
|
+
SVG_STATIC_ALT: 'lit/static-html.js',
|
|
50
|
+
MATHML_STATIC: 'lit-html/static.js',
|
|
51
|
+
MATHML_STATIC_ALT: 'lit/static-html.js',
|
|
52
|
+
UNSAFE_STATIC: 'lit-html/static.js',
|
|
53
|
+
UNSAFE_STATIC_ALT: 'lit/static-html.js',
|
|
54
|
+
REF: 'lit-html/directives/ref.js',
|
|
55
|
+
REF_ALT: 'lit/directives/ref.js',
|
|
56
|
+
CLASS_MAP: 'lit-html/directives/class-map.js',
|
|
57
|
+
CLASS_MAP_ALT: 'lit/directives/class-map.js',
|
|
58
|
+
STYLE_MAP: 'lit-html/directives/style-map.js',
|
|
59
|
+
STYLE_MAP_ALT: 'lit/directives/style-map.js',
|
|
60
|
+
REST: 'jsx-lit',
|
|
61
|
+
LITERAL_MAP: 'jsx-lit',
|
|
62
|
+
JSX_LIT: 'jsx-lit',
|
|
63
|
+
} as const;
|
|
64
|
+
export const ERROR_MESSAGES = {
|
|
65
|
+
NO_PROGRAM_FOUND: 'No program found for JSX transformation.',
|
|
66
|
+
INVALID_OPENING_TAG: 'Invalid opening tag found.',
|
|
67
|
+
EMPTY_JSX_EXPRESSION: 'Empty JSX expression found.',
|
|
68
|
+
ONLY_STRING_LITERALS: 'Only string literals are supported for JSX attributes.',
|
|
69
|
+
INVALID_DIRECTIVE_VALUE: 'Invalid value in directive expression.',
|
|
70
|
+
UNKNOWN_JSX_ATTRIBUTE_TYPE: 'Unknown JSX attribute type found.',
|
|
71
|
+
EMPTY_EXPRESSION: 'Empty expression found in JSX.',
|
|
72
|
+
IDENTIFIER_NOT_FOUND: (name: string): string => `Identifier '${ name }' not found in any accessible scope`,
|
|
73
|
+
TAG_NAME_NOT_FOUND: (tagName: string): string => `Tag name '${ tagName }' not found in any accessible scope`,
|
|
74
|
+
NO_STATEMENT_PATH: (tagName: string): string => `Could not find statement-level path for tagName: ${ tagName }`,
|
|
75
|
+
UNKNOWN_TEMPLATE_TYPE: (type: string): string => `Unknown template type: ${ type }`,
|
|
76
|
+
INVALID_BIND_TYPE: (type: string): string => `Invalid bind type: ${ type }`,
|
|
77
|
+
} as const;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PluginPass } from '@babel/core';
|
|
2
|
+
import type { VisitNodeFunction } from '@babel/traverse';
|
|
3
|
+
import { isJSXElement, isJSXIdentifier, type Program } from '@babel/types';
|
|
4
|
+
import { isValidHTMLNesting } from 'validate-html-nesting';
|
|
5
|
+
|
|
6
|
+
import { isComponent } from './compiler-utils.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// From https://github.com/MananTank/babel-plugin-validate-jsx-nesting/blob/main/src/index.js
|
|
10
|
+
const JSXValidator = {
|
|
11
|
+
JSXElement(path: any) {
|
|
12
|
+
const elName = path.node.openingElement.name;
|
|
13
|
+
const parent = path.parent;
|
|
14
|
+
|
|
15
|
+
if (!isJSXElement(parent) || !isJSXIdentifier(elName))
|
|
16
|
+
return;
|
|
17
|
+
|
|
18
|
+
const elTagName = elName.name;
|
|
19
|
+
if (isComponent(elTagName))
|
|
20
|
+
return;
|
|
21
|
+
|
|
22
|
+
const parentElName = parent.openingElement.name;
|
|
23
|
+
if (!isJSXIdentifier(parentElName))
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
const parentElTagName = parentElName.name;
|
|
27
|
+
if (!isComponent(parentElTagName)) {
|
|
28
|
+
if (!isValidHTMLNesting(parentElTagName, elTagName)) {
|
|
29
|
+
throw path.buildCodeFrameError(
|
|
30
|
+
`Invalid JSX: <${ elTagName }> cannot be child of <${ parentElTagName }>`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
export const preprocess: VisitNodeFunction<PluginPass, Program> = (path, state): void => {
|
|
39
|
+
path.traverse(JSXValidator);
|
|
40
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type PluginPass } from '@babel/core';
|
|
2
|
+
import type { NodePath, VisitNode } from '@babel/traverse';
|
|
3
|
+
import * as t from '@babel/types';
|
|
4
|
+
|
|
5
|
+
import { getTemplateType, isJSXElementStatic, isJSXFunctionElementComponent } from './compiler-utils.js';
|
|
6
|
+
import { CompiledTranspiler, TemplateTranspiler } from './transpiler.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export const transformJSXElement: VisitNode<
|
|
10
|
+
PluginPass, t.JSXElement | t.JSXFragment
|
|
11
|
+
> = (path): void => {
|
|
12
|
+
// If the parent is a JSX element, we do not need to transform it.
|
|
13
|
+
// The below condition will handle the case where the JSX element
|
|
14
|
+
// is nested inside another JSX element.
|
|
15
|
+
if (t.isJSXElement(path.parent) || t.isJSXFragment(path.parent))
|
|
16
|
+
return;
|
|
17
|
+
|
|
18
|
+
// If the parent is not a JSX element,
|
|
19
|
+
// we need to wrap the JSX in a tagged template expression
|
|
20
|
+
return void path.replaceWith(processJSXElement(path));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const processJSXElement = (path: NodePath<t.JSXElement | t.JSXFragment>) => {
|
|
25
|
+
const isStatic = isJSXElementStatic(path);
|
|
26
|
+
const templateType = getTemplateType(path);
|
|
27
|
+
const isFunctionComponent = isJSXFunctionElementComponent(path);
|
|
28
|
+
|
|
29
|
+
if (isFunctionComponent)
|
|
30
|
+
return new TemplateTranspiler().createFunctionalComponent(path);
|
|
31
|
+
|
|
32
|
+
if (isStatic || templateType !== 'html')
|
|
33
|
+
return new TemplateTranspiler().start(path);
|
|
34
|
+
|
|
35
|
+
return new CompiledTranspiler().start(path);
|
|
36
|
+
};
|