es6 模块的转码
ES6 module transpiler
是 square 公司开源的一个转码器,可以将 ES6 模块转为 CommonJS 模块或 AMD 模块的写法,从而在浏览器中使用。
安装
npm install -g es6-module-transpiler
将 ES6 模块文件转码
compile-modules convert file1.js file2.js
SystemJS
它是一个垫片库(polyfill),可以在浏览器内加载 ES6 模块、AMD 模块和 CommonJS 模块,将其转为 ES5 格式。它在后台调用的是 Google 的 Traceur 转码器。
先在网页内载入 system.js 文件。
<script src="system.js"></script>
使用 System.import 方法加载模块文件
<script>
System.import('./app.js') // 可以自动转码
</script>
System.import 使用异步加载,返回一个 Promise 对象,可以针对这个对象编程。
// app/es6-file.js:
export class q {
constructor() {
this.es6 = 'hello'
}
}
<script>
System.import('app/es6-file').then(function(m) {
console.log(new m.q().es6) // hello
})
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: JS Module 的加载实现
下一篇: TypeScript 常见问题
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论