elsassph / haxe-modular

Haxe-JS code splitting, NPM dependencies bundling, hot-reload
116 stars 14 forks source link

Support Haxe 4 js output with js-es=6 defined #92

Closed malec-palec closed 4 years ago

malec-palec commented 5 years ago

Currently, Haxe Modular is unable to process ES6 classes generated by Haxe 4. It breaks on a simple sample:

// Generated by Haxe 4.0.0-rc.3+e3df7a448
(function ($global) { "use strict";
class Main {
    constructor() {
        console.log("src/Main.hx:5:","Hello world!");
    }
    static main() {
        new Main();
    }
}
Main.main();
})({});

with the following error:

C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:2490
  throw err
  ^

SyntaxError: Unexpected token (3:6)
    at Parser.pp$4.raise (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:2488:13)
    at Parser.pp.unexpected (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:623:8)
    at Parser.pp.semicolon (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:600:59)
    at Parser.pp$1.parseExpressionStatement (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:1025:8)
    at Parser.pp$1.parseStatement (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:780:22)
    at Parser.pp$1.parseBlock (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:1040:23)
    at Parser.pp$3.parseFunctionBody (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:2362:22)
    at Parser.pp$1.parseFunction (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:1132:8)
    at Parser.pp$3.parseExprAtom (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:1999:17)
    at Parser.pp$3.parseExprSubscripts (C:\haxe\ModularES6\node_modules\acorn\dist\acorn.js:1872:19)

ES6 classes support will be a nice to have feature, it provides much cleaner js output and works especially great with ES6 modules' import/export statements.

elsassph commented 5 years ago

I was waiting for the output to stabilise before spending time on the parser. Could be worth stating to look into it.

vrescobar commented 5 years ago

I would like to see this one together with es6 as library (no main, using :@expose on some classes). For a larger project with other es6 code which ends up bundled with webpack and post-processed by babel.

Bad enough I can compile everything as a JS and then import that JS, but then I lose all the webpack goodies like automatic rebuild on changes

vrescobar commented 5 years ago

Nevermind... @malec-palec @elsassph yesterday I found myself having this very same problem and I ended up creating a fork of the webpack loader to support raw haxe's builds. It works great with Haxe 4 and ES6 builds but I never tried to split the code into parts since I believe I do not need the feature (I use conditional builds with DCE and webpack passes the whole thing to babel).

https://github.com/vrescobar/webpack-simple-haxe-loader

Since it contains just the minimum amount of features, now it might be trivial to modify the loader to support other variants which deviates from haxe's original behave.

elsassph commented 5 years ago

If you're still interested in ES6 support, the PR is done and I would appreciate if you want to test it.