鍍金池/ 問答/Java  HTML/ 使用extract-text-webpack-plugin打包時出錯

使用extract-text-webpack-plugin打包時出錯

我使用的是webpack4,想試一下獨(dú)立打包CSS文件,代碼如下:

const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const extractWebpackPlugin = require('extract-text-webpack-plugin');

const isDev = process.env.NODE_ENV === 'development';

const config = {
  target: 'web',
  mode: 'development',
  entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.[hash].js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.css$/,
        use:[
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.js?$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(jpg|jpeg|gif|bmp|png)$/,
        use:[
          {
            loader: 'url-loader',
            options: {
              limit: 1024,
              name: '[name]-[hash].[ext]'
            }
          }
        ]
      },
      {
        test: /\.styl(us)?$/,
        use:[
          'style-loader',
          'css-loader',
          'stylus-loader'
        ]
      }
    ]
  },
  plugins:[
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin(),
    new webpack.DefinePlugin({
      'process.env':{
        NODE_ENV: isDev ? '"development"' : '"production"'
      }
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ]
}

if(isDev){
  config.module.rules.push({
    test: /\.styl(us)?$/,
      use:[
      'style-loader',
      'css-loader',
      'stylus-loader'
    ]
  });
  config.devtool = '#cheap-module-eval-source-map';   //方便調(diào)試代碼
  config.devServer = {
    port: 8888,
    host: '0.0.0.0',
    overlay:{
      errors: true   //編譯時出現(xiàn)錯誤顯示到網(wǎng)頁上
    },
    hot: true
  }
} else{
  config.output.filename = '[name].[hash].js';
  config.module.rules.push(
    {
      test: /\.css$/,
      use: extractWebpackPlugin.extract({
        fallback: 'style-loader',
        use: ['css-loader']
      })
    }
    //{
    //  test: /\.styl(us)?$/,
    //  use: extractWebpackPlugin.extract({
    //    fallback: 'style-loader',
    //    use: [
    //      'css-loader',
    //      'stylus-loader'
    //    ]
    //  })
    //}
  );
  config.plugins.push(
    new extractWebpackPlugin('styles.css')
  )

}

module.exports = config;

運(yùn)行 npm run build 時,出錯了這樣的錯誤:
圖片描述

實在是不知道什么原因,求大家賜教,謝謝。

回答
編輯回答
六扇門

extract-text-webpack-plugin 已經(jīng)不支持 webpack4 了,請使用 mini-css-extract-pluginhttps://www.npmjs.com/package...

2018年1月26日 13:57