我对文档的理解
我看到 Babel 6 现在有三个预设:es2015、react 和 stage-x。我读到我可以.babelrc
像这样设置它们:
{
"presets": ["es2015", "react", "stage-0"]
}
或直接在 package.JSON 中,如下所示:
{
...,
"version": x.x.x,
"babel": {
"presets": ["es2015", "react", "stage-0"]
},
...,
}
我可以进一步将 babel-loader 与 webpack 一起使用,如下所示:
loader: 'babel?presets[]=es2015'
我的问题
因此,为了编译一切又好又干净,我将babel-loader
刚刚更新为可与 Babel6 一起使用的所有内容添加到 webpack 配置中,如下所示:
module.exports = function(options) {
var jsLoaders = ['babel?presets[]=es2015'];
[...]
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: jsLoaders
},
{
test: /\.jsx$/,
exclude: /node_modules/,
loaders: options.production ? jsLoaders : ['react-hot'].concat(jsLoaders)
},
[...]
现在,当我在没有 .babelrc
设置 root 或预设选项的情况下进行编译时package.JSON
,即仅使用 webpack 配置中设置的 babel-loader es2015 预设,我在我的 React 组件类中收到关于静态 propTypes 的意外标记错误:
ERROR in ./app/components/form/index.jsx
Module build failed: SyntaxError: /Library/WebServer/Documents/yarsk.test/app/components/form/index.jsx: Unexpected token (19:19)
17 | // ES6 React Component:
18 | export default class SurveyForm extends Component {
> 19 | static propTypes = {
| ^
在 GitHub 上,我被告知这是一个stage-1
功能,即transform-class-properties
. 所以我想stage-0
马上实施。
但是
当我通过像上面那样添加.babelrc
或定义来package.JSON
这样做时,我得到了一个非常奇怪的构建失败错误:
ERROR in ./app/components/form/index.jsx
Module build failed: Error: /Library/WebServer/Documents/yarsk.test/app/components/form/index.jsx: We don't know what to do with this node type. We were previously a Statement but we can't fit in here?
at NodePath.insertAfter (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/modification.js:181:13)
at NodePath.replaceWithMultiple (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/replacement.js:92:8)
at handleClassWithSuper (/Library/WebServer/Documents/yarsk.test/node_modules/babel-plugin-transform-class-constructor-call/lib/index.js:80:10)
at PluginPass.Class (/Library/WebServer/Documents/yarsk.test/node_modules/babel-plugin-transform-class-constructor-call/lib/index.js:101:11)
at newFn (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/visitors.js:233:27)
at NodePath._call (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/context.js:72:18)
at NodePath.call (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/context.js:44:17)
at NodePath.visit (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/context.js:102:12)
at TraversalContext.visitQueue (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:151:16)
at TraversalContext.visitSingle (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:111:19)
at TraversalContext.visit (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:195:19)
at Function.traverse.node (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/index.js:139:17)
at NodePath.visit (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/path/context.js:106:22)
at TraversalContext.visitQueue (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:151:16)
at TraversalContext.visitMultiple (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:106:17)
at TraversalContext.visit (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/context.js:193:19)
at Function.traverse.node (/Library/WebServer/Documents/yarsk.test/node_modules/babel-traverse/lib/index.js:139:17)
@ ./app/index.jsx 9:0-28
或者简而言之: Module build failed: Error: /.../index.jsx: We don't know what to do with this node type. We were previously a Statement but we can't fit in here?
这就是我被困的地方。当我能够像这样使用 babel-loader 进行编译并且一切正常时,我使用 Babel5 编写了这个组件:
loader: 'babel?optional[]=runtime&stage=0
现在我正在编译提到的错误。
- 这是一个
babel-loader
问题,还是一个babel
问题? - 我必须在哪里配置
stage-0
才能不抛出错误?
更新
当使用预设集进行编译并使用提到的类导出错误的解决方法(在创建类之前不得导出类)时,设置预设的顺序会更改错误消息。当我stage-0
第一次设置时,现在的错误是'this' is not allowed before super() (This is an error on an internal node. Probably an internal error)
当我把stage-0
第二个或第三个放在上面时,我会收到有关语法错误的消息。
最新的
有关这些错误的最新进展,请参阅我的帖子或phabricator 上的新 babel 问题跟踪器了解更多信息。(基本上编译从 6.2.1 开始是固定的,但现在还有其他事情发生)
本文中提到的所有错误在 Babel 6.3.x 中都已完全修复。如果您仍然遇到问题,请更新您的依赖项。