Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torgeir/9615866 to your computer and use it in GitHub Desktop.
Save torgeir/9615866 to your computer and use it in GitHub Desktop.
Make summary.

Make summary

Expose env variables.

export PATH := node_modules/.bin:$(PATH)

Keep file lists.

template_source := templates/*.handlebars
template_js := build/templates.js

Define targets that depend on files in the file list. $@ contains path of the target.

$(template_js): $(template_source)
    mkdir -p $(dir $@)
    handlebars $(template_source) > $@

Use $(wildcard ..) function to find all files in a folder.

source_files := $(wildcard lib/*.coffee)

For every file in the source_files list replace %.coffee with build/%.js, e.g. replaces lib/foo.coffee with build/lib/foo.coffee. So make now knows the names of all the files before they exists.

build_files  := $(source_files:%.coffee=build/%.js)

Thats why targets can depend on them. $< gives the name of the first dependency of the current target. You can also run make build/lib/concert.js to make only concert.js.

build/%.js: %.coffee
    coffee -co $(dir $@) $<

Only changed files (last-modified time) will be rebuilt. $^ lists all the dependencies, separated with spaces. Handy to combine all dependencies into an aggregate file.

app_bundle := build/app.js

libraries  := vendor/jquery.js \
    node_modules/handlebars/dist/handlebars.runtime.js \
    node_modules/underscore/underscore.js \
    node_modules/backbone/backbone.js

$(app_bundle): $(libraries) $(build_files) $(template_js)
    uglifyjs -cmo $@ $^

We need a "phony" rule, i.e. a rule that is not an actual file, to run our task all

.PHONY: all

all: $(app_bundle);

This gist is a summary of https://blog.jcoglan.com/2014/02/05/building-javascript-projects-with-make/

Additional resources: http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment