How can I compile individual CoffeeScript files in the assets directory?

StackOverflow https://stackoverflow.com/questions/21316338

  •  01-10-2022
  •  | 
  •  

I have an application built in Brunch.io that builds the main framework into app.js, but in my assets folder I have various small coffeescript scripts that need to be compiled individually, and not merged together into one.

Is there a way to compile the CS first and then move it to the public folder like everything else in assets?

I notice that there are some plugins for doing this exact thing with Jade templates (so you can have .jade files in your assets folder). Would this need to be a new plugin?

If there's no obvious way to do it, is there at least a way to define the config file so that it can watch a different folder and compile each .coffee file to it's own .js file, without either joining them all together, or needing to specify each file in the config?

有帮助吗?

解决方案

Since there are no answers, here's the (slightly hacky) way I ended up doing it:

I installed the After-Brunch plugin and added these two commands to my config file, which get run after every compile by Brunch:

  plugins:
    afterBrunch: [
      'find public/ -type f -name "*.coffee" -delete'
      'coffee --compile --output public app/assets/'
    ]

So this simply explicitly deletes the .coffee files that Brunch moved to public/, and then compiles all the .coffee files in assets/ and moves them to public/. Hacky but it works fine.

This annoyingly feels quite backwards: it would have felt much cleaner (even if only in my head) to compile those .coffee files that were already moved into public/ by Brunch, but Coffee doesn't have an in-place conversion option (i.e. replacing the files) that I know of, and running the converter on the files in public/ first and then deleting all *.coffee files didn't work, because the delete command executed before the compile command was finished... But again, this distinction is probably just in my head -- it's just as efficient doing it this way.

If anyone has a more Brunch-like solution, that would be great.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top