Skip to content

Commit c8ba770

Browse files
authored
Merge pull request #31 from lukasjuhas/master
Better static asset revisioning using randomly generated hash when running gulp tasks.
2 parents 3dad157 + 230fb3e commit c8ba770

5 files changed

Lines changed: 39 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# barebones changelog
2+
## 2.0.4
3+
* Better static asset revisioning using randomly generated hash when running gulp tasks.
4+
25
## 2.0.3
36
* update get_post_thumbnail_url function and add ability to get specific size.
47

functions.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function barebones_scripts()
7272
wp_deregister_script('jquery');
7373
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3', true);
7474
wp_enqueue_script('jquery');
75-
wp_enqueue_script('script', get_stylesheet_directory_uri() . '/js/script.min.js?' . time(), ['jquery'], null, true);
75+
wp_enqueue_script('script', get_stylesheet_directory_uri() . '/js/script.min.js?' . getPackageHash(), ['jquery'], null, true);
7676
}
7777

7878
add_action('wp_enqueue_scripts', 'barebones_scripts');
@@ -173,7 +173,18 @@ function barebones_tiny_mce_before_init($settings)
173173

174174
add_filter('tiny_mce_before_init', 'barebones_tiny_mce_before_init');
175175

176+
/**
177+
* Get hash from package.json used for assets url hash
178+
* @return string
179+
*/
180+
function getPackageHash()
181+
{
182+
$package = file_get_contents(get_bloginfo( 'stylesheet_directory' ) . "/package.json");
183+
$packageJson = json_decode($package, true);
176184

185+
// if there is problem, fallback to time.
186+
return isset($packageJson['hash']) ? $packageJson['hash'] : time();
187+
}
177188

178189
/**
179190
* Get post thumbnail url

gulpfile.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// process.env.DISABLE_NOTIFIER = true;
33

44
var gulp = require('gulp');
5-
var gulpImagemin = require('gulp-imagemin');
65
var elixir = require('laravel-elixir');
76

87
// Assets path
@@ -15,15 +14,34 @@ elixir.config.css.autoprefix.options.browsers = ['last 15 versions'];
1514
var Task = elixir.Task;
1615
elixir.extend('imagemin', function(src, dest) {
1716
new Task('imagemin', function() {
17+
var gulpImagemin = require('gulp-imagemin');
1818
return gulp.src(elixir.config.assetsPath + src)
1919
.pipe(gulpImagemin())
2020
.pipe(gulp.dest(dest));
2121
}).watch(elixir.config.assetsPath + src);
2222
});
2323

24+
// Create a Hash in package.json
25+
elixir.extend('hash', function() {
26+
new Task('hash', function() {
27+
var fs = require('fs');
28+
var fileName = './package.json';
29+
var file = require(fileName);
30+
31+
// generate a new hash
32+
file.hash = ( 0 | Math.random() * 9e6 ).toString(36);
33+
// save to package.json
34+
fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) {
35+
if (err) return console.log(err);
36+
console.log('writing to ' + fileName);
37+
});
38+
})
39+
});
40+
2441
// Run elixir tasks
2542
elixir(function(mix) {
2643
mix.sass('barebones.scss', 'style.css')
2744
.scripts(['script.js'], 'js/script.min.js')
28-
.imagemin('/images/**/*', './img');
45+
.imagemin('/images/**/*', './img')
46+
.hash();
2947
});

header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
88
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
99
<link rel="dns-prefetch" href="//google-analytics.com">
10-
<link rel="stylesheet" href="<?php echo get_bloginfo('stylesheet_url') . '?' . time(); ?>">
10+
<link rel="stylesheet" href="<?php echo get_bloginfo('stylesheet_url') . '?' . getPackageHash(); ?>">
1111
<?php wp_head(); ?>
1212
<!--[if lt IE 10]>
1313
<script src="//cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script>

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "barebones",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
4+
"hash": "brbns",
45
"author": "Benchmark Studios",
56
"description": "A lightweight and skeletal WordPress boilerplate theme for HTML5 and beyond",
67
"license": "MIT",
@@ -17,6 +18,7 @@
1718
},
1819
"private": true,
1920
"devDependencies": {
21+
"fs": "^0.0.2",
2022
"gulp": "^3.8.8",
2123
"gulp-imagemin": "^3.0.1",
2224
"imagemin-gifsicle": "^5.1.0",

0 commit comments

Comments
 (0)