Skip to content

Commit

Permalink
fix(karma-webpack): handle outputPath correctly (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
alabbas-ali authored and matthieu-foucault committed Nov 22, 2018
1 parent 670f153 commit 6d6a69a
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ var isBlocked = false

const normalize = (file) => file.replace(/\\/g, '/')

const getOutputPath = (outputPath) => {
for (var i = 0; i < outputPath.length; i++) {
if (
outputPath[i].indexOf(".js") !== -1 &&
outputPath[i].indexOf(".js.map") === -1
) {
return outputPath[i];
}
}
return null;
}

var escapeRegExp = function(str) {
// See details here https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
Expand Down Expand Up @@ -130,7 +142,12 @@ function Plugin(
if ({}.hasOwnProperty.call(this.entries, entry)) {
var entryPath = this.entries[entry]
var outputPath = stats.assetsByChunkName[entry]
this.outputs[entryPath] = outputPath
if (Array.isArray(outputPath)) {
outputPath = getOutputPath(outputPath)
}
if (outputPath !== null) {
this.outputs[entryPath] = outputPath
}
}
}

Expand Down Expand Up @@ -237,11 +254,7 @@ Plugin.prototype.readFile = function(file, callback) {
var doRead = function() {
if (optionsCount > 1) {
async.times(optionsCount, function(idx, callback) {
if (Array.isArray(this.outputs[file])) {
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file][0]), callback);
} else {
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file]), callback);
}
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file]), callback)
}.bind(this), function(err, contents) {
if (err) {
return callback(err)
Expand All @@ -258,12 +271,7 @@ Plugin.prototype.readFile = function(file, callback) {
})
} else {
try {
var fileContents = ''
if (Array.isArray(this.outputs[file])) {
fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file][0]));
} else {
fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file]));
}
var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file]))

callback(undefined, fileContents)
} catch (e) {
Expand Down Expand Up @@ -306,11 +314,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
}

var outputPath = webpackPlugin.outputs[normalize(filename)]
if( Array.isArray(outputPath)){
file.path = normalize(path.join(basePath, outputPath[0]));
} else {
file.path = normalize(path.join(basePath, outputPath));
}
file.path = normalize(path.join(basePath, outputPath))

done(err, content && content.toString())
})
Expand Down

0 comments on commit 6d6a69a

Please sign in to comment.