Skip to content

Commit

Permalink
refactor(karma-webpack): remove compiler legacy code (`compiler.plu…
Browse files Browse the repository at this point in the history
…gin`) (#342)
  • Loading branch information
ryanclark authored and michael-ciniawsky committed Sep 1, 2018
1 parent 08317c9 commit e68abc3
Showing 1 changed file with 15 additions and 50 deletions.
65 changes: 15 additions & 50 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,13 @@ function Plugin(
const applyPlugins = compiler.compilers || [compiler];

applyPlugins.forEach(function(compiler) {
if (compiler.hooks) {
compiler.hooks.thisCompilation.tap(this.plugin, (compilation, params) => {
compilation.dependencyFactories.set(
SingleEntryDependency,
params.normalModuleFactory
);
});
compiler.hooks.make.tapAsync(this.plugin, this.make.bind(this));
} else {
compiler.plugin('this-compilation', (compilation, params) => {
compilation.dependencyFactories.set(
SingleEntryDependency,
params.normalModuleFactory
);
});
compiler.plugin('make', this.make.bind(this));
}
compiler.hooks.thisCompilation.tap(this.plugin, (compilation, params) => {
compilation.dependencyFactories.set(
SingleEntryDependency,
params.normalModuleFactory
);
});
compiler.hooks.make.tapAsync(this.plugin, this.make.bind(this));
}, this);

function handler(callback) {
Expand All @@ -148,31 +138,9 @@ function Plugin(
}
}

let hooks = ['invalid', 'watch-run', 'run'];

if (compiler.hooks) {
hooks = [
{ method: 'sync', name: 'invalid' },
{ method: 'async', name: 'watchRun' },
{ method: 'async', name: 'run' },
];
}

hooks.forEach(function(hook) {
if (compiler.hooks) {
if (hook.method === 'sync') {
compiler.hooks[hook.name].tap(this.plugin, () => handler());
} else {
compiler.hooks[hook.name].tapAsync(this.plugin, (_, callback) =>
handler(callback)
);
}
} else {
compiler.plugin(hook, (_, callback) => {
handler(callback);
});
}
}, this);
compiler.hooks.invalid.tap(this.plugin, () => handler());
compiler.hooks.watchRun.tap(this.plugin, () => handler());
compiler.hooks.run.tapAsync(this.plugin, (_, callback) => handler(callback));

function done(stats) {
const applyStats = Array.isArray(stats.stats) ? stats.stats : [stats];
Expand Down Expand Up @@ -214,13 +182,8 @@ function Plugin(
}
}

if (compiler.hooks) {
compiler.hooks.done.tap(this.plugin, done.bind(this));
compiler.hooks.invalid.tap(this.plugin, invalid.bind(this));
} else {
compiler.plugin('done', done.bind(this));
compiler.plugin('invalid', invalid.bind(this));
}
compiler.hooks.done.tap(this.plugin, done.bind(this));
compiler.hooks.invalid.tap(this.plugin, invalid.bind(this));

webpackMiddlewareOptions.publicPath = path.join(
os.tmpdir(),
Expand Down Expand Up @@ -270,7 +233,9 @@ Plugin.prototype.make = function(compilation, callback) {
let entry = file;

if (this.wrapMocha) {
entry = `${require.resolve('./mocha-env-loader')}?name=${compilation.name}!${entry}`;
entry = `${require.resolve('./mocha-env-loader')}?name=${
compilation.name
}!${entry}`;
}

const dep = new SingleEntryDependency(entry);
Expand Down

0 comments on commit e68abc3

Please sign in to comment.