-
-
Save baires/2915330 to your computer and use it in GitHub Desktop.
Simple, quick way to concatenate, minify, and version static files in a Wordpress theme
This file contains hidden or 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
| # Filename-based cache busting | |
| # taken from https://github.com/h5bp/html5-boilerplate/ | |
| <IfModule mod_rewrite.c> | |
| RewriteCond %{REQUEST_FILENAME} !-f | |
| RewriteCond %{REQUEST_FILENAME} !-d | |
| RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L] | |
| </IfModule> |
This file contains hidden or 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
| #!/bin/sh | |
| # Run `bash build.sh` whenever you need to perform these tasks | |
| # A separate `watch.sh` script is better for continuous compilation | |
| # of `.scss` files during development. | |
| # concatenate and minify the CSS | |
| # requires: Sass (Ruby) | |
| END_CSS="style.css" | |
| END_MIN_CSS="style.min.css" | |
| SASS_COMMAND="sass --load-path scss --style" | |
| $SASS_COMMAND expanded scss/style.scss:$END_CSS | |
| $SASS_COMMAND compressed scss/style.scss:$END_MIN_CSS | |
| echo "SCSS-to-CSS build compilation successful" | |
| # concatenate all JS files | |
| # explicitly name and order the files if needed | |
| cat js/*.js > js/site.js | |
| echo "JS concatenation successful" | |
| # minify the combined JS | |
| # requires: UglifyJS (Node) | |
| uglifyjs --output js/site.min.js js/site.js | |
| echo "JS uglification successful" | |
| exit 0 |
This file contains hidden or 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
| <?php | |
| /* | |
| * Check environment | |
| */ | |
| function is_prod() { | |
| if ( home_url() == '<production-url>' ) { | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| /* | |
| * Basic file versioning | |
| */ | |
| function version($filename) { | |
| // get the absolute path to the file | |
| $pathToFile = TEMPLATEPATH . '/' . $filename; | |
| //check if the file exists | |
| if (file_exists($pathToFile)) { | |
| $needle = '.'; | |
| // return the versioned filename | |
| $versioned = '.' . filemtime($pathToFile) . '.'; | |
| // the position of the last instance of '.' | |
| $pos = strrpos($filename, $needle); | |
| if ($pos !== false) { | |
| // replace and return | |
| return '/' . substr_replace($filename, $versioned, $pos, strlen($needle)); | |
| } | |
| } | |
| else { | |
| // return the original filename | |
| return '/' . $filename; | |
| } | |
| } | |
| ?> |
This file contains hidden or 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
| <?php | |
| // other code | |
| // only reference the minified file in production | |
| // version which ever file is referenced | |
| is_prod() ? $cssfile = 'style.min.css' : $cssfile = 'style.css'; | |
| echo '<link rel="stylesheet" href="' . get_bloginfo('stylesheet_directory') . version($cssfile) . '">'; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment