andreypopp / reactify

[DEPRECATED] Browserify transform for JSX (superset of JavaScript used in React library by Facebook)
MIT License
690 stars 68 forks source link

Reactify is not able to handle import/export declarations, even with Harmony tag (Fix inside) #60

Closed TimeBomb closed 9 years ago

TimeBomb commented 9 years ago

Heya. I've been tinkering around with reactify and ES6 for a little while now, and have been blocked by the errors: Illegal export declaration while parsing file and Illegal import declaration while parsing file.

After some digging, I found that React's JSX Transformer, used by react-tools, does not appear to parse import or export declarations unless you explicitly set a certain flag. Not the harmony flag. For some reason, this flag is currently undocumented. The important flag here is es6module, and it can be found being used in React's JSXTransformer. https://github.com/facebook/react/blob/master/docs/js/JSXTransformer.js

Specifically, take a look at line #8746: https://github.com/facebook/react/blob/master/docs/js/JSXTransformer.js#L8746

var isModule = extra.sourceType === 'module' || extra.sourceType === 'nonStrictModule';

        if (isModule && lookahead.type === Token.Keyword) {
            switch (lookahead.value) {
            case 'export':
                return parseExportDeclaration();
            case 'import':
                return parseImportDeclaration();
            }
        }

While I'm still looking into the code, and may not be fully aware of all the implications of this flag, it does appear that, without this flag (or nonStrictEs6Module), it will not parse import or export declarations, leading to the troublesome error I've been getting.

Right now, I've set the es6module flag to be turned on manually in reactify, though an official configuration for this would be appreciated. Cheers!

TimeBomb commented 9 years ago

I just noticed this issue is essentially a duplicate of https://github.com/andreypopp/reactify/issues/58. I'll close this so we don't have duplicate issues. Thanks again.