Reputation: 11836
Scenario: I use a simple function to minify and compress JS files during the deployment like this:
for i in public/js/*.js; do uglifyjs --overwrite --no-copyright "$i"; done
The problem with this approach is that it minifies and overwrites original files. I would like to somehow introduce a versioning of minified JS and CSS files.
Let's say I have a variable with the version: "123". How to modify my script to write files with this version? It should work with CSS and JS files like this:
style.css -> style.123.css
script.js -> script.123.js
Upvotes: 1
Views: 260
Reputation: 7131
should work in sh too:
VERSION=321; for i in public/js/*.js; do NEW=$(echo $i | sed s/\\./.$VERSION./) ; cp $i $NEW; uglifyjs --overwrite --no-copyright $NEW; done
Upvotes: 1
Reputation: 58666
This is a silly approach. Use a version control tool for versioning your sources.
But of course, you're right not to want to modify the files in place.
Call your source files foo.src.js
(or whatever). (Keep the last suffix .js
so your editor recognizes the language). Then have a build step which produces foo.js
. When you're developing you can have a "null compile" step which just copies foo.src.js
to foo.js
. When you make a release, then the copy step is changed to do the uglifyjs
instead of a copy. Either way, you never edit the generated foo.js
by hand, of course, even when it is just a copy. You might want to, instead of just copying, add a comment on top "generated file, do not edit".
The exact details are up to you. You could have foo.js
be the name of the original source file and foo.u.js
be the uglified one as long as you refer to the right names when loading.
Upvotes: 0
Reputation: 25736
Something like this?
VERSION=123; for i in public/js/*.js; do REV=${i/%.js/.${VERSION}.js}; cp "${i}" "${REV}"; uglifyjs --overwrite --no-copyright "${REV}"; done
REV=${i/%.js/.${VERSION}.js}
replaces the last occurrence of ".js" by ".123.js".
Upvotes: 2