Implemented custom asset versioning to make creating releases easier

This commit is contained in:
Dan Brown 2015-12-16 17:09:44 +00:00
parent f4b342a575
commit 2a4ff6fb6a
5 changed files with 57 additions and 6 deletions

30
app/helpers.php Normal file
View File

@ -0,0 +1,30 @@
<?php
if (! function_exists('versioned_asset')) {
/**
* Get the path to a versioned file.
*
* @param string $file
* @return string
*
* @throws \InvalidArgumentException
*/
function versioned_asset($file)
{
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
}
if (isset($manifest[$file])) {
return '/' . $manifest[$file];
}
if (file_exists(public_path($file))) {
return '/' . $file;
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
}

View File

@ -25,7 +25,10 @@
],
"psr-4": {
"BookStack\\": "app/"
}
},
"files": [
"app/helpers.php"
]
},
"autoload-dev": {
"classmap": [

View File

@ -1,8 +1,26 @@
var elixir = require('laravel-elixir');
// Custom extensions
var gulp = require('gulp');
var Task = elixir.Task;
var fs = require('fs');
elixir.extend('queryVersion', function(inputFiles) {
new Task('queryVersion', function() {
var manifestObject = {};
var uidString = Date.now().toString(16).slice(4);
for (var i = 0; i < inputFiles.length; i++) {
var file = inputFiles[i];
manifestObject[file] = file + '?version=' + uidString;
}
var fileContents = JSON.stringify(manifestObject, null, 1);
fs.writeFileSync('public/build/manifest.json', fileContents);
}).watch(['./public/css/*.css', './public/js/*.js']);
});
elixir(function(mix) {
mix.sass('styles.scss')
.sass('print-styles.scss')
.browserify(['jquery-extensions.js', 'global.js'], 'public/js/common.js')
.version(['css/styles.css', 'css/print-styles.css', 'js/common.js']);
.queryVersion(['css/styles.css', 'css/print-styles.css', 'js/common.js']);
});

View File

@ -1,7 +1,7 @@
{
"private": true,
"devDependencies": {
"gulp": "^3.8.8",
"gulp": "^3.9.0",
"insert-css": "^0.2.0"
},
"dependencies": {

View File

@ -9,8 +9,8 @@
<meta charset="utf-8">
<!-- Styles and Fonts -->
<link rel="stylesheet" href="{{ elixir('css/styles.css') }}">
<link rel="stylesheet" media="print" href="{{ elixir('css/print-styles.css') }}">
<link rel="stylesheet" href="{{ versioned_asset('css/styles.css') }}">
<link rel="stylesheet" media="print" href="{{ versioned_asset('css/print-styles.css') }}">
<link href='//fonts.googleapis.com/css?family=Roboto:400,400italic,500,500italic,700,700italic,300italic,100,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/libs/material-design-iconic-font/css/material-design-iconic-font.min.css">
@ -79,6 +79,6 @@
</section>
@yield('bottom')
<script src="{{ elixir('js/common.js') }}"></script>
<script src="{{ versioned_asset('js/common.js') }}"></script>
</body>
</html>