diff --git a/.gitignore b/.gitignore index ceeda20..7c08aa5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ assets/css/main.css.map assets/css/*main*.css assets/js/*scripts*.js assets/js/vendor/modernizr.min.js +assets-manifest.json diff --git a/Gruntfile.js b/Gruntfile.js index 69bc647..4c631c9 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -25,8 +25,8 @@ module.exports = function(grunt) { all: [ 'Gruntfile.js', 'assets/js/*.js', - '!assets/js/*.min.js', - '!assets/js/scripts.js' + '!assets/js/scripts.js', + '!assets/**/*.min.*' ] }, less: { @@ -72,12 +72,6 @@ module.exports = function(grunt) { } } }, - version: { - assets: { - src: ['assets/css/main.min.css', 'assets/js/scripts.min.js'], - dest: 'lib/scripts.php' - } - }, modernizr: { build: { devFile: 'assets/vendor/modernizr/modernizr.js', @@ -92,6 +86,18 @@ module.exports = function(grunt) { parseFiles: true } }, + filerev: { + assets: { + src: ['assets/css/main.min.css', 'assets/js/scripts.min.js'] + } + }, + filerev_assets: { + assets: { + options: { + dest: 'assets-manifest.json' + } + } + }, watch: { less: { files: [ @@ -146,7 +152,8 @@ module.exports = function(grunt) { 'less:build', 'uglify', 'modernizr', - 'version' + 'filerev', + 'filerev_assets' ]); }; diff --git a/lib/scripts.php b/lib/scripts.php index 0c47406..a650840 100644 --- a/lib/scripts.php +++ b/lib/scripts.php @@ -3,18 +3,23 @@ * Enqueue scripts and stylesheets * * Enqueue stylesheets in the following order: - * 1. /theme/assets/css/main.3368efd2.min.css + * 1. /theme/assets/css/main.css * * Enqueue scripts in the following order: * 1. jquery-1.11.0.min.js via Google CDN * 2. /theme/assets/js/vendor/modernizr.min.js - * 3. /theme/assets/js/main.min.js (in footer) + * 3. /theme/assets/js/main.js (in footer) */ function roots_scripts() { + // The build task in Grunt renames production assets with a hash + // Read the asset names from assets-manifest.json + $get_assets = file_get_contents(get_template_directory_uri() . '/assets-manifest.json'); + $assets = json_decode($get_assets, true); + if (WP_ENV === 'development') { wp_enqueue_style('roots_main_dev', get_template_directory_uri() . '/assets/css/main.css', false, null); } else { - wp_enqueue_style('roots_main', get_template_directory_uri() . '/assets/css/main.3368efd2.min.css', false, null); + wp_enqueue_style('roots_main', get_template_directory_uri() . '/' . $assets['assets/css/main.min.css'], false, null); } // jQuery is loaded using the same method from HTML5 Boilerplate: @@ -32,7 +37,7 @@ function roots_scripts() { wp_register_script('modernizr', get_template_directory_uri() . '/assets/js/vendor/modernizr.min.js', array(), null, false); wp_register_script('roots_scripts_dev', get_template_directory_uri() . '/assets/js/scripts.js', array(), null, true); - wp_register_script('roots_scripts', get_template_directory_uri() . '/assets/js/scripts.0fc6af96.min.js', array(), null, true); + wp_register_script('roots_scripts', get_template_directory_uri() . '/' . $assets['assets/js/scripts.min.js'], array(), null, true); wp_enqueue_script('modernizr'); wp_enqueue_script('jquery'); if (WP_ENV === 'development') { diff --git a/package.json b/package.json index d9509ae..e15b59b 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "grunt-contrib-uglify": "~0.2.4", "grunt-contrib-watch": "~0.5.3", "grunt-contrib-less": "~0.8.1", - "grunt-wp-assets": "~0.2.1", + "grunt-filerev": "~0.2.1", + "grunt-filerev-assets": "~0.3.0", "grunt-modernizr": "~0.5.1", "load-grunt-tasks": "~0.3.0" }