Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(karma-webpack): remove compiler legacy code (compiler.plugin) #342

Merged
merged 1 commit into from
Sep 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try with using compilation instead of thisCompilation, the latter is an alias if I remember right and will be removed in a feature version of webpack. But it's not important to do this now, I just saw it, so in case it bugs for whatever reason don't bother :)

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=${
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems prettier wanted to change this line on this commit for some reason..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

compilation.name
}!${entry}`;
}

const dep = new SingleEntryDependency(entry);
Expand Down