Merge pull request #666 from 0dp/gulp-rev - thx @0dp

gulp-rev
This commit is contained in:
Holger 2018-05-15 15:02:14 +02:00 committed by GitHub
commit fa9243dc8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 14 deletions

View File

@ -20,6 +20,7 @@ var cleanCSS = require( 'gulp-clean-css' );
var gulpSequence = require( 'gulp-sequence' ); var gulpSequence = require( 'gulp-sequence' );
var replace = require( 'gulp-replace' ); var replace = require( 'gulp-replace' );
var autoprefixer = require( 'gulp-autoprefixer' ); var autoprefixer = require( 'gulp-autoprefixer' );
var rev = require('gulp-rev');
// Configuration file to keep your code DRY // Configuration file to keep your code DRY
var cfg = require( './gulpconfig.json' ); var cfg = require( './gulpconfig.json' );
@ -94,6 +95,16 @@ gulp.task( 'cssnano', function() {
.pipe( gulp.dest( paths.css ) ); .pipe( gulp.dest( paths.css ) );
}); });
gulp.task( 'rev', function() {
// by default, gulp would pick `assets/css` as the base,
// so we need to set it explicitly:
gulp.src([paths.css + '/theme.min.css', paths.js + '/theme.min.js'], {base: './'})
.pipe(rev())
.pipe(gulp.dest('./')) // write rev'd assets to build dir
.pipe(rev.manifest())
.pipe(gulp.dest('./')); // write manifest to build dir
});
gulp.task( 'minifycss', function() { gulp.task( 'minifycss', function() {
return gulp.src( paths.css + '/theme.css' ) return gulp.src( paths.css + '/theme.css' )
.pipe( sourcemaps.init( { loadMaps: true } ) ) .pipe( sourcemaps.init( { loadMaps: true } ) )
@ -116,7 +127,7 @@ gulp.task( 'cleancss', function() {
}); });
gulp.task( 'styles', function( callback ) { gulp.task( 'styles', function( callback ) {
gulpSequence( 'sass', 'minifycss' )( callback ); gulpSequence( 'sass', 'minifycss', 'rev' )( callback );
} ); } );
// Run: // Run:
@ -129,11 +140,11 @@ gulp.task( 'browser-sync', function() {
// Run: // Run:
// gulp watch-bs // gulp watch-bs
// Starts watcher with browser-sync. Browser-sync reloads page automatically on your browser // Starts watcher with browser-sync. Browser-sync reloads page automatically on your browser
gulp.task( 'watch-bs', ['browser-sync', 'watch', 'scripts'], function() { gulp.task( 'watch-bs', ['browser-sync', 'watch', 'scripts'], function() {
} ); } );
// Run: // Run:
// gulp scripts. // gulp scripts.
// Uglifies and concat all JS files into one // Uglifies and concat all JS files into one
gulp.task( 'scripts', function() { gulp.task( 'scripts', function() {
var scripts = [ var scripts = [
@ -157,6 +168,8 @@ gulp.task( 'scripts', function() {
gulp.src( scripts ) gulp.src( scripts )
.pipe( concat( 'theme.js' ) ) .pipe( concat( 'theme.js' ) )
.pipe( gulp.dest( paths.js ) ); .pipe( gulp.dest( paths.js ) );
rev();
}); });
// Deleting any file inside the /src folder // Deleting any file inside the /src folder

View File

@ -5,23 +5,35 @@
* @package understrap * @package understrap
*/ */
/**
* @param string $filename
* @return string
*/
function asset_path($filename) {
$manifest_path = get_stylesheet_directory_uri() .'/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 $manifest[$filename];
}
return $filename;
}
if ( ! function_exists( 'understrap_scripts' ) ) { if ( ! function_exists( 'understrap_scripts' ) ) {
/** /**
* Load theme's JavaScript and CSS sources. * Load theme's JavaScript and CSS sources.
*/ */
function understrap_scripts() { function understrap_scripts() {
// Get the theme data.
$the_theme = wp_get_theme(); wp_enqueue_style( 'understrap-styles', get_stylesheet_directory_uri() . '/'. asset_path('css/theme.min.css'), array(), null);
$theme_version = $the_theme->get( 'Version' );
$css_version = $theme_version . '.' . filemtime(get_template_directory() . '/css/theme.min.css');
wp_enqueue_style( 'understrap-styles', get_stylesheet_directory_uri() . '/css/theme.min.css', array(), $css_version );
wp_enqueue_script( 'jquery'); wp_enqueue_script( 'jquery');
wp_enqueue_script( 'popper-scripts', get_template_directory_uri() . '/js/popper.min.js', array(), $theme_version, true); wp_enqueue_script( 'popper-scripts', get_template_directory_uri() . '/js/popper.min.js', array(), false, true);
$js_version = $theme_version . '.' . filemtime(get_template_directory() . '/js/theme.min.js'); wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/' . asset_path('js/theme.min.js'), array(), null, true );
wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/js/theme.min.js', array(), $js_version, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' ); wp_enqueue_script( 'comment-reply' );
} }