2

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.

polarblau
  • 17,649
  • 7
  • 63
  • 84

2 Answers2

1

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.

polarblau
  • 17,649
  • 7
  • 63
  • 84
  • I tried using your example, for stitching some common js files with grunt but I get this error when running the task: node_modules/stitch/node_modules/async/lib/async.js:22 if (called) throw new Error("Callback was already called."); Any idea? Thanks! – Nick Dima Feb 12 '13 at 15:01
  • I’m sorry, not really OOTB. Maybe you would like to open a separate question? – polarblau Feb 14 '13 at 07:10
  • For me the "Callback was already called" issue was due to a missing option in the Gruntfile, specifically the "dest" option. My gruntfile looks like the following for stitch: ''' stitch: { options: { paths: [__dirname + '/www/js/views', __dirname + '/www/js/models', __dirname + '/www/js/collections', __dirname + '/www/js/layouts'], dest: __dirname + '/www/js/package.js' } } ''' – JTE Apr 22 '13 at 03:46
0

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

fahad19
  • 96
  • 2