Include Everything in a Directory but one thing - gruntjs

I want to run grunt-contrib-concat over a directory and match everything in it except for one subdirectory, however, within that subdirectory I want to match one file. So it looks like this:
// Include everything
topDir/
// Include only one file in this specific directory
topDir/**/subdir/onlyIncludeThisFromThisDirectory.whatever
Is there an easy way to do this?

That's easy as:
[
'topDir/**/*.whatevet',
'!topDir/**/subdir/**/*',
'topDir/**/subdir/onlyIncludeThisFromThisDirectory.whatever']`
]
The pattern with leading ! is the excluding pattern.
Remember that you should always include .extension to the matching pattern, when using "Files Array" mapping, because pattern **/* matches directories too.
To avoid this, you can use dynamic mapping:
files: {
expand: true,
cwd: 'topDir/',
src: ['**/*', '!**/subdir/**/*', '**/subdir/onlyIncludeThisFromThisDirectory.whatever'],
dest: 'dest/',
filter: 'isFile'
}
See documentation for more info.

Related

Grunt Exclude Files from dot directories

I have tried a number of suggestions from Stackoverflow on how to do this, but I can't get them to work. The following excludes the first two files, but does not exclude the nginx files.
cwd: 'server',
src ['**/*','!app.js','!config/local.env.js','!config/local.env.sample.js','!routes.js','!.ebextensions/02Nginx.sub.yml','!.ebextensions/02Nginx.config'],
dest: '/',
expand: true
I have tried other combinations:
'!**/01Nginx.config'
I have added 'dot:true'. Nothing prevents that file from going to the server.
Any suggestions?
Second file selection below the first was overriding my selection. Simple enough.

How to specify relative paths in grunt for less plugin

I apologize for being a complete grunt newbie. I have node.js installed, i have grunt installed, and I am able to run "grunt less" on a gruntfile.js with a less target. It "runs", but it doesn't do anything.
My .less files live in a source respository: C:\Workspace\dev, in directories like:
C:\Workspace\dev\Webs\RP\Content\p1\less\p1.less
C:\Workspace\dev\Webs\RP\Content\p2\less\p2.less
My gruntfile.js file lives in C:\Tools\Grunt (at least as I am learning), so I need to run the "grunt less" command from C:\Tools\Grunt.
Some questions:
1. How can I run grunt from "anywhere" rather than where the gruntfile.js lives? I'm trying to integrate compiling less files as part of the build.
How do I specify the "home directory" for the .less files so I don't have to specify full paths to source and dest? In my case, home directory would be C:\Workspace\dev\Webs\RP\Content, and my less files: would be something like:
"rp1/less/rp1.css": "rp1/less/rp1.less" (there are several that need to be compiled).
Thanks in advance.
module.exports = function(grunt){
require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks);
grunt.initConfig({
less: {
options: {
paths: ["/c/Workspace/dev/Webs/RP/Content"]
},
files: {
"rp1/less/rp1.css": "rp1/less/rp1.less",
"rp1/less/ie9.css": "rp1/less/ie9.less",
"rp2/less/rp2.css": "rp2/less/rp2.less",
"rp3/less/rp3.css": "rp3/less/rp3.less",
"rp4/less/rp4.css": "rp4/less/rp4.less",
"rp4/less/ie9.css": "rp4/less/ie9.less",
"rp5/less/rp5.css": "rp5/less/rp5.less",
"rp5/less/ie9.css": "rp5/less/ie9.less"
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.registerTask('default', ['less']);
};
You should read up on how to use the files object in Grunt. Basically, you want a wildcard pattern, rather than having to specify each file individually. I think something like this might work:
files: [
{
expand: true,
src: ['**/*.less'],
},
]
Haven't tested though. You might need a 'dest' property also (either empty or just './') if it's not smart enough to figure it out on its own.
Also consider using gulp instead of grunt if you're just starting your project, I find the way it separates out the 'src' and 'dest' config rather than combining them into a 'files' object much more natural. Also I've worked with less and sass and have found the latter to be vastly superior (mostly because of the mixin libraries available, but it's also a more capable language in its own right), and it's pretty easy to move across from one to the other.

How can I have my source Less files be in a non-public folder and have CSS (their sourcemaps) be public?

I'm using Grunt (grunt-contrib-less) to process my Less files into CSS. This is working, but the sourcemaps are not.
My Less files are in /assets/less, and Grunt compiles them into /public/css.
/public/ is my folder that is publicly-viewable on the internet. I imagine there is a way to get sourcemaps to work even when my source Less files are not in the publicly-accessible folder. But I haven't found any examples.
My grunt-contrib-less config is:
options: {
compress: true,
yuicompress: true,
optimization: 2,
sourceMap: true,
sourceMapFilename: "public/css/main.css.map", //Write the source map to a separate file with the given filename.
sourceMapBasepath: "../assets/less", //Sets the base path for the Less file paths in the source map.
sourceMapRootpath: "/",//Adds this path onto the Less file paths in the source map.
},
files: [
{
expand: true, // Enable dynamic expansion.
cwd: 'assets/less/', // Src matches are relative to this path.
src: ['*.less'], // Actual pattern(s) to match.
dest: 'public/css/', // Destination path prefix.
ext: '.css', // Dest filepaths will have this extension.
extDot: 'first' // Extensions in filenames begin after the first dot
},
]
I wonder what you mean when you say "not working".
Notice that the source maps do not contain a copy of your original files. Source maps hold information about your original files, this information contains the filename (uri) and line number, something like that shown below:
Line: 1, column: 436 in generated code maps to:
{"source":"src/jquery.js","line":1966,"column":26,"name":"jQuery"}
Debug tools can use this information to show you the corresponding line of you in your original source files, which is impossible when the tool can not read this file.
So i expect that your source maps are correct, but worthless for your debug tool.
You should add the following options to your config:
options: {
sourceMap:true,
outputSourceFiles: true
}
See also: Does grunt-contrib-less support --source-map-map-inline?

Globbing file pattern

I am new to the Grunt Task Runer. I'm trying to do some file matching in one of my configurations. With this file matching, I need ignore all of my test files, except for one. The one test file that I need to keep is named 'basic.test.js' In an attempt to do this, I currently have the following configuration:
files: [
'src/**/*.js',
'!src/**/*.test.js',
'src/root/basic.test.js'
]
At this time, I am still getting ALL of my tests. This means that my tests in the other test files are still being seen. I'm trying to confirm if I'm doing my globbing pattern correctly. Does my globbing pattern look correct for my scenario?
Thank you!
If you only want one test then there is no need to match and then unmatch all the others. Just include the one test:
files: [
'src/root/basic.test.js'
]

How could I specify output file name based on input file name?

How could I specify output file name based on input file name?
I'm specifically trying to use grunt task (grunt-closure-tools or grunt-closure-compiler) to compile (minify) multiple javascript files, let's say all satisfying '/source/**/*.js' and want to output them in format $(original_file_path_without_extension).min.js
In all samples I've seen, the output is specified as single file only but I need to minify each file separately and into the same folder where the original file comes from.
Finally, I figured out the configuration. The trick is in building the files object dynamically (as described here). My configuration for grunt-closure-tools looks like this:
closureCompiler: {
options: {
// .. YOUR OPTIONS (ommited)
},
minify: {
files: [
{
expand: true,
src: ['source/**/*.js', '!source/**/*.min.js'],
ext: '.min.js'
}
]
}
}
Closure-compiler is designed to simultaneously compile all of your javascript into a single file to minimize requests. There are really only two use cases where separate output files are supported:
Multiple modules
In order to preserve renaming references, you'll have to compile your files simultaneously. The way to do that and maintain separate files is with modules. See How do I split my javascript into modules using Google's Closure Compiler?
Non-related Files
If your files don't have inter-dependencies, then you would simply run your grunt task multiple times - one for each file.

Resources