Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 463
Bryan Avatar asked May 17 '15 22:05

Bryan


People also ask

What can you do with CoffeeScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

How do I use CoffeeScript in HTML?

You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.

How do you make a function in CoffeeScript?

Functions in CoffeeScript To define a function here, we have to use a thin arrow (->). Behind the scenes, the CoffeeScript compiler converts the arrow in to the function definition in JavaScript as shown below. (function() {}); It is not mandatory to use the return keyword in CoffeeScript.


1 Answers

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
like image 178
fiedl Avatar answered Oct 13 '22 04:10

fiedl