From 277150f551f7a37e8c0c01582b6655c96a255728 Mon Sep 17 00:00:00 2001 From: Thomas Allmer Date: Sat, 2 Mar 2019 01:02:32 +0100 Subject: [PATCH] fix: allow for same filenames to be added --- lib/karma-webpack.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/karma-webpack.js b/lib/karma-webpack.js index 44e016f..05a35b2 100644 --- a/lib/karma-webpack.js +++ b/lib/karma-webpack.js @@ -37,6 +37,24 @@ function registerExtraWebpackFiles(config, _controller) { }); } +/** + * Simple hash function by bryc + * https://gist.github.com/iperelivskiy/4110988#gistcomment-2697447 + */ +function hash(s) { + let h = 0xdeadbeef; + for (let i = 0; i < s.length; i++) { + h = Math.imul(h ^ s.charCodeAt(i), 2654435761); // eslint-disable-line no-bitwise + } + return (h ^ (h >>> 16)) >>> 0; // eslint-disable-line no-bitwise +} + +function getPathKey(filePath, withExtension = false) { + const pathParts = path.parse(filePath); + const key = `${pathParts.name}.${hash(filePath)}`; + return withExtension ? `${key}${pathParts.ext}` : key; +} + function configToWebpackEntries(config) { const filteredPreprocessorsPatterns = []; const { preprocessors } = config; @@ -65,7 +83,7 @@ function configToWebpackEntries(config) { const webpackEntries = {}; filteredFiles.forEach((filePath) => { - webpackEntries[path.parse(filePath).name] = filePath; + webpackEntries[getPathKey(filePath)] = filePath; }); return webpackEntries; @@ -97,7 +115,8 @@ function preprocessorFactory(config, emitter) { file.path = normalize(transformPath(file.path)); // eslint-disable-line no-param-reassign - const bundleContent = controller.bundlesContent[path.parse(file.path).base]; + const bundleContent = + controller.bundlesContent[getPathKey(file.path, true)]; done(null, bundleContent); }; }