Run function loaded from ecmascript 6 module

时光怂恿深爱的人放手 提交于 2019-12-13 05:15:01

问题


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:


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.



来源:https://stackoverflow.com/questions/26454517/run-function-loaded-from-ecmascript-6-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!