Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off seperate chunks for css , vue cli 3 webpack 4

I am using a project created with the latest version of vue cli 3 . I am using the default config , My router has many dynamically imported routes . Both my css and js are split into multiple chunk while running in production . While the behaviour with js is desirable . My css files are quite small I would like to turn off the chunks for css.

How do I configure webpack to do this via the vue.config.js file . Please help with the exact command as I find the webpack config and chain syntax very confusing .Thanks : )

like image 989
Sainath S.R Avatar asked Feb 18 '19 13:02

Sainath S.R


2 Answers

  1. Create a file in your root of project vue.config.js

I am using few other options too but your needed one is this.

const path = require('path');

module.exports = {
  lintOnSave: true,
  filenameHashing: false,
  chainWebpack: config => {
    config.optimization.delete('splitChunks')
  }
};

This will delete the chunks created and will let you use only single file of CSS as well as JS.

  1. filenameHashing: false this part will remove the hashing on files.
  2. config.optimization.delete('splitChunks') this will remove chunks.

More specifically with your requirement.

Use these configurations

module.exports = {
  lintOnSave: true,
  filenameHashing: false,
  configureWebpack: {
    optimization: {
      cacheGroups: {
        default: false,
        // Merge all the CSS into one file
        styles: {
          name: 'styles',
          test: /\.s?css$/,
          chunks: 'all',
          minChunks: 1,
          reuseExistingChunk: true,
          enforce: true,
        },
      },
    }
  }
};

Through this way, your JS code will be split into chunks but CSS File will be merged all in one.

More if you want to configure your JS chunks as well you can use these settings.

module.exports = {
  lintOnSave: true,

  filenameHashing: false,
  configureWebpack:{
    optimization: {
      cacheGroups: {
        default: false,
        // Custom common chunk
        bundle: {
          name: 'commons',
          chunks: 'all',
          minChunks: 3,
          reuseExistingChunk: false,
        },
        // Customer vendor
        vendors: {
          chunks: 'initial',
          name: 'vendors',
          test: 'vendors',
        },
        // Merge all the CSS into one file
        styles: {
          name: 'styles',
          test: /\.s?css$/,
          chunks: 'all',
          minChunks: 1,
          reuseExistingChunk: true,
          enforce: true,
        },
      },
    }
  }
};
like image 187
Junaid Farooq Avatar answered Nov 02 '22 21:11

Junaid Farooq


For Webpack 4, you can see this example (vue.config.js):

const WebpackBundleAnalyzer= require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
  lintOnSave: false,
  runtimeCompiler: true,
  filenameHashing:false,
  productionSourceMap: false,
  configureWebpack: {
    resolve: {
       symlinks: false
    },
    optimization: {
      splitChunks: {
       cacheGroups: {
          app: {
            chunks: 'all',
            name: 'main',
            test: /[\\/]src[\\/](.*)[\\/]/,
          },
          flagIcons: {
            chunks: 'all',
            name: 'flagIcons',                
           //test: /[\\/]node_modules[\\/]@coreui[\\/]((icons).*)[\\/]/,
            test: /[\\/]node_modules[\\/]@coreui[\\/]icons[\\/]js[\\/]((flag).*)[\\/]/,
          },
          freeIcons: {
            chunks: 'all',
            name: 'freeIcons',                
           //test: /[\\/]node_modules[\\/]@coreui[\\/]((icons).*)[\\/]/,
            test: /[\\/]node_modules[\\/]@coreui[\\/]icons[\\/]js[\\/]((free).*)[\\/]/,
          },
          brandIcons: {
            chunks: 'all',
            name: 'brandIcons',                
            test: /[\\/]node_modules[\\/]@coreui[\\/]icons[\\/]js[\\/]((brand).*)[\\/]/,
          },
          vendors: {
            chunks: 'all',
            name: 'vendors',
            test: /[\\/]node_modules[\\/]@coreui[\\/]((?!icons).*)[\\/]/,
          },
          // Merge all the CSS into one file
          styles: {
            name: 'styles',
            test: /\.s?css$/,
            chunks: 'all',
            minChunks: 1,
            reuseExistingChunk: true,
            enforce: true,
          }
        }
      }
    },

    plugins:[new WebpackBundleAnalyzer()]
  }
}

you may remove "WebpackBundleAnalyzer" after you are satisfied with the results.

like image 21
Ghodrat Ashournia Avatar answered Nov 02 '22 21:11

Ghodrat Ashournia