`Invalid mapping` error trying to use babel require hook and polyfill with react jsx transpile

2k views Asked by At

I'm trying to use babel for both ES6 and JSX transpilation for mocha tests.

Suppose we have test.jsx like this:

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

Running babel test.jsx gives us valid transformed code. No problem.

I would expect that if I create a test.js file like this:

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

And then run node test.js, it would transpile my jsx for me in line, but instead I get an error:

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)

According to the docs, it looks like this should work. Am I missing something obvious, or should I report this as a bug in babel?

1

There are 1 answers

7
loganfsmyth On BEST ANSWER

This was a bug in acorn-jsx, the JSX parser that Babel uses. The recommended fix would be:

rm -rf node_modules/babel && npm install