Makefile refactoring and cleanup.
[pwl6.git] / rules / javascript.mk
1 # This is free and unencumbered software released into the public
2 # domain. To the extent possible under law, the author of this file
3 # waives all copyright and related or neighboring rights to it.
4
5 $(call include-once.,programs.mk)
6
7 NPM ?= npm
8 NPMROOT ?= $(CURDIR)
9
10 npmbindir = $(NPMROOT)/node_modules/.bin
11 .PRECIOUS: $(npmbindir)/%
12
13 npmbin = $(call command-v,$1 $(npmbindir)/$1)
14
15 JSTEST ?= $(npmbindir)/jstest
16 JSHINT ?= $(call npmbin,jshint)
17
18 uglifyjs_npm_package := uglify-js
19
20 UGLIFY ?= $(call npmbin,uglifyjs)
21 UGLIFYFLAGS ?= --comments \
22 --compress $(UGLIFYCOMPRESSFLAGS) \
23 --mangle $(UGLIFYMANGLEFLAGS)
24
25 jsstampprefix ?= $(stampprefix)
26 jsuglyprefix ?=
27
28 JSTESTFLAGS += $(if $(JSTESTFORMAT),--format $(JSTESTFORMAT))
29 JSTEST_NODE_PATH ?= $(VPATH)
30 JSTESTENV += $(if $(JSTEST_NODE_PATH),NODE_PATH=$(JSTEST_NODE_PATH))
31 JSTESTFORMAT ?= spec
32
33 jshint-stampify = $(patsubst %.js,$(jsstampprefix)%.js.lint,$1)
34 jstest-stampify = $(patsubst %.js,$(jsstampprefix)%.js.test,$1)
35 uglify-stampify = $(patsubst %.js,$(jsuglyprefix)%.min.js,$1)
36
37 find-js = $(shell find $1 $(if $2,$2 -o) -type f -name '*.js' -print)
38
39 UGLIFY.js = $(UGLIFY) $(UGLIFYFLAGS)
40 LINT.js = $(JSHINT) $(JSHINTFLAGS)
41 TEST.js = $(JSTESTENV) $(JSTEST) $(JSTESTFLAGS)
42
43 $(jsuglyprefix)/%.min.js: %.js | $(UGLIFY)
44 $(MKDIR) $(@D)
45 $(UGLIFY.js) < $< > $@
46
47 $(jsstampprefix)%.js.lint: %.js | $(JSHINT)
48 $(MKDIR) $(@D)
49 $(LINT.js) $<
50 touch $@
51
52 $(jsstampprefix)%.js.test: %.js | $(JSTEST)
53 $(MKDIR) $(@D)
54 $(call quiet,$(TEST.js) $<,$@)
55
56 $(npmbindir)/%:
57 $(NPM) install $(firstword $(value $(@F)_npm_package) $(@F))