2017-03-07 50 views
5

我想实现提取文本webpack插件使用webpack 2,我从头开始构建我的webpack.config.js。当我想添加插件时,我按照npm上的说明操作。然而,这使我有以下错误:无法读取未定义的提取文本webpack插件属性'查询'

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) 

回答

8

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

与安装最新版本:

npm install --save-dev [email protected] 

或者与Yarn你可以运行:

yarn upgrade extract-text-webpack-plugin 
+0

我假设按照他们的npm指南我会得到最新的版本?在“安装”下它说,对于webpack2; 'npm install --save-dev extract-text-webpack-plugin'''所以我认为那样会好的。谢谢! – Kevin

+0

它应该,但如果你已经安装它,npm将尊重语义版本控制方案。所以如果它是一个确切的版本(没有'^'),它会停留在那个版本上。不完全是你所期望的。添加依赖项时,Yarn的行为会有所不同,并且将始终使用最新版本,而不管当前版本如何。 –

+0

很酷,感谢您的额外信息 – Kevin

0

我有相同的问题。请注意,webpack 2.x只适用于解压缩文本插件版本2.1.2 对于webpack 3,使用版本3.0.0

相关问题