Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract css as a separate .css file in Vite?

In webpack it is possible to generate output in such a way:

  • 1 file with bundled js code
  • 1 file with bundled css

This is part of the webpack config, that results in such output:

const MiniCssExtractPlugin = require('mini-css-extract-plugin')

...

plugins: [
  new MiniCssExtractPlugin({
    filename: 'assets/css/styles.css',
    chunkFilename: '[id].css',
  }),
  ...
],

...

{
  test: /\.css$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
    },
    {
      loader: 'css-loader',
    },
  ],
},

...

How to achieve this with vite?

The vite templates by default generate config, where js and css are bundled into a single file and css is injected at runtime.

like image 343
Daniel Avatar asked Nov 23 '25 12:11

Daniel


1 Answers

You can use the option build.cssCodeSplit to turn off the runtime CSS injection.

From the Vite docs:

If you'd rather have all the CSS extracted into a single file, you can disable CSS code splitting by setting build.cssCodeSplit to false.

like image 104
NotWoods Avatar answered Nov 26 '25 01:11

NotWoods



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!