grunt-contrib-imagemin skipping files and folders - gruntjs

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.

Related

grunt compress to include hidden subfolders

I'm trying to bundle a number of python dependencies from /dist into the main zip file to deploy it to lambda. These dependencies have hidden subfolders with required files (for whatever reason).
For instance:
dist/psycopg2/.libs or dist/numpy/.libs
Grunt skips those hidden subfolders with just this in compress.dist.files
{
cwd: 'dist/',
src: ['**', '**/.*'],
dest: '/',
expand: true
}
A hidden folder in dist/.somefolder is included that way but nothing in those subfolders. How can I get grunt to compress those as well
Turns out this works:
{
cwd: 'dist/',
src: ['**'],
dest: '/',
dot: true,
expand: true
}

In grunt , how to exclude .git folder from clean in dist folder

Currently my code is:
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/{,*/}*',
'!<%= yeoman.dist %>/.git/**'
]
}]
},
server: '.tmp'
},
But it is still deleting .git folder in dist folder.
Just ignoring the .git in src array should avoid deleting it.
The following task works for me (before section):
clean: {
before: {
src: ['dist/**/*', '!dist/.git/**', '!dist/.gitignore', 'temp'] //do not clean the git folder
},
after: {
src: ['temp']
}
},
The clean task above clears the entire contents of the dist and temp folders except for what's inside dist/.git folder and the dist/.gitignore file.
I use this task as part of the build and deployment flow and it works fine, I keep my commits after each build&deploy although the dist folder is cleaned (without .git's).
Hope this helps someone!

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 less multiple css files keeping folder structure

I have a branding application where I would like to have this output:
/branding/
/default/
default.css
/brand1/
brand1.css
/brand2/
brand2.css
This one should be output from a branding folder with same structure but with .less files
So I would like to do something like this:
less: {
production: {
options: {
},
files: {
'dist/branding/**/*.css': 'branding/**/*.less'
}
}
}
I just seen examples on this where they all go to same folder, but I want to keep this dynamic because in my case there is like a ton of brandings, and the branding folders have more than just a css file, they also have other artifacts like images and so on.
Any suggestions?
If I understand you correctly you want LESS files under branding compiled to a dist/branding folder and to keep the folder structure.
To do that you would do something like this:
files: [
{
expand: true, // Recursive
cwd: "branding", // The startup directory
src: ["**/*.less"], // Source files
dest: "dist/branding", // Destination
ext: ".css" // File extension
}
]
You can visit http://rajdeepdeb.me/less-to-css-grunt-for-multiple-files/
Add the following configuration in your grunt file
...rest of the grunt configuration,
files: [{
expand: true,
cwd: '/',
src: ['**/*.less'],
dest: '/',
ext: '.css',
extDot: 'last'
}],
... rest of the grunt configuration

How to exclude a directory from grunt-contrib-imagemin

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

Resources