【发布时间】:2017-01-17 14:52:14
【问题描述】:
我目前正在 Safari Developer Preview 上测试 ES2015 覆盖率(声称支持 100% ES2015,包括模块)。
我做了一个简单的测试,使用了我在使用 ES2015 代码进行开发时经常使用的相同语法(以及用于转译的 Babel.JS 和用于捆绑的 Browserify)。
如果不在 import 语句中包含 .js 扩展名,我的代码将无法正常工作。这是标准行为吗?我以为你可以省略它。
/* filename: scripts/alert.js */
export default class Alert {
constructor(message) {
this.message = message;
}
show() {
alert(this.message);
}
}
// Another file
/* filename: scripts/index.js */
import Alert from "./alert.js"; // this won't work if I change it to 'import Alert from "./alert";'
(new Alert("Hello, World!")).show();
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>ES2015 Modules</title>
</head>
<body>
<h1>ES2015 Modules</h1>
<script async="async" type="module" src"scripts/index.js">
</script>
</body>
</html>
【问题讨论】:
-
环境使用 ModuleSpecifier 来加载模块。这不是语言规定的,因此环境可能需要也可能不需要
.js扩展。 -
我不知道。我想我必须始终将
.js扩展名作为开发过程中的一个好习惯(即使是编译和捆绑)
标签: javascript safari ecmascript-6 es6-module-loader es6-modules