jshint grunt exported options - gruntjs

Hi I'm trying to achieve the following. I'm using grunt for jshint validating.
Somewhere in a file I have used:
var logger = function () {
// some ode
}
Because logger is never actually used jshint correctly shows me the following error.
W098: 'logger' is defined but never used.
I could set unused to false and it would work perfectly. But I actually want the option to take place in other files and warn me about unused variables. So the unused option is not gonna work for me.
I also saw that I could use a inline comment like this:
* exported EXPORTED_LIB */
But I would actually prefer to avoid cluttering my files with such comments. Is there any chance I can specify an exported options in my grunt file like I can for example for globals.
Heres the jshint part of my gruntfile:
jshint: {
// global options
options: {
camelcase: true,
curly: true,
eqeqeq: true,
forin: true,
immed: true,
indent: 4,
latedef: true,
newcap: true,
noarg: true,
nonew: true,
plusplus: false,
quotmark: 'single',
undef: true,
unused: true,
strict: true,
maxparams: 4,
maxdepth: 4,
trailing: true,
maxlen: 120,
browser: true,
node: true
},
server_logger: {
src: [BASE_PATH_SERVER_LOGGER, '/**/*.js'].join(''),
options: {
browser: false
}
},
client_logger: {
src: [BASE_PATH_CLIENT_LOGGER, '/**/*.js'].join(''),
options: {
node: false,
devel: true
}
}
}
Thanks for your time.
Best regards
Playerwtf
UPDATE: I made an issue on jshint github repository here

This was recently fixxed and works now as I would expect it.
github-issue
As an example I use it like this in my gruntfile
client_logger: {
expand: true,
cwd: BASE_PATH_CLIENT_LOGGER,
src: '**/*.js',
options: {
node: false,
devel: true,
globals: {
logger: true,
expect: true,
it: true,
describe: true,
beforeEach: true,
afterEach: true
},
exported: ['logger']
}
}
But the npm module was not yet updated. If you want this to work you will have to manually copy the newest version from the jshint github repository and replace the one in the current module or wait until it is updated.

i think you can exclude files in your src-files, so you could exclude your logger file from your basic linting (i suppose the logger file is logger.js here), and lint the logger file separatly with the unused-flag turned off.
read more about that here -> "! at the beginning of a pattern will negate the match"
you could set the cwd (and leave the join stuff). see more about that in the docs: Building the files object dynamically
jshint: {
// global options
options: {
... your global options here
},
server_logger: {
options: {
browser: false
},
files: [{
cwd: BASE_PATH_SERVER_LOGGER,
src: ['/**/*.js', '!logger.js']
}]
},
client_logger: {
options: {
node: false,
devel: true
},
files: [{
cwd: BASE_PATH_CLIENT_LOGGER,
src: ['/**/*.js', '!logger.js']
}]
},
lint_logger: {
options: {
unused: false
},
files: [{
src: ['logger.js']
}]
}
}
not 100% sure if that works, but i think it should at least lead you into the right direction. if you need to specify a path and not only a file for excluding you could put your logger-file in a separate folder an just exclude that folder!

Related

Create subtasks that override `options` but use task level `src`

My gruntfile thus far
uglify: {
src: [
...
],
dest: 'js/application.min.js',
options: {
'compress': {},
'reserveDOMCache': true,
'enclose': undefined,
'exportAll': false,
'expression': false,
'preserveComments': false,
'report': 'min',
'sourceMap': false,
'sourceMapIn': undefined,
'sourceMapIncludeSources': false,
'sourceMapName': undefined,
'wrap': undefined
},
development: {
options: {
'beautify': false,
'mangle': true
}
},
production: {
options: {
'beautify': true,
'mangle': false
}
}
}
However when I run the task uglify:development it will respond with No files created.
As far as I know this is not possible. You need to explicitly define a src for each target.
You could declare a variable outside of the config and add it to each target:
var mySources = ['file1.txt', 'file2.txt']; //declared outside config
development: {
src: mySources, //add variable to each target
Or you can declare a variable inside the config:
mySourcesInside: ['file1.txt'], //declared within config
development: {
src: '<%= mySourcesInside%>', //reference variable in each target
Alternatively you could use something like grunt-override-config https://github.com/masakura/grunt-override-config and declare just one uglify target and overrides for the options.

Single config files for all tasks

I'd like to make configuration of grunt tasks a bit easier. Currently I've got a lot of different configuration files, like a .csslintrc, jshintrc, bower.json and so on.
It would be really cool if I could concatenate all these configuration files into one single file. This configuration file could look something like
{
"csslint": {
"important": 1,
// ...
},
"jshint": {
//...
},
"bower": {
//...
}
}
My only solution so far would be using a preprocessor and simply insert the options in the tasks (I couldn't figure out how to insert options otherwise). But this doesn't seem to be a very beautiful way...
Most (all?) grunt tasks let you define options in the Gruntfile itself instead of their respective .*rc files.
So if you do that, you nearly get your one single configuration file for free.
For example:
jshint: {
dist: {
options: {
curly: true,
eqeqeq: true,
},
src: ['path/to/**/*.js']
}
},
csslint: {
dist: {
options: {
import: 2
},
src: ['path/to/**/*.css']
}
},
bower: {
install: {
options: {
targetDir: './lib',
layout: 'byType',
install: true,
verbose: false,
cleanTargetDir: false,
cleanBowerDir: false,
bowerOptions: {}
}
}
}

Grunt: SASS creates unwanted .map file

I've got grunt to successfully compile SASS to CSS. The problem I'm having is it's creating a file within the CSS directory called something like master.css.map.
I tried writing sourceMap: false, which I found as being the solution for a LESS setup within the options section, whereas it doesn't make a difference.
sass: {
build: {
options: {
compress: false,
sourceMap: false
},
files: [{
expand: true,
cwd: 'sass/',
src: ['*.scss'],
dest: 'css/',
ext: '.css'
}]
}
}
Do I need to declare something else or is there a dependency that can stop the .map file from being made?
You need to write like this:
options: {
compress: false,
sourcemap: 'none'
}
Here's a link for further clarification.
Hope this helps.

Maintaining folder structure while minifying using grunt

I have built an application using javascript. I have multiple .js files that are managed across multiple folders and sub-folders. I minified and uglified them using grunt-contrib-uglify. It does it pretty well. But my problem is that it iteratively goes through every .js file and minifies them but rather than maintaining folder structure,all of the minified js files are placed in a single destination folder.
For example:-
I have a JS folder.It has 5 sub-folders-
a,b,c,d,e
Each one is having 60-70 .js files . Now when I minify them, these all are minified and placed in a single JS-Min folder. But the folder structure is all gone.
Please suggest a way using which I can maintain the folder and sub-folder structure. I searched for it at grunt's official site,but to no solution.
My Gruntfile.js is as follows-
module.exports = function (grunt) {
grunt.initConfig({
requirejs_obfuscate: {
options: {
mangle: true,
dir: 'jsm',
salt: 'salt',
root: 'gruntfiles',
length: 6,
quotes: 'double',
concurrency: 2,
keepLinefeeds: false,
keepIndentations: false,
encodeStrings: true,
encodeNumbers: true,
moveStrings: true,
replaceNames: true,
variableExclusions: [ '^_get_', '^_set_', '^_mtd_' ],
}
},
jsObfuscate: {
test: {
options: {
concurrency: 2,
keepLinefeeds: false,
keepIndentations: false,
encodeStrings: true,
encodeNumbers: true,
moveStrings: true,
replaceNames: true,
variableExclusions: [ '^_get_', '^_set_', '^_mtd_' ]
},
files: {
src:'jsm/*.js' , // source files mask
dest:'jso/',
strings: true,
expand: true,
flatten: true,
ext: '.js'
}
}
},
uglify: {
mangle:{all:true},
squeeze: {dead_code:true},
codegen: {quote_keys: true},
files: {
cwd: 'js/',
src: ['*.js', '**/*.js'],
dest:'jsm/',
strings: true,
expand: true,
flatten: true
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-minified');
grunt.loadNpmTasks('grunt-encoding');
grunt.loadNpmTasks('grunt-requirejs-obfuscate');
grunt.loadNpmTasks('js-obfuscator');
grunt.registerTask('default', ['uglify','requirejs_obfuscate']);
};

Comma first validation with no space

I am building a JSHint set for a JavaScript suite that I am writing. My habit with writing objects is as follows:
var obj = {
hello: 'World'
,foo: 'Bar'
,name: 'Dave'
};
I use Grunt and grunt-contrib-jshint to run the validations with the following options:
browser: true
eqeqeq: false
laxcomma: true
white: true
indent: 2
When I run grunt jshint I get errors that a space is expected after the commas. How do I ignore this space?
You can ignore specific errors in the way written in the doc;
https://github.com/gruntjs/grunt-contrib-jshint
here is a quote
Ignoring specific warnings
If you would like to ignore a specific warning:
[L24:C9] W015: Expected '}' to have an indentation at 11 instead at 9.
You can toggle it by prepending - to the warning id as an option:
grunt.initConfig({
jshint: {
ignore_warning: {
options: {
'-W015': true,
},
src: ['**/*.js'],
},
},
});
Update.
Also, I am using such config and don't have comma errors
,defaultOptions = {
curly: true,
eqeqeq: true,
immed: true,
latedef: 'nofunc',
newcap: true,
noarg: true,
nonew: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
camelcase: true,
quotmark: true,
// lax
laxcomma: true,
// environment
browser: true,
devel: true,
jquery: true,
}

Resources