Javascript – Webpack how to use to merge css files

cssjavascriptwebpack

I use Webpack to merge javascript files. But i do not understand how merge css files like javascript

var webpack=require('webpack');

module.exports = {
        context: __dirname ,
        entry: {one:["./script/born.js","./script/create_game.js"], two:["./css/destop.css", "./css/main_page.css"]},
        output: {
            path: __dirname,
            filename: "/production/[name].js"
        },
        plugins: [
            new webpack.optimize.UglifyJsPlugin({
                compress: {
                    warnings: false
                }
            })

Is it possible to do such a thing?

Best Answer

To move every require("style.css") in entry chunks into a separate CSS file. And do not inline css into the JS bundle, but separate it in a CSS bundle file (styles.css). Use extract-text-webpack-plugin. There is an example from its documentation:

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

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallbackLoader: "style-loader",
          loader: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ]
}

If you need to merge json data or some another data that may be converted to json by loaders chain use merge-webpack-plugin. If you need to join togather some more specific things use join-webpack-plugin.