I want to separate my application logic into multiple Javascript files for sanity and developer friendliness, stored in the /src folder. These files should be linted and concatenated into /dist/app.js during the build process. I am using grunt for my build process, as it already comes with handy lint and concat tasks.
+ |- grunt.js |- readme |-vendors |-backbone.js |- src |- core.js |- user.js |- dist |-app.js
I am running into an annoying problem. I use backbone.js for application structure, and most of my source files start by defining models by extending Backbone.Model. When linting these files, JSHint complains that Backbone is not defined, and rightly so - backbone resides outside in its own directory. Including all necessary scripts in the right order is something I assume is done in the html. Each individual source file should only know about itself.
I know that I can suppress these undefined warnings by setting lint's undef flag in grunt.js to false but I want to keep it set to true in order to be warned about other undefined variables in my application, as it is a common pointer to typos. Is there a clean way to tell grunt (or lint) what files to include prior to linting them? Am I doing something wrong with my build process, or with my application architecture? Or is this simply something I have to live with?
The jshint
options allow you to specify a list of globals that come from other libraries you are using, in your 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 at the bottom. This allows JSHint to ignore these variables, but still run your undef: true
setting (as shown above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With