Grunt Copy Flatten not working as expected - gruntjs

I have a directory structure as follows:
source/
libraries/
d3.js
lodash.js
//etc
I have grunt-copy setup as follows:
copy: {
main: {
files: [
{
src: ["source/libraries/*.js"],
dest: "build/",
flatten: true
}
I expect it to flatten the output into build, so that I will have
build/
d3.js
//etc
Instead, I get a reproduction of the original directory structure in build:
build/
source/
libraries/
d3.js
//etc
What gives? Am I not using flatten properly?

Well, if you're only using flatten because you want everything in source/libraries to go into build, I would suggest actually using the cwd (current working directory) option instead. If, on the other hand, you actually have subfolders in source/libraries then you probably want that src line to be source/libraries/**/*.js.
In any case, if you can use cwd instead it would look like this:
copy: {
main: {
files: [
{
src: ["*.js"],
dest: "build/",
cwd: "source/libraries/"
}
]
}
For the other case, maybe this? (Notice the expand option set to true)
copy: {
main: {
files: [
{
src: ["source/libraries/**/*.js"],
dest: "build/",
flatten: true,
expand: true
}
]
}
}

Related

Grunt "copy" noProcess option is not working?

It's look like noProcess option is ignored in my init settings for "copy" plugin.
Any one knows why this is happening ? I dont get any errors but the source folder/files (src/source/**) that it suppose to be ignored is copied over to destiantion folder.
Those are my settings in the init function:
copy: {
options: {
noProcess: ["!src/source/**"]
},
prod: {
files: [
{
expand: true,
src: ['src/**'],
dest: 'build/'
}
]
}
}
Definition
grunt.loadNpmTasks('grunt-contrib-copy');
Call:
grunt.registerTask("prod", ["concat", "uglify", "htmlmin", "imagemin", "copy"]);
Anyone see the problem or whats more important a solution ?
It looks like this setting does the job. But still, I do not understand how the noProcess option should be used to work as expected.
copy: {
prod: {
files: [
{
expand: true,
src: ['src/**', '!src/source/**/*', '!src/source'],
dest: 'build/'
}
]
}
}

Tell Grunt to put source into same target dir

Code that I have:
coffee: {
compile: {
files: {
'server/api/**/*.js': ['server/api/**/*.coffee'] // compile and concat into single file
}
}
},
Meaning, the target Dir should be the same where the .coffee file was found. Above code in Grunt does however create the directory "**" and puts the file "*.js" into it.
This is what I want:
server/api/sample/sample.coffee -> server/api/sample/sample.js
server/api/sample2/sample2.coffee -> server/api/sample2/sample2.js
To compile your files dinamically you have to do it:
glob_to_multiple: {
expand: true,
flatten: true,
cwd: 'server/api',
src: ['**/*.coffee'],
dest: 'server/api',
ext: '.js',
extDot: 'last'
}

Grunt dynamic mapping configuration to output one file

Is it possible using the dynamic mapping configuration to output one minimized file instead of one per src file?
cssmin: {
target: {
files: [{
expand: true,
cwd: 'release/css',
src: ['*.css', '!*.min.css'],
dest: 'css/finalfile.css',
ext: '.min.css'
}]
}
}
This configuration creates folder named css/finalfile.css and generates one output file per one src file.
Any help appreciated.
You can use standard file config to do that.
cssmin: {
target: {
files: {
'output.css': ['app/**/*.css']
}
}
}
Above will minify and concatenate all the CSS files from app directory into output.css

Copy files with grunt skip source folder

There is source folder and publish folder in the project. I want to copy all files and folders from source to publish.
Related code from Gruntfiles.js:
grunt.initConfig({
copy: {
main: {
files: [
{ src: ['source/**/*'], dest: 'publish/'},
]
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('default', ['copy']);
However it copies source folder itself and puts in publish folder.
Tried a lot of variations from grunt-copy documentation and cannot find solution.
Try this configuration:
grunt.initConfig({
copy: {
main: {
cwd: 'source',
src: ['**/*'],
dest: 'publish/',
expand: true
}
}
});
If you want to only copy some parts of SRC and build the others on the normal flow (SASS, CONCAT, MINIFY plugins), you may choose to:
copy: {
main: {
files: [
{expand: true, cwd: '../src/', src: ['images/*'], dest: '../public/images'},
...
]
}
}
The key point above is the CWD which allows you to copy folders as is instead of copying the "src" inside "public".

Copy files but lose folder structure

I'm trying to copy everything in my css directory, including any nested folders and files in those folders.
copy: {
main: {
options: {
expand: true
},
files: [
{ src: 'public/static/css/**', dest: '../../../'}
]
}
}
The above works but it appears to be keeping the directory structure.
So in my root where the files are copied to I have:
>public>static>css>styles.css
>public static>css>nested-folder>styles.css
But I would just like to have:
styles.css
>nested-folder>styles.css
Is this possible?
Set the current working directory, cwd, in the options. There are some instructions in the Grunt docs and your copy options would look something like:
copy: {
main: {
options: {
expand: true,
cwd: 'public/static/css',
src : [ '**' ],
dest: '../../../'
}
}
}

Resources