“无效映射”错误尝试使用babel需要hook和polyfill with react jsx transile

时间:2015-06-22 15:15:21

标签: source-maps react-jsx babeljs

我尝试使用babel进行ESOC和JSX转换进行摩卡测试。

假设我们有test.jsx这样:

var React = require("react");
React.createClass({
  render: function(){
    return (<div>Hello World</div>);
  }
});

运行babel test.jsx会为我们提供有效的转化代码。没问题。

如果我像这样创建一个test.js文件,我希望如此:

require("babel/register");
require("./test.jsx");

然后运行node test.js,它会为我排队我的jsx,但我得到一个错误:

c:\Users\user\dev\app\node_modules\babel\node_modules\babel-core\lib\babel\t
ransformation\file\index.js:628
      throw err;
            ^
Error: c:/Users/user/dev/app/test.jsx: Invalid mapping: {"generated":{"line"
:6,"column":11},"source":"c:/Users/user/dev/app/test.jsx","name":null}
    at SourceMapGenerator_validateMapping [as _validateMapping] (c:/Users/user/dev/app/node_modules\babel\node_modules\source-map\lib\source-map\source-map-
generator.js:275:15)
    at SourceMapGenerator_addMapping [as addMapping] (c:/Users/user/dev/app/
node_modules\babel\node_modules\source-map\lib\source-map\source-map-generator.j
s:105:14)
    at SourceMap.mark (c:/Users/user/dev/app/node_modules\babel\node_modules
\babel-core\lib\babel\generation\source-map.js:65:9)
    at CodeGenerator.print (c:/Users/user/dev/app/node_modules\babel\node_mo
dules\babel-core\lib\babel\generation\index.js:236:16)
    at NodePrinter.plain (c:/Users/user/dev/app/node_modules\babel\node_modu
les\babel-core\lib\babel\generation\node\printer.js:16:27)
    at CodeGenerator.ReturnStatement (c:/Users/user/dev/app/node_modules\bab
el\node_modules\babel-core\lib\babel\generation\generators\statements.js:120:13)

    at CodeGenerator.print c:/Users/user/dev/app/node_modules\babel\node_mo
dules\babel-core\lib\babel\generation\index.js:238:22)
    at NodePrinter.plain (c:/Users/user/dev/app/node_modules\babel\node_modu
les\babel-core\lib\babel\generation\node\printer.js:16:27)
    at CodeGenerator.printJoin (c:/Users/user/dev/app/node_modules\babel\nod
e_modules\babel-core\lib\babel\generation\index.js:286:13)
    at NodePrinter.sequence (c:/Users/user/dev/app/node_modules\babel\node_m
odul

es\babel-core\lib\babel\generation\node\printer.js:23:27)

根据文档,它看起来应该有效。我错过了一些明显的东西,还是应该将其报告为babel中的错误?

1 个答案:

答案 0 :(得分:2)

这是a bug in acorn-jsx,Babel使用的JSX解析器。推荐的修复方法是:

{{1}}