Just started playing with Grunt and would like to use Stitch to combine my modules for the use on the client side. I don’t have much experience with commonJS in general and both libaries are new to me — so please bear with me.
Here's an excerpt from my Grunt file:
var fs = require('fs');
var stitch = require('stitch');
module.exports = function(grunt) {
grunt.initConfig({
… SNIP …
stitch: {
dist: {
src: ['lib'],
dest: 'dist/<%= pkg.name %>.js'
}
}
});
grunt.registerMultiTask('stitch', 'Compile common.js modules with stitch', function() {
var done = this.async();
var paths = this.file.src.map(function(dir){
return __dirname + '/' + dir;
});
stitch.createPackage({ paths: paths }).compile(function(error, src){
if (error) {
return grunt.log.writeln(error);
}
file.write(name, src);
grunt.log.writeln("File '" + name + "' created.");
done();
});
});
};
When running grunt stitch
however, I'm getting:
ReferenceError: can't compile /path/to/file.js
file is not defined
The absolute path is correct, though, and the file does exist.
Any advice would be appreciated.
It turns out that a reference within one of the files I’m trying to combine was incorrect.
This question should probably be closed as too localized, but in the unlikely case someone might have the same problem — check all require
calls and see if they point to the correct file. Remember that the path will be relative to the root not the requiring file.
However, this error message in particular is very cryptic and basically useless IMO.
given the compiler's nature is asynchronous for stitch, looping through the files with grunt.util.async.forEachSeries
solved the issue for me: https://github.com/fahad19/grunt-stitch-extra/blob/master/tasks/stitch_extra.js#L43
The grunt plugin can be found here: https://github.com/fahad19/grunt-stitch-extra