Skip to content

Conversation

@marcoroth
Copy link
Owner

@marcoroth marcoroth commented Nov 6, 2025

This pull request exposes new rewrite and rewriteString functions in the @herb-tools/rewriter package. This allows for more ergonomic use of rewriters.

Examples:

rewrite:

import { Herb } from "@herb-tools/node-wasm"
import { rewrite } from "@herb-tools/rewriter"
import { tailwindClassSorter } from "@herb-tools/rewriter/loader"
 
await Herb.load()

const template = `<div class="text-red-500 p-4 mt-2"></div>`
const parseResult = Herb.parse(template)

const { node, output } = await rewrite(template, [tailwindClassSorter()])

output
// Result: "<div class="mt-2 p-4 text-red-500"></div>"

rewriteString:

import { Herb } from "@herb-tools/node-wasm"
import { rewrite } from "@herb-tools/rewriter"
import { tailwindClassSorter } from "@herb-tools/rewriter/loader"
 
const template = `<div class="text-red-500 p-4 mt-2"></div>`

const output = await rewriteString(Herb, template, [tailwindClassSorter()])
// Result: "<div class="mt-2 p-4 text-red-500"></div>"

Additionally, it cleans up the interface of the rewriters to accept and return Node instead of ParseResult. This allows for more flexibility and also allows for partial rewrites of a tree/document.

@marcoroth marcoroth changed the title Rewriter: Expose rewrite function and refine types Rewriter: Expose rewrite/rewriteString functions and refine types Nov 6, 2025
@marcoroth marcoroth merged commit d6f003d into main Nov 6, 2025
12 checks passed
@marcoroth marcoroth deleted the rewriter-rewrite branch November 6, 2025 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants