CoffeeScript: coffee -w name-of-file.coffee compla

2019-04-22 17:30发布

In CofeeScript I am creating a global object by doing this:

window.App = 
  init : ->
    ...

Running coffee -w app.coffee complains window is not defined and doesn't rewrite the app.js file.

However, running coffee -c app.coffee compiles without a problem. How can I get coffee -w to accept global window?

CoffeeScript version is 1.1.1 (from coffee -v)

Thanks!

1条回答
SAY GOODBYE
2楼-- · 2019-04-22 18:21

If you want to watch a file and have it compiled you need to do:

coffee -wc file.coffee

Using only the -w flag causes coffee to just run the script when it changes, as if you had run:

coffee file.coffee

In regards to the window is not defined error, if you want to make your script runnable both in a browser and in node.js, then you can do this:

root = exports ? this

class Thing
  constructor: (@name) ->
  whoAreYou: ->
    alert @name

root.Thing = Thing

Another useful flag combination is -wp which just pipes the compile javascript to standard out each time you make a change to the file.

查看更多
登录 后发表回答