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 path = require('path');
|
||||||
const processCliArgs = require('karma/lib/cli').process;
|
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
|
* Start Karma. This prepends the Karma `files` config with all library files
|
||||||
* sorted in dependency order.
|
* sorted in dependency order.
|
||||||
@@ -22,15 +55,8 @@ function serve(config, manager, callback) {
|
|||||||
const server = new Server(config, exit);
|
const server = new Server(config, exit);
|
||||||
|
|
||||||
const files = server.get('config.files');
|
const files = server.get('config.files');
|
||||||
const dependencies = manager.getDependencies().map(script => script.path);
|
|
||||||
dependencies.reverse().forEach(filePath => {
|
let lookup = insertDependencies(manager, files);
|
||||||
files.unshift({
|
|
||||||
pattern: filePath,
|
|
||||||
included: true,
|
|
||||||
served: true,
|
|
||||||
watched: true
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// stop goog base.js from trying to load deps.js
|
// stop goog base.js from trying to load deps.js
|
||||||
files.unshift({
|
files.unshift({
|
||||||
@@ -40,6 +66,11 @@ function serve(config, manager, callback) {
|
|||||||
watched: false
|
watched: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
manager.on('update', () => {
|
||||||
|
lookup = insertDependencies(manager, files, lookup);
|
||||||
|
server.refreshFiles();
|
||||||
|
});
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ var path = require('path');
|
|||||||
module.exports = function(karma) {
|
module.exports = function(karma) {
|
||||||
karma.set({
|
karma.set({
|
||||||
frameworks: ['mocha'],
|
frameworks: ['mocha'],
|
||||||
|
client: {
|
||||||
|
runInParent: true
|
||||||
|
},
|
||||||
files: [
|
files: [
|
||||||
{
|
{
|
||||||
pattern: path.resolve(__dirname, require.resolve('jquery/dist/jquery.js')),
|
pattern: path.resolve(__dirname, require.resolve('jquery/dist/jquery.js')),
|
||||||
|
|||||||
Reference in New Issue
Block a user