0

As part of our Grunt build we are transpiling some typescript code into a few separate files, and then concatenating the resulting javascript along with all the rest of our javascript code. Unfortunately I can't get the dynamic buildup of filenames to work along with grunt-concat.

This is the relevant snippet from our Gruntfile - see the part on generatedTypeScriptFiles, which doesn't work.

var jsfiles = [
    'js/external_libraries/inherit.js',
    'js/external_libraries/modernizr.js',
    'js/baz.js'
];


grunt.initConfig({
  gitinfo : {}, //will be populated with values from Git
  options : {
    "packageName"         : nconf.get("name"),
    "frameworkVersion"    : nconf.get("version"),
    "frameworkOutputPath" : nconf.get("frameworkOutputPath"),
    "workerOutputPath"    : nconf.get("workerOutputPath"),
    "sourceMapPath"       : nconf.get("sourceMapPath")
  },

  typescript : {
    foo: {
      src     : ['js/Foo/*.ts'],
      dest    : 'generated/Foo.js',
    },
    bar : {
      src     : ['js/Bar/*.ts'],
      dest    : 'generated/Bar.js',
    }
  },

  generatedTypeScriptFiles : {
    all : function () {
      var tsf = [];
      for (var key in this.typescript) {
        if(this.typescript[key].dest) {
          tsf.push(this.typescript[key].dest);
        }
      }
      return tsf;
    }()
  },

  scriptfiles : {
    hybrid : function () {
      return jsfiles.concat('<%= generatedTypeScriptFiles.all %>');
    }(),
    web    : function () {
      return jsfiles.concat('<%= generatedTypeScriptFiles.all %>');
    }()
  }

  concat : {
    web    : {
      options : {
        separator : ';',
      },
      src     : '<%= scriptfiles.web %>',
      dest    : '<%= options.frameworkOutputPath %>'
    }
  }
}

I am guessing that for our case where we actually know all the resulting filenames in our typescript build step, we could just build up the filenames beforehand - outside of grunt.initConfig. That should fix things, right? Or is there another way?

oligofren
  • 20,744
  • 16
  • 93
  • 180

1 Answers1

0

Maybe I misunderstand the problem or what you're trying to do, but why don't you just glob all .js files from the "generated" directory? Like this:

var jsfiles = [
    'js/external_libraries/inherit.js',
    'js/external_libraries/modernizr.js',
    'js/baz.js',
    'generated/*.js'
];
grunt.initConfig({
  typescript : {
    foo: {
      src     : ['js/Foo/*.ts'],
      dest    : 'generated/Foo.js',
    },
    bar : {
      src     : ['js/Bar/*.ts'],
      dest    : 'generated/Bar.js',
    }
  },
  concat : {
    web    : {
      options : {
        separator : ';',
      },
      src     : jsFiles,
      dest    : '<%= options.frameworkOutputPath %>'
    }
  }
});
Creynders
  • 4,573
  • 20
  • 21
  • That is absolutely a valid solution. On second thought, I guess what I was wondering was if there was a way of building up values during one step of a Grunt build and use those values in a later stage/step. That is a more general question than what I asked, so I guess the wording of my original question was somewhat unfortunate. – oligofren May 06 '14 at 14:41
  • Sure, you can store them in the config object (grunt.config.set) during one task and then retrieve them in another task. Here's more info: http://gruntjs.com/api/grunt.config – Creynders May 06 '14 at 14:45
  • After taking another look at your code, I assume what goes wrong is that `generatedTypeScriptFiles.all` isn't substituted, right? That's because the templating function isn't called, but you could do that manually: http://gruntjs.com/api/grunt.template – Creynders May 06 '14 at 14:47