Closed andreavaccari closed 7 months ago
@andreavaccari Which Carbon components are you importing?
Try using the optimizeImports
preprocessor in conjunction with the Carbon Svelte library.
This is what my vite.config.js
looks like:
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { defineConfig } from "vite";
import { optimizeImports, optimizeCss } from "carbon-preprocess-svelte";
export default defineConfig(({ mode }) => {
return {
plugins: [
svelte({ preprocess: [optimizeImports()] }),
mode === 'production' && optimizeCss()
],
};
});
When I run yarn build
, the CSS bundle is smaller (514.98kb --> 103.54kb)
Hi @metonym, is the order in which one lists optimizeImports
, optimizeCss
, and sveltePreprocess
important?
I have a similar issue when using Sveltekit; I'm not sure if the problem is my config, Sveltekit's processing, or optimizeCss()
.
This is my svelte.config.js:
import adapter from '@sveltejs/adapter-auto'
import { optimizeImports, optimizeCss } from 'carbon-preprocess-svelte'
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
vite: {
plugins: [optimizeCss()]
}
},
preprocess: [optimizeImports()]
}
export default config
/src/routes/index.svelte
<script>
import { DataTable } from 'carbon-components-svelte'
import 'carbon-components-svelte/css/white.css'
export let data
export let raw
</script>
<h1>Pricing</h1>
<p>
Raw data: {JSON.stringify(raw)}
</p>
<p>
Refined data: {JSON.stringify(data)}
</p>
<DataTable headers={data.headers} rows={data.rows} />
After running npm run build
one of the files is 633kB:
.svelte-kit/output/server/entries/pages/index.svelte.js 633.33 KiB
How do I ensure optimizeCss()
is configured properly?
Docs reference: https://github.com/carbon-design-system/carbon-preprocess-svelte/blob/19785adebad8ae92387b03fd3e32a0dd2ac8248b/README.md#usage-2
Using:
Following up with some trial and error.
For context, I'm using Sveltekit. Here's a reproduction demo: https://stackblitz.com/edit/carbon-optimize-css-demo?file=src%2Froutes%2Findex.svelte,vite.config.js&terminal=dev
npm run build
.svelte-kit/output/client/_app/immutable/assets/index-43c78f14.css
582.15 KiB / gzip: 52.89 KiB
svelte-preprocess
in the vite config:I am also having this issues:
in my main.css:
@import "carbon-components-svelte/css/g10.css";
@tailwind base;
@tailwind components;
@tailwind utilities;
With my vite.config.js
:
import {
defineConfig,
splitVendorChunkPlugin
} from 'vite'
import {
svelte
} from '@sveltejs/vite-plugin-svelte'
import path from 'path'
import preprocess from 'svelte-preprocess';
import {
optimizeCss,
optimizeImports
} from "carbon-preprocess-svelte";
export default defineConfig(({
mode
}) => {
return {
plugins: [svelte({
preprocess: [preprocess(), optimizeImports()],
experimental: {
prebundleSvelteLibraries: true,
}
}),
mode === 'production' && optimizeCss(),
splitVendorChunkPlugin(),
],
},
}
})
And I get a css bundle over 300kb.
Node v16.15.1 npm 8.12.1 svelte@3.42.3 vite@3.0.0 carbon-preprocess-svelte@0.9.1 carbon-components-svelte@0.70.10
This is a very belated reply. A lot has changed since this library was first released (SvelteKit was <v1).
I rewrote the library in v0.11.0 – specifically focusing on the optimizeCss
plugin:
Hi @metonym , thank you for your work on Svelte and Carbon.
I would appreciate your help with the following setup:
vite.config.js
Css.svelte
Main.svelte
The Carbon theme is not used at all but ends up included in the final
style.css
. Tailwind is correctly purged. I believe the plugin is not being triggered but I cannot figure out what I'm doing wrong. Could you point me in the right direction?