LearnNewsExamplesServices
Frontmatter
id7158
titleCreate a Reusable, AST-Based Build-Time Processor
stateClosed
labels
enhancement
assigneestobiu
createdAtAug 2, 2025, 1:28 PM
updatedAtAug 2, 2025, 1:44 PM
githubUrlhttps://github.com/neomjs/neo/issues/7158
authortobiu
commentsCount0
parentIssue7130
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtAug 2, 2025, 1:44 PM

Create a Reusable, AST-Based Build-Time Processor

Closed v10.3.0 enhancement
tobiu
tobiu commented on Aug 2, 2025, 1:28 PM

1. Summary

Refactor the entire build-time template transformation pipeline out of buildScripts/buildESModules.mjs and into a new, self-contained, reusable utility module.

2. Rationale

Currently, the logic for finding and transforming html templates is tightly coupled to the buildESModules.mjs script. To support template transformation in other build environments (like the Webpack-based dist/dev and dist/production), this logic must be extracted. Creating a single, reusable processor ensures consistency, reduces code duplication, and makes the build system more modular and maintainable.

3. Scope & Implementation Plan

  1. Create New Utility: Create a new file at buildScripts/util/astTemplateProcessor.mjs.
  2. Move Core Logic:
    • Move the jsonToAst function from buildESModules.mjs into the new utility.
    • Create and export a primary function, e.g., processFileContent(fileContent).
    • This function will encapsulate the entire transformation pipeline:
      • Parsing the input string with acorn.
      • Performing the post-order traversal to find html templates.
      • Calling processHtmlTemplateLiteral() from templateBuildProcessor.mjs to get the VDOM object.
      • Using the moved jsonToAst to convert the VDOM object back into an AST node.
      • Replacing the original template node in the main AST.
      • Generating the final, transformed code string with astring.
  3. Refactor buildESModules.mjs:
    • Remove the moved logic (jsonToAst, the traversal, etc.).
    • Import the new processFileContent function from astTemplateProcessor.mjs.
    • In the minifyFile function, call processFileContent() to transform the file's content before passing it to Terser.

4. Definition of Done

  • The new buildScripts/util/astTemplateProcessor.mjs module exists and contains the full transformation logic.
  • buildESModules.mjs is simplified and correctly uses the new reusable utility.
  • Running npm run build-dist-esm produces the exact same correct output as it does now, confirming the refactoring was successful.
tobiu assigned to @tobiu on Aug 2, 2025, 1:28 PM
tobiu added the enhancement label on Aug 2, 2025, 1:28 PM
tobiu added parent issue #7130 on Aug 2, 2025, 1:28 PM
tobiu referenced in commit ce24aaa - "Create a Reusable, AST-Based Build-Time Processor #7158" on Aug 2, 2025, 1:44 PM
tobiu closed this issue on Aug 2, 2025, 1:44 PM