grunt-majestic-updateversions
v0.3.2
Published
Updates versions of CSS and JS files
Downloads
2
Readme
grunt-majestic-updateversions
Updates versions of CSS and JS files
Getting Started
This plugin requires Grunt ~0.4.5
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-majestic-updateversions --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-majestic-updateversions');
The "majestic_updateversions" task
Overview
In your project's Gruntfile, add a section named majestic_updateversions
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
majestic_updateversions: { css: {}, js: {} }
});
Options
No options
Please use the task as it's given.
Usage Examples
grunt.initConfig({
majestic_updateversions: { css: {}, js: {} }
});
Then in your registered tasks, use it with grunt-contrib-watch:
watch: {
watch_js_files: {
files : ['js/src/**/*.js'],
tasks : ['concat', 'majestic_updateversions:js']
},
watch_css_files: {
files : ['css/scss/**/*.scss'],
tasks : ['compass:dev', 'majestic_updateversions:css']
}
}
Everytime the task is run, a file called 'versions.json' is updated on the same folder of Gruntfile.js file:
{
"css": 1697536117444,
"js": 1697536117444
}
As you can see, we're storing there the date in unix time of when the task was run.
Then, you'll need to create a function on your backend to retrieve the info from that file. In PHP (Drupal 7) it would be like
function get_current_version($type) {
$allowedTypes = array('css', 'js');
$pathToVersionsJsonFile = path_to_theme() . '/versions.json';
$versionsJson = file_get_contents($pathToVersionsJsonFile);
$versions = json_decode($versionsJson, true);
if (in_array($type, $allowedTypes) && isset($versions[$type])) {
return $versions[$type];
}
}
And call that function from the template:
<script type='text/javascript' src="<?php echo $jsPath; ?>/app.min.js?ver=<?php echo get_current_version('js'); ?>"></script>
and
<link rel="stylesheet" href="<?php echo $cssPath; ?>/style.css?ver=<?php echo get_current_version('css'); ?>" />
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Release History
(Nothing yet)