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

fix(karma-webpack): correctly map entries to outputted assets (config.output) #348

Merged
merged 3 commits into from
Sep 3, 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
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