Can I use CoffeeScript to write my Electron (Atom Shell) application?

人盡茶涼 提交于 2019-12-08 19:31:51

问题


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.


回答1:


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.




回答2:


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:


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.



来源:https://stackoverflow.com/questions/30292835/can-i-use-coffeescript-to-write-my-electron-atom-shell-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!