diff --git a/gulpfile.js b/gulpfile.js index b4d336b..13f5075 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,6 +20,7 @@ var cleanCSS = require( 'gulp-clean-css' ); var gulpSequence = require( 'gulp-sequence' ); var replace = require( 'gulp-replace' ); var autoprefixer = require( 'gulp-autoprefixer' ); +var rev = require('gulp-rev'); // Configuration file to keep your code DRY var cfg = require( './gulpconfig.json' ); @@ -94,6 +95,16 @@ gulp.task( 'cssnano', function() { .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() { return gulp.src( paths.css + '/theme.css' ) .pipe( sourcemaps.init( { loadMaps: true } ) ) @@ -116,7 +127,7 @@ gulp.task( 'cleancss', function() { }); gulp.task( 'styles', function( callback ) { - gulpSequence( 'sass', 'minifycss' )( callback ); + gulpSequence( 'sass', 'minifycss', 'rev' )( callback ); } ); // Run: @@ -129,11 +140,11 @@ gulp.task( 'browser-sync', function() { // Run: // gulp watch-bs // 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: -// gulp scripts. +// Run: +// gulp scripts. // Uglifies and concat all JS files into one gulp.task( 'scripts', function() { var scripts = [ @@ -157,6 +168,8 @@ gulp.task( 'scripts', function() { gulp.src( scripts ) .pipe( concat( 'theme.js' ) ) .pipe( gulp.dest( paths.js ) ); + + rev(); }); // Deleting any file inside the /src folder diff --git a/inc/enqueue.php b/inc/enqueue.php index ff7858d..27f3fc3 100644 --- a/inc/enqueue.php +++ b/inc/enqueue.php @@ -5,23 +5,35 @@ * @package understrap */ +/** + * @param string $filename + * @return string + */ +function asset_path($filename) { + $manifest_path = get_stylesheet_directory_uri() .'/rev-manifest.json'; + if (isset($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' ) ) { /** * Load theme's JavaScript and CSS sources. */ function understrap_scripts() { - // Get the theme data. - $the_theme = wp_get_theme(); - $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_style( 'understrap-styles', get_stylesheet_directory_uri() . '/'. asset_path('css/child-theme.min.css'), array(), null); wp_enqueue_script( 'jquery'); - wp_enqueue_script( 'popper-scripts', get_template_directory_uri() . '/js/popper.min.js', array(), $theme_version, true); - - $js_version = $theme_version . '.' . filemtime(get_template_directory() . '/js/theme.min.js'); - wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/js/theme.min.js', array(), $js_version, true ); + wp_enqueue_script( 'popper-scripts', get_template_directory_uri() . '/js/popper.min.js', array(), false, true); + + wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/' . asset_path('js/child-theme.min.js'), array(), null, true ); if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); }