Skip to content

Commit

Permalink
[#439] Append a hash to minified file names.
Browse files Browse the repository at this point in the history
This will enforce browsers to retrieve again the file when the hash
changes, making sure users don't use old versions of the JS code.

The minify script is changed in several ways, in addition to
calculating and appending the hash to the file name:

* To revert previous minification, we now use git reset --hard. It's
  dangerous but prevents the regexp logic from getting too wild.

* We now loop through the php files for every js file, making the
  script much less efficient. Again, this is done to simplify regexp
  logic.

* Explicitly delete source map files from previous minification, to
  prevent maps with different hashes to pile up.
  • Loading branch information
jaragunde committed May 4, 2022
1 parent 1497620 commit 98e81bf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ help:
rm footer

minify:
# WARNING: this will remove any unstaged changes! Do not run on a development directory
for i in `find -name "*.min.js" -not -path "*web/vuejs/*"`; do rm $$i; done
for i in `find -name "*.js" -not -path "*web/vuejs/*"` ; do \
for i in `find -name "*.min.js.map" -not -path "*web/vuejs/*"`; do rm $$i; done
#revert any previous minification changes
git reset --hard HEAD
for i in `find -name "*.js" -not -path "*web/vuejs/*" -not -path "*vendor*"` ; do \
#extract file name to be used in the uglify output \
FILE=`basename -s .js $$i`; \
DIR=`dirname $$i`; \
HASH=`md5sum $$i | awk '{ print $$1 }'`; \
cd $$DIR; \
uglifyjs $${FILE}.js --output $${FILE}.min.js \
--source-map "filename=$${FILE}.min.js.map" -c -m; \
uglifyjs $${FILE}.js --output $${FILE}.$${HASH}.min.js \
--source-map "filename='$${FILE}.$${HASH}.min.js.map'" -c -m; \
cd -; \
done
for i in `find web -name "*.php" -not -path "*web/holiday*"`; do \
#revert any previous minification changes \
sed 's/<script src="\(.*\).min.js">/<script src="\1.js">/' $$i > tmp; \
#modify script tags to link the minified file \
sed 's/<script src="\(.*\).js">/<script src="\1.min.js">/' tmp > $$i; \
for j in `find web -name "*.php"`; do \
#modify script tags to link the minified file \
sed "s/<script src=\"\(.*\)$${FILE}.js\">/<script src=\"\1$${FILE}.$${HASH}.min.js\">/" $$j > tmp; \
cp tmp $$j; \
done \
done
rm tmp

Expand Down

0 comments on commit 98e81bf

Please sign in to comment.