From ab4dde9a7b859e815e11925a0f5cc5c986360264 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 03:24:33 +0100 Subject: [PATCH] fix(karma-webpack): correctly map `entries` to outputted `assets` (`config.output`) (#347) --- src/karma-webpack.js | 82 ++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index e65e344..e5c0d4e 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -90,11 +90,11 @@ function Plugin( } if (!webpackOptions.output.filename) { - webpackOptions.output.filename = '[name].js' + webpackOptions.output.filename = '[name].js'; } if (!webpackOptions.output.chunkFilename) { - webpackOptions.output.chunkFilename = '[id].bundle.js' + webpackOptions.output.chunkFilename = '[id].bundle.js'; } // For webpack 4+, optimization.splitChunks and optimization.runtimeChunk must be false. @@ -111,6 +111,8 @@ function Plugin( this.files = []; this.basePath = basePath; this.waiting = []; + this.entries = new Map(); + this.outputs = new Map(); this.plugin = { name: 'KarmaWebpack' }; let compiler; @@ -157,6 +159,20 @@ function Plugin( applyStats.forEach((stats) => { stats = stats.toJson(); + this.outputs.clear(); + + const entries = Object.keys(stats.assetsByChunkName); + for (let i = 0; i < entries.length; i++) { + const entry = entries[i]; + + if (this.entries.has(entry)) { + const entryPath = this.entries.get(entry); + const outputPath = stats.assetsByChunkName[entry]; + + this.outputs.set(entryPath, outputPath); + } + } + assets.push(...stats.assets); if (stats.assets.length === 0) { noAssets = true; @@ -237,6 +253,8 @@ Plugin.prototype.addFile = function(entry) { }; Plugin.prototype.make = function(compilation, callback) { + this.entries.clear(); + async.forEach( this.files.slice(), (file, callback) => { @@ -250,24 +268,27 @@ Plugin.prototype.make = function(compilation, callback) { const dep = new SingleEntryDependency(entry); - compilation.addEntry( - '', - dep, - path.relative(this.basePath, file).replace(/\\/g, '/'), - (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 && - dep.module.error.error.code === 'ENOENT' - ) { - this.files = this.files.filter((f) => file !== f); - invalidate(this.middleware); - } - callback(err); - } + const filename = path.relative(this.basePath, file).replace(/\\/g, '/'); + const name = path.join( + path.dirname(filename), + path.basename(filename, path.extname(filename)) ); + + this.entries.set(name, filename); + + compilation.addEntry('', dep, name, (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 && + dep.module.error.error.code === 'ENOENT' + ) { + this.files = this.files.filter((f) => file !== f); + invalidate(this.middleware); + } + callback(err); + }); }, callback ); @@ -287,7 +308,7 @@ Plugin.prototype.readFile = function(file, callback) { os.tmpdir(), '_karma_webpack_', String(idx), - file.replace(/\\/g, '/') + this.outputs.get(file) ), callback ); @@ -313,7 +334,7 @@ Plugin.prototype.readFile = function(file, callback) { } else { try { const fileContents = middleware.fileSystem.readFileSync( - path.join(os.tmpdir(), '_karma_webpack_', file.replace(/\\/g, '/')) + path.join(os.tmpdir(), '_karma_webpack_', this.outputs.get(file)) ); callback(null, fileContents); @@ -355,17 +376,18 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) { invalidate(webpackPlugin.middleware); } + const filename = path.relative(basePath, file.originalPath); // read blocks until bundle is done - webpackPlugin.readFile( - path.relative(basePath, file.originalPath), - (err, content) => { - if (err) { - throw err; - } - - done(err, content && content.toString()); + webpackPlugin.readFile(filename, (err, content) => { + if (err) { + throw err; } - ); + + const outputPath = webpackPlugin.outputs.get(filename); + file.path = path.join(basePath, outputPath); + + done(err, content && content.toString()); + }); }; }