Skip to content

Commit cad9f8b

Browse files
Silviu-Marianjoshwiens
authored andcommitted
fix: readFile on windows (#226)
1 parent 4216f13 commit cad9f8b

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/karma-webpack.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function Plugin(
1616
/* config.files */ files,
1717
/* config.frameworks */ frameworks,
1818
customFileHandlers,
19-
emitter) {
19+
emitter
20+
) {
2021
webpackOptions = _.clone(webpackOptions) || {}
2122
webpackMiddlewareOptions = _.clone(webpackMiddlewareOptions || webpackServerOptions) || {}
2223

@@ -193,7 +194,7 @@ Plugin.prototype.readFile = function(file, callback) {
193194
var middleware = this.middleware
194195
var optionsCount = this.optionsCount
195196

196-
function doRead() {
197+
var doRead = function() {
197198
if (optionsCount > 1) {
198199
async.times(optionsCount, function(idx, callback) {
199200
middleware.fileSystem.readFile('/_karma_webpack_/' + idx + '/' + file.replace(/\\/g, '/'), callback)
@@ -212,20 +213,27 @@ Plugin.prototype.readFile = function(file, callback) {
212213
callback(null, Buffer.concat(contents))
213214
})
214215
} else {
215-
middleware.fileSystem.readFile('/_karma_webpack_/' + file.replace(/\\/g, '/'), callback)
216-
}
217-
}
218-
if (!this.waiting) {
219-
try {
220-
doRead()
221-
} catch (e) {
222-
// If this is an error from `readFileSync` method, wait for the next tick. Credit #69 @mewdriller
223-
if (e.message.substring(0, 20) === "Path doesn't exist '") { // eslint-disable-line quotes
224-
this.waiting = [process.nextTick.bind(process, this.readFile.bind(this, file, callback))]
225-
} else {
226-
throw e
216+
try {
217+
var fileContents = middleware.fileSystem.readFileSync('/_karma_webpack_/' + file.replace(/\\/g, '/'))
218+
219+
callback(undefined, fileContents)
220+
} catch (e) {
221+
// If this is an error from `readFileSync` method, wait for the next tick.
222+
// Credit #69 @mewdriller
223+
if (e.code === 'ENOENT') {
224+
// eslint-disable-line quotes
225+
this.waiting = [process.nextTick.bind(process, this.readFile.bind(this, file, callback))]
226+
227+
// throw otherwise
228+
} else {
229+
callback(e)
230+
}
227231
}
228232
}
233+
}.bind(this)
234+
235+
if (!this.waiting) {
236+
doRead()
229237
} else {
230238
// Retry to read once a build is finished
231239
// do it on process.nextTick to catch changes while building

0 commit comments

Comments
 (0)