Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: bundle multiple vendor css in one separate file?

I want to bundle several standard libraries into two files bundle.js and bundle.css with webpack 3.8

This is my webpack.config.js:

const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const path = require("path");

module.exports = {
  entry: {
    'vendor': [
      'jquery',
      'popper.js',
      'bootstrap',
    ],

  },

    output: {
    path: path.resolve(__dirname, 'public/js'),
    filename: 'bundle.js'
  },

  module: {
              rules:[
                    {
                    test: /\.css$/,
                    use: ExtractTextPlugin.extract({
                      fallback: "style-loader",
                      use: "css-loader"
                    })
                  }
                ],


  },

  plugins: [

    new webpack.optimize.UglifyJsPlugin({
      uglifyOptions: {
        compress: true 
      }
    }),

    new ExtractTextPlugin("styles.css"),

  ]
};

After running webpack, I get only bundle.js file:

    Asset    Size  Chunks                    Chunk Names
bundle.js  264 kB       0  [emitted]  [big]  vendor

How should I config webpack to produce bundle.css ?

like image 376
sr9yar Avatar asked Nov 29 '17 06:11

sr9yar


People also ask

Can webpack split code into separate files?

Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel.

Can you have multiple entry points in a single webpack configuration file?

webpack.config.jsWe can also pass an array of file paths to the entry property which creates what is known as a "multi-main entry". This is useful when you would like to inject multiple dependent files together and graph their dependencies into one "chunk".

Does webpack remove unused CSS?

purgecss-webpack-plugin allows you to eliminate most of the CSS as ideally we would bundle only the CSS classes we are using.

Does webpack compile CSS?

Loaders are the node-based utilities built for webpack to help webpack to compile and/or transform a given type of resource that can be bundled as a javascript module. css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string.


1 Answers

There are two ways to do it.

First

You can import your .css files in the files provided at entry point.

for eg.

if entry: index.js

in index.js you have to import your css files

import './*.css'      // all css files in root

Second

you can provide it in your entry point.

entry: {
   {
    'vendor': [
      'jquery',
      'popper.js',
      'bootstrap',
    ],
    'styles': '/*.css'    
  }
}
like image 105
Pravesh Khatri Avatar answered Sep 24 '22 15:09

Pravesh Khatri