Excellente news, Chrome supporte maintenant nativement les modules ES6. Enfin!

Exemple:

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test module ES6</title>
</head>
<body>
<p>2+3 = <span class="result"></span></p>

<script type="module">
import { add } from './common.js';
(function () {
document.querySelector('.result').innerText = add(2, 3);
console.log(add(2, 3));
}());
</script>

</body>
</html>

common.js

1
2
3
4
console.log('common.js');
export function add (a, b) {
return a + b;
}