Search code examples
webpackvue-clivue-cli-3

Exclude some css files from a vue-cli bundle


I have a vue cli project and I'm trying to exclude css files from being bundled in chunk-vendors.css

The reason being, that I have a theme file included in my app that overrides all of the styles of a library and those library's styles are not needed anymore

The files in question are located in node_modules/element-ui/lib/theme-chalk/*.css

I can't seem to find the configuration that I need to change in my vue.config.js file

My simplified vue.config.js:

const prefixer = require('postcss-prefix-selector');
const autoprefixer = require('autoprefixer');
const path = require('path');

module.exports = {
    css: {
        extract: true,
        loaderOptions: {
            postcss: {
                // This is a workaround to add specificity to the app's theme
                // to correctly override all the rules I'm trying to remove
                plugins: () => [
                    prefixer({
                        prefix: 'body',
                        exclude: [/body .*/]
                    }),
                    autoprefixer({})
                ],
                // This doesn't seem to do anything
                exclude: path.resolve('node_modules/element-ui/lib/theme-chalk')
            },
        }
    },
};

But the files don't get excluded

What option should I use to exclude css files from my bundle?


Solution

  • I figured it out, when adding element-ui with vue-cli-plugin-element

    The plugin added some lines to babel.config.js

    module.exports = {
      'presets': [
        '@vue/cli-plugin-babel/preset'
      ],
      'plugins': [
        // The following was added by vue-cli-plugin-element
        [
          'component',
          {
            'libraryName': 'element-ui',
            'styleLibraryName': 'theme-chalk'
          }
        ]
      ]
    }
    

    By changing it to:

    module.exports = {
      'presets': [
        '@vue/cli-plugin-babel/preset'
      ],
      'plugins': [
        // The following was added by vue-cli-plugin-element
        [
          'component',
          {
            'libraryName': 'element-ui',
            'style': false
          }
        ]
      ]
    }
    

    I got rid of the useless css imports