Can I use CoffeeScript to write my Electron (Atom

2019-06-17 06:53发布

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.

3条回答
我只想做你的唯一
2楼-- · 2019-06-17 07:40

I think, the main file main.js has to be javascript. But you can require a coffee file, for example application.coffee, from there using coffee-script.

main.js

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

application.coffee

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

Installing coffee-script

Include it in your package.json:

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

And run:

npm install
查看更多
叛逆
3楼-- · 2019-06-17 07:49

There is no way to do it (atom doesn't ship with a coffeescript compiler), but you can use the watch option of coffeescript,

-w, --watch watch scripts for changes and rerun commands

For example:

coffee -w main.coffee in your case.

查看更多
地球回转人心会变
4楼-- · 2019-06-17 07:52

I've recently discovered that instead of transpiling to Javascript, you can do something like:

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

and then in src/app/boot.coffee you can use regular CoffeeScript :)

I found it in the app https://github.com/postcasio/hearthdash so there are more examples there.

查看更多
登录 后发表回答