2

Just started exploring Meteor, and right off the bat I've got something that's throwing me for a loop:

1) I installed it and ran meteor add coffeescript.

2) I converted the myapp.js file to .coffee.

3) I saved the file.

At this point, Meteor generates a new myapp.js file by compiling the coffeescript file. Ok, that seems logical.

4) I ran meteor. I see the app at localhost:3000, however if I hit the button the button click event is firing twice: once from myapp.js and once from myapp.coffee.

Just to see a test I added this:

Template.hello.events "click input": ->
  # template data, if any, is available in 'this'
  console.log "You pressed the button"  if typeof console isnt "undefined"
  $('body').append('<div>test</div>')

When I click the button two divs get added.

Now, if I delete the compiled myapp.js file, the "you pressed the button" only fires once, and only one div is appended to the page. However if I save the .coffee file a new .js file is generated each time, and now all the JS executes twice.

How are you supposed to use CoffeeScript with Meteor so your code only executes once?

Andrew
  • 42,517
  • 51
  • 181
  • 281

3 Answers3

2

When I use webstorm, a commercial text editor, I pick between having a filewatcher autogenerate js from coffeescript or not. If it autogenerates, it is your editor producing js, but meteor thinks it should look for js or coffee.

In short, probably remove file watchers/coffee converters from your text editor for coffeescript projects. Or, remove coffeescript package from meteor. Not both

Jim Mack
  • 1,437
  • 11
  • 16
  • A commercial editor from jetbrains. I suspect your editor structure is doing this. What do you use? It is not meteor generating the js file. – Jim Mack Sep 17 '13 at 03:52
  • To clarify: As @Akshat points out, meteor does make a build folder and compiled js from coffeescript goes there. We are talking about js files appearing in your source tree. – Jim Mack Sep 17 '13 at 07:05
  • Interesting, I'm just using Sublime Text 2, which as far as I know has never compiled stuff automatically before... I have the coffee-script npm package installed globally, in addition to being added as a meteor package. Is it possible there's a conflict there? – Andrew Sep 17 '13 at 15:02
  • As an experiment, meteor remove coffeescript, make a new coffeescript file up with no matching js, have it pull an alert, verify no js, (then not your env), add coffee back to meteor, see if it pulls an alert twice (then meteor) – Jim Mack Sep 17 '13 at 18:57
  • Do you have any build/package/ci/autotest stuff going on? – Jim Mack Sep 17 '13 at 19:01
  • Jim Mack: If I remove meteor coffeescript the behavior stops. No automatic creation of the duplicate file. – Andrew Sep 19 '13 at 02:27
0

You might have not gotten rid of the original myapp.js, if you are certain the other one was thrown from a file called myapp.js

The coffeescript compiled js files should be myapp.coffee.js (which you can only see in chrome's web inspector).

You should also not be seeing these compiled js files in your project directory, if they do come there when you save your .coffee are others they are the rogue files, perhaps from your code ide?.

Meteor does compile the coffeescript into js but does it in a temporary directory in the hidden .meteor directory. If you're using coffeescript meteor intends the experience to be as if it were javascript so you wouldn't notice any extra files.

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • Ok, so that's interesting. My text editor has never compiled files automatically before, nor do I have anything else running that I would expect to do that. Any guess as to what might be running that could compile twice? – Andrew Sep 17 '13 at 15:03
0

So, I ran meteor remove coffeescript and meteor add coffeescript, and now it works as expected, no duplicate files. Kinda strange, but at least it's working!

Andrew
  • 42,517
  • 51
  • 181
  • 281
  • Makes no sense, but happy for you! I worked on a mixed js/coffeescript project all of August, and never had this problem. – Jim Mack Sep 19 '13 at 03:48