From c84a0249a3bdbc2c030dd77c0936b3256ecb58e1 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 02:11:02 +0100 Subject: [PATCH 1/3] fix(karma-webpack): fix issue with tests hanging due to filename config --- src/karma-webpack.js | 84 +++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index e65e344..2fea231 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.entryFilesMap = new Map(); + this.outputFilesMap = new Map(); this.plugin = { name: 'KarmaWebpack' }; let compiler; @@ -157,6 +159,20 @@ function Plugin( applyStats.forEach((stats) => { stats = stats.toJson(); + this.outputFilesMap.clear(); + + const assetKeys = Object.keys(stats.assetsByChunkName); + for (let i = 0; i < assetKeys.length; i++) { + const entryName = assetKeys[i]; + + if (this.entryFilesMap.has(entryName)) { + const entryPath = this.entryFilesMap.get(entryName); + const outputPath = stats.assetsByChunkName[entryName]; + + this.outputFilesMap.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.entryFilesMap.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.entryFilesMap.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 ); @@ -313,7 +334,11 @@ 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.outputFilesMap.get(file) + ) ); callback(null, fileContents); @@ -355,17 +380,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.outputFilesMap.get(filename); + file.path = path.join(basePath, outputPath); + + done(err, content && content.toString()); + }); }; } From 1f47c75560838310ddb7f33f668150a2cdfc581b Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 02:58:18 +0100 Subject: [PATCH 2/3] refactor(karma-webpack): code review comments --- src/karma-webpack.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/karma-webpack.js b/src/karma-webpack.js index 2fea231..bb94a91 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -111,8 +111,8 @@ function Plugin( this.files = []; this.basePath = basePath; this.waiting = []; - this.entryFilesMap = new Map(); - this.outputFilesMap = new Map(); + this.entries = new Map(); + this.outputs = new Map(); this.plugin = { name: 'KarmaWebpack' }; let compiler; @@ -159,17 +159,17 @@ function Plugin( applyStats.forEach((stats) => { stats = stats.toJson(); - this.outputFilesMap.clear(); + this.outputs.clear(); - const assetKeys = Object.keys(stats.assetsByChunkName); - for (let i = 0; i < assetKeys.length; i++) { - const entryName = assetKeys[i]; + const entries = Object.keys(stats.assetsByChunkName); + for (let i = 0; i < entries.length; i++) { + const entry = entries[i]; - if (this.entryFilesMap.has(entryName)) { - const entryPath = this.entryFilesMap.get(entryName); - const outputPath = stats.assetsByChunkName[entryName]; + if (this.entries.has(entry)) { + const entryPath = this.entries.get(entry); + const outputPath = stats.assetsByChunkName[entry]; - this.outputFilesMap.set(entryPath, outputPath); + this.outputs.set(entryPath, outputPath); } } @@ -253,7 +253,7 @@ Plugin.prototype.addFile = function(entry) { }; Plugin.prototype.make = function(compilation, callback) { - this.entryFilesMap.clear(); + this.entries.clear(); async.forEach( this.files.slice(), @@ -274,7 +274,7 @@ Plugin.prototype.make = function(compilation, callback) { path.basename(filename, path.extname(filename)) ); - this.entryFilesMap.set(name, 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 @@ -334,11 +334,7 @@ Plugin.prototype.readFile = function(file, callback) { } else { try { const fileContents = middleware.fileSystem.readFileSync( - path.join( - os.tmpdir(), - '_karma_webpack_', - this.outputFilesMap.get(file) - ) + path.join(os.tmpdir(), '_karma_webpack_', this.outputs.get(file)) ); callback(null, fileContents); @@ -387,7 +383,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) { throw err; } - const outputPath = webpackPlugin.outputFilesMap.get(filename); + const outputPath = webpackPlugin.outputs.get(filename); file.path = path.join(basePath, outputPath); done(err, content && content.toString()); From 560d7cf88add70d536d19c0eb3ae02c8830641ab Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 3 Sep 2018 02:59:39 +0100 Subject: [PATCH 3/3] fix(karma-webpack): change readFile logic for multi-compilers --- 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 bb94a91..e5c0d4e 100644 --- a/src/karma-webpack.js +++ b/src/karma-webpack.js @@ -308,7 +308,7 @@ Plugin.prototype.readFile = function(file, callback) { os.tmpdir(), '_karma_webpack_', String(idx), - file.replace(/\\/g, '/') + this.outputs.get(file) ), callback );