Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying ES6 imports with Chrome but it doesn't seem to work

I am contemplating moving from Dart to ES6 but Chrome doesn't seem to support the new import statement which is critical to me.

I used the (named export) code from this site: http://www.2ality.com/2014/09/es6-modules-final.html

I tried it even with

<module import="main"><module>

I get the error: "Unexpected token import"

Any information if they will support it before the final release ?

code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>ES6</title>
</head>
<body bgcolor="blue">
  <script type="module" src="main.js"></script>
</body>
</html>

main.js

import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5

lib.js:

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
like image 615
Ray Hulha Avatar asked Feb 19 '16 02:02

Ray Hulha


2 Answers

It works now, finally in Chrome 60 with the Experimental Web Platform features enabled.

Here is a test:
https://github.com/paulirish/es-modules-todomvc

See here for status news:
https://www.chromestatus.com/features/5365692190687232

like image 127
Ray Hulha Avatar answered Oct 10 '22 11:10

Ray Hulha


Safari Tech Review 19, via WebKit, now supports modules.

https://twitter.com/Constellation/status/806660783312543744

https://webkit.org/status/

like image 30
backspaces Avatar answered Oct 10 '22 11:10

backspaces