Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt.js - How to efficiently ignore (black list) node_modules folder?

Tags:

gruntjs

I was wondering why my jshint grunt task is so dead slow. Thats short example config:

var config = {
    jshint: {
        scripts: ['**/*.js', '!node_modules/**']
    },
    watch: {
        files: ['**/*.js', '!node_modules/**'],
        tasks: ['jshint']
    }
}

What does that pattern? If I have understood things correctly, both file patterns are using grunt api to build file list for task. That pattern works, it filters everything inside node_modules away but it does it extremely slowly because before applying filter, grunt digs whole node_modules directory recursively (~100 MB).

Is there any way to actually tell grunt not to even look at node_modules?

That example config takes about 30s on my laptop. If using whitelist pattern istead of blacklisting, jshint task takes just couple of seconds. But whitelisting means I have to be looking Gruntfile all the time if I do any refactoring etc which is super annoying.

Current while list pattern looks like this:

var allJSFiles = [
    '*.js',
    '{browser,server,config,models,routes,tasks,schema,test,utils,views}/**/*.js',
     '!browser/bower_components/**'
];
like image 260
Antti Pihlaja Avatar asked Jul 17 '14 12:07

Antti Pihlaja


2 Answers

Can't you just add your JS files to a new folder off root? That way you can "ignore" node_modules by not including it in the list.

Sample folder structure

- root
- node_modules
  - jshint
- src
  // your bespoke code

Grunt config

var config = {
  jshint: {
    scripts: ['src/**/*.js']
  },
  watch: {
    files: ['src/**/*.js'],
    tasks: ['jshint']
  }
}
like image 105
Andy Avatar answered Nov 03 '22 14:11

Andy


Like Andy already mentioned, I'd recommend a different file structure as well, keeping your code in a "src" or "public" directory for example.

The problem here is that your rule

'**/*.js'

always digs through all directories and only later excludes node_modules, which cannot be prevented at this point.

Not only because of this, but also to seperate code from other assets (images? documentation?), another structure should be the way to go.

like image 35
Xosofox Avatar answered Nov 03 '22 13:11

Xosofox