Last active
December 1, 2021 04:02
-
-
Save btrepp/164b0153aa9f8d7e258765af7f1c12fc to your computer and use it in GitHub Desktop.
Simple makefile that does javascript fingerprinting so cache times can be configured forever. Requires xidel, grep, md5sum and sed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This make file will build a fingerprinted version of the application into the OUTDIR variable. | |
#It checks html files for src="stuff.js" references, and replaces them with fingerpints | |
#The finger printed file keeps the same directory structure in OUTDIR. | |
#You can debug your app locally by just loading the files directly. Then run make to get | |
#The cacheable version in OUTDIR | |
#Only files referenced in the html are copied into OUTDIR. | |
#Variables | |
OUTDIR=dist | |
#Html pages for application | |
HTML_SRC:= index.html | |
HTML_TARGETS:=$(addprefix $(OUTDIR)/,$(HTML_SRC)) | |
#Javascript source | |
ASSETS_SRC:=$(shell xml sel -N x="http://www.w3.org/1999/xhtml" -t -v "//x:script/@src|//x:link/@href" $(HTML_SRC) | grep -v 'http') | |
ASSETS_TARGETS:=$(shell sha1sum $(ASSETS_SRC) | sed -e 's@^\(\w*\)\s\*\(.*\)\.\(js\|css\)@$(OUTDIR)\/\2\.\1\.\3@g') | |
.PHONY: all clean html js css assets | |
all: html assets Makefile | |
html: $(HTML_TARGETS) | |
assets: $(ASSETS_TARGETS) | |
print-%: | |
@echo '$*=$($*)' | |
clean: | |
rm -Rf $(ASSETS_TARGETS) $(HTML_TARGETS) | |
$(HTML_TARGETS) : $(OUTDIR)/%.html : %.html $(ASSETS_TARGETS) | |
@mkdir -p $(@D) | |
sha1sum $(ASSETS_SRC) \ | |
| sed 's%^\([0-9a-f]*\) \*\(.*\)\.\(js\|css\)$$%s@\2\.\3@\2\.\1\.\3@%' \ | |
| sed -f - $< > $@ | |
.SECONDEXPANSION: | |
$(ASSETS_TARGETS): $(OUTDIR)/% : $$(basename $$(basename %))$$(suffix %) | |
@mkdir -p $(@D) | |
cp $< $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment