2016-12-02 64 views
2

所以我最近升級到了一個React應用程序的webpack 2。它直接從盒子裏運行,沒有我改變任何東西,但版本號!大。Webpack 2遷移中斷ES6模塊導入

但是,現在我試圖讓它爲我捆綁ES6模塊(導入,導出等),並且babel和webpack似乎沒有很好地協作。

我所做的唯一變化,試圖完成,這是我的.babelrc從:

{ 
    "presets": ["es2015", "stage-1", "react"], 
    "plugins": ["transform-object-rest-spread"], 
} 

這樣:

{ 
    "presets": [["es2015", {"modules": false}], "stage-1", "react"], 
    "plugins": ["transform-object-rest-spread"], 
} 

的WebPack仍然捆綁這件事而不發出一個錯誤,但是當我打開應用程序,我得到了可怕的"Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components)."

Image of error Image of second error

堆棧跟蹤把我帶回到我的應用程序的第一行(ReactDOM.render(...

從我所看到的這個錯誤,當您嘗試使用import { Component } from './component.js'而非import Component from './component.js'導入其他組件的默認出口通常出現。雖然我不明白爲什麼會出現這種情況,因爲我知道我的代碼與babel一起編譯它。

任何提示?

回答

1

看起來像["es2015", {"modules": false}]是沒有必要的,並導致錯誤。

通過將babelrc保持原樣並將以下插件添加到我的webpack配置中,獲得所需的行爲:new webpack.optimize.CommonsChunkPlugin({...})new HtmlWebpackPlugin({...})

還根據this great tutorial進行了一些其他配置更改,這些更改還可以幫助您分別捆綁供應商模塊,並向您演示如何根據您的react-router路由將代碼拆分爲塊。