Skip to content Skip to sidebar Skip to footer

Run Function Loaded From Ecmascript 6 Module

I try to use ecmascript 6 modules system for first time. I use traceur compiler. Given two es6 files: // app.js export function row() { alert('row'); } // init.js import { ro

Solution 1:

By pre-compiling your code as modules to ES5, you are now taking it out of the world of the automatic import/module loading system in ES6 and you need to use ES5 mechanisms to load it. So, you need to include the compiled code without the type=module attribute and then get() the module that kicks off the rest of the world.

So, the following works for me:

<scriptsrc="/path/to/compiled/app.js"></script><scriptsrc="/path/to/compiled/init.js"></script><script>System.get('public_js/init');
</script>

Since you are pre-compiling the code, I recommend that you concatenate all of the compiled code into a single JS file to avoid including them all.

If you use Traceur without compiling your code first, then you can live within the ES6 constructs. This includes type="module" and/or import 'module-name'.

Edit Thinking about this further, app.js is correctly compiled as a module. init.js, however, doesn't need to be compiled as a module. You are compiling the code with the --module flag. If, instead, you compile init.js with the --script flag, it will not encapsulate the init code as a module, and you don't need to call System.get by hand. Just something to think about.

Post a Comment for "Run Function Loaded From Ecmascript 6 Module"