Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Stable hashes #60

Open
gforcada opened this issue Dec 7, 2018 · 3 comments
Open

Stable hashes #60

gforcada opened this issue Dec 7, 2018 · 3 comments

Comments

@gforcada
Copy link

gforcada commented Dec 7, 2018

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? 🤔

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).

@Rulexec
Copy link

Rulexec commented Jan 4, 2019

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.

@jakub-g
Copy link
Contributor

jakub-g commented Oct 31, 2019

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.

@jakub-g
Copy link
Contributor

jakub-g commented Oct 31, 2019

Ah well. There's been a PR to fix this for 2 years but not merged, and the loader is no longer supported:
#49 (comment) good timing to find all this out :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants