Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript *there*?

后端 未结 3 1007
栀梦
栀梦 2020-11-29 02:03

Is there a way to send CoffeeScript to the client\'s browser and have it compiled to JavaScript there?



        
3条回答
  •  情书的邮戳
    2020-11-29 02:19

    Jeremy already has this one, but let me add some important details and caveats:

    1. At 39k gzipped (compare to jQuery at 29k), coffee-script.js is a big file; so unless you're actually letting your users run their own CoffeeScript, you really shouldn't use it in production.
    2. As mentioned in the documentation, each CoffeeScript snippet will be in its own anonymous closure. So your example snippet wouldn't do anything—squares wouldn't be visible outside of the script. Instead, you'd want to change it to window.squares = ....
    3. All CoffeeScript code, whether external or inline, will run after all JavaScript code on the page. That's because coffee-script.js doesn't read your
提交回复
热议问题