Skip to content

Commit 395eab4

Browse files
ryanclarkmichael-ciniawsky
authored andcommitted
refactor(karma-webpack): upgrade plugin system (tapable)
BREAKING CHANGE: requires `webpack >= v4.0.0`
1 parent 1ac16ea commit 395eab4

File tree

3 files changed

+176
-47
lines changed

3 files changed

+176
-47
lines changed

package-lock.json

+104-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Use webpack with karma",
66
"license": "MIT",
77
"engines": {
8-
"node": ">= 4"
8+
"node": ">=6.11.5"
99
},
1010
"main": "lib",
1111
"files": [
@@ -33,7 +33,7 @@
3333
"loader-utils": "^1.0.0",
3434
"lodash": "^4.0.0",
3535
"source-map": "^0.5.6",
36-
"webpack-dev-middleware": "^2.0.6"
36+
"webpack-dev-middleware": "^3.0.1"
3737
},
3838
"devDependencies": {
3939
"babel-cli": "^6.0.0",
@@ -55,7 +55,7 @@
5555
"karma-spec-reporter": "^0.0.32",
5656
"mocha": "^4.0.0",
5757
"standard-version": "^4.0.0",
58-
"webpack": "^4.0.0"
58+
"webpack": "^4.1.1"
5959
},
6060
"bugs": "https://github.com/webpack-contrib/karma-webpack/issues",
6161
"repository": "https://github.com/webpack-contrib/karma-webpack.git",

src/karma-webpack.js

+69-18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ var escapeRegExp = function(str) {
2020
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
2121
}
2222

23+
function invalidate(middleware) {
24+
if (middleware.context.watching) {
25+
return middleware.context.watching.invalidate()
26+
}
27+
28+
return middleware.invalidate()
29+
}
30+
2331
function Plugin(
2432
/* config.webpack */ webpackOptions,
2533
/* config.webpackServer */ webpackServerOptions,
@@ -75,6 +83,7 @@ function Plugin(
7583
this.files = []
7684
this.basePath = basePath
7785
this.waiting = []
86+
this.plugin = {name: 'KarmaWebpack'}
7887

7988
var compiler
8089

@@ -91,23 +100,56 @@ function Plugin(
91100
var applyPlugins = compiler.compilers || [compiler]
92101

93102
applyPlugins.forEach(function(compiler) {
94-
compiler.plugin('this-compilation', function(compilation, params) {
95-
compilation.dependencyFactories.set(SingleEntryDependency, params.normalModuleFactory)
96-
})
97-
compiler.plugin('make', this.make.bind(this))
98-
}, this);
103+
if (compiler.hooks) {
104+
compiler.hooks
105+
.thisCompilation
106+
.tap(this.plugin, (compilation, params) => {
107+
compilation.dependencyFactories.set(SingleEntryDependency, params.normalModuleFactory)
108+
})
109+
compiler.hooks
110+
.make
111+
.tapAsync(this.plugin, this.make.bind(this))
112+
} else {
113+
compiler.plugin('this-compilation', function(compilation, params) {
114+
compilation.dependencyFactories.set(SingleEntryDependency, params.normalModuleFactory)
115+
})
116+
compiler.plugin('make', this.make.bind(this))
117+
}
118+
}, this)
119+
120+
function handler(callback) {
121+
isBlocked = true
122+
123+
if (typeof callback === 'function') {
124+
callback(null)
125+
}
126+
}
99127

100-
['invalid', 'watch-run', 'run'].forEach(function(name) {
101-
compiler.plugin(name, function(_, callback) {
102-
isBlocked = true
128+
var hooks = ['invalid', 'watch-run', 'run']
103129

104-
if (typeof callback === 'function') {
105-
callback()
130+
if (compiler.hooks) {
131+
hooks = [
132+
{method: 'sync', name: 'invalid'},
133+
{method: 'async', name: 'watchRun'},
134+
{method: 'async', name: 'run'}
135+
]
136+
}
137+
138+
hooks.forEach(function(hook) {
139+
if (compiler.hooks) {
140+
if (hook.method === 'sync') {
141+
compiler.hooks[hook.name].tap(this.plugin, () => handler())
142+
} else {
143+
compiler.hooks[hook.name].tapAsync(this.plugin, (_, callback) => handler(callback))
106144
}
107-
})
108-
})
145+
} else {
146+
compiler.plugin(hook, function(_, callback) {
147+
handler(callback)
148+
})
149+
}
150+
}, this)
109151

110-
compiler.plugin('done', function(stats) {
152+
function done(stats) {
111153
var applyStats = Array.isArray(stats.stats) ? stats.stats : [stats]
112154
var assets = []
113155
var noAssets = false
@@ -139,12 +181,21 @@ function Plugin(
139181
blocked[i]()
140182
}
141183
blocked = []
142-
}.bind(this))
143-
compiler.plugin('invalid', function() {
184+
}
185+
186+
function invalid() {
144187
if (!this.waiting) {
145188
this.waiting = []
146189
}
147-
}.bind(this))
190+
}
191+
192+
if (compiler.hooks) {
193+
compiler.hooks.done.tap(this.plugin, done.bind(this))
194+
compiler.hooks.invalid.tap(this.plugin, invalid.bind(this))
195+
} else {
196+
compiler.plugin('done', done.bind(this))
197+
compiler.plugin('invalid', invalid.bind(this))
198+
}
148199

149200
webpackMiddlewareOptions.publicPath = path.join(os.tmpdir(), '_karma_webpack_', '/')
150201
var middleware = this.middleware = new webpackDevMiddleware(compiler, webpackMiddlewareOptions)
@@ -197,7 +248,7 @@ Plugin.prototype.make = function(compilation, callback) {
197248
this.files = this.files.filter(function(f) {
198249
return file !== f
199250
})
200-
this.middleware.invalidate()
251+
invalidate(this.middleware)
201252
}
202253
callback(err)
203254
}.bind(this))
@@ -259,7 +310,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
259310
return function(content, file, done) {
260311
if (webpackPlugin.addFile(file.originalPath)) {
261312
// recompile as we have an asset that we have not seen before
262-
webpackPlugin.middleware.invalidate()
313+
invalidate(webpackPlugin.middleware)
263314
}
264315

265316
// read blocks until bundle is done

0 commit comments

Comments
 (0)