我目前在Safari開發人員預覽測試ES2015覆蓋(其要求,以支持100%ES2015,模塊包括在內)。導入ES2015模塊需要的文件擴展名
我做了一個簡單的測試,使用使用ES2015代碼(與Babel.JS爲transpiling和Browserify捆綁一起)進行開發時,我一直在定期使用相同的語法。
沒想到我的代碼不會不包括.js
擴展在import語句工作。那是標準行爲嗎?我以爲你可以省略。
/* 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'擴展名。 – CodingIntrigue
我不知道。我想我必須始終把'.js'擴展只是可以肯定的,因爲開發(甚至transpilation和捆綁)期間的好做法 – Zorgatone