@pithyjs/vite-plugin 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +237 -0
- package/dist/aotCodegen/generateBindings.d.ts +184 -0
- package/dist/aotCodegen/generateBindings.d.ts.map +1 -0
- package/dist/aotCodegen/generateBindings.js +326 -0
- package/dist/aotCodegen/generateBindings.js.map +1 -0
- package/dist/aotCodegen/generateDomFactory.d.ts +59 -0
- package/dist/aotCodegen/generateDomFactory.d.ts.map +1 -0
- package/dist/aotCodegen/generateDomFactory.js +302 -0
- package/dist/aotCodegen/generateDomFactory.js.map +1 -0
- package/dist/aotCodegen/index.d.ts +18 -0
- package/dist/aotCodegen/index.d.ts.map +1 -0
- package/dist/aotCodegen/index.js +17 -0
- package/dist/aotCodegen/index.js.map +1 -0
- package/dist/aotCodegen/serializeNode.d.ts +74 -0
- package/dist/aotCodegen/serializeNode.d.ts.map +1 -0
- package/dist/aotCodegen/serializeNode.js +169 -0
- package/dist/aotCodegen/serializeNode.js.map +1 -0
- package/dist/aotCodegen/structuralCodegen.d.ts +38 -0
- package/dist/aotCodegen/structuralCodegen.d.ts.map +1 -0
- package/dist/aotCodegen/structuralCodegen.js +56 -0
- package/dist/aotCodegen/structuralCodegen.js.map +1 -0
- package/dist/aotCodegen/utils.d.ts +38 -0
- package/dist/aotCodegen/utils.d.ts.map +1 -0
- package/dist/aotCodegen/utils.js +48 -0
- package/dist/aotCodegen/utils.js.map +1 -0
- package/dist/babelPlugin/babelPlugin.d.ts +44 -0
- package/dist/babelPlugin/babelPlugin.d.ts.map +1 -0
- package/dist/babelPlugin/babelPlugin.js +392 -0
- package/dist/babelPlugin/babelPlugin.js.map +1 -0
- package/dist/babelPlugin/extractOriginalTagNames.d.ts +20 -0
- package/dist/babelPlugin/extractOriginalTagNames.d.ts.map +1 -0
- package/dist/babelPlugin/extractOriginalTagNames.js +44 -0
- package/dist/babelPlugin/extractOriginalTagNames.js.map +1 -0
- package/dist/babelPlugin/extractUsedIdentifiers.d.ts +26 -0
- package/dist/babelPlugin/extractUsedIdentifiers.d.ts.map +1 -0
- package/dist/babelPlugin/extractUsedIdentifiers.js +206 -0
- package/dist/babelPlugin/extractUsedIdentifiers.js.map +1 -0
- package/dist/buildPlugin/plugin.d.ts +79 -0
- package/dist/buildPlugin/plugin.d.ts.map +1 -0
- package/dist/buildPlugin/plugin.js +142 -0
- package/dist/buildPlugin/plugin.js.map +1 -0
- package/dist/designSystemPlugin/collector.d.ts +63 -0
- package/dist/designSystemPlugin/collector.d.ts.map +1 -0
- package/dist/designSystemPlugin/collector.js +175 -0
- package/dist/designSystemPlugin/collector.js.map +1 -0
- package/dist/designSystemPlugin/cssTokens.d.ts +34 -0
- package/dist/designSystemPlugin/cssTokens.d.ts.map +1 -0
- package/dist/designSystemPlugin/cssTokens.js +56 -0
- package/dist/designSystemPlugin/cssTokens.js.map +1 -0
- package/dist/designSystemPlugin/enumerate.d.ts +24 -0
- package/dist/designSystemPlugin/enumerate.d.ts.map +1 -0
- package/dist/designSystemPlugin/enumerate.js +430 -0
- package/dist/designSystemPlugin/enumerate.js.map +1 -0
- package/dist/designSystemPlugin/extractor.d.ts +95 -0
- package/dist/designSystemPlugin/extractor.d.ts.map +1 -0
- package/dist/designSystemPlugin/extractor.js +150 -0
- package/dist/designSystemPlugin/extractor.js.map +1 -0
- package/dist/designSystemPlugin/generator.d.ts +33 -0
- package/dist/designSystemPlugin/generator.d.ts.map +1 -0
- package/dist/designSystemPlugin/generator.js +256 -0
- package/dist/designSystemPlugin/generator.js.map +1 -0
- package/dist/designSystemPlugin/index.d.ts +18 -0
- package/dist/designSystemPlugin/index.d.ts.map +1 -0
- package/dist/designSystemPlugin/index.js +19 -0
- package/dist/designSystemPlugin/index.js.map +1 -0
- package/dist/designSystemPlugin/patterns/a11y.d.ts +19 -0
- package/dist/designSystemPlugin/patterns/a11y.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/a11y.js +230 -0
- package/dist/designSystemPlugin/patterns/a11y.js.map +1 -0
- package/dist/designSystemPlugin/patterns/borders.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/borders.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/borders.js +474 -0
- package/dist/designSystemPlugin/patterns/borders.js.map +1 -0
- package/dist/designSystemPlugin/patterns/colors.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/colors.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/colors.js +134 -0
- package/dist/designSystemPlugin/patterns/colors.js.map +1 -0
- package/dist/designSystemPlugin/patterns/iconography.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/iconography.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/iconography.js +61 -0
- package/dist/designSystemPlugin/patterns/iconography.js.map +1 -0
- package/dist/designSystemPlugin/patterns/index.d.ts +22 -0
- package/dist/designSystemPlugin/patterns/index.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/index.js +44 -0
- package/dist/designSystemPlugin/patterns/index.js.map +1 -0
- package/dist/designSystemPlugin/patterns/layout.d.ts +34 -0
- package/dist/designSystemPlugin/patterns/layout.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/layout.js +917 -0
- package/dist/designSystemPlugin/patterns/layout.js.map +1 -0
- package/dist/designSystemPlugin/patterns/motion.d.ts +19 -0
- package/dist/designSystemPlugin/patterns/motion.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/motion.js +162 -0
- package/dist/designSystemPlugin/patterns/motion.js.map +1 -0
- package/dist/designSystemPlugin/patterns/radius.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/radius.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/radius.js +107 -0
- package/dist/designSystemPlugin/patterns/radius.js.map +1 -0
- package/dist/designSystemPlugin/patterns/shadows.d.ts +19 -0
- package/dist/designSystemPlugin/patterns/shadows.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/shadows.js +161 -0
- package/dist/designSystemPlugin/patterns/shadows.js.map +1 -0
- package/dist/designSystemPlugin/patterns/smartContainer.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/smartContainer.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/smartContainer.js +96 -0
- package/dist/designSystemPlugin/patterns/smartContainer.js.map +1 -0
- package/dist/designSystemPlugin/patterns/spacing.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/spacing.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/spacing.js +333 -0
- package/dist/designSystemPlugin/patterns/spacing.js.map +1 -0
- package/dist/designSystemPlugin/patterns/typography.d.ts +18 -0
- package/dist/designSystemPlugin/patterns/typography.d.ts.map +1 -0
- package/dist/designSystemPlugin/patterns/typography.js +416 -0
- package/dist/designSystemPlugin/patterns/typography.js.map +1 -0
- package/dist/designSystemPlugin/plugin.d.ts +39 -0
- package/dist/designSystemPlugin/plugin.d.ts.map +1 -0
- package/dist/designSystemPlugin/plugin.js +351 -0
- package/dist/designSystemPlugin/plugin.js.map +1 -0
- package/dist/designSystemPlugin/types.d.ts +156 -0
- package/dist/designSystemPlugin/types.d.ts.map +1 -0
- package/dist/designSystemPlugin/types.js +49 -0
- package/dist/designSystemPlugin/types.js.map +1 -0
- package/dist/htmlTransform/htmlTransform.d.ts +47 -0
- package/dist/htmlTransform/htmlTransform.d.ts.map +1 -0
- package/dist/htmlTransform/htmlTransform.js +86 -0
- package/dist/htmlTransform/htmlTransform.js.map +1 -0
- package/dist/iconsPlugin/index.d.ts +18 -0
- package/dist/iconsPlugin/index.d.ts.map +1 -0
- package/dist/iconsPlugin/index.js +17 -0
- package/dist/iconsPlugin/index.js.map +1 -0
- package/dist/iconsPlugin/optimize.d.ts +54 -0
- package/dist/iconsPlugin/optimize.d.ts.map +1 -0
- package/dist/iconsPlugin/optimize.js +253 -0
- package/dist/iconsPlugin/optimize.js.map +1 -0
- package/dist/iconsPlugin/plugin.d.ts +49 -0
- package/dist/iconsPlugin/plugin.d.ts.map +1 -0
- package/dist/iconsPlugin/plugin.js +242 -0
- package/dist/iconsPlugin/plugin.js.map +1 -0
- package/dist/iconsPlugin/types.d.ts +49 -0
- package/dist/iconsPlugin/types.d.ts.map +1 -0
- package/dist/iconsPlugin/types.js +30 -0
- package/dist/iconsPlugin/types.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/pithyPlugin/plugin.d.ts +38 -0
- package/dist/pithyPlugin/plugin.d.ts.map +1 -0
- package/dist/pithyPlugin/plugin.js +119 -0
- package/dist/pithyPlugin/plugin.js.map +1 -0
- package/dist/routerPlugin/manifest.d.ts +33 -0
- package/dist/routerPlugin/manifest.d.ts.map +1 -0
- package/dist/routerPlugin/manifest.js +223 -0
- package/dist/routerPlugin/manifest.js.map +1 -0
- package/dist/routerPlugin/plugin.d.ts +51 -0
- package/dist/routerPlugin/plugin.d.ts.map +1 -0
- package/dist/routerPlugin/plugin.js +164 -0
- package/dist/routerPlugin/plugin.js.map +1 -0
- package/dist/routerPlugin/scanner.d.ts +69 -0
- package/dist/routerPlugin/scanner.d.ts.map +1 -0
- package/dist/routerPlugin/scanner.js +290 -0
- package/dist/routerPlugin/scanner.js.map +1 -0
- package/dist/ssrPlugin/plugin.d.ts +76 -0
- package/dist/ssrPlugin/plugin.d.ts.map +1 -0
- package/dist/ssrPlugin/plugin.js +439 -0
- package/dist/ssrPlugin/plugin.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.aot.structural-codegen",
|
|
4
|
+
"title": "AOT Structural Directive Codegen",
|
|
5
|
+
"category": "compiler",
|
|
6
|
+
"description": "Generates code for structural directives (@if, @for, @switch) in AOT-compiled templates.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "experimental",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "aotCodegen.test.ts" },
|
|
11
|
+
"integration": { "required": false, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
import type { PithyNode } from '../../../compiler/src/parse/types';
|
|
17
|
+
export { collectIfChain, isDefaultCase, isStructuralNode, } from '../../../compiler/src/structuralUtils';
|
|
18
|
+
/**
|
|
19
|
+
* Create a shallow copy of a PithyNode with structural directive fields removed.
|
|
20
|
+
* Preserves all other properties (tag, attrs, children, non-structural directives).
|
|
21
|
+
*
|
|
22
|
+
* Used to prepare nodes for sub-factory generation — the structural directive
|
|
23
|
+
* itself is handled at the parent level, not inside the sub-factory.
|
|
24
|
+
*
|
|
25
|
+
* AOT-specific: the runtime doesn't need this because it processes directives
|
|
26
|
+
* in-place rather than generating factories.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const node = { type: 'element', tagName: 'div',
|
|
31
|
+
* directives: { if: 'show()', events: { click: 'fn()' } } };
|
|
32
|
+
* const stripped = stripStructuralDirective(node);
|
|
33
|
+
* // stripped.directives → { events: { click: 'fn()' } } (no @if)
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
/** @codexApi {"parent":"pithy.aot.structural-codegen","name":"stripStructuralDirective","stability":"experimental","signature":"(node: PithyNode) => PithyNode"} */
|
|
37
|
+
export declare function stripStructuralDirective(node: PithyNode): PithyNode;
|
|
38
|
+
//# sourceMappingURL=structuralCodegen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structuralCodegen.d.ts","sourceRoot":"","sources":["../../src/aotCodegen/structuralCodegen.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF,OAAO,KAAK,EACV,SAAS,EAEV,MAAM,mCAAmC,CAAC;AAI3C,OAAO,EACL,cAAc,EACd,aAAa,EACb,gBAAgB,GACjB,MAAM,uCAAuC,CAAC;AAE/C;;;;;;;;;;;;;;;;;GAiBG;AACH,oKAAoK;AACpK,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,CA6BnE"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.aot.structural-codegen",
|
|
4
|
+
"title": "AOT Structural Directive Codegen",
|
|
5
|
+
"category": "compiler",
|
|
6
|
+
"description": "Generates code for structural directives (@if, @for, @switch) in AOT-compiled templates.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "experimental",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "aotCodegen.test.ts" },
|
|
11
|
+
"integration": { "required": false, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
// Re-export shared utilities from compiler source
|
|
17
|
+
// (single source of truth — also used by runtime bindConditionals/bindSwitchCases)
|
|
18
|
+
export { collectIfChain, isDefaultCase, isStructuralNode, } from '../../../compiler/src/structuralUtils';
|
|
19
|
+
/**
|
|
20
|
+
* Create a shallow copy of a PithyNode with structural directive fields removed.
|
|
21
|
+
* Preserves all other properties (tag, attrs, children, non-structural directives).
|
|
22
|
+
*
|
|
23
|
+
* Used to prepare nodes for sub-factory generation — the structural directive
|
|
24
|
+
* itself is handled at the parent level, not inside the sub-factory.
|
|
25
|
+
*
|
|
26
|
+
* AOT-specific: the runtime doesn't need this because it processes directives
|
|
27
|
+
* in-place rather than generating factories.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const node = { type: 'element', tagName: 'div',
|
|
32
|
+
* directives: { if: 'show()', events: { click: 'fn()' } } };
|
|
33
|
+
* const stripped = stripStructuralDirective(node);
|
|
34
|
+
* // stripped.directives → { events: { click: 'fn()' } } (no @if)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
/** @codexApi {"parent":"pithy.aot.structural-codegen","name":"stripStructuralDirective","stability":"experimental","signature":"(node: PithyNode) => PithyNode"} */
|
|
38
|
+
export function stripStructuralDirective(node) {
|
|
39
|
+
// Strip @default from attrs if present
|
|
40
|
+
let attrs = node.attrs;
|
|
41
|
+
if (attrs && '@default' in attrs) {
|
|
42
|
+
const { '@default': _default, ...restAttrs } = attrs;
|
|
43
|
+
attrs = Object.keys(restAttrs).length > 0 ? restAttrs : undefined;
|
|
44
|
+
}
|
|
45
|
+
if (!node.directives) {
|
|
46
|
+
// No directives to strip, but @default may have been in attrs
|
|
47
|
+
return attrs !== node.attrs ? { ...node, attrs } : node;
|
|
48
|
+
}
|
|
49
|
+
const { if: _if, else: _else, elseIf: _elseIf, for: _for, switch: _switch, case: _case, ...rest } = node.directives;
|
|
50
|
+
return {
|
|
51
|
+
...node,
|
|
52
|
+
attrs,
|
|
53
|
+
directives: Object.keys(rest).length > 0 ? rest : undefined,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=structuralCodegen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structuralCodegen.js","sourceRoot":"","sources":["../../src/aotCodegen/structuralCodegen.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAOF,kDAAkD;AAClD,mFAAmF;AACnF,OAAO,EACL,cAAc,EACd,aAAa,EACb,gBAAgB,GACjB,MAAM,uCAAuC,CAAC;AAE/C;;;;;;;;;;;;;;;;;GAiBG;AACH,oKAAoK;AACpK,MAAM,UAAU,wBAAwB,CAAC,IAAe;IACtD,uCAAuC;IACvC,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACvB,IAAI,KAAK,IAAI,UAAU,IAAI,KAAK,EAAE,CAAC;QACjC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,SAAS,EAAE,GAAG,KAAK,CAAC;QACrD,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IACpE,CAAC;IAED,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;QACrB,8DAA8D;QAC9D,OAAO,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAC1D,CAAC;IAED,MAAM,EACJ,EAAE,EAAE,GAAG,EACP,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,OAAO,EACf,GAAG,EAAE,IAAI,EACT,MAAM,EAAE,OAAO,EACf,IAAI,EAAE,KAAK,EACX,GAAG,IAAI,EACR,GAAG,IAAI,CAAC,UAAU,CAAC;IAEpB,OAAO;QACL,GAAG,IAAI;QACP,KAAK;QACL,UAAU,EACR,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAE,IAAyB,CAAC,CAAC,CAAC,SAAS;KACxE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.aot.utils",
|
|
4
|
+
"title": "AOT String Utilities",
|
|
5
|
+
"category": "compiler",
|
|
6
|
+
"description": "Utility functions for escaping and formatting strings in AOT-generated code.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "experimental",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "aotCodegen.test.ts" },
|
|
11
|
+
"integration": { "required": false, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Escape a string for use inside a single-quoted JS string literal.
|
|
18
|
+
*
|
|
19
|
+
* Handles: null byte, backslash, single quote, newline, carriage return,
|
|
20
|
+
* tab, and JS line separators (U+2028, U+2029).
|
|
21
|
+
* Does NOT escape double quotes (unnecessary inside single-quoted strings).
|
|
22
|
+
*
|
|
23
|
+
* Used by both `serializeNode` and `serializeBindings` to produce
|
|
24
|
+
* safe string literals in generated factory code.
|
|
25
|
+
*
|
|
26
|
+
* @param s - Raw string to escape
|
|
27
|
+
* @returns Escaped string safe for embedding in `'...'` JS literals
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* escapeJS("it's a test") // "it\\'s a test"
|
|
32
|
+
* escapeJS("line1\nline2") // "line1\\nline2"
|
|
33
|
+
* escapeJS("back\\slash") // "back\\\\slash"
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
/** @codexApi {"parent":"pithy.aot.utils","name":"escapeJS","stability":"stable","signature":"(s: string) => string"} */
|
|
37
|
+
export declare function escapeJS(s: string): string;
|
|
38
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/aotCodegen/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wHAAwH;AACxH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAU1C"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.aot.utils",
|
|
4
|
+
"title": "AOT String Utilities",
|
|
5
|
+
"category": "compiler",
|
|
6
|
+
"description": "Utility functions for escaping and formatting strings in AOT-generated code.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "experimental",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "aotCodegen.test.ts" },
|
|
11
|
+
"integration": { "required": false, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Escape a string for use inside a single-quoted JS string literal.
|
|
18
|
+
*
|
|
19
|
+
* Handles: null byte, backslash, single quote, newline, carriage return,
|
|
20
|
+
* tab, and JS line separators (U+2028, U+2029).
|
|
21
|
+
* Does NOT escape double quotes (unnecessary inside single-quoted strings).
|
|
22
|
+
*
|
|
23
|
+
* Used by both `serializeNode` and `serializeBindings` to produce
|
|
24
|
+
* safe string literals in generated factory code.
|
|
25
|
+
*
|
|
26
|
+
* @param s - Raw string to escape
|
|
27
|
+
* @returns Escaped string safe for embedding in `'...'` JS literals
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* escapeJS("it's a test") // "it\\'s a test"
|
|
32
|
+
* escapeJS("line1\nline2") // "line1\\nline2"
|
|
33
|
+
* escapeJS("back\\slash") // "back\\\\slash"
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
/** @codexApi {"parent":"pithy.aot.utils","name":"escapeJS","stability":"stable","signature":"(s: string) => string"} */
|
|
37
|
+
export function escapeJS(s) {
|
|
38
|
+
return s
|
|
39
|
+
.replace(/\\/g, '\\\\')
|
|
40
|
+
.replace(/'/g, "\\'")
|
|
41
|
+
.replace(/\0/g, '\\x00')
|
|
42
|
+
.replace(/\n/g, '\\n')
|
|
43
|
+
.replace(/\r/g, '\\r')
|
|
44
|
+
.replace(/\t/g, '\\t')
|
|
45
|
+
.replace(/\u2028/g, '\\u2028')
|
|
46
|
+
.replace(/\u2029/g, '\\u2029');
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/aotCodegen/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wHAAwH;AACxH,MAAM,UAAU,QAAQ,CAAC,CAAS;IAChC,OAAO,CAAC;SACL,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;SACpB,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC;SACvB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;SACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;SACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;SACrB,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC;SAC7B,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.babel.html-context-plugin",
|
|
4
|
+
"title": "Babel HTML Context Injection Plugin",
|
|
5
|
+
"category": "plugin",
|
|
6
|
+
"description": "Babel plugin that injects compilation context into html`…` tagged template literals for AOT and scope ID generation.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "stable",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "babelPluginAot.test.ts" },
|
|
11
|
+
"integration": { "required": true, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
import type { PluginObj, PluginPass } from '@babel/core';
|
|
17
|
+
import { type ComponentScopesMap } from './extractUsedIdentifiers';
|
|
18
|
+
interface PluginState extends PluginPass {
|
|
19
|
+
importedIdentifiers: Set<string>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configuration options for the Pithy Babel HTML context injection plugin.
|
|
23
|
+
*/
|
|
24
|
+
/** @codexApi {"parent":"pithy.babel.html-context-plugin","name":"PithyBabelPluginOptions","stability":"stable","signature":"interface PithyBabelPluginOptions"} */
|
|
25
|
+
export interface PithyBabelPluginOptions {
|
|
26
|
+
componentScopes?: ComponentScopesMap;
|
|
27
|
+
/** Enable AOT template compilation. Default: false. */
|
|
28
|
+
aot?: boolean;
|
|
29
|
+
/** Fail the build if any template falls back to JIT (uses ${expr} syntax). Default: false. */
|
|
30
|
+
strictAot?: boolean;
|
|
31
|
+
/** File path for deterministic scope ID generation (typically the Vite module id). */
|
|
32
|
+
filename?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Babel plugin that transforms `html\`...\`` tagged template expressions.
|
|
36
|
+
* In non-AOT mode, rewrites to `html(str, ctx)`. In AOT mode, pre-compiles
|
|
37
|
+
* to a DOM factory function eliminating runtime HTML parsing.
|
|
38
|
+
* @param options - Plugin configuration (AOT mode, component scopes, filename)
|
|
39
|
+
* @returns Babel plugin object
|
|
40
|
+
*/
|
|
41
|
+
/** @codexApi {"parent":"pithy.babel.html-context-plugin","name":"pithyBabelPlugin","stability":"stable","signature":"(options?: PithyBabelPluginOptions) => PluginObj<PluginState>"} */
|
|
42
|
+
export default function pithyBabelPlugin(options?: PithyBabelPluginOptions): PluginObj<PluginState>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=babelPlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babelPlugin.d.ts","sourceRoot":"","sources":["../../src/babelPlugin/babelPlugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAGF,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzD,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,0BAA0B,CAAC;AAWlC,UAAU,WAAY,SAAQ,UAAU;IACtC,mBAAmB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAClC;AAiCD;;GAEG;AACH,mKAAmK;AACnK,MAAM,WAAW,uBAAuB;IACtC,eAAe,CAAC,EAAE,kBAAkB,CAAC;IACrC,uDAAuD;IACvD,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,8FAA8F;IAC9F,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,sFAAsF;IACtF,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAoBD;;;;;;GAMG;AACH,wLAAwL;AACxL,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,OAAO,CAAC,EAAE,uBAAuB,GAChC,SAAS,CAAC,WAAW,CAAC,CAgaxB"}
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.babel.html-context-plugin",
|
|
4
|
+
"title": "Babel HTML Context Injection Plugin",
|
|
5
|
+
"category": "plugin",
|
|
6
|
+
"description": "Babel plugin that injects compilation context into html`…` tagged template literals for AOT and scope ID generation.",
|
|
7
|
+
"since": "0.0.1",
|
|
8
|
+
"stability": "stable",
|
|
9
|
+
"testing": {
|
|
10
|
+
"unit": { "required": true, "covered": true, "file": "babelPluginAot.test.ts" },
|
|
11
|
+
"integration": { "required": true, "covered": false },
|
|
12
|
+
"e2e": { "required": false, "covered": false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
import * as t from '@babel/types';
|
|
17
|
+
import { parseFragment } from 'parse5';
|
|
18
|
+
import { parse5ToPithyTree } from '../../../compiler/src';
|
|
19
|
+
import { extractUsedIdentifiers, } from './extractUsedIdentifiers';
|
|
20
|
+
import { extractOriginalTagNames, extractOriginalAttributeNames, } from './extractOriginalTagNames';
|
|
21
|
+
import { warnOnLowercaseComponentTags } from '../../../compiler/src/utils';
|
|
22
|
+
import { escapeComponentTags } from '../../../compiler/src/parse/escapeComponentTags';
|
|
23
|
+
import { createHash } from 'crypto';
|
|
24
|
+
import { parse as babelParse } from '@babel/parser';
|
|
25
|
+
import { generateDomFactory } from '../aotCodegen';
|
|
26
|
+
/**
|
|
27
|
+
* Extract parameter names from arrow function expressions in template strings.
|
|
28
|
+
* Used to exclude event handler params (e.g., `ev`) from the hoisted context object.
|
|
29
|
+
* @param s - The expression string to scan for arrow functions
|
|
30
|
+
* @returns Set of parameter names found
|
|
31
|
+
*/
|
|
32
|
+
/** @codexApi {"parent":"pithy.babel.html-context-plugin","name":"extractArrowParamNames","stability":"internal","signature":"(s: string) => Set<string>"} */
|
|
33
|
+
function extractArrowParamNames(s) {
|
|
34
|
+
// Very conservative matcher:
|
|
35
|
+
// - "(ev) =>", "(ev: Event) =>", "ev =>"
|
|
36
|
+
// - avoids destructuring/complex params on purpose (we don't want those in ctx anyway)
|
|
37
|
+
const out = new Set();
|
|
38
|
+
const re = /(?:\(\s*([A-Za-z_$][\w$]*)\s*(?::[^)]*)?\s*\)|\b([A-Za-z_$][\w$]*)\b)\s*=>/g;
|
|
39
|
+
let m;
|
|
40
|
+
while ((m = re.exec(s))) {
|
|
41
|
+
const name = m[1] || m[2];
|
|
42
|
+
if (name)
|
|
43
|
+
out.add(name);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
// Names that should never be hoisted into ctx even if seen in expressions
|
|
48
|
+
const RESERVED_LOCAL_EVENT_NAMES = new Set([
|
|
49
|
+
'$event',
|
|
50
|
+
'event',
|
|
51
|
+
'ev',
|
|
52
|
+
'e',
|
|
53
|
+
'evt',
|
|
54
|
+
]);
|
|
55
|
+
/**
|
|
56
|
+
* Generate a deterministic scope ID from a file path.
|
|
57
|
+
* Uses base64url-safe encoding (only [A-Za-z0-9_-]) since the ID is
|
|
58
|
+
* interpolated into attribute names like data-scope-{id}.
|
|
59
|
+
* Note: pithyPlugin uses raw base64 slicing — the two will diverge for
|
|
60
|
+
* paths containing characters that produce +/= in base64.
|
|
61
|
+
*/
|
|
62
|
+
function generateScopeId(filepath) {
|
|
63
|
+
const base64 = Buffer.from(filepath).toString('base64');
|
|
64
|
+
// Convert to base64url-safe variant: only [A-Za-z0-9_-] allowed
|
|
65
|
+
// (scopeId is interpolated into attribute names like data-scope-{id})
|
|
66
|
+
const safe = base64
|
|
67
|
+
.replace(/\+/g, '-')
|
|
68
|
+
.replace(/\//g, '_')
|
|
69
|
+
.replace(/=+$/, '');
|
|
70
|
+
return 'pithy-' + safe.slice(0, 6);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Babel plugin that transforms `html\`...\`` tagged template expressions.
|
|
74
|
+
* In non-AOT mode, rewrites to `html(str, ctx)`. In AOT mode, pre-compiles
|
|
75
|
+
* to a DOM factory function eliminating runtime HTML parsing.
|
|
76
|
+
* @param options - Plugin configuration (AOT mode, component scopes, filename)
|
|
77
|
+
* @returns Babel plugin object
|
|
78
|
+
*/
|
|
79
|
+
/** @codexApi {"parent":"pithy.babel.html-context-plugin","name":"pithyBabelPlugin","stability":"stable","signature":"(options?: PithyBabelPluginOptions) => PluginObj<PluginState>"} */
|
|
80
|
+
export default function pithyBabelPlugin(options) {
|
|
81
|
+
const componentScopes = options?.componentScopes;
|
|
82
|
+
const enableAot = options?.aot ?? false;
|
|
83
|
+
let importedIdentifiers;
|
|
84
|
+
// AOT state — accumulated per file, injected in Program.exit
|
|
85
|
+
let aotFactoryBlocks;
|
|
86
|
+
let aotNeedsApplyBindings;
|
|
87
|
+
let aotHashCounter;
|
|
88
|
+
let aotDisabledByCollision;
|
|
89
|
+
// Track html`` usage for dead import stripping
|
|
90
|
+
let totalHtmlTagsFound;
|
|
91
|
+
let totalHtmlTagsAotCompiled;
|
|
92
|
+
// Local name of html imported from @pithyjs/core (handles aliased imports like `import { html as h }`)
|
|
93
|
+
let pithyCoreHtmlLocalName;
|
|
94
|
+
// Set of local names of html imported from non-@pithyjs/core packages (e.g., lit)
|
|
95
|
+
let foreignHtmlLocalNames;
|
|
96
|
+
return {
|
|
97
|
+
name: 'pithy:html-context-inject',
|
|
98
|
+
pre() {
|
|
99
|
+
importedIdentifiers = new Set();
|
|
100
|
+
aotFactoryBlocks = [];
|
|
101
|
+
aotNeedsApplyBindings = false;
|
|
102
|
+
aotHashCounter = new Map();
|
|
103
|
+
aotDisabledByCollision = false;
|
|
104
|
+
totalHtmlTagsFound = 0;
|
|
105
|
+
totalHtmlTagsAotCompiled = 0;
|
|
106
|
+
pithyCoreHtmlLocalName = null;
|
|
107
|
+
foreignHtmlLocalNames = new Set();
|
|
108
|
+
},
|
|
109
|
+
visitor: {
|
|
110
|
+
Program: {
|
|
111
|
+
enter(path, _state) {
|
|
112
|
+
for (const node of path.node.body) {
|
|
113
|
+
if (t.isImportDeclaration(node) && node.importKind !== 'type') {
|
|
114
|
+
for (const spec of node.specifiers) {
|
|
115
|
+
// Skip type-only specifiers — they don't exist at runtime
|
|
116
|
+
if (t.isImportSpecifier(spec) && spec.importKind === 'type')
|
|
117
|
+
continue;
|
|
118
|
+
if (t.isImportSpecifier(spec) ||
|
|
119
|
+
t.isImportDefaultSpecifier(spec) ||
|
|
120
|
+
t.isImportNamespaceSpecifier(spec)) {
|
|
121
|
+
importedIdentifiers.add(spec.local.name);
|
|
122
|
+
}
|
|
123
|
+
// Detect html imports and track their source package
|
|
124
|
+
if (t.isImportSpecifier(spec) &&
|
|
125
|
+
spec.importKind !== 'type' &&
|
|
126
|
+
t.isIdentifier(spec.imported) &&
|
|
127
|
+
spec.imported.name === 'html') {
|
|
128
|
+
if (node.source.value === '@pithyjs/core') {
|
|
129
|
+
pithyCoreHtmlLocalName = spec.local.name;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
foreignHtmlLocalNames.add(spec.local.name);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Check for __applyBindings binding collision early — if a user-defined
|
|
139
|
+
// binding exists, AOT factories would reference the wrong symbol.
|
|
140
|
+
if (enableAot && path.scope.hasBinding('__applyBindings')) {
|
|
141
|
+
aotDisabledByCollision = true;
|
|
142
|
+
console.warn(`[pithy:aot] Disabling AOT — existing binding "__applyBindings" ` +
|
|
143
|
+
`would shadow the runtime import.`);
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
exit(path) {
|
|
147
|
+
if (aotFactoryBlocks.length === 0)
|
|
148
|
+
return;
|
|
149
|
+
// 1. Add __applyBindings import (only if needed)
|
|
150
|
+
if (aotNeedsApplyBindings) {
|
|
151
|
+
const hasImport = path.node.body.some(stmt => t.isImportDeclaration(stmt) &&
|
|
152
|
+
stmt.importKind !== 'type' &&
|
|
153
|
+
stmt.source.value === '@pithyjs/core/runtime/applyBindings' &&
|
|
154
|
+
stmt.specifiers.some(s => t.isImportSpecifier(s) &&
|
|
155
|
+
s.importKind !== 'type' &&
|
|
156
|
+
t.isIdentifier(s.local) &&
|
|
157
|
+
s.local.name === '__applyBindings'));
|
|
158
|
+
if (!hasImport) {
|
|
159
|
+
const importDecl = t.importDeclaration([
|
|
160
|
+
t.importSpecifier(t.identifier('__applyBindings'), t.identifier('__applyBindings')),
|
|
161
|
+
], t.stringLiteral('@pithyjs/core/runtime/applyBindings'));
|
|
162
|
+
// Insert after directive prologues and existing imports.
|
|
163
|
+
// Two-phase scan: first skip all consecutive directives, then all imports.
|
|
164
|
+
let importInsertIndex = 0;
|
|
165
|
+
const body = path.node.body;
|
|
166
|
+
while (importInsertIndex < body.length &&
|
|
167
|
+
t.isExpressionStatement(body[importInsertIndex]) &&
|
|
168
|
+
t.isStringLiteral(body[importInsertIndex].expression)) {
|
|
169
|
+
importInsertIndex++;
|
|
170
|
+
}
|
|
171
|
+
while (importInsertIndex < body.length &&
|
|
172
|
+
t.isImportDeclaration(body[importInsertIndex])) {
|
|
173
|
+
importInsertIndex++;
|
|
174
|
+
}
|
|
175
|
+
path.node.body.splice(importInsertIndex, 0, importDecl);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// 2. Parse factory code blocks into AST and inject after last import
|
|
179
|
+
const statementsToInject = [];
|
|
180
|
+
for (const block of aotFactoryBlocks) {
|
|
181
|
+
const ast = babelParse(block, {
|
|
182
|
+
sourceType: 'module',
|
|
183
|
+
plugins: [],
|
|
184
|
+
});
|
|
185
|
+
if (ast.program?.body) {
|
|
186
|
+
statementsToInject.push(...ast.program.body);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (statementsToInject.length === 0)
|
|
190
|
+
return;
|
|
191
|
+
// Find insertion point: after directive prologues and import declarations.
|
|
192
|
+
// Two-phase scan: first skip all consecutive directives, then all imports.
|
|
193
|
+
let insertIndex = 0;
|
|
194
|
+
const bodyForFactory = path.node.body;
|
|
195
|
+
while (insertIndex < bodyForFactory.length &&
|
|
196
|
+
t.isExpressionStatement(bodyForFactory[insertIndex]) &&
|
|
197
|
+
t.isStringLiteral(bodyForFactory[insertIndex].expression)) {
|
|
198
|
+
insertIndex++;
|
|
199
|
+
}
|
|
200
|
+
while (insertIndex < bodyForFactory.length &&
|
|
201
|
+
t.isImportDeclaration(bodyForFactory[insertIndex])) {
|
|
202
|
+
insertIndex++;
|
|
203
|
+
}
|
|
204
|
+
path.node.body.splice(insertIndex, 0, ...statementsToInject);
|
|
205
|
+
// 3. Strip unused `html` import when all html`` tags were AOT-compiled.
|
|
206
|
+
// This enables Rollup to tree-shake html() → parseHtml → parse5 entirely.
|
|
207
|
+
// Safety: use Babel binding analysis (scope.crawl + getBinding) to verify
|
|
208
|
+
// the html identifier has no remaining references after AOT transforms.
|
|
209
|
+
// This covers all non-tagged-template usages: html(), const t = html, etc.
|
|
210
|
+
if (enableAot &&
|
|
211
|
+
totalHtmlTagsFound > 0 &&
|
|
212
|
+
totalHtmlTagsFound === totalHtmlTagsAotCompiled &&
|
|
213
|
+
pithyCoreHtmlLocalName) {
|
|
214
|
+
// Re-crawl scope to get accurate reference counts after AOT transforms
|
|
215
|
+
path.scope.crawl();
|
|
216
|
+
const binding = path.scope.getBinding(pithyCoreHtmlLocalName);
|
|
217
|
+
const stillReferenced = binding && binding.references > 0;
|
|
218
|
+
if (!stillReferenced) {
|
|
219
|
+
for (let i = path.node.body.length - 1; i >= 0; i--) {
|
|
220
|
+
const stmt = path.node.body[i];
|
|
221
|
+
if (!t.isImportDeclaration(stmt) || stmt.importKind === 'type')
|
|
222
|
+
continue;
|
|
223
|
+
// Only strip html from @pithyjs/core — never touch other packages (e.g., lit)
|
|
224
|
+
if (stmt.source.value !== '@pithyjs/core')
|
|
225
|
+
continue;
|
|
226
|
+
const htmlIdx = stmt.specifiers.findIndex(s => t.isImportSpecifier(s) &&
|
|
227
|
+
s.importKind !== 'type' &&
|
|
228
|
+
t.isIdentifier(s.imported) &&
|
|
229
|
+
s.imported.name === 'html');
|
|
230
|
+
if (htmlIdx !== -1) {
|
|
231
|
+
stmt.specifiers.splice(htmlIdx, 1);
|
|
232
|
+
// If no specifiers remain, remove the entire import declaration
|
|
233
|
+
if (stmt.specifiers.length === 0) {
|
|
234
|
+
path.node.body.splice(i, 1);
|
|
235
|
+
}
|
|
236
|
+
break; // html is typically imported once
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
TaggedTemplateExpression(path) {
|
|
244
|
+
// Only transform html`` from @pithyjs/core — leave other packages' html (e.g., lit) untouched.
|
|
245
|
+
// When we detected a @pithyjs/core html import, match its local name (handles aliases).
|
|
246
|
+
// When no @pithyjs/core import was found, fall back to matching literal "html" (bare usage),
|
|
247
|
+
// but skip if "html" is known to come from a foreign package.
|
|
248
|
+
const tagName = t.isIdentifier(path.node.tag)
|
|
249
|
+
? path.node.tag.name
|
|
250
|
+
: null;
|
|
251
|
+
if (!tagName)
|
|
252
|
+
return;
|
|
253
|
+
if (foreignHtmlLocalNames.has(tagName))
|
|
254
|
+
return;
|
|
255
|
+
const expectedName = pithyCoreHtmlLocalName ?? 'html';
|
|
256
|
+
if (tagName !== expectedName)
|
|
257
|
+
return;
|
|
258
|
+
totalHtmlTagsFound++;
|
|
259
|
+
const quasi = path.node.quasi;
|
|
260
|
+
const templateStr = quasi.quasis
|
|
261
|
+
.map(q => q.value.cooked ?? q.value.raw)
|
|
262
|
+
.join('');
|
|
263
|
+
// Warn about lowercase component tags used for imported components
|
|
264
|
+
if (importedIdentifiers.size > 0) {
|
|
265
|
+
warnOnLowercaseComponentTags(templateStr, importedIdentifiers);
|
|
266
|
+
}
|
|
267
|
+
// Escape PascalCase component tags to prevent parse5 from entering
|
|
268
|
+
// special insertion modes (e.g., "in select", "in table") that corrupt
|
|
269
|
+
// the parse tree by dropping non-conforming children.
|
|
270
|
+
const escapedStr = escapeComponentTags(templateStr);
|
|
271
|
+
const fragment = parseFragment(escapedStr, {
|
|
272
|
+
sourceCodeLocationInfo: true,
|
|
273
|
+
});
|
|
274
|
+
// Both casing maps must use the escaped string so offsets match parse5.
|
|
275
|
+
const tagNameCasingMap = extractOriginalTagNames(escapedStr);
|
|
276
|
+
const attributeNameCasingMap = extractOriginalAttributeNames(escapedStr);
|
|
277
|
+
const trees = (fragment.childNodes ?? [])
|
|
278
|
+
.map(child => parse5ToPithyTree(child, importedIdentifiers, tagNameCasingMap, undefined, attributeNameCasingMap))
|
|
279
|
+
.filter((t) => t != null);
|
|
280
|
+
if (trees.length === 0)
|
|
281
|
+
return;
|
|
282
|
+
// Collect identifiers referenced by expressions in the template
|
|
283
|
+
const usedIdentifiers = new Set();
|
|
284
|
+
for (const tree of trees) {
|
|
285
|
+
const ids = extractUsedIdentifiers(tree, componentScopes);
|
|
286
|
+
ids.forEach(id => usedIdentifiers.add(id));
|
|
287
|
+
}
|
|
288
|
+
// Also collect arrow-function param names appearing in attribute expressions
|
|
289
|
+
// so we can remove them from usedIdentifiers (e.g., "(ev) => ..." or "ev => ...").
|
|
290
|
+
const arrowParams = extractArrowParamNames(templateStr);
|
|
291
|
+
// Compute which imported identifiers are actually used as components
|
|
292
|
+
const usedComponents = new Set();
|
|
293
|
+
for (const tree of trees) {
|
|
294
|
+
if (tree.type === 'element' &&
|
|
295
|
+
tree.isComponent &&
|
|
296
|
+
tree.originalTagName) {
|
|
297
|
+
usedComponents.add(tree.originalTagName);
|
|
298
|
+
}
|
|
299
|
+
const checkChildren = (node) => {
|
|
300
|
+
if (node.children) {
|
|
301
|
+
for (const child of node.children) {
|
|
302
|
+
if (child.type === 'element' &&
|
|
303
|
+
child.isComponent &&
|
|
304
|
+
child.originalTagName) {
|
|
305
|
+
usedComponents.add(child.originalTagName);
|
|
306
|
+
}
|
|
307
|
+
checkChildren(child);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
checkChildren(tree);
|
|
312
|
+
}
|
|
313
|
+
// Final filter:
|
|
314
|
+
// - drop arrow params (local to inline handlers)
|
|
315
|
+
// - drop common event aliases ($event, event, ev, e, evt)
|
|
316
|
+
const filteredUsed = [...usedIdentifiers].filter(id => !arrowParams.has(id) && !RESERVED_LOCAL_EVENT_NAMES.has(id));
|
|
317
|
+
// Build context object (shared between AOT and non-AOT paths)
|
|
318
|
+
const contextProperties = [
|
|
319
|
+
// Only hoist filtered used identifiers
|
|
320
|
+
...filteredUsed.map(id => t.objectProperty(t.identifier(id), t.identifier(id), false, true)),
|
|
321
|
+
// Add only the components that are actually used in the template
|
|
322
|
+
...[...usedComponents].map(id => t.objectProperty(t.identifier(id), t.identifier(id), false, true)),
|
|
323
|
+
// Add imported identifiers as a special property for downstream checks
|
|
324
|
+
t.objectProperty(t.identifier('__importedIdentifiers'), t.newExpression(t.identifier('Set'), [
|
|
325
|
+
t.arrayExpression([...importedIdentifiers].map(id => t.stringLiteral(id))),
|
|
326
|
+
])),
|
|
327
|
+
];
|
|
328
|
+
const contextObject = t.objectExpression(contextProperties);
|
|
329
|
+
// ── AOT codegen path ──────────────────────────────────────────────
|
|
330
|
+
if (enableAot && !aotDisabledByCollision) {
|
|
331
|
+
// Templates with ${expr} expressions can't be AOT-compiled safely.
|
|
332
|
+
// PithyJS uses {{ expr }} syntax, not ${}, so this is an edge case guard.
|
|
333
|
+
if (quasi.expressions.length > 0) {
|
|
334
|
+
// Fall through to non-AOT path below
|
|
335
|
+
const msg = `[pithy:aot:jit-fallback] Template contains \${expr} expressions — skipping AOT. ` +
|
|
336
|
+
`Refactor to {{ expr }} syntax for AOT compilation.` +
|
|
337
|
+
(options?.filename ? ` (${options.filename})` : '');
|
|
338
|
+
if (options?.strictAot) {
|
|
339
|
+
throw path.buildCodeFrameError(msg);
|
|
340
|
+
}
|
|
341
|
+
console.warn(msg);
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
try {
|
|
345
|
+
// Generate deterministic hash from template content
|
|
346
|
+
const rawHash = createHash('sha256')
|
|
347
|
+
.update(templateStr)
|
|
348
|
+
.digest('hex')
|
|
349
|
+
.slice(0, 8);
|
|
350
|
+
const count = aotHashCounter.get(rawHash) ?? 0;
|
|
351
|
+
const hash = count > 0 ? `${rawHash}_${count}` : rawHash;
|
|
352
|
+
// Generate scope ID from filename (or fall back to hash)
|
|
353
|
+
const filename = options?.filename ??
|
|
354
|
+
path.hub?.file
|
|
355
|
+
?.opts?.filename ??
|
|
356
|
+
'';
|
|
357
|
+
const scopeId = filename
|
|
358
|
+
? generateScopeId(filename)
|
|
359
|
+
: `pithy-${rawHash.slice(0, 6)}`;
|
|
360
|
+
// Call AOT codegen
|
|
361
|
+
const aotResult = generateDomFactory(trees, hash, scopeId);
|
|
362
|
+
// Only increment counter after successful codegen — if codegen
|
|
363
|
+
// throws, the hash slot stays available for the next attempt.
|
|
364
|
+
aotHashCounter.set(rawHash, count + 1);
|
|
365
|
+
// Accumulate factory code for Program.exit injection
|
|
366
|
+
aotFactoryBlocks.push(aotResult.code);
|
|
367
|
+
// Detect actual applyBindings call sites rather than any occurrence of the
|
|
368
|
+
// identifier name. This avoids false positives from template text that may
|
|
369
|
+
// contain the string "__applyBindings" inside HTML or attribute values.
|
|
370
|
+
if (aotResult.code.includes('return __applyBindings(')) {
|
|
371
|
+
aotNeedsApplyBindings = true;
|
|
372
|
+
}
|
|
373
|
+
totalHtmlTagsAotCompiled++;
|
|
374
|
+
// Replace html`...` with __f_hash(contextObject)
|
|
375
|
+
path.replaceWith(t.callExpression(t.identifier(aotResult.factoryName), [
|
|
376
|
+
contextObject,
|
|
377
|
+
]));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
catch (err) {
|
|
381
|
+
// AOT codegen failed — fall through to runtime path
|
|
382
|
+
console.warn(`[pithy:aot] Codegen failed, falling back to runtime:`, err);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// ── Non-AOT path (original behavior) ─────────────────────────────
|
|
387
|
+
path.replaceWith(t.callExpression(path.node.tag, [quasi, contextObject]));
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
//# sourceMappingURL=babelPlugin.js.map
|