N
N
N!crO2018-04-16 19:10:28
JavaScript
N!crO, 2018-04-16 19:10:28

How to implement separate css compilation in Webpack 4?

webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  devtool: 'source-map',
  entry: './src/js/index.js',

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/main.js'
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: { presets: ['env'] }
        }
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.(png|jpg|jpeg|gif|svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'img/'
            }  
          }
        ]
      }
    ]
  },

  plugins: [new HtmlWebpackPlugin({
    template: 'src/index.html'
  })],

  devServer: {
    contentBase: './src',
    compress: true,
    open: true,
    watchContentBase: true
  }
};

package.json
{
  "name": "project,
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --hot --mode development"
  },
  "author": "N!crO",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.10",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "node-sass": "^4.7.2",
    "sass-loader": "^6.0.7",
    "style-loader": "^0.20.3",
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.11",
    "webpack-dev-server": "^3.1.1"
  },
  "dependencies": {
    "npm": "^5.7.1"
  }
}


When you run the "npm run build" command, you need to compile it into a separate file, and when you "npm start" you need to start dev-server and write css to head-> style.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shamonov, 2018-04-26
@DMShamonov

If I understood the task correctly - to put *.css into a separate bundle, then you need this npm package https://www.npmjs.com/package/extract-text-webpack...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question