Description:
The current build-time approach, which uses regular expressions to find and replace templates, has proven to be brittle and incorrect. It cannot handle nested html templates and can be easily fooled by JSDoc comments or strings that happen to contain the `html`` sequence, leading to build failures. To ensure correctness and consistency with the runtime environment, the build process must be refactored to use a proper JavaScript parser.
Implementation Details:
- Tools:
acorn (to parse JS into an Abstract Syntax Tree) and astring (to generate JS code from the AST).
- Method:
- In the build script, for each
.mjs file, use acorn to parse the entire file content into an AST.
- Traverse the AST, specifically looking for
TaggedTemplateExpression nodes where the tag is an Identifier with the name html.
- Process these template nodes recursively (post-order traversal) to correctly handle nested templates from the inside out.
- The logic from
HtmlTemplateProcessorLogic will be used to convert the template into its VDOM object representation.
- The original
TaggedTemplateExpression node in the AST will be replaced with a new AST node representing the generated VDOM object (using an object-to-AST converter).
- Finally, use
astring to generate the final, correct JavaScript code from the modified AST.
- This new, robust process will replace the fragile regex-based
replace loop.
Description: The current build-time approach, which uses regular expressions to find and replace templates, has proven to be brittle and incorrect. It cannot handle nested
htmltemplates and can be easily fooled by JSDoc comments or strings that happen to contain the `html`` sequence, leading to build failures. To ensure correctness and consistency with the runtime environment, the build process must be refactored to use a proper JavaScript parser.Implementation Details:
acorn(to parse JS into an Abstract Syntax Tree) andastring(to generate JS code from the AST)..mjsfile, useacornto parse the entire file content into an AST.TaggedTemplateExpressionnodes where thetagis anIdentifierwith the namehtml.HtmlTemplateProcessorLogicwill be used to convert the template into its VDOM object representation.TaggedTemplateExpressionnode in the AST will be replaced with a new AST node representing the generated VDOM object (using an object-to-AST converter).astringto generate the final, correct JavaScript code from the modified AST.replaceloop.