carbon-design-system / carbon-preprocess-svelte

Svelte preprocessors for the Carbon Design System
Apache License 2.0
68 stars 6 forks source link

`optmizeCss` has no effect on build size #17

Closed andreavaccari closed 7 months ago

andreavaccari commented 3 years ago

Hi @metonym , thank you for your work on Svelte and Carbon.

I would appreciate your help with the following setup:

import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { optimizeCss } from "carbon-preprocess-svelte";

export default defineConfig({
  plugins: [
    svelte(),
    optimizeCss(),
  ],
  build: {
    lib: {
      name: "Button",
      entry: "src/Main.svelte",
      fileName: "index",
    },
  },
});
<style global lang="postcss">
  @import "carbon-components-svelte/css/white";
  @import "tailwindcss/base";
  @import "tailwindcss/components";
  @import "tailwindcss/utilities";
</style>
<script lang="ts">
  import "./Css.svelte";
  export let label: string;
</script>

<button type="button" class="btn">
  {label}
</button>

<style lang="postcss">
  .btn {
    @apply font-bold;
  }
</style>

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?

metonym commented 3 years 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)

andreavaccari commented 2 years ago

Hi @metonym, is the order in which one lists optimizeImports, optimizeCss, and sveltePreprocess important?

theetrain commented 2 years ago

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:

theetrain commented 2 years ago

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

  1. Load the link above
  2. In the terminal, run npm run build
  3. Observe the large CSS output:
    .svelte-kit/output/client/_app/immutable/assets/index-43c78f14.css
    582.15 KiB / gzip: 52.89 KiB
I also tried using svelte-preprocess in the vite config: ```js import { sveltekit } from '@sveltejs/kit/vite' import preprocess from 'svelte-preprocess' import { optimizeImports, elements, optimizeCss } from 'carbon-preprocess-svelte' /** @type {import('vite').UserConfig} */ const config = { plugins: [sveltekit({ preprocess: preprocess([optimizeImports(), elements()]) }), optimizeCss()], server: { port: 3000 }, preview: { port: 3000 } } export default config ```
ericschmar commented 2 years ago

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

metonym commented 7 months ago

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: