Run function loaded from ecmascript 6 module

2019-08-29 11:17发布

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 { row } from 'public_js/app';
row();

Traceur (I use grunt-traceur task) compiles them to:

// app.js
System.register("public_js/app", [], function() {
  "use strict";
  var __moduleName = "public_js/app";
  function row() {
    alert('row');
  }
  return {get row() {
      return row;
    }};
});

// init.js
System.register("public_js/init", [], function() {
  "use strict";
  var __moduleName = "public_js/init";
  var row = System.get("public_js/app").row;
  row();
  return {};
});

I include compiled version of init.js to my HTML via simple script tag:

<script src="/path/to/compiled/init.js" type="module"></script>

And nothing happens. I don't see my alert. What am I doing wrong?

1条回答
再贱就再见
2楼-- · 2019-08-29 11:42

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:

<script src="/path/to/compiled/app.js"></script>
<script src="/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.

查看更多
登录 后发表回答