carbon-preprocess-svelte
v0.14.4
Published
Zero-dependency Svelte preprocessors and build plugins for the Carbon Design System
Maintainers
Readme
carbon-preprocess-svelte
Zero-dependency Svelte preprocessors and build plugins for the Carbon Design System
It makes a carbon-components-svelte app ship less: it rewrites Carbon's barrel imports so Svelte compiles less, prunes the Carbon CSS your app can't render with the props it passes, and rewrites Carbon components for the props your app actually passes. It reads your installed Carbon, so it matches your version. Its one blind spot: a Carbon class your code builds at run time, never written as a literal bx-- token, gets pruned, so name it in safelist.
One app, built four ways (examples/vite-matrix@svelte-5):
| Variant | CSS gzip | JS gzip |
| --- | --- | --- |
| optimizeImports only | 93.96 kB | 152.73 kB |
| optimizeCss() | 22.14 kB (-76.4%) | 152.73 kB |
| plus optimizeComponents() | 22.14 kB (-76.4%) | 101.27 kB (-33.7%) |
| plus hydration: false | 22.14 kB (-76.4%) | 99.51 kB (-34.8%) |
Every build says what it pruned:
$ vite build
…
carbon-preprocess-svelte: pruned Carbon CSS
assets/index-CU4gbKFa.css 606.3 kB → 53.2 kB -91%It has no dependencies, and runs on Node 22 or later, or Bun.
Quick start
npm i -D carbon-preprocess-svelte// vite.config.js, for SvelteKit 3 (which takes its options in sveltekit())
import adapter from "@sveltejs/adapter-static";
import { sveltekit } from "@sveltejs/kit/vite";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
import {
optimizeComponents,
optimizeCss,
optimizeImports,
} from "carbon-preprocess-svelte";
export default {
plugins: [
optimizeComponents(), // before sveltekit(), which compiles what it rewrites
sveltekit({
preprocess: [vitePreprocess(), optimizeImports()],
adapter: adapter(),
}),
optimizeCss(),
],
};npx vite build # prints the pruned stylesheets, as abovePick a tool
Each works on its own.
| Tool | For | Does |
| --- | --- | --- |
| optimizeImports | any bundler, as a Svelte preprocessor | rewrites Carbon imports to source paths |
| optimizeCss | Vite, SvelteKit, Astro, Rollup, Rolldown | prunes unused Carbon styles |
| OptimizeCssPlugin | Webpack, Rspack | optimizeCss for Webpack and Rspack |
| optimizeComponents | Vite, SvelteKit, Astro, Rollup, Rolldown | rewrites Carbon components for the props your app passes |
| OptimizeComponentsPlugin | Webpack, Rspack | optimizeComponents for Webpack and Rspack |
| optimizeCarbonBun | Bun.build | optimizeComponents and optimizeCss for Bun |
| createCarbonOptimizer | any bundler without the plugins above | both, driven by your build script |
| optimizeCarbonCss | esbuild, any post-build script | the CSS pruning as a function |
| CLI | any pipeline without a plugin hook | prunes built CSS files with one command |
Without a plugin hook, run the CLI after the build:
npx carbon-preprocess-svelte optimize-css "dist/**/*.css"
npx carbon-preprocess-svelte optimize-css "dist/**/*.css" --dry-run --jsonIt exits 0 when done, and 2 when called wrong, given nothing to prune, or unable to read the app; --json prints the result, or the error, as one object on stdout.
Docs
optimizeImports: set-ups for SvelteKit, Vite, Rollup, Webpack and RspackoptimizeCss: set-ups, how it prunes by props, its options, andOptimizeCssPluginoptimizeComponents: what it rewrites, client-only apps, andOptimizeComponentsPlugin- Bun:
optimizeCarbonBun - From a build script:
createCarbonOptimizerandoptimizeCarbonCss - CLI: the command, its output, and every option
- Your Carbon version: how it reads your installed Carbon
- Examples: a runnable set-up for every supported bundler
