react icon indicating copy to clipboard operation
react copied to clipboard

JSX: Illegal Export declaration

Open volkanunsal opened this issue 11 years ago • 9 comments

The export syntax used to work fine with JSX harmony flag turned on before. Now it seems to throw this error:

Error: Parse Error: Line 1: Illegal export declaration

Here is a sample code:

export default var HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

React.render(<HelloMessage name="John" />, mountNode);

volkanunsal avatar Mar 11 '15 19:03 volkanunsal

cc @jeffmo – know what's up?

sophiebits avatar Mar 11 '15 19:03 sophiebits

babel gives some helpful information when transforming the same code. It looks like you can't actually have the var there and if it worked before it was a bug.

SyntaxError: code.js: Unexpected token (1:15)
> 1 | export default var HelloMessage = React.createClass({
    |                ^

And actually testing this with [email protected] also gives a parse error (and mentions var being illegal there). Error: Parse Error: Line 1: Unexpected token var

zpao avatar Mar 11 '15 19:03 zpao

I didn't have the var there in my original code. But it doesn't work without the var either.

var HelloMessage;
export default HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

React.render(<HelloMessage name="John" />, mountNode);

Try it here.

volkanunsal avatar Mar 11 '15 19:03 volkanunsal

Ah, this is because previously esprima would parse all import/exports. But the spec has since been updated such that incorrect use of import/export is a parse error, so there was an option added to esprima, which we exposed on jsx as either --es6module or --non-strict-es6module. Either of these should actually work here.

See #3143 for some discussion. @jeffmo and I also talked outside of github and decided to make the default be that no options would be passed, meaning es6 modules wouldn't get parsed (why did we say that @jeffmo?). Now this should work with the --non-strict-es6module option but I screwed that up, but that's tangential.

zpao avatar Mar 11 '15 21:03 zpao

For this particular issue, the answer is probably just to give the --es6module flag; Although beware that the old version of esprima was not spec compliant in that it did not enforce that modules are "use strict" by default. The new parser does (and this is one of two things the --es6module flag toggles).

@zpao: I think the two candidate options that make sense to be the default are either non-module or regular, spec-compliant module (they are the only two spec-compliant modes of the parser).

There is a 3rd mode of the parser called "non-strict-module" which is meant as a temporary mode for people who are transitioning existing commonjs modules to ES6 modules, but haven't had the chance to verify that their old CJS modules are strict-compatible yet. It would be a bad thing to turn this on by default because it would be too easy to build not-implicit-strict es6 modules with the expectation that they would work in a spec-compliant ES6 browser and forget that this is not the case (and things would break in a spec-compliant VM in very subtle ways).

I don't remember how we decided on which option to default to -- but I suppose it makes sense to choose whichever one aligns with "most people". I'd speculate that most people aren't using ES6 modules yet, but OTOH I wouldn't be opposed to encouraging a change in that momentum. Also the plan is for React and Flow to switch to ES6 modules in the near future anyway...so maybe it's time to rip off that band-aid?

(No matter what though, it should always be the case that people can choose script vs module via a flag toggle)

jeffmo avatar Mar 17 '15 15:03 jeffmo

Can you elaborate on the --es6module flag? Or more specifically, how would one fix the parse error when using export on the client via type="text/jsx;harmony=true"?

ghost avatar Mar 30 '15 11:03 ghost

Never mind my previous comment. Using Babel instead.

ghost avatar Mar 31 '15 02:03 ghost

Yea, right now JSXTransformer has a limited set of options. We could perhaps parse everything out of there but not sure how much it's worth it (also, it's not a production code thing so supporting es6 modules there isn't a huge priority).

But going to close this out as we shipped 0.13.1 with a fix for the command line parsing.

zpao avatar Mar 31 '15 17:03 zpao

Use module.exports = component name; instead of export default component name

praWINK avatar Apr 20 '17 13:04 praWINK