Skip to content

Commit

Permalink
Asset improvements (gulp)
Browse files Browse the repository at this point in the history
  • Loading branch information
retlehs committed Oct 27, 2014
1 parent 5389bd6 commit e8822a2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Include your project-specific ignores in this file
# Read about how to use .gitignore: https://help.github.com/articles/ignoring-files
assets/dist
assets/rev-manifest.json
bower_components
node_modules
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
'lib/wrapper.php', // Theme wrapper class
'lib/sidebar.php', // Sidebar class
'lib/config.php', // Configuration
'lib/assets.php', // Scripts and stylesheets
'lib/activation.php', // Theme activation
'lib/titles.php', // Page titles
'lib/nav.php', // Custom nav modifications
'lib/gallery.php', // Custom [gallery] modifications
'lib/scripts.php', // Scripts and stylesheets
'lib/extras.php', // Custom functions
);

Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ gulp.task('images', 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(gulp.dest('assets'))
.pipe(gulp.dest('assets/dist'))
.pipe($.rev.manifest())
.pipe(gulp.dest('assets'));
.pipe(gulp.dest('assets/dist'));
});

gulp.task('watch', function() {
Expand Down
50 changes: 26 additions & 24 deletions lib/scripts.php → lib/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,26 @@
* - An ID has been defined in config.php
* - You're not logged in as an administrator
*/
function roots_scripts() {
/**
* The build task in Grunt renames production assets with a hash
* Read the asset names from assets-manifest.json
*/
function roots_asset_path($filename_dev, $filename) {
if (WP_ENV === 'development') {
$assets = array(
'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'
);
return get_template_directory_uri() . '/assets/dist/' . $filename_dev;
}

$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 {
$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'
);
$manifest = [];
}

wp_enqueue_style('roots_css', get_template_directory_uri() . $assets['css'], false, null);
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:
Expand All @@ -46,19 +42,25 @@ function roots_scripts() {
*/
if (!is_admin() && current_theme_supports('jquery-cdn')) {
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);
}

if (is_single() && comments_open() && get_option('thread_comments')) {
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('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
function roots_jquery_local_fallback($src, $handle = null) {
Expand Down

0 comments on commit e8822a2

Please sign in to comment.