Search code examples
csswebpackwebpack-3

Webpack especific css bundle


I have the following code in webpack 3.11

var path = require('path');
var webpack = require("webpack");
var HtmlWebpackPlugin = require('html-webpack-plugin');

const MinifyPlugin = require("babel-minify-webpack-plugin");
if (process.argv.indexOf('-p') !== -1) {
  process.env.NODE_ENV = 'production';
}

let config = {
  output: {
    path: __dirname + '/dist',
  }
}

let ExtractTextPlugin = require('extract-text-webpack-plugin');
let extractSCSS = new ExtractTextPlugin('bundle.css');

module.exports = {
  entry: {
    bundle2: "./src2/main2.js",
    bundle: "./src/main.js",
  },
  output: {
    path: config.output.path,
    filename: '[name].js',
  },

  module: {
    rules: [{
        test: /\.scss$/,
        use: extractSCSS.extract({
          use: [{
            loader: "css-loader"
          }, {
            loader: "sass-loader"
          }]
        })
      }
    ]
  },
  devtool: 'source-map',
  plugins: [
    extractSCSS
  ],
  devServer: rutasServer
};

What I would like, for example, is to be able to create a bundle.css from the scss files that exist in the src folder and at the same time create a bundle2.css for the scss files that exist in the src2 folder. is it possible?


Solution

  • You can create different entries and use css-minify-plugin or extract-plugin on them.

    on your webpack.config.js:

     entry: {
                bundle1: ['./src/your/path.scss'],
                bundle2: ['./src/your/other-path.scss'],
            },
    

    and at plugins:

    new ExtractTextPlugin({ filename: 'css/[name].css' }),
    

    Actually ExtractTextPlugin is deprecated. Try using mini-css-extract-plugin