8

I'm using ES6 modules transpiled to ES5 with traceur.
Transpilation is done via grunt + grunt-traceur

Traceur allows you to pick which module handler to use: its own, AMD, commonJS or inline.
I have tried most of them, but none seems to to work. Why?

TestClass.js

export default class TestClass {
    constructor() {
        alert('test');
    }
}

Main.js

import TestClass from './TestClass';

var test = new TestClass();

Gruntfile.js (extract)

traceur: {
    options: {
        experimental: true,
        blockBinding: true,
        modules: 'amd'
    }
}

index.html (extract)

<script src="js/vendor/traceur-runtime.js"></script>
<script src="js/vendor/require.js"></script>

<script defer async src="js/compiled/Main.js"></script>

Error given

Uncaught Error: Mismatched anonymous define() module: function ($__0) {

It seems that there are issues with the grunt plugin, but even using an older version doesn't seem to help.

Code was adapted from an article.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Razor
  • 27,418
  • 8
  • 53
  • 76
  • You might also want to take a look at [6to5](http://6to5.github.io/). It has very easy to use [module formatters](http://6to5.github.io/modules.html) in several different formats which play well with other tools. – James Kyle Dec 05 '14 at 03:03

1 Answers1

4

It seems that I had very similar problem (and googled your question trying to find solution).

I had not seen error provided by you, anyway post here my implemetation, maybe it helps you.

First of all you need to load both js script with treceur runtime. Like this:

<script src="js/vendor/traceur-runtime.js"></script>
<script defer async src="js/compiled/TestClass.js" type="module"></script>
<script defer async src="js/compiled/Main.js" type="module"></script>

Note that you must specify that your scripts is module-s in type attribute.

Than you have to load init module:

<script>
    System.get('public_js/init'); 
    // pass your init module name as a parameter
    // you can see it in private __moduleName variable in compiled init.js
</script>

That implemetation works well for mine. I use 0.2.9 version of grunt-traceur and 0.0.72 version of treceur runtime. Hope this helps you.

Community
  • 1
  • 1
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
  • thanks I had the same problem, Be nice if grunt-traceur allowed you to list files that should be run immediately and not be assumed to be modules for deferred loading. – Josh Mc Jan 28 '15 at 21:22