无法阅读属性'查询' undefined extract-text-webpack-plugin

时间:2017-03-07 23:40:03

标签: webpack webpack-2 extract-text-plugin

我尝试使用webpack 2实现extract-text-webpack-plugin,并且我从头开始构建我的webpack.config.js。当我想添加插件时,我按照npm上的说明操作。但是这给了我以下错误:

TypeError: Cannot read property 'query' of undefined

我环顾四周,并没有抓住其他人对此插件遇到同样问题。在假设这是一个错误之前,我首先要问我是否犯了错误。

我的webpack.config.js是

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
  context: path.resolve(__dirname, './src'),
  entry: {
    app: './main.js',
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: '[name].bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [/node_modules/],
        use: [{
          loader: 'babel-loader',
          options: { presets: ['es2015'] }
        }]
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
        ]
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ],
};

,完整错误是

/node_modules/extract-text-webpack-plugin/index.js:134
    if(!loader.query) return loader.loader;
              ^

TypeError: Cannot read property 'query' of undefined
    at getLoaderWithQuery (/node_modules/extract-text-webpack-plugin/index.js:134:12)
    at Array.map (native)
    at Function.ExtractTextPlugin.extract (/node_modules/extract-text-webpack-plugin/index.js:201:4)
    at Object.<anonymous> (/webpack.config.js:33:32)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)

2 个答案:

答案 0 :(得分:10)

您使用的是extract-text-webpack-plugin的过时版本,已在v2.0.0的第一个候选版本之前删除。你可能有测试版。

安装最新版本:

npm install --save-dev extract-text-webpack-plugin@latest

或者使用Yarn即可运行:

yarn upgrade extract-text-webpack-plugin

答案 1 :(得分:1)

我有完全相同的问题。请注意,webpack 2.x仅适用于extract-text-plugin版本2.1.2 对于webpack 3,请使用3.0.0版