-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathassets.mk
More file actions
162 lines (128 loc) · 4.75 KB
/
assets.mk
File metadata and controls
162 lines (128 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#---------------------------
# Tools
#---------------------------
# The minify tool, for minifying CSS
MINIFY := node_modules/.bin/minify
# The jsx tool, for transforming embedded tags in JS
JSX := node_modules/.bin/jsx
# The uglifyjs tool, for minifying JS and generating source maps
UGLIFY := node_modules/.bin/uglifyjs
# All required JS CLI tools
JSTOOLS := ${MINIFY} ${UGLIFY} ${JSX}
# Installs JS CLI tools via npm
${JSTOOLS}:
@npm install
#---------------------------
# Directories
#---------------------------
# Source, build, and output directories for JS
SRCJS := priv/assets/javascripts
GENJS := priv/assets/build/javascripts
OUTJS := priv/www/javascripts
# Source, build, and output directories for CSS
SRCCSS := priv/assets/stylesheets
GENCSS := priv/assets/build/stylesheets
OUTCSS := priv/www/stylesheets
# All build directories, including subdirectories
GENDIRS := ${GENJS} ${GENCSS} ${GENJS}/vendor ${GENJS}/app
# Creates build directories
${GENDIRS}:
@mkdir -p $@
#---------------------------
# Stylesheets
#---------------------------
# Source CSS files, in explicit order
CSS := ${SRCCSS}/bootstrap.css ${SRCCSS}/application.css
# CSS minified target files
MINCSS := $(patsubst ${SRCCSS}/%.css,${GENCSS}/%.min.css,${CSS})
# Copy concatenated/minified CSS to public folder
${OUTCSS}/%.css: ${GENCSS}/%.css
@echo "Copy $< to $@"
@cp $< $@
# Concatenate minified CSS
${GENCSS}/application.css: ${MINCSS}
@echo "Concatenate: $+"
@cat $+ > $@
# Generate minified CSS from plain CSS
${GENCSS}/%.min.css: ${MINIFY} ${SRCCSS}/%.css
@echo "Minify: $(lastword $^)"
@${MINIFY} --output $@ $(lastword $^)
#---------------------------
# Javascripts
#---------------------------
# Function to extract a module name for minispade from the filename
jsmodname = $(strip $(patsubst ${GENJS}/app/%.js,%,$(1)))
# Function to derive the source map name from the minified filename
sourcemap = $(strip $(patsubst %.min.js,%.map,$(1)))
# Function to derive the minified filename from the source filename
minjs = $(strip $(patsubst %.spade.js,%.min.js,$(1)))
# Source vendor files, in explicit order.
VENDOR_JS := es5-sham.js \
es5-shim.js \
minispade.js \
jquery.js \
react.js \
routie.js
# Minified vendor JS files
VENDOR_SRC := $(patsubst %.js,${SRCJS}/vendor/%.js,${VENDOR_JS})
# All application source JS files
APP_SRC := $(shell find ${SRCJS}/app -name "*.js" -not -name ".\#*")
# Minified application JS files
APP_SPADE := $(patsubst ${SRCJS}/%.js,${GENJS}/%.spade.js,${APP_SRC})
# Copies concatenated/minified JS to the output directory
${OUTJS}/%.js: ${GENJS}/%.js
@echo "Copy $< to $@"
@cp $< $@
# Copies source maps to the output directory
${OUTJS}/%.js.map: ${GENJS}/%.js.map
@echo "Copy $< to $@"
@cp $< $@
# Concatenates all minified application sources to a single file.
${GENJS}/application.js.map ${GENJS}/application.js: ${UGLIFY} ${APP_SPADE}
@echo "Uglify application.js"
@${UGLIFY} ${APP_SPADE} --source-map ${GENJS}/application.js.map -p $(words $(subst /, ,${GENJS})) \
--source-map-include-sources \
--source-map-url /javascripts/application.js.map \
--output ${GENJS}/application.js
# Concatenates all minified vendor sources to a single file.
${GENJS}/vendor.js.map ${GENJS}/vendor.js: ${VENDOR_SRC}
@echo "Uglify vendor.js"
@${UGLIFY} ${VENDOR_SRC} --source-map ${GENJS}/vendor.js.map -p $(words $(subst /, ,${GENJS})) \
--source-map-include-sources \
--source-map-url /javascripts/vendor.js.map \
--output ${GENJS}/vendor.js
# Wrap app modules in minispade
%.spade.js: %.js
@echo "Minispade: $< as '$(call jsmodname,$<)'"
@echo "minispade.register('$(call jsmodname,$<)', function(){" > $@
@cat $< >> $@
@echo "});" >> $@
# Filter app modules through JSX
${GENJS}/app/%.js: ${JSX} ${GENJS}/app ${SRCJS}/app/%.js
@echo "JSX: $(filter %.js,$^)"
@${JSX} --no-cache-dir $(dir $(lastword $^)) $(@D)
#---------------------------
# Top-level targets
#---------------------------
# Target asset files, concatenated and minified.
ASSETS := priv/www/stylesheets/application.css \
priv/www/javascripts/vendor.js \
priv/www/javascripts/vendor.js.map \
priv/www/javascripts/application.js \
priv/www/javascripts/application.js.map \
# These are fake targets
.PHONY: assets clean_assets
# Installs the build tools, generates build directories and then the
# assets.
assets: ${JSTOOLS} ${GENDIRS} ${ASSETS}
# Adds clean_assets
clean: clean_assets
# Cleans generated assets
clean_assets:
@echo "Removing generated assets:"
@rm -rfv ${ASSETS} ${GENDIRS}
# If fswatch is installed, watches for changes in the files and then
# runs clean_assets and assets targets.
auto: clean_assets assets
@fswatch -e '#' -l 3 priv/assets/javascripts priv/assets/stylesheets | \
xargs -n1 -I{} ${MAKE} clean_assets assets