karma-runner / karma-commonjs

A Karma plugin. Test CommonJS modules.
MIT License
73 stars 30 forks source link

Add "iit" preprocessor and optimize file loading #1

Open vojtajina opened 11 years ago

vojtajina commented 11 years ago

Similar to https://github.com/karma-runner/karma-closure parse source files (probably using https://github.com/shtylman/node-required to build a dependency tree and optimize file loading (load only required files):

itsjamie commented 10 years ago

I'm actively working towards this, hopefully making it slightly easier to setup and less "work" and hopefully solve an issue I ran into. (Where the require worked when running in browserify, but failed when running in this context)

I find myself wondering about the API for Karma.

Is it possible for the framework to return an asynchronous callback or a promise for it's initialization function?

The approach I currently have is the config.preprocessor array would be filtered looking for 'commonjs' entries.

var initCommonJS = function(preprocessors, files) {
    _.filter(preprocessors, function filterCommonJS(frameworks, pattern) {
      if (frameworks.indexOf('commonjs') >= 0) {
        var matches = glob.sync(pattern);
        async.each(matches, function(filePath, cb) {  
          resolver(filePath).then(function(deps) {
            dependencies = _.union(dependencies, deps);
            cb(null);
          });
        }, function() {
          dependencies = _.unique(dependencies, 'filename');

          _.each(dependencies, function(dependency) {
            files.push({
              pattern: dependency.filename,
              included: true,
              served: true,
              watched: true
            });
          });

          // Include the file that resolves all the dependencies on the client.
          files.push({
            pattern: BRIDGE_FILE_PATH,
            included: true,
            served: true,
            watched: false
          });
        });
      }
    });
  }
};
initCommonJS.$inject = ['config.preprocessors', 'config.files'];

Dependencies resolver is a wrapper around node-required that flattens the recursive tree it builds, would probably be switched out for npm dep flatten-required async npm dep asnyc _ npm dep lodash

Issues currently:

browser: {
   chai: "path"
}