Copy .js and compile .coffee during Makefile task

I have a node project with:

  • .coffeescript source in src
  • compiled coffeescript output to lib
  • shell scripts in bin

How can I adjust my Makefile, shown below, to also copy .js files in src to the lib directory?

BIN = ./node_modules/.bin
SRC = $(wildcard src/*.coffee)
LIB = $(SRC:src/%.coffee=lib/%.js)

init:
    npm install

clean:
    @rm -r -f $(LIB)

build: $(LIB)

dist: clean init build

lib/%.js: src/%.coffee
    $(call coffeetime)

define coffeetime
    @mkdir -p $(@D)
    $(BIN)/coffee -bcp $< > $@
endef

Also, if you have any other suggestions to improve the Makefile, please share.

Something like this might work.

  • Add SRCJS = $(wildcard src/*.js)
  • Change LIB = $(SRC:src/%.coffee=lib/%.js) to LIB = $(SRC:src/%.coffee=lib/%.js) $(SRCJS:src/%=lib/%).
  • Add:

    lib/%.js: src/%.js
        @cp $< $@
    

If it doesn't work (for some reason) then you may need to use a static pattern rule but I think something like the above should work.

I would suggest you to use Grunt instead. There is a few neat task that handle Coffeescript compiling very well. Like: https://github.com/gruntjs/grunt-contrib-coffee

Also, depending on your needs, Node.js can cope with Coffeescript directly if needed.