Asset improvements (gulp)

Ref #1149 & https://github.com/roots/roots/blob/assets-improvements/lib/assets.php
This commit is contained in:
Ben Word
2014-10-27 18:45:35 -05:00
parent 5389bd63c3
commit e8822a2d67
4 changed files with 32 additions and 30 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,5 @@
# Include your project-specific ignores in this file # Include your project-specific ignores in this file
# Read about how to use .gitignore: https://help.github.com/articles/ignoring-files # Read about how to use .gitignore: https://help.github.com/articles/ignoring-files
assets/dist assets/dist
assets/rev-manifest.json
bower_components bower_components
node_modules node_modules

View File

@@ -15,11 +15,11 @@ $roots_includes = array(
'lib/wrapper.php', // Theme wrapper class 'lib/wrapper.php', // Theme wrapper class
'lib/sidebar.php', // Sidebar class 'lib/sidebar.php', // Sidebar class
'lib/config.php', // Configuration 'lib/config.php', // Configuration
'lib/assets.php', // Scripts and stylesheets
'lib/activation.php', // Theme activation 'lib/activation.php', // Theme activation
'lib/titles.php', // Page titles 'lib/titles.php', // Page titles
'lib/nav.php', // Custom nav modifications 'lib/nav.php', // Custom nav modifications
'lib/gallery.php', // Custom [gallery] modifications 'lib/gallery.php', // Custom [gallery] modifications
'lib/scripts.php', // Scripts and stylesheets
'lib/extras.php', // Custom functions 'lib/extras.php', // Custom functions
); );

View File

@@ -97,11 +97,12 @@ gulp.task('images', function() {
}); });
gulp.task('version', function() { gulp.task('version', function() {
return gulp.src(['assets/dist/css/main.min.css', 'assets/dist/js/scripts.min.js'], { base: 'assets' }) return gulp.src(['assets/dist/css/main.min.css', 'assets/dist/js/scripts.min.js'], { base: 'assets/dist' })
.pipe(gulp.dest('assets/dist'))
.pipe($.rev()) .pipe($.rev())
.pipe(gulp.dest('assets')) .pipe(gulp.dest('assets/dist'))
.pipe($.rev.manifest()) .pipe($.rev.manifest())
.pipe(gulp.dest('assets')); .pipe(gulp.dest('assets/dist'));
}); });
gulp.task('watch', function() { gulp.task('watch', function() {

View File

@@ -14,30 +14,26 @@
* - An ID has been defined in config.php * - An ID has been defined in config.php
* - You're not logged in as an administrator * - You're not logged in as an administrator
*/ */
function roots_scripts() { function roots_asset_path($filename_dev, $filename) {
/**
* The build task in Grunt renames production assets with a hash
* Read the asset names from assets-manifest.json
*/
if (WP_ENV === 'development') { if (WP_ENV === 'development') {
$assets = array( return get_template_directory_uri() . '/assets/dist/' . $filename_dev;
'css' => '/assets/dist/css/main.css',
'js' => '/assets/dist/js/scripts.js',
'modernizr' => '/bower_components/modernizr/modernizr.js',
'jquery' => '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js'
);
} else {
$get_assets = file_get_contents(get_template_directory() . '/assets/rev-manifest.json');
$assets = json_decode($get_assets, true);
$assets = array(
'css' => '/assets/' . $assets[get_template_directory() . '/assets/dist/css/main.min.css'],
'js' => '/assets/' . $assets[get_template_directory() . '/assets/dist/js/scripts.min.js'],
'modernizr' => '/assets/dist/js/modernizr.min.js',
'jquery' => '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'
);
} }
wp_enqueue_style('roots_css', get_template_directory_uri() . $assets['css'], false, null); $manifest_path = get_template_directory() . '/assets/dist/rev-manifest.json';
if (file_exists($manifest_path)) {
$manifest = json_decode(file_get_contents($manifest_path), true);
} else {
$manifest = [];
}
if (array_key_exists($filename, $manifest)) {
return get_template_directory_uri() . '/assets/dist/' . $manifest[$filename];
}
}
function roots_assets() {
wp_enqueue_style('roots_css', roots_asset_path('css/main.css', 'css/main.min.css'), false, null);
/** /**
* jQuery is loaded using the same method from HTML5 Boilerplate: * jQuery is loaded using the same method from HTML5 Boilerplate:
@@ -46,7 +42,13 @@ function roots_scripts() {
*/ */
if (!is_admin() && current_theme_supports('jquery-cdn')) { if (!is_admin() && current_theme_supports('jquery-cdn')) {
wp_deregister_script('jquery'); wp_deregister_script('jquery');
wp_register_script('jquery', $assets['jquery'], array(), null, true);
if (WP_ENV === 'development') {
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js', array(), null, true);
} else {
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', array(), null, true);
}
add_filter('script_loader_src', 'roots_jquery_local_fallback', 10, 2); add_filter('script_loader_src', 'roots_jquery_local_fallback', 10, 2);
} }
@@ -54,11 +56,11 @@ function roots_scripts() {
wp_enqueue_script('comment-reply'); wp_enqueue_script('comment-reply');
} }
wp_enqueue_script('modernizr', get_template_directory_uri() . $assets['modernizr'], array(), null, true); wp_enqueue_script('modernizr', roots_asset_path('../../bower_components/modernizr/modernizr.js', 'js/modernizr.min.js'), array(), null, true);
wp_enqueue_script('jquery'); wp_enqueue_script('jquery');
wp_enqueue_script('roots_js', get_template_directory_uri() . $assets['js'], array(), null, true); wp_enqueue_script('roots_js', roots_asset_path('js/scripts.js', 'js/scripts.min.js'), array(), null, true);
} }
add_action('wp_enqueue_scripts', 'roots_scripts', 100); add_action('wp_enqueue_scripts', 'roots_assets', 100);
// http://wordpress.stackexchange.com/a/12450 // http://wordpress.stackexchange.com/a/12450
function roots_jquery_local_fallback($src, $handle = null) { function roots_jquery_local_fallback($src, $handle = null) {