google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 580 forks source link

System.import not found #2035

Closed bennlich closed 8 years ago

bennlich commented 8 years ago

Hey, there! I've been successfully using traceur as documented here (more or less) like so:

<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script type="module">
  import "myModule.js";
</script>

Then I wanted to turn on some experimental features, so I tried following the instructions here, using

<script>
  // Set some experimental options
  var metadata = {
    traceurOptions: {
      properTailCalls: true,
      symbols: true,
      arrayComprehension: true,
      asyncFunctions: true,
      asyncGenerators: exponentiation,
      forOn: true,
      generatorComprehension: true
    }
  };
  System.import('./myModule.js', {metadata: metadata});
</script>

instead of

<script type="module">
  import "myModule.js";
</script>

But System.import apparently doesn't exist? Is the above-linked wiki page old?

I realize that System.js includes a System with System.import, but surely there's a way to stick to traceur pure and simple?

johnjbarton commented 8 years ago

Yes the wiki page is old. But System.import certainly exists, we use it in our code and tests.

A couple things you can try: paste the error you see, and console.log('System', System); just before you use System.import()

bennlich commented 8 years ago

Here are the results:

screen shot 2015-12-05 at 11 08 45 am

@mariusnita tipped me off that manually adding

window.System = new traceur.runtime.BrowserTraceurLoader();

seems to fix this (and it does).

Somewhat aside: do you have to use System.import if you want to turn on experimental features?

johnjbarton commented 8 years ago

Our internal boot-up creates a System object w/o the standard functions. So you need to instantiate the full Loader as we do here https://github.com/google/traceur-compiler/blob/master/demo/repl.html#L50 and here https://github.com/google/traceur-compiler/blob/42c43262c1687d44f9db38a351cbde9201625f9a/src/browser/System.js#L17

I don't think the wiki page is erroneous, at least on this point. Although System may become part of the standard JS runtime eventually, it's still sadly up in the air. And in any case we are moving towards separating the compiler and runtime. I guess we could add System to the bootstrap.js code.

For the browser, yes, import() is how we set the options. There are open bugs on allowing options to be set via script-tag attributes. Another alternative is to recompile the compiler with new options.

bennlich commented 8 years ago

Okay, awesome. Thanks for this. Maybe I should add a quick line to the Options in the browser section about creating System? It seems weird that I'd be the only one running into this.

BennettJames commented 8 years ago

For what it's worth, I just ran into this as well @bennlich. An extra line of clarification would have been very much appreciated.

bennlich commented 8 years ago

K. Added.

bennlich commented 8 years ago

@BennettJames @johnjbarton I added examples here and here. Lemme know what you think.

BennettJames commented 8 years ago

Definitely seems a little more obvious; thanks!