Kevin Mann Kevin Mann - 3 months ago 6
CSS Question

Add a comment with date or an incrementing "version" number to js, css or html files using a preprocessor?

I am working on a project for which the end client gets a lot of build.

Sometimes a fix is reported not repaired when it has been and it turns out the latest build has not been uploaded by they tech team. Our solution to this has been to comment by hand a date of release as we send it out, then we can check they upload and compare to our build.

We want to remove the risk of one of the team forgetting to version number a build, is it possible using a pre-processor to add one as a comment or even a JS var.

I know uglify.js has a 'define' parameter function, would this work, no one in the team has experience in this? We are also using compass to combine scss files.


Without knowing more about your build process it's difficult to give the best answer. (How are uglify and compass invoked?)

Here is one method:

(echo -n '// '; date; uglify input) > output

This is a Linux command that prepends the output of the date command to the output of uglify (source). A similar batch file could be written for Windows environments. Or Grunt could be used for a cross-platform solution...

Myself, I developed a system that automatically incorporates the Git version when building my project.

  • The build output contains a version like this: v0.2.9.9 6gec43+ It's a combination of tags and fragment of the Git commit SHA. The SHA fragment usually pinpoints the exact version of the project source files.
  • The process is automated via git post-commit hooks. After each commit, a shell script is run. This script writes version.js, a tiny JavaScript module that simply stores the current project version (or 'UNKNOWN' if not available for some reason. Could fall back to the date, too.)
  • View my script, based on this script.