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: allow for same filenames to be added #401

Merged
merged 1 commit into from
Mar 7, 2019
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
23 changes: 21 additions & 2 deletions lib/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +83,7 @@ function configToWebpackEntries(config) {

const webpackEntries = {};
filteredFiles.forEach((filePath) => {
webpackEntries[path.parse(filePath).name] = filePath;
webpackEntries[getPathKey(filePath)] = filePath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?
Can't we just use filePath as the attribute key?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the key that webpack uses internally... it does not like '/' in it...
also it is the url that get's displayed when you build... so if that is too long it's quite annoying 🙈

});

return webpackEntries;
Expand Down Expand Up @@ -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);
};
}
Expand Down