How to exclude a directory from grunt-contrib-imagemin - gruntjs

I have the following snippet in my Gruntfile.js:
imagemin: {
options: {
optimizationLevel: 7,
cache: false
},
dist: {
files: [{
expand: true,
cwd: 'Assets/img/',
src: ['**/*.{png,jpg,gif}', '!optimised/*.*'],
dest: 'Assets/img/optimised/'
}]
}
}
When I run grunt imagemin the files in /optimised get optimised again, what's the correct pattern to make sure I exclude whatever files I have in my 'optimised' folder?
I've tried the globbing pattern ! that's used to negate a match but can't make it work.

Just found the answer, maybe this might help someone else as I couldn't find anything like it on SO.
src: ['**/*.{png,jpg,gif}', '!optimised/**']
More about globbing patterns: http://gruntjs.com/configuring-tasks#globbing-patterns

Related

imagemin not minifying SVGs

I'm using imagemin with Grunt and it works as expected with PNGs/JPGs, but not with SVGs.
When I add an SVG into my project, Grunt says:
>> File "../public/microsite/src/img/Sketch.svg" added.
Running "imagemin:dynamic" (imagemin) task
Minified 0 images (saved 0 B)
Is this simply because it can't optimise them any further, or because it's not trying to at all? Even if an SVG can't be optimised, which I doubt is the case, I'd still like it imagemin to place it in the dist folder.
This is my config:
imagemin: {
dynamic: {
options: {
optimizationLevel: 3,
svgoPlugins: [{ removeViewBox: false }]
},
files: [{
expand: true,
cwd: '../public/microsite/src/img/',
src: ['*.{png, jpg, svg}'],
dest: '../public/microsite/build/img/'
}]
}
}
I figured out the spaces in the src object were invalid. I removed them, which allowed all image files to be minified. src: ['*.*'] also works.

grunt-bower-task and Polymer

I cannot seem to find an easy way to copy all the files from Polymer to using grunt-bower-task.
grunt.initConfig({
bower: {
install: {
options: {
targetDir: 'wwwroot/lib',
layout: 'byComponent',
install: true,
copy: true,
verbose: true,
cleanTargetDir: false,
bowerOptions: {}
}
}
}
I understand that only the main files defined inside each element's bower.json file get copied over. I am also aware that I could put a exportsOverride section in my own bower.json to include more files like this -
"exportsOverride": {
"*": {
"": "*.*",
"demo": "demo/*.*",
"test": "test/*.*"
}
}
But this doesn't cover all cases as some elements have more sub-folders than just demo and test. Do I have to manually look them all up and add their paths to the exportsOverride, or there's an easy way that I've overlooked?
hate to provide a sample of a fail....
FWIW recently , i had very similar issue ... worked it and failed
what i did is abandon the attempt to flatten everything out in the "dist" tag for a first polymer project. Rather i just ran minify/ugly on one or two elements leaving the HTTP2 type file structure ( deep and many many, dirs/files. )
// the process belo NG . Manual edit needed on "polymer-min.html" go end and chg the js file name
copy: {
main: {
files: [
// includes files within path
{expand: true, src: ['*html'], dest: 'dest/', filter: 'isFile'},
// includes files within path and its sub-directories
{expand: true, src: ['js/**', 'images/**' ,'css/**' ,'elements/**' ,'bower_components/**'], dest: 'dest/'},
{ src: ['tmp/csp/build-csp.html'], dest: 'dest/bower_components/cast-button-polymer/cast-button-polymer-min.html',
filter: 'isFile',
options: {
process: function (content, srcpath) {
return content.replace(/build-csp.js/g,"cast-button-polymer-min.js");
},
},
},
{ src: ['tmp/csp/build-csp-min.js'], dest: 'dest/bower_components/cast-button-polymer/cast-button-polymer-min.js', filter: 'isFile'},
],
},
},

grunt-contrib-copy: Multiple Copy Tasks

Was just wondering if it's possible to set the 'copy' task to do selective copies? Say, if one task wanted to target some files for copying, while another task may want to target others.
I see the 'main' is used in all the examples, but I can't find reference to if other names are able to be used, or another way to accomplish this, outside of using grunt-multi-dest
copy: {
main: {
files: [
{
cwd: 'src_static/img/',
src: ['**'],
dest: '../mainProject/assets/img/'
}
],
onlyIcons: {
files: [
{
cwd: 'src_static/img/icons/',
src: ['**'],
dest: '../mainProject/assets/img/icons/'
}
],
}
}
grunt.registerTask('copy-all', ['copy']);
grunt.registerTask('copy-icons', ['copy:onlyIcons']);
Although closed, I was asked to reference the question I posted as an issue on the grunt-contrib-copy site: https://github.com/gruntjs/grunt-contrib-copy/issues/230#issuecomment-96467261
Thanks.
-Keith
For anyone coming across this now, this actually works:
grunt.registerTask('copy-all', ['copy']);
grunt.registerTask('copy-icons', ['copy:onlyIcons']);
This is going off of KDCinfo's initial Gruntfile config:
copy: {
main: {
files: [{
cwd: 'src_static/img/',
src: ['**'],
dest: '../mainProject/assets/img/'
}]
},
onlyIcons: {
files: [{
cwd: 'src_static/img/icons/',
src: ['**'],
dest: '../mainProject/assets/img/icons/'
}],
}
}
and shows that the copy.main and copy.onlyIcons must be called as copy:main and copy:onlyIcons within grunt.registerTask().
Looks like grunt-multi-dest appears to be the clear winner. Even then, there's not much downside to just including and using it. It fills the gap nicely.

Grunt, css min to concat and minify all css

I have a bunch of various css from plugins and separate style sheets I am using, and I am trying to build a task that will combine and minify all of them. Right now I'm trying to do this with cssmin, I am not sure if I am on the right path, as this is my first time trying this, but here is what I am trying.
cssmin: {
target: {
files: {
'css/output.css': ['css/*.css', 'css/*.min.css']
},
files: [{
expand: true,
cwd: 'css',
src: ['css/output.css'],
dest: 'build/css',
ext: '.min.css'
}]
}
}
The idea is that it will take all css and min.css files in my css folder and combine them into 1 output.css then minify that build/css as a min.css file. I am not too sure if this is how this is suppose to work but this is my first attempt in trying so. The basic idea is combine and minify everything into 1 file in the bottom of my tasks (after I have auto prefixed and used uncss to strip bootstrap). I would appreciate any guidance, is this the right direction with this? This doesn't seem to work correctly, so would appreciate any help.
Thanks for reading!
I am not sure... but this works for me, and only have to include cssmin task in my grunt.registerTask line of code. It minifies all my autoprefixed .css, except the already minified versions and combine them into one big minified stylesheet. Hope it helps ^^
cssmin: {
minify: {
files: [{
expand: true,
cwd: 'src/styles',
src: ['**/*.css', '!**/*.min.css'],
dest: 'public/assets/styles',
ext: '.min.css'
}]
},
options: {
shorthandCompacting: false,
roundingPrecision: -1
},
combine: {
files: {
'public/assets/styles/style.css': ['!public/assets/styles/**/*.min.css', 'public/assets/styles/**/*.css']
}
}
}
minify task is not necessary. When you concatenate several files, cssmin minifies the content automatically.
cssmin: {
options: {
shorthandCompacting: false,
roundingPrecision: -1
},
combine: {
files: {
'css/output.min.css': ['css/*.css', '!css/*.min.css']
}
}
}

grunt-contrib-imagemin skipping files and folders

I'm trying to use grunt-contrib-imagemin to recursively process a directory of images. Config looks like this:
imagemin: {
mytarget: {
options: {
optimizationLevel: 7
},
files: [{
expand: true,
cwd: '../../uploads',
src: '{,*/}*.{png,jpg,jpeg}',
dest: '.tmp/uploads'
}]
}
}
In this form it only processes one sub-directory of the 'uploads' folder. I can't see what I'm doing wrong, can anyone spot my dumb mistake?
Thanks,
Toby
Have you tried src: '**/*.{png,jpg,jpeg}', instead of src: '{,*/}*.{png,jpg,jpeg}',?
Please see here for more details.

Resources