Provide Karma with updated dependencies on change
This commit is contained in:
@@ -3,6 +3,39 @@ const closure = require('closure-util');
|
||||
const path = require('path');
|
||||
const processCliArgs = require('karma/lib/cli').process;
|
||||
|
||||
function insertDependencies(manager, files, previousLookup) {
|
||||
previousLookup = previousLookup || {};
|
||||
let firstIndex = NaN;
|
||||
const original = files.filter((obj, index) => {
|
||||
if (previousLookup[obj.pattern]) {
|
||||
if (isNaN(firstIndex)) {
|
||||
firstIndex = index;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (isNaN(firstIndex)) {
|
||||
firstIndex = 0;
|
||||
}
|
||||
const lookup = {};
|
||||
const dependencies = manager.getDependencies().map(script => {
|
||||
lookup[script.path] = true;
|
||||
return {
|
||||
pattern: script.path,
|
||||
included: true,
|
||||
served: true,
|
||||
watched: false
|
||||
};
|
||||
});
|
||||
original.splice.apply(original, [firstIndex, 0].concat(dependencies));
|
||||
files.length = 0;
|
||||
files.push.apply(files, original);
|
||||
|
||||
return lookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Karma. This prepends the Karma `files` config with all library files
|
||||
* sorted in dependency order.
|
||||
@@ -22,15 +55,8 @@ function serve(config, manager, callback) {
|
||||
const server = new Server(config, exit);
|
||||
|
||||
const files = server.get('config.files');
|
||||
const dependencies = manager.getDependencies().map(script => script.path);
|
||||
dependencies.reverse().forEach(filePath => {
|
||||
files.unshift({
|
||||
pattern: filePath,
|
||||
included: true,
|
||||
served: true,
|
||||
watched: true
|
||||
});
|
||||
});
|
||||
|
||||
let lookup = insertDependencies(manager, files);
|
||||
|
||||
// stop goog base.js from trying to load deps.js
|
||||
files.unshift({
|
||||
@@ -40,6 +66,11 @@ function serve(config, manager, callback) {
|
||||
watched: false
|
||||
});
|
||||
|
||||
manager.on('update', () => {
|
||||
lookup = insertDependencies(manager, files, lookup);
|
||||
server.refreshFiles();
|
||||
});
|
||||
|
||||
server.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@ var path = require('path');
|
||||
module.exports = function(karma) {
|
||||
karma.set({
|
||||
frameworks: ['mocha'],
|
||||
client: {
|
||||
runInParent: true
|
||||
},
|
||||
files: [
|
||||
{
|
||||
pattern: path.resolve(__dirname, require.resolve('jquery/dist/jquery.js')),
|
||||
|
||||
Reference in New Issue
Block a user