How can I resolve dependencies when linting single javascript files with grunt?

I want to split the application logic into several Javascript files for the convenience and friendliness of the developer, stored in the / src folder. These files must be lithified and combined in /dist/app.js during the build process. I use grunt for my build process, as it already comes with convenient lint and concat tasks.

     +
     | - grunt.js
     | - readme
     | -vendors
        | -backbone.js
     | - src
        | - core.js
        | - user.js
     | - dist
        | -app.js

I am facing an annoying problem. I use backbone.js to structure the application, and most of my source files start with defining models, extending Backbone.Model . When dragging and dropping these files, JSHint complains that the Backbone is undefined and correct, so the main trunk is outside in its own directory. By including all the necessary scripts in the correct order, I assume this is done in html. Each individual source file should know only about itself.

I know that I can suppress these undefined warnings by setting the lint undef flag in grunt.js false , but I want to keep it true to be warned about other undefined variables in my application, as this is a regular pointer to typos. Is there a clean way to tell grunts (or lint) which files to include before simulating them? Am I doing something wrong with my build process or application architecture? Or is it just what I need to live with?

+8
javascript jshint gruntjs
source share
2 answers

jshint parameters jshint you to specify a list of global variables that come from other libraries you use in the grunt.js file:

 jshint: { options: { curly: true, eqeqeq: true, immed: false, latedef: true, newcap: true, noarg: true, sub: true, undef: true, boss: true, eqnull: true, browser: true }, globals: { jQuery: true, Backbone: true, _: true } }, 

note the globals setting below. This allows JSHint to ignore these variables, but still configure undef: true (as shown above).

+11
source share

An alternative approach (which does not rely on grunt) is to add a jshint comment in your js files:

 /* global Backbone, jQuery, _ */ 
0
source share

All Articles