K
K
konstantin_a2019-09-26 11:08:27
webpack
konstantin_a, 2019-09-26 11:08:27

How to configure webpack 4 to work with css in development mode?

Unable to configure webpack 4 to work with css in dev mode. When run in dev mode, the styles simply don't pull up. I would also like to configure the ability to livereload for css.
My package.json

{
  "name": "layout",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --mode production",
    "dev": "webpack-dev-server --mode development --open"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/preset-env": "^7.6.2",
    "babel-loader": "^8.0.6",
    "babel-preset-stage-3": "^6.24.1",
    "css-loader": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "path": "^0.12.7",
    "style-loader": "^1.0.0",
    "webpack": "^4.40.2",
    "webpack-cli": "^3.3.9",
    "webpack-dev-server": "^3.8.1"
  },
  "dependencies": {}
}

and webpack.config.js
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

let conf = {
  entry: './src/index.js',
  output:{
    path: path.resolve(__dirname, './dist'),
    publicPath: './dist',
    filename: 'main.js'
  },
  devServer: {
    inline: true,
    port: 8080
  },
  module: {
    rules:[ 
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/env']
          }
        },
        exclude: '/node_modules/'
      },
      
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css",
      ignoreOrder: false,
    }),
  ]
};

module.exports = (env, options) => {
  console.log('process.env.NODE_ENV = ' + options.mode);
  conf.devtool = options.mode === 'production'? false : 'eval-sourcemap';
  conf.module.rules.push({
    test: /\.css$/,
    use: [
        {
          loader: MiniCssExtractPlugin.loader,
          options: {
            publicPath: '../',
            hot: true,
            // hmr: options.mode === 'development'/* ,
            reloadAll: true
          }
        }, 
        "css-loader"
    ]
    
  })
  return conf;
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question