unplugin-strip-whitespace
v0.2.1
Published
Unplugin to strip inter-node whitespace in Astro and Svelte templates (Vite/Rollup/Webpack/Rspack/etc).
Maintainers
Readme
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-whitespaceUsage
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
