CoffeeScript

CoffeeScript 是一種編譯成 JavaScript 的語言,它允許您使用較短的語法和其他功能,例如 存在運算子較短的陣列切片語法區塊正規表示式 等。

Parcel 使用 @parcel/transformer-coffeescript 外掛自動支援 CoffeeScript。當偵測到 .coffee 檔案時,它會自動安裝到您的專案中。

CoffeeScript 編譯成 JavaScript,並按照 JavaScript 文件 中所述進行處理。

範例用法

#
index.html
<script type="module" src="app.coffee"></script>
app.coffee
console.log 'Hello world!'

URL 相依性

#

在 JavaScript 檔案中,URL 相依性 可以使用 URL 建構函式與 import.meta.url 結合建立。這可以用於參照 URL,例如圖片、工作執行緒服務工作執行緒 等。

CoffeeScript 目前不支援 import.meta。您可以改用 CommonJS __filename 變數加上 file: 前綴,將其轉換成 URL。例如,以下是如何在 CoffeeScript 中建立一個工作執行緒

new Worker new URL('worker.js', 'file:' + __filename),
type: 'module'

其他類型的相依性,例如圖片,也是如此

img = document.createElement 'img'
img.src = new URL 'hero.jpg', 'file:' + __filename
document.body.appendChild img