Skip to content

Commit

Permalink
fix(karma-webpack): correctly map entries to outputted assets (`c…
Browse files Browse the repository at this point in the history
…onfig.output`) (#348)
  • Loading branch information
ryanclark authored and michael-ciniawsky committed Sep 3, 2018
1 parent 2309401 commit d2f5a53
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ function Plugin(
this.files = []
this.basePath = basePath
this.waiting = []
this.entries = {}
this.outputs = {}

var compiler

Expand Down Expand Up @@ -119,11 +121,22 @@ function Plugin(
applyStats.forEach(function(stats) {
stats = stats.toJson()

this.outputs = {}
var entries = Object.keys(stats.assetsByChunkName)
for (let i = 0; i < entries.length; i++) {
var entry = entries[i]
if ({}.hasOwnProperty.call(this.entries, entry)) {
var entryPath = this.entries[entry]
var outputPath = stats.assetsByChunkName[entry]
this.outputs[entryPath] = outputPath
}
}

assets.push(...stats.assets)
if (stats.assets.length === 0) {
noAssets = true
}
})
}.bind(this))

if (!this.waiting || this.waiting.length === 0) {
this.notifyKarmaAboutChanges()
Expand Down Expand Up @@ -184,6 +197,8 @@ Plugin.prototype.addFile = function(entry) {
}

Plugin.prototype.make = function(compilation, callback) {
this.entries = {}

async.forEach(this.files.slice(), function(file, callback) {
var entry = file

Expand All @@ -193,7 +208,12 @@ Plugin.prototype.make = function(compilation, callback) {

var dep = new SingleEntryDependency(entry)

compilation.addEntry('', dep, path.relative(this.basePath, file).replace(/\\/g, '/'), function(err) {
var filename = path.relative(this.basePath, file).replace(/\\/g, '/')
var name = path.join(path.dirname(filename), path.basename(filename, path.extname(filename)))

this.entries[name] = filename

compilation.addEntry('', dep, name, function(err) {
// If the module fails because of an File not found error, remove the test file
if (dep.module && dep.module.error &&
dep.module.error.error &&
Expand All @@ -215,8 +235,8 @@ Plugin.prototype.readFile = function(file, callback) {
var doRead = function() {
if (optionsCount > 1) {
async.times(optionsCount, function(idx, callback) {
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), file.replace(/\\/g, '/')), callback)
}, function(err, contents) {
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 @@ -232,7 +252,7 @@ Plugin.prototype.readFile = function(file, callback) {
})
} else {
try {
var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', file.replace(/\\/g, '/')))
var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file]))

callback(undefined, fileContents)
} catch (e) {
Expand Down Expand Up @@ -266,12 +286,17 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
webpackPlugin.middleware.invalidate()
}

var filename = path.relative(basePath, file.originalPath)

// read blocks until bundle is done
webpackPlugin.readFile(path.relative(basePath, file.originalPath), function(err, content) {
webpackPlugin.readFile(filename, function(err, content) {
if (err) {
throw err
}

var outputPath = webpackPlugin.outputs[filename]
file.path = path.join(basePath, outputPath)

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

0 comments on commit d2f5a53

Please sign in to comment.