A command-line tool to automatically convert a directory of static HTML files into a structured, component-based React application.
This tool analyzes your HTML to identify layouts and repeating elements, extracting them into individual JSX components with props. It intelligently handles assets like SVGs and ensures the generated code is clean, formatted, and ready for development.
- Automatic Componentization: Detects repetitive blocks of HTML and extracts them into reusable React components.
- Intelligent Prop Generation: Creates props for dynamic content such as text, links, and image sources that differ between component instances.
- Smart SVG Handling: Automatically detects
<svg>tags, saves them as external.svgfiles in a public directory, and replaces them with an<img>tag pointing to the new file. - Flexible Configuration: Easily customize paths, component detection rules, and code formatting through a central
config.jsonfile. - Code Formatting: Uses Prettier to automatically format all generated
.jsxfiles for consistency and readability.
The conversion is a two-step process:
-
Parse to AST (
index.js): First, the script reads all.htmlfiles from the input directory (specified inconfig.json). It useshtmlparser2to parse each file into an Abstract Syntax Tree (AST), which is a structured JSON representation of the HTML. These ASTs are saved to an intermediate directory. -
Convert to React (
converter.js): Second, the main script reads the generated ASTs. It analyzes the structure to find reusable "component candidates" based on rules in your configuration, generates props for them, and then writes the final.jsxfiles for both components and pages into the configured output directory.
The entire conversion process is controlled by a config.json file located in the scripts directory. This allows you to easily adapt the tool to different project structures and change component detection behavior without modifying the source code.
Here is the default configuration structure:
{
"paths": {
"htmlInput": "../html",
"asts": "../asts",
"reactOutput": "./react_output",
"components": "components",
"pages": "pages",
"public": "public",
"svgs": "svgs"
},
"componentization": {
"minChildrenForRepetition": 2,
"minRepetitionsForComponent": 2,
"layoutIdentifiers": [
"navbar", "footer", "sidebar", "main-content", "container"
],
"selfClosingTags": [
"img", "br", "hr", "input", "meta", "link", "area", "base", "col",
"embed", "param", "source", "track", "wbr"
]
},
"formatting": {
"prettier": {
"parser": "babel",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
}
}paths: Defines all the input and output directories. All paths are relative to thescriptsdirectory.componentization: Controls how the script identifies components.minRepetitionsForComponent: The number of times a structurally identical element must appear to be considered a reusable component.layoutIdentifiers: A list of class names or IDs that the script should treat as major layout components (e.g., 'navbar', 'footer').
formatting.prettier: An object containing Prettier formatting options to ensure your generated code is clean and consistent.
Follow these steps to set up and run the converter.
Make sure you have Node.js (version 14 or higher) and npm installed on your machine.
Your project should be organized with the following directory structure. The scripts and their configuration live in a scripts folder.
project-root/
│
├── html/
│ ├── index.html
│ ├── about.html
│ └── contact.html
│
├── scripts/
│ ├── converter.js
│ └── index.js
│
├── config.json <-- Your configuration file
├── package.json
└── ...
html/: The default input directory for your source.htmlfiles. You can change this inconfig.json.scripts/: This is where the conversion scripts and their configuration live.
The scripts rely on a few Node.js packages.
First, create a package.json file if you don't have one:
npm init -yNext, install the required dependencies:
npm install fs-extra htmlparser2 prettier dom-serializerYour package.json will now include these packages in its dependencies.
Before running the scripts, you must create the configuration file.
- In your
scriptsfolder, create a new file namedconfig.json. - Copy and paste the default configuration from the Configuration section above into this new file.
- Adjust the paths and rules as needed for your project.
The conversion process is run from the command line in two steps. The scripts will automatically use the settings from your config.json.
This command reads the files in your configured htmlInput directory and creates the intermediate .json files in your configured asts directory.
node scripts/index.js
```You will see output confirming that each HTML file has been processed.
#### **Step 2: Generate the React Components**
This command reads the ASTs and generates the final React project in your `reactOutput` directory.
```bash
node scripts/converter.jsThe console will log the creation of each new component and page.
After running the scripts, the react_output directory (or whatever you named it in config.json) will be created inside your scripts folder. It will have the following structure:
scripts/
│
└── react_output/
│
├── components/
│ ├── ReusableItem.jsx
│ └── ... (all other reusable components)
│
├── pages/
│ ├── Index.jsx
│ └── ... (all your main page components)
│
└── public/
└── svgs/
├── icon-xxxxxxxx.svg
└── ... (all extracted SVG files)
The names of the components, pages, public, and svgs directories are all defined in your config.json. You can now take the contents of this output directory and integrate them into a React project (e.g., one created with Create React App or Vite).