Strip inter-node whitespace in Astro and Svelte templates.
This repo contains:
This project strips inter-node whitespace (whitespace-only gaps between elements/expressions) in .astro and .svelte templates.
Importantly, this is a rewrite, not a simple "delete all whitespace text nodes" pass: in some cases we preserve the original formatting/newlines by moving whitespace into places that don’t create runtime text nodes. For example, when encountering <p>\n text, we may rewrite it to <p \n >text.
transform hook with enforce: "pre", so the whitespace is removed before Astro/Svelte compilation.astro:build and vite-plugin-svelte) so it consistently runs early.Minifying the built output (HTML emitted after compilation) can introduce hydration mismatches: the server-rendered HTML no longer matches what the client-side framework expects to hydrate (especially around text nodes / whitespace at component boundaries). By stripping whitespace before the template processor/compiler runs, the compiled output and hydration expectations stay in sync.
astro add astro-strip-whitespace
# or: pnpm add -D astro-strip-whitespace
# or: npm i -D astro-strip-whitespace
# or: yarn add -D astro-strip-whitespace
pnpm add -D unplugin-strip-whitespace
# or: npm i -D unplugin-strip-whitespace
# or: yarn add -D unplugin-strip-whitespace
In astro.config.mjs / astro.config.ts:
import { defineConfig } from "astro/config";
import stripWhitespace from "astro-strip-whitespace";
export default defineConfig({
integrations: [stripWhitespace()],
});
In vite.config.ts:
import { defineConfig } from "vite";
import stripWhitespace from "unplugin-strip-whitespace/vite";
export default defineConfig({
plugins: [stripWhitespace()],
});
This project ships per-bundler entrypoints. Examples:
unplugin-strip-whitespace/rollupunplugin-strip-whitespace/webpackunplugin-strip-whitespace/esbuildunplugin-strip-whitespace/nuxtunplugin-strip-whitespace/rspackunplugin-strip-whitespace/farmRefer to the bundler-specific unplugin docs for the exact wiring pattern.
This is a monorepo containing Rust crates and JavaScript packages:
strip-whitespace/
├── crates/ # [Rust] Rust crates excluding fuzz targets
│ ├── core/ # Core Rust library for parsing & rewriting templates
│ └── wasm/ # WASM wrapper for JavaScript integration
├── packages/ # [JS] JavaScript packages
│ ├── wasm/ # WASM bindings generated from crates/wasm (private package)
│ ├── unplugin-strip-whitespace/ # Unplugin for Vite/Rollup/Webpack/etc
│ └── astro-strip-whitespace/ # Astro integration wrapper
├── examples/ # [JS] Example projects
│ └── e2e-astro/ # End-to-end Astro test app
├── fixtures/ # Test fixtures
├── fuzz/ # [Rust] Fuzzing targets for Rust crates
├── scripts/ # Build and utility scripts
└── assets/ # Documentation assets (images, SVGs)
pnpm test - run JS tests (Vitest)pnpm run build - build WASM + packagespnpm run build:e2e-astro / pnpm run dev:e2e-astro - run the Astro example app