@litsx/babel-preset-litsx 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/README.md +37 -0
- package/dist/index.cjs +30 -0
- package/dist/index.cjs.map +1 -0
- package/dist/internal/transform-litsx-components.cjs +2518 -0
- package/dist/internal/transform-litsx-components.cjs.map +1 -0
- package/dist/internal/transform-litsx-dom-refs.cjs +16 -0
- package/dist/internal/transform-litsx-dom-refs.cjs.map +1 -0
- package/dist/internal/transform-litsx-hooks.cjs +45 -0
- package/dist/internal/transform-litsx-hooks.cjs.map +1 -0
- package/dist/internal/transform-litsx-properties.cjs +1254 -0
- package/dist/internal/transform-litsx-properties.cjs.map +1 -0
- package/dist/pipeline.cjs +97 -0
- package/dist/pipeline.cjs.map +1 -0
- package/package.json +80 -0
- package/src/index.js +15 -0
- package/src/internal/transform-litsx-class-generation.js +109 -0
- package/src/internal/transform-litsx-components.js +549 -0
- package/src/internal/transform-litsx-dom-refs.js +9 -0
- package/src/internal/transform-litsx-handlers.js +220 -0
- package/src/internal/transform-litsx-hooks.js +38 -0
- package/src/internal/transform-litsx-param-rewrites.js +227 -0
- package/src/internal/transform-litsx-program.js +229 -0
- package/src/internal/transform-litsx-properties.js +1246 -0
- package/src/internal/transform-litsx-refs.js +231 -0
- package/src/internal/transform-litsx-static-hoists.js +754 -0
- package/src/internal/transform-litsx-wrapper-utils.js +243 -0
- package/src/pipeline.js +90 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
let t;
|
|
2
|
+
|
|
3
|
+
export function setWrapperUtilsBabelTypes(nextTypes) {
|
|
4
|
+
t = nextTypes;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function emitWrapperWarnings(meta, warn) {
|
|
8
|
+
if (!meta || typeof warn !== "function" || !Array.isArray(meta.warnings)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
meta.warnings.forEach((warning) => warn(warning));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function pruneWrapperImports(meta) {
|
|
16
|
+
if (!meta || !Array.isArray(meta.cleanups)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
meta.cleanups.forEach((cleanup) => {
|
|
21
|
+
if (!cleanup || !cleanup.shouldRemoveImport || !cleanup.importSpecifierPath) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const importDecl = cleanup.importSpecifierPath.parentPath;
|
|
25
|
+
cleanup.importSpecifierPath.remove();
|
|
26
|
+
if (importDecl.node.specifiers.length === 0) {
|
|
27
|
+
importDecl.remove();
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function maybeTransformWrappedVariableDeclarator({
|
|
33
|
+
varPath,
|
|
34
|
+
resolvedPluginOptions,
|
|
35
|
+
state,
|
|
36
|
+
transformFunction,
|
|
37
|
+
updateTransformState,
|
|
38
|
+
getWrapperMetadata,
|
|
39
|
+
}) {
|
|
40
|
+
if (typeof getWrapperMetadata !== "function") {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const initPath = varPath.get("init");
|
|
45
|
+
if (!initPath || !initPath.isCallExpression()) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const wrapperMeta = getWrapperMetadata(initPath);
|
|
50
|
+
if (!wrapperMeta) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
55
|
+
state?.__litsxWarnings?.push(warning);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const programPath = varPath.findParent((p) => p.isProgram());
|
|
59
|
+
const localName = t.isIdentifier(varPath.node.id)
|
|
60
|
+
? varPath.node.id.name
|
|
61
|
+
: undefined;
|
|
62
|
+
|
|
63
|
+
const classNode = transformFunction(
|
|
64
|
+
wrapperMeta.functionPath,
|
|
65
|
+
programPath,
|
|
66
|
+
localName,
|
|
67
|
+
{
|
|
68
|
+
...resolvedPluginOptions,
|
|
69
|
+
...wrapperMeta.options,
|
|
70
|
+
typeResolver: state?.__litsxTypeResolver,
|
|
71
|
+
warn: (warning) => {
|
|
72
|
+
state?.__litsxWarnings?.push(warning);
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
if (!classNode) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (t.isIdentifier(varPath.node.id)) {
|
|
82
|
+
varPath.scope.removeBinding(varPath.node.id.name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const declarationPath = varPath.parentPath;
|
|
86
|
+
if (!declarationPath.isVariableDeclaration()) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
declarationPath.replaceWith(classNode);
|
|
91
|
+
declarationPath.requeue();
|
|
92
|
+
pruneWrapperImports(wrapperMeta);
|
|
93
|
+
updateTransformState?.(state, classNode);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function handlePotentialComponentExport({
|
|
98
|
+
exportPath,
|
|
99
|
+
state,
|
|
100
|
+
isDefault = false,
|
|
101
|
+
transformFunction,
|
|
102
|
+
isInsideFunctionOrClass,
|
|
103
|
+
updateTransformState,
|
|
104
|
+
getWrapperMetadata,
|
|
105
|
+
}) {
|
|
106
|
+
const declaration = exportPath.node.declaration;
|
|
107
|
+
const typeResolver = state?.__litsxTypeResolver || null;
|
|
108
|
+
if (!declaration || isInsideFunctionOrClass(exportPath)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (
|
|
113
|
+
t.isFunctionDeclaration(declaration) ||
|
|
114
|
+
(t.isVariableDeclaration(declaration) &&
|
|
115
|
+
declaration.declarations.length === 1 &&
|
|
116
|
+
t.isArrowFunctionExpression(declaration.declarations[0].init))
|
|
117
|
+
) {
|
|
118
|
+
const funcPath = exportPath.get("declaration");
|
|
119
|
+
const declarationPath = funcPath.isVariableDeclaration()
|
|
120
|
+
? funcPath.get("declarations.0.init")
|
|
121
|
+
: funcPath;
|
|
122
|
+
let exportName;
|
|
123
|
+
if (t.isFunctionDeclaration(declaration) && declaration.id) {
|
|
124
|
+
exportName = declaration.id.name;
|
|
125
|
+
} else if (
|
|
126
|
+
t.isVariableDeclaration(declaration) &&
|
|
127
|
+
t.isIdentifier(declaration.declarations[0].id)
|
|
128
|
+
) {
|
|
129
|
+
exportName = declaration.declarations[0].id.name;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const classNode = transformFunction(
|
|
133
|
+
declarationPath,
|
|
134
|
+
exportPath.findParent((p) => p.isProgram()),
|
|
135
|
+
exportName,
|
|
136
|
+
{
|
|
137
|
+
...state?.__litsxResolvedPluginOptions,
|
|
138
|
+
typeResolver,
|
|
139
|
+
warn: (warning) => {
|
|
140
|
+
state?.__litsxWarnings?.push(warning);
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
if (!classNode) return true;
|
|
146
|
+
|
|
147
|
+
if (exportName) {
|
|
148
|
+
exportPath.scope.removeBinding(exportName);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
exportPath.insertBefore(
|
|
152
|
+
isDefault
|
|
153
|
+
? t.exportDefaultDeclaration(classNode)
|
|
154
|
+
: t.exportNamedDeclaration(classNode, [])
|
|
155
|
+
);
|
|
156
|
+
exportPath.remove();
|
|
157
|
+
updateTransformState?.(state, classNode);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (
|
|
162
|
+
typeof getWrapperMetadata === "function" &&
|
|
163
|
+
t.isVariableDeclaration(declaration) &&
|
|
164
|
+
declaration.declarations.length === 1
|
|
165
|
+
) {
|
|
166
|
+
const declaratorPath = exportPath.get("declaration.declarations.0");
|
|
167
|
+
const initPath = declaratorPath.get("init");
|
|
168
|
+
const exportName = t.isIdentifier(declaratorPath.node.id)
|
|
169
|
+
? declaratorPath.node.id.name
|
|
170
|
+
: undefined;
|
|
171
|
+
const programPath = exportPath.findParent((p) => p.isProgram());
|
|
172
|
+
|
|
173
|
+
if (initPath.isCallExpression()) {
|
|
174
|
+
const wrapperMeta = getWrapperMetadata(initPath);
|
|
175
|
+
if (!wrapperMeta) return false;
|
|
176
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
177
|
+
state?.__litsxWarnings?.push(warning);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const classNode = transformFunction(
|
|
181
|
+
wrapperMeta.functionPath,
|
|
182
|
+
programPath,
|
|
183
|
+
exportName,
|
|
184
|
+
{
|
|
185
|
+
...state?.__litsxResolvedPluginOptions,
|
|
186
|
+
...wrapperMeta.options,
|
|
187
|
+
typeResolver,
|
|
188
|
+
warn: (warning) => {
|
|
189
|
+
state?.__litsxWarnings?.push(warning);
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
if (!classNode) return true;
|
|
194
|
+
|
|
195
|
+
if (exportName) {
|
|
196
|
+
exportPath.scope.removeBinding(exportName);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
exportPath.replaceWith(t.exportNamedDeclaration(classNode, []));
|
|
200
|
+
exportPath.requeue();
|
|
201
|
+
pruneWrapperImports(wrapperMeta);
|
|
202
|
+
updateTransformState?.(state, classNode);
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (typeof getWrapperMetadata === "function" && isDefault && t.isCallExpression(declaration)) {
|
|
208
|
+
const callPath = exportPath.get("declaration");
|
|
209
|
+
const wrapperMeta = getWrapperMetadata(callPath);
|
|
210
|
+
if (!wrapperMeta) return false;
|
|
211
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
212
|
+
state?.__litsxWarnings?.push(warning);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const programPath = exportPath.findParent((p) => p.isProgram());
|
|
216
|
+
const inferredName = wrapperMeta.functionPath.node.id
|
|
217
|
+
? wrapperMeta.functionPath.node.id.name
|
|
218
|
+
: undefined;
|
|
219
|
+
|
|
220
|
+
const classNode = transformFunction(
|
|
221
|
+
wrapperMeta.functionPath,
|
|
222
|
+
programPath,
|
|
223
|
+
inferredName,
|
|
224
|
+
{
|
|
225
|
+
...state?.__litsxResolvedPluginOptions,
|
|
226
|
+
...wrapperMeta.options,
|
|
227
|
+
typeResolver,
|
|
228
|
+
warn: (warning) => {
|
|
229
|
+
state?.__litsxWarnings?.push(warning);
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
if (!classNode) return true;
|
|
234
|
+
|
|
235
|
+
exportPath.replaceWith(t.exportDefaultDeclaration(classNode));
|
|
236
|
+
exportPath.requeue();
|
|
237
|
+
pruneWrapperImports(wrapperMeta);
|
|
238
|
+
updateTransformState?.(state, classNode);
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return false;
|
|
243
|
+
}
|
package/src/pipeline.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import transformJsxHtmlTemplate from "@litsx/babel-plugin-transform-jsx-html-template";
|
|
2
|
+
import transformLitsxScopedElements from "@litsx/babel-plugin-transform-litsx-scoped-elements";
|
|
3
|
+
import transformLitsxDomRefs from "./internal/transform-litsx-dom-refs.js";
|
|
4
|
+
import transformLitsxHooks from "./internal/transform-litsx-hooks.js";
|
|
5
|
+
import transformLitsxComponents from "./internal/transform-litsx-components.js";
|
|
6
|
+
|
|
7
|
+
const NATIVE_TRANSFORM_OPTION_KEYS = [
|
|
8
|
+
"defaultDomMode",
|
|
9
|
+
"typeResolutionMode",
|
|
10
|
+
"inMemoryFiles",
|
|
11
|
+
"typescriptSession",
|
|
12
|
+
"suppressNativeClassNameWarning",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const HOOK_FEATURE_PATTERN = /\b(?:useOnConnect|useAfterUpdate|useOnCommit|useMemoValue|useStableCallback|useEvent|useEmit|usePrevious|useReducedState|useState|useControlledState|useAsyncState|useOptimistic|useExpose|useExternalStore|useHost|useHostContent|useSlot|useTextContent|useTransition|useDeferredValue|useStyle|useRef|useCallbackRef)\b/;
|
|
16
|
+
const REF_FEATURE_PATTERN = /\buseRef\b|\bref\s*=/;
|
|
17
|
+
const SCOPED_ELEMENTS_PATTERN = /<\s*(?:[A-Z][\w.]*(?=[\s/>])|[a-z][\w]*-[\w-]*(?=[\s/>]))/;
|
|
18
|
+
const LIGHT_DOM_PATTERN = /\^lightDom\b/;
|
|
19
|
+
|
|
20
|
+
export function normalizeTransformLitsxOptions(options = {}) {
|
|
21
|
+
const transformLitsxOptions = {
|
|
22
|
+
...(options.transformLitsx || {}),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
for (const key of NATIVE_TRANSFORM_OPTION_KEYS) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(options, key)) {
|
|
27
|
+
transformLitsxOptions[key] = options[key];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return transformLitsxOptions;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function detectLitsxSourceFeatures(source, options = {}) {
|
|
35
|
+
const text = typeof source === "string" ? source : "";
|
|
36
|
+
const transformOptions = normalizeTransformLitsxOptions(options);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
hooks: HOOK_FEATURE_PATTERN.test(text),
|
|
40
|
+
domRefs: REF_FEATURE_PATTERN.test(text),
|
|
41
|
+
scopedElements:
|
|
42
|
+
transformOptions.defaultDomMode === "light" ||
|
|
43
|
+
LIGHT_DOM_PATTERN.test(text) ||
|
|
44
|
+
SCOPED_ELEMENTS_PATTERN.test(text),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function shouldIncludeFeaturePlugin(sourceFeatures, key) {
|
|
49
|
+
if (!sourceFeatures) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return sourceFeatures[key] === true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createLitsxPresetPlugins(options = {}, sourceFeatures = null) {
|
|
57
|
+
const plugins = [
|
|
58
|
+
[transformLitsxComponents, normalizeTransformLitsxOptions(options)],
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
if (shouldIncludeFeaturePlugin(sourceFeatures, "hooks")) {
|
|
62
|
+
plugins.push([transformLitsxHooks, options.transformLitsxHooks || {}]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (shouldIncludeFeaturePlugin(sourceFeatures, "domRefs")) {
|
|
66
|
+
plugins.push([transformLitsxDomRefs, options.transformLitsxDomRefs || {}]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (shouldIncludeFeaturePlugin(sourceFeatures, "scopedElements")) {
|
|
70
|
+
plugins.push([transformLitsxScopedElements, options.transformLitsxScopedElements || {}]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (options.jsxTemplate !== false) {
|
|
74
|
+
if (options.jsxTemplateOptions && Object.keys(options.jsxTemplateOptions).length > 0) {
|
|
75
|
+
plugins.push([transformJsxHtmlTemplate, options.jsxTemplateOptions]);
|
|
76
|
+
} else {
|
|
77
|
+
plugins.push(transformJsxHtmlTemplate);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return plugins;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
default as transformLitsxComponents,
|
|
86
|
+
createTransformFunctionToClassPlugin as createTransformLitsxComponentsPlugin,
|
|
87
|
+
} from "./internal/transform-litsx-components.js";
|
|
88
|
+
export {
|
|
89
|
+
setTypescriptModule,
|
|
90
|
+
} from "./internal/transform-litsx-properties.js";
|