2017-03-07 31 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

相關問題