From bef6e42b360e883648e0dd8fd392df97139b6db2 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 02:54:15 +0100 Subject: [PATCH 1/3] fix(karma-webpack): correctly map `entries` to outputted `assets` (`config.output`) --- src/karma-webpack.js | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index 20cc3323..8565c97a 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -79,6 +79,8 @@ function Plugin( this.files = [] this.basePath = basePath this.waiting = [] + this.entryFilesMap = {} + this.outputFilesMap = {} var compiler @@ -119,11 +121,22 @@ function Plugin( applyStats.forEach(function(stats) { stats = stats.toJson() + this.outputFilesMap = {} + var assetKeys = Object.keys(stats.assetsByChunkName) + for (let i = 0; i < assetKeys.length; i++) { + var entryName = assetKeys[i] + if (Object.prototype.hasOwnProperty.call(this.entryFilesMap, entryName)) { + var entryPath = this.entryFilesMap[entryName] + var outputPath = stats.assetsByChunkName[entryName] + this.outputFilesMap[entryPath] = outputPath + } + } + assets.push(...stats.assets) if (stats.assets.length === 0) { noAssets = true } - }) + }.bind(this)) if (!this.waiting || this.waiting.length === 0) { this.notifyKarmaAboutChanges() @@ -184,6 +197,8 @@ Plugin.prototype.addFile = function(entry) { } Plugin.prototype.make = function(compilation, callback) { + this.entryFilesMap = {} + async.forEach(this.files.slice(), function(file, callback) { var entry = file @@ -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.entryFilesMap[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 && @@ -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.outputFilesMap[file]), callback) + }.bind(this), function(err, contents) { if (err) { return callback(err) }; @@ -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.outputFilesMap[file])) callback(undefined, fileContents) } catch (e) { @@ -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.outputFilesMap[filename] + file.path = path.join(basePath, outputPath) + done(err, content && content.toString()) }) } From 483f04197b674f97c9a09891aa7423533265ba85 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 03:00:43 +0100 Subject: [PATCH 2/3] refactor(karma-webpack): code review comments --- src/karma-webpack.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index 8565c97a..c176d419 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -79,8 +79,8 @@ function Plugin( this.files = [] this.basePath = basePath this.waiting = [] - this.entryFilesMap = {} - this.outputFilesMap = {} + this.entries = {} + this.outputs = {} var compiler @@ -121,14 +121,14 @@ function Plugin( applyStats.forEach(function(stats) { stats = stats.toJson() - this.outputFilesMap = {} - var assetKeys = Object.keys(stats.assetsByChunkName) - for (let i = 0; i < assetKeys.length; i++) { - var entryName = assetKeys[i] - if (Object.prototype.hasOwnProperty.call(this.entryFilesMap, entryName)) { - var entryPath = this.entryFilesMap[entryName] - var outputPath = stats.assetsByChunkName[entryName] - this.outputFilesMap[entryPath] = outputPath + this.outputs = {} + var entries = Object.keys(stats.assetsByChunkName) + for (let i = 0; i < entries.length; i++) { + var entry = entries[i] + if (Object.prototype.hasOwnProperty.call(this.entries, entry)) { + var entryPath = this.entries[entry] + var outputPath = stats.assetsByChunkName[entry] + this.outputs[entryPath] = outputPath } } @@ -197,7 +197,7 @@ Plugin.prototype.addFile = function(entry) { } Plugin.prototype.make = function(compilation, callback) { - this.entryFilesMap = {} + this.entries = {} async.forEach(this.files.slice(), function(file, callback) { var entry = file @@ -211,7 +211,7 @@ Plugin.prototype.make = function(compilation, callback) { var filename = path.relative(this.basePath, file).replace(/\\/g, '/') var name = path.join(path.dirname(filename), path.basename(filename, path.extname(filename))) - this.entryFilesMap[name] = 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 @@ -235,7 +235,7 @@ 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), this.outputFilesMap[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) @@ -252,7 +252,7 @@ Plugin.prototype.readFile = function(file, callback) { }) } else { try { - var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputFilesMap[file])) + var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file])) callback(undefined, fileContents) } catch (e) { @@ -294,7 +294,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) { throw err } - var outputPath = webpackPlugin.outputFilesMap[filename] + var outputPath = webpackPlugin.outputs[filename] file.path = path.join(basePath, outputPath) done(err, content && content.toString()) From fd1f4495f144a480f2351178a88d6e8a7bc2e4f3 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 03:02:48 +0100 Subject: [PATCH 3/3] refactor(karma-webpack): change Object.prototype to {} --- src/karma-webpack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index c176d419..f3930a52 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -125,7 +125,7 @@ function Plugin( var entries = Object.keys(stats.assetsByChunkName) for (let i = 0; i < entries.length; i++) { var entry = entries[i] - if (Object.prototype.hasOwnProperty.call(this.entries, entry)) { + if ({}.hasOwnProperty.call(this.entries, entry)) { var entryPath = this.entries[entry] var outputPath = stats.assetsByChunkName[entry] this.outputs[entryPath] = outputPath