Replace image urls to data uri. Also warns if a data uri would be duplicated, so developer can move them under one CSS rule to save size.
This plugin requires Grunt ~0.4.1
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-css-img-2-data-uri --save-dev
One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-css-img-2-data-uri');
In your project's Gruntfile, add a section named css_img_2_data_uri
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
css_img_2_data_uri: {
debug: {
files: [{
expand: true,
src: 'src/*.css',
dest: 'debug'
}], options: {
missingFiles: true,
pathAsComment: true,
refresh: true,
reportDuplicates: false
}
},
release: {
files: {
src: 'src/main.css' : 'release/main.css', //dest:src
src: 'src/reset.css' : 'release/reset.css',
}
}
}
})
Type: Array
List of css files which needs to be processed.
Type: Boolean
If set to true, it throws an error when the same image found twice, so the grunt build would stop.
Type: Boolean
If set to true (and throwOnDuplicate is not true), it will report the possible duplicates (as line numbers). Set to false to suppress messages. Default value: true
Type: Boolean
If set to true and a missing entry is thrown while reading a local image, the error will be consumed and not thrown. The uri will remain intact.
Type: Char
Set to any character to replace quoted uri values with specific quotes, e.g ', " or empty string to strip quotes from value. Default behavior is to keep the existing quotes if any.
Type: Boolean
If set to true the replaced uri path is kept in a comment, for reference or refreshing. Note: if refresh option is used you still need to use pathAsComment to maintain the value. Default value: false
Type: Boolean
If set to true and the url is followed by a file path, its content will be re-encoded. Used in case the file has changed. Combine with pathAsComment to be able to refresh again and again. Useful if css has embedded images using Visual Studio's Web Essentials. The comment must follow the url, after a single space. Comments not pointing to a valid file will be ignored and left intact. If a comment matches the url it's not duplicated. Default value: false
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.