CoffeeScript

CoffeeScript 是一种可转换为 JavaScript 的语言,它允许你使用更短的语法和其他功能,例如 存在运算符更短的数组拼接语法块正则表达式 等。

¥CoffeeScript is a language that transpiles to JavaScript, which allows you to use a shorter syntax and other features like the existential operator, shorter array-splicing syntax, block regular expressions and more.

Parcel 使用 @parcel/transformer-coffeescript 插件自动支持 CoffeeScript。当检测到 .coffee 文件时,它将自动安装到你的项目中。

¥Parcel supports CoffeeScript automatically using the @parcel/transformer-coffeescript plugin. When a .coffee file is detected, it will be installed into your project automatically.

CoffeeScript 被编译为 JavaScript 并按照 JavaScript 文档 中的描述进行处理。

¥CoffeeScript is compiled to JavaScript and processed as described in the JavaScript docs.

用法示例

#

¥Example usage

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

URL 依赖

#

¥URL dependencies

在 JavaScript 文件中,可以使用 URL 构造函数结合 import.meta.url 创建 URL 依赖。这可用于引用图片、workers服务工作线程 等 URL。

¥In JavaScript files, URL dependencies can be created using the URL constructor combined with import.meta.url. This can be used to reference URLs such as images, workers, service workers, and more.

CoffeeScript 目前不支持 import.meta。相反,你可以使用带有 file: 前缀的 CommonJS __filename 变量将其转换为 URL。例如,以下是在 CoffeeScript 中创建工作线程的方法:

¥CoffeeScript does not currently support import.meta. Instead, you can use the CommonJS __filename variable with the file: prefix to convert it to a URL. For example, here's how you could create a worker in CoffeeScript:

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

对于其他类型的依赖(例如图片)也是如此:

¥The same goes for other types of dependencies like images:

img = document.createElement 'img'
img.src = new URL 'hero.jpg', 'file:' + __filename
document.body.appendChild img
Parcel 中文网 - 粤ICP备13048890号