unplugin-strip-whitespace
unplugin-strip-whitespace
Strip inter-node whitespace in .astro and .svelte templates before they’re compiled.
This package is an unplugin plugin, so it works with Vite, Rollup, Webpack, Rspack, esbuild, Farm, Nuxt, and more.
If you’re using Astro, you may prefer the dedicated integration: astro-strip-whitespace.
Install
pnpm add -D unplugin-strip-whitespace
# npm i -D unplugin-strip-whitespace
# yarn add -D unplugin-strip-whitespace
Usage
Vite
// vite.config.ts
import { defineConfig } from "vite";
import stripWhitespace from "unplugin-strip-whitespace/vite";
export default defineConfig({
plugins: [stripWhitespace()],
});
Rollup
// rollup.config.ts
import stripWhitespace from "unplugin-strip-whitespace/rollup";
export default {
plugins: [stripWhitespace()],
};
Webpack
// webpack.config.js
const stripWhitespace = require("unplugin-strip-whitespace/webpack");
module.exports = {
plugins: [stripWhitespace.default()],
};
Rspack
// rspack.config.ts
import stripWhitespace from "unplugin-strip-whitespace/rspack";
export default {
plugins: [stripWhitespace()],
};
esbuild
import stripWhitespace from "unplugin-strip-whitespace/esbuild";
// ...
plugins: [stripWhitespace()],
Nuxt
This package also ships a small Nuxt module.
// nuxt.config.ts
export default defineNuxtConfig({
modules: ["unplugin-strip-whitespace/nuxt"],
unpluginStripWhitespace: {
preserveBlankLines: true,
},
});
Options
All options are optional.
import type { StripWhitespaceOptions } from "unplugin-strip-whitespace";
selectLanguage:("astro" | "svelte")[]or(id, content) => "astro" | "svelte" | false- Default behavior processes
.astroand.sveltefiles, skippingnode_modulesand any id containing a query string (?).
- Default behavior processes
preserveBlankLines:booleanor(lang, id, content) => boolean- When
true, skips stripping for gaps that contain an empty line (useful as a “section break” marker).
- When
movePluginBefore:RegExp|(name) => boolean|false|{ vite?: …; rollup?: …; ... }- Attempts to move this plugin earlier in the final plugin list (where supported).
- Default:
/^astro:build|^vite-plugin-svelte$/.
skipOnError:boolean- When
true, errors are logged as warnings and the transform is skipped.
- When
Example:
stripWhitespace({
preserveBlankLines: true,
movePluginBefore: {
vite: /^astro:build|^vite-plugin-svelte$/,
},
});
What it strips (and why)
This removes whitespace-only gaps between nodes in templates. It’s designed to be safe for hydration by running before Astro/Svelte compilation, rather than minifying the final HTML output.
License
MIT
Related Packages
Integration of Kubb for Vite, webpack, Rollup, Rolldown, Rspack, esbuild, Farm,...
Run Poolster OpenAPI generation from Vite, Rollup, webpack, esbuild, Rspack, Rol...
Lightning CSS integration for Vite, Rollup, esbuild, Webpack, Vue CLI, and more.
Unplugin for Styleframe. Use it with Vite, Webpack, Astro, Nuxt, Rollup, Rspack,...
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 0.2.2 | ^3.0 | MIT | |
| 0.2.1 | ^3.0 | MIT | |
| 0.2.0 | ^3.0 | MIT |