Gathering detailed insights and metrics for carbon-preprocess-svelte
Gathering detailed insights and metrics for carbon-preprocess-svelte
Gathering detailed insights and metrics for carbon-preprocess-svelte
Gathering detailed insights and metrics for carbon-preprocess-svelte
carbon-components-svelte
Svelte implementation of the Carbon Design System
svelte-preprocess
A Svelte preprocessor wrapper with baked-in support for commonly used preprocessors
carbon-icons-svelte
Carbon Design System SVG icons as Svelte components
rollup-plugin-svelte
Compile Svelte components with Rollup
Svelte preprocessors for the Carbon Design System
npm install carbon-preprocess-svelte
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
69 Stars
146 Commits
5 Forks
9 Watching
1 Branches
33 Contributors
Updated on 25 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.7%
855
Compared to previous day
Last week
-9.1%
4,847
Compared to previous week
Last month
15.1%
21,329
Compared to previous month
Last year
24.6%
246,786
Compared to previous year
Svelte preprocessors for the Carbon Design System.
Install carbon-preprocess-svelte
as a development dependency.
1# npm 2npm i -D carbon-preprocess-svelte 3 4# pnpm 5pnpm i -D carbon-preprocess-svelte 6 7# Yarn 8yarn add -D carbon-preprocess-svelte 9 10# Bun 11bun add -D carbon-preprocess-svelte
script
block, making development compile times dramatically faster.optimizeCss
plugin for Webpack that removes unused Carbon styles.optimizeImports
optimizeImports
is a Svelte preprocessor that rewrites barrel imports from Carbon components/icons/pictograms packages to their source Svelte code paths. This can significantly speed up development and build compile times while preserving typeahead and autocompletion offered by integrated development environments (IDE) like VS Code.
The preprocessor optimizes imports from the following packages:
1- import { Button } from "carbon-components-svelte"; 2+ import Button from "carbon-components-svelte/src/Button/Button.svelte"; 3 4- import { Add } from "carbon-icons-svelte"; 5+ import Add from "carbon-icons-svelte/lib/Add.svelte"; 6 7- import { Airplane } from "carbon-pictograms-svelte"; 8+ import Airplane from "carbon-pictograms-svelte/lib/Airplane.svelte";
[!NOTE] When this preprocessor was first created, there was no workaround to optimize slow cold start times with Vite in development. Today, @sveltejs/vite-plugin-svelte enables
prebundleSvelteLibraries: true
by default. However, this preprocessor is still useful for non-Vite bundlers, like Rollup and Webpack. Also, it can further improve cold start development times even withprebundleSvelteLibraries: true
.
See examples/sveltekit.
1// svelte.config.js 2import adapter from "@sveltejs/adapter-static"; 3import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"; 4import { optimizeImports } from "carbon-preprocess-svelte"; 5 6/** @type {import('@sveltejs/kit').Config} */ 7const config = { 8 preprocess: [ 9 // Preprocessors are run in sequence. 10 // If using TypeScript, the code must be transpiled first. 11 vitePreprocess(), 12 optimizeImports(), 13 ], 14 kit: { 15 adapter: adapter(), 16 }, 17}; 18 19export default config;
See examples/vite.
1// vite.config.js 2import { svelte } from "@sveltejs/vite-plugin-svelte"; 3import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"; 4import { optimizeImports } from "carbon-preprocess-svelte"; 5 6/** @type {import('vite').UserConfig} */ 7export default { 8 plugins: [ 9 svelte({ 10 preprocess: [ 11 // Preprocessors are run in sequence. 12 // If using TypeScript, the code must be transpiled first. 13 vitePreprocess(), 14 optimizeImports(), 15 ], 16 }), 17 ], 18};
This code is abridged; see examples/rollup for a full set-up.
1// rollup.config.js 2import svelte from "rollup-plugin-svelte"; 3import { optimizeImports } from "carbon-preprocess-svelte"; 4 5export default { 6 plugins: [ 7 svelte({ 8 preprocess: [optimizeImports()], 9 }), 10 ], 11};
This code is abridged; see examples/webpack for a full set-up.
1// webpack.config.js 2const { optimizeImports } = require("carbon-preprocess-svelte"); 3 4module.exports = { 5 module: { 6 rules: [ 7 { 8 test: /\.svelte$/, 9 use: { 10 loader: "svelte-loader", 11 options: { 12 hotReload: !PROD, 13 preprocess: [optimizeImports()], 14 compilerOptions: { dev: !PROD }, 15 }, 16 }, 17 }, 18 ], 19 }, 20};
optimizeCss
optimizeCss
is a Vite plugin that removes unused Carbon styles at build time. The plugin is compatible with Rollup (Vite extends the Rollup plugin API).
1$ vite build 2 3Optimized index-CU4gbKFa.css 4- Before: 606.26 kB 5+ After: 53.22 kB (-91.22%) 6 7dist/index.html 0.34 kB │ gzip: 0.24 kB 8dist/assets/index-CU4gbKFa.css 53.22 kB │ gzip: 6.91 kB 9dist/assets/index-Ceijs3eO.js 53.65 kB │ gzip: 15.88 kB
[!NOTE] This is a plugin and not a Svelte preprocessor. It should be added to the list of
vite.plugins
. For Vite set-ups, this plugin is not run during development and is only executed when building the app (i.e.,vite build
). For Rollup and Webpack, you should conditionally apply the plugin to only execute when building for production.
See examples/sveltekit.
1// vite.config.js 2import { sveltekit } from "@sveltejs/kit/vite"; 3import { optimizeCss } from "carbon-preprocess-svelte"; 4import { defineConfig } from "vite"; 5 6export default defineConfig({ 7 plugins: [sveltekit(), optimizeCss()], 8});
See examples/vite.
1// vite.config.js 2import { svelte } from "@sveltejs/vite-plugin-svelte"; 3import { optimizeCss } from "carbon-preprocess-svelte"; 4 5/** @type {import('vite').UserConfig} */ 6export default { 7 plugins: [svelte(), optimizeCss()], 8};
This code is abridged; see examples/rollup for a full set-up.
1// rollup.config.js 2import svelte from "rollup-plugin-svelte"; 3import { optimizeCss } from "carbon-preprocess-svelte"; 4 5const production = !process.env.ROLLUP_WATCH; 6 7export default { 8 plugins: [ 9 svelte({ 10 preprocess: [optimizeImports()], 11 }), 12 13 // Only apply the plugin when building for production. 14 production && optimizeCss(), 15 ], 16};
optimizeCss
API1optimizeCss({
2 /**
3 * By default, the plugin will print the size
4 * difference between the original and optimized CSS.
5 *
6 * Set to `false` to disable verbose logging.
7 * @default true
8 */
9 verbose: false,
10
11 /**
12 * By default, pre-compiled Carbon StyleSheets ship `@font-face` rules
13 * for all available IBM Plex fonts, many of which are not actually
14 * used in Carbon Svelte components.
15 *
16 * The default behavior is to preserve the following IBM Plex fonts:
17 * - IBM Plex Sans (300/400/600-weight and normal-font-style rules)
18 * - IBM Plex Mono (400-weight and normal-font-style rules)
19 *
20 * Set to `true` to disable this behavior and
21 * retain *all* IBM Plex `@font-face` rules.
22 * @default false
23 */
24 preserveAllIBMFonts: false,
25});
OptimizeCssPlugin
For Webpack users, OptimizeCssPlugin
is a drop-in replacement for optimizeCss
. The plugin API is identical to that of optimizeCss
.
This code is abridged; see examples/webpack for a full set-up.
1// webpack.config.js 2const { OptimizeCssPlugin } = require("carbon-preprocess-svelte"); 3 4const PROD = process.env.NODE_ENV === "production"; 5 6module.exports = { 7 plugins: [ 8 // Only apply the plugin when building for production. 9 PROD && new OptimizeCssPlugin(), 10 ], 11};
Refer to examples for common set-ups.
Refer to the contributing guidelines.
No vulnerabilities found.
No security vulnerabilities found.