7

Does anything special have to be done to get Electron to run my main.coffee file? I have a main.js file (that works) that I converted to CoffeeScript (hence main.coffee), but when I run Electron main.coffee I get an error like the following:

App threw an error when running [SyntaxError: /Users/foo/develop/electron/main.coffee:13
app.on('window-all-closed', ->
                             ^
Unexpected token >]

I can only assume this is a CoffeeScript issue, since when I commented the offending code with CoffeeScript's block comment (###), I got the following:

App threw an error when running [SyntaxError: /Users/foo/develop/electron/main.coffee:13
###
^
Unexpected token ILLEGAL]

I added coffee-script to my packages.json as a dependency, and made sure it was installed to my local node_modules directory like my other application dependencies, but that didn't seem to help.

4

3 回答 3

16

我认为,主文件main.js必须是 javascript。但是你可以要求一个咖啡文件,例如application.coffee,从那里使用coffee-script

main.js

// main.js
require('coffee-script').register();
require('./application')

应用程序.coffee

# application.coffee
app = require('app')
BrowserWindow = require('browser-window')
# ...

安装咖啡脚本

将其包含在您的package.json

{
  ...
  "devDependencies": {
    "electron-prebuilt": "^0.33.1",
    "coffee-script": "~1.10.0"
  }
}

并运行:

npm install
于 2015-09-24T12:56:30.140 回答
7

我最近发现,您可以执行以下操作,而不是转译为 Javascript:

<script>
  require('coffee-script').register();
  require('../src/app/boot');

然后在 src/app/boot.coffee 你可以使用普通的 CoffeeScript :)

我在应用程序https://github.com/postcasio/hearthdash中找到了它,所以那里有更多示例。

于 2015-08-14T12:54:20.287 回答
6

没有办法做到这一点(atom不附带coffeescript编译器),但你可以使用coffeescript的watch选项,

-w, --watch 监视脚本的更改和重新运行命令

例如:

coffee -w main.coffee 在你的情况下。

于 2015-05-18T07:46:35.760 回答