I am trying to find a better / working solution to forward my ECMA Script 6 code to ES5. I would like to use the module loader and use inheritance. The closest thing I have so far is to use Babel 6 with preinstalled es2015 and transform-es2015-modules-systemjs plugin. Here is my .babelrc file:
{ "presets": ["es2015"], "plugins": ["transform-es2015-modules-systemjs"] }
And my files are structured as follows:
- dist (transpiled files in the same structure as the src folder) - src - classes - Point.js - ColorPoint.js app.js index.html
The content of app.js as follows:
import ColorPoint from 'classes/ColorPoint.js'; let cp = new ColorPoint(25, 8, 'green'); console.log(cp.toString());
The definition of Point.js as follows:
export default class Point {
And the definition of ColorPoint.js looks like this:
import Point from 'classes/Point.js'; export default class ColorPoint extends Point {
And just for completeness, the important part of index.html looks like this:
<script src="node_modules/systemjs/dist/system.js"></script> <script> System.config({ baseURL: 'dist' }); System.import('app.js'); </script>
I transfer the entire src folder to the dist folder using the command:
babel src -d dist
The problem is that Babel adds one line to the top of the transpiled ColorPoint.js file, which splits System.js at runtime. Error:
Uncaught Error: Module http://localhost/es6-tutorial/dist/classes/ColorPoint.js interpreted as global module format, but called System.register.
But when I delete this line at the top of this file, it works again:
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
I guess this might be a bug in the transpiler. I hope to get some recommendations from someone who has successfully implemented module inheritance and loading before. Or maybe give me a current working example that I can look at.