How to set up Babel 6 stage 0 with React and Webpack
The two pretty heavy bugs I ran into here, namely the direct export of an ES6 class with a static property and a problem with the ES6 constructor are discussed in the answers of this thread and can be found on GitHub explicitly here (export bug) and here (constructor bug). (GitHub Issue Tracker has been closed and issues, bugs and requests have moved here.)
These are both officially confirmed bugs, fixed since Babel 6.3.17
(Maybe one or two earlier, not before 6.3.x, this is the version I'm on and everything is working as it was with Babel5. Happy coding everyone.)
(For the records:)
So if you get the following error message in the CLI:
We don't know what to do with this node type. We were previously a Statement but we can't fit in here?
Chances are you are exporting an ES6 class with a static property like this or in a similar manner (note that this doesn't seem to be connected to the class being extended anymore but rather to a class having a static property):
import React, { Component, PropTypes } from 'react'
export default class ClassName extends Component {
static propTypes = {...}
// This will not get compiled correctly for now, as of Babel 6.1.4
}
The simple workaround as mentioned by Stryzhevskyi and several people on GitHub:
import React, { Component, PropTypes } from 'react'
class ClassName extends Component {
static propTypes = {...}
}
export default ClassName // Just export the class after creating it
The second issue is about the following error:
'this' is not allowed before super() (This is an error on an internal node. Probably an internal error)
Despite being a legit rule as pointed out by Dominic Tobias this is a confirmed bug where it appears that extended classes having their own properties will throw this or a similar message. As for now I have not seen any workarounds for this one. Lots of people rolled back to Babel5 for this reason for now (as of 6.1.4).
Supposedly this was fixed with the release of Babel 6.1.18 (see above GitHub issue) but people, me included, still see the same exact problem happening.
Also take note that for now the order in which you load the babel presets stage-x
, react
and es2015
seems to be important and may change your output.
As of Babel 6.2.1
both of these bugs are fixed, code compiles fine. But... there is still another one that probably affects a lot of people working with react, that throws ReferenceError: this hasn't been initialised - super() hasn't been called
at runtime. Referenced here. Stay tuned...
Completely fixed since Babel 6.3.17
(Maybe one or two earlier, not before 6.3.x, this is the version I'm on and everything is working as it was with Babel5. Happy coding everyone.)
Try to replace your export with such construction:
class SurveyForm extends Component {/*implementation*/}
export default SurveyForm
Here is a working example with Babel 6, React, Webpack and Sequelize:
https://github.com/BerndWessels/react-webpack
Basically this is the .babelrc
{
"presets": [
"es2015",
"react",
"stage-0"
],
"env": {
"development": {
"plugins": [
"babel-relay-plugin-loader",
[
"react-transform",
{
"transforms": [
{
"transform": "react-transform-hmr",
"imports": [
"react"
],
"locals": [
"module"
]
},
{
"transform": "react-transform-catch-errors",
"imports": [
"react",
"redbox-react"
]
}
]
}
]
]
},
"production": {
"plugins": [
"babel-relay-plugin-loader"
]
}
}
}
and these are the module versions
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
That works for me.
After having the same issues, I was able to get this working with React using the below WebPack config.
module.exports = {
entry: './app/Index.js',
output: { path: __dirname, filename: 'dist/bundle.js' },
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
query: {
presets: ['react']
}
}
]
}
};
I also needed to install babel-preset-react too.
npm install --save-dev babel-preset-react
EDIT: Of course you might still need to also include the ES2015 preset if you are writing ES6 as well.
Babel Presets can be found here: https://github.com/babel/babel/tree/development/packages