6

I'm kinda new to git. Also, this is my first project where I'm automating the deployment process. So far it's been bliss to being able to do git push dev and have files uploaded, config files copied, etc.

Now I want to minify JS/CSS files when I push to my dev server. I was thinking of installing some command-line tool on the server for minifying and for-each js/css file on certain folder, compress and save, on a post-receive git hook.

Is this a good approach? (cause I've read about adding compressed files to the repo and other ideas I don't feel quite convinced)

If so, which tools are best for the task?

Ignacio
  • 7,947
  • 15
  • 63
  • 74
  • Are you trying to speed up 'git push'? – the.malkolm Dec 03 '12 at 18:22
  • No, sorry, the goal is to have js/css files minified. I'll make that clear in my question. – Ignacio Dec 03 '12 at 18:57
  • 1
    OK, why don't you just minify files on client side? – the.malkolm Dec 03 '12 at 21:15
  • 1
    I guess that's a valid option. And that's part of what I'm asking. Of course, some restrictions apply: -I dont want to version minified files. -I want to use non-minified files on local development – Ignacio Dec 04 '12 at 01:14
  • Do you want the files to be replaced by minified versions or is it fine for the minified files to have a different name? How does your deployment process work at the moment? – Chronial Dec 04 '12 at 01:33
  • For now I'd be happy if my files just get overwritten. Plus, it would make things easier, just run a script that minifies on the server and overwrites. I know it's not ideal. It would be better if local env loaded file.js and staging loaded file.min.js but that involves some more tinkering I think. – Ignacio Dec 04 '12 at 02:42
  • What I mean is, if I make a few changes in the way I load my js I could just load file.js in dev and file.min.js in staging, or even load all minified an concat files all at once. The second alternative involves a bit more rework I don't wish to do at this point, so I want to start by implementing the first approach (just overwriting original files) and then maybe go a step further. But I'm pretty confident on that point. – Ignacio Dec 04 '12 at 02:50

3 Answers3

2

http://git-scm.com/book/ch7-2.html

I assume that you will never make a commit on server i.e. server will be used to only checkout updated master and never update it. This trick will automatically minify any *.css files on checkout:

# within repo
$ echo '*.css filter=minify' >> .git/info/attributes
$ git config filter.minify.clean  cat
$ git config filter.minify.smudge minify-command

Where the minify-command should be the command that minifies *.css files i.e.

$ cat foo.css | minify-command > foo-minified.css

Is it close to what you want?

the.malkolm
  • 2,391
  • 16
  • 16
  • That's a safe assumption. 'dev' is just a remote used to checkout master and then copy files I need to webroot. – Ignacio Dec 04 '12 at 16:29
  • 1
    Also, I think that might do the trick, although I'd have to do a little reading cause I actually don't understand half of it :$ – Ignacio Dec 04 '12 at 16:33
  • Yep, don't trust me! Double check it yourself :) – the.malkolm Dec 04 '12 at 16:38
  • 1
    I'm marking this as accepted, althought, I just used google closure compiler and compressed files in my post-receive hook. – Ignacio Dec 07 '12 at 12:26
0

By rewriting files on dev and not having the minified files in your local repo, the two repositories will always be out of sync with each other.

You might want to rethink the way you deploy your site to dev, instead of pushing to dev, you might want to pull on dev from a prestine repository for example.

Peter van der Does
  • 14,018
  • 4
  • 38
  • 42
  • But my "dev" repo is just an auxiliary repo created for this purpose alone. Also, I followed this guide for the setup: http://toroid.org/ams/git-website-howto – Ignacio Dec 04 '12 at 11:40
0

Maybe you're looking for adding a post-checkout hook (or maybe it's another hook, according to your deploy system), and then launch with that the script which will minify your files.

You can also use the hook to check the modified date of the concerned files before doing that

Asenar
  • 6,732
  • 3
  • 36
  • 49