Stable hashes
I'm using script-loader to inject some Foundation JS on our bundles and noticed that if I change the path where the repository is, the contenthash changes, this completely broke our deployment pipeline as the assets are generated more than once for different distribution places and the hashes ended up being different.
Try it yourselves at https://github.com/gforcada/webpack-stable-hashes
The solution I found myself is move the import to the JS file rather than at the webpack configuration like this:
app.js
require('foundation-sites/path/to/file.js');
webpack.js
module.exports = {
entry: {
app: './app.js'
},
...
Might be something worth mentioning on the README? :thinking:
Our webpack configuration is quite complex and it took me a complete morning to pin it down to this specific lines (we have several lines like '!!script-loader!foundation-sites/dist/plugins/foundation.core.js', on our webpack configuration).
I have the same issue.
Added console.log to node_modules/webpack/lib/JavascriptModulesPlugin.js:
for (const m of chunk.modulesIterable) {
if (typeof m.source === "function") {
console.log(m.resource, m.hash);
console.log(m._source._value.length < 1000 ? m._source._value : '---');
hash.update(m.hash);
}
}
Got:
/home/*****/project/*****/file.js 8b5a75fd3bca4058217f2b28a03b9fd4
require("!!/home/*****/project/*****/node_modules/script-loader/addScript.js")(require("!!/home/*****/project/*****/node_modules/raw-loader/index.js!/home/*****/project/*****/file.js"))
/home/*****/project/*****/node_modules/script-loader/addScript.js 9748abffdacfb17ed677318848c99c10
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
module.exports = ...
/home/*****/project/*****/file.js 9f397e18825b8fb3229435f20033b121
--- (big file)
First generated module in the chunk contains absolute paths and brokes contenthash.
But both input and optput file.js are the same.
I've been reading docs today and checked the code of this loader and the open issues and it all finally seems to make sense (to some extent at least; see #56; although in my case the paths should not change between the builds, so I'm not sure if this is exactly same as my issue).
https://webpack.js.org/contribute/writing-a-loader/
Absolute Paths
Don't insert absolute paths into the module code as they break hashing when the root for the project is moved. There's a stringifyRequest method in loader-utils which can be used to convert an absolute path to a relative one.
https://github.com/webpack-contrib/script-loader/blob/master/index.js#L13-L15
In this loader, path.join(__dirname, ...) and require.resolve(...) are used which both return absolute paths. Changing those calls to return relative paths should fix this issue.
Ah well. There's been a PR to fix this for 2 years but not merged, and the loader is no longer supported: https://github.com/webpack-contrib/script-loader/pull/49#issuecomment-547527651 good timing to find all this out :)