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

chunk hash is changed by manualChunks even if not modify any module #4394

Closed
CaptainLiao opened this issue Feb 9, 2022 · 6 comments · Fixed by #4397
Closed

chunk hash is changed by manualChunks even if not modify any module #4394

CaptainLiao opened this issue Feb 9, 2022 · 6 comments · Fixed by #4397

Comments

@CaptainLiao
Copy link

CaptainLiao commented Feb 9, 2022

rollup: v14.16.1
platform: window10
node: v14.16.1

I use rollup to build my webapp,and create many named chunks by manualChunks.

question is: every time i run rollup build command, all chunks's hash would be changed, even if not modify any module/content, just rerun build command!

I find the source code:

    assignManualChunks(getManualChunk) {
        ......
        for (const module of this.graph.modulesById.values()) {
            if (module instanceof Module) {
                const manualChunkAlias = getManualChunk(module.id, manualChunksApi);
                if (typeof manualChunkAlias === 'string') {
                    addModuleToManualChunk(manualChunkAlias, module, manualChunkAliasByEntry);
                }
            }
        }
        return manualChunkAliasByEntry;
    }

and make some change (just sort this.graph.modulesById.values() by module id):

    assignManualChunks(getManualChunk) {
        ......
        const getCharSize = str => [...str].map(i => i.charCodeAt()).reduce((sum, i) => sum + i, 0)
        const sortedMoules = [...this.graph.modulesById.values()].sort((a, b) => getCharSize(a.id) - getCharSize(b.id))
        for (const module of sortedMoules) {
           ......
        }
        return manualChunkAliasByEntry;
    }

everything is ok. the change is right?

I guess this.graph.modulesById.values() has not fixed sequence, so same chunk have different module sequence in every build, right?

I find there has orderedModule, but this.graph.modulesById.values() is not sorted, this can be fixed? thx.

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Feb 10, 2022

rollup: v14.16.1
platform: window10
node: v14.16.1

@CaptainLiao which version of rollup are you using? there was a fix in 2.67.1 to make the chunks build more deterministic which might fix your issue.

@CaptainLiao
Copy link
Author

CaptainLiao commented Feb 10, 2022

@dnalborczyk
Thank you reply~

my rollup version is 2.61.1. sorry for my careless.

later I update rollup v2.67.1, but get same result——chunks's hash also be changed.

first build:

......
dist/static/js/bjuniversalstudios-b24b1af3.js                 69.74 KiB / gzip: 34.63 KiB
dist/static/js/swiper-fc8ca029.js                             122.62 KiB / gzip: 33.25 KiB
dist/static/css/vender-6dc981a5.css                           90.16 KiB / gzip: 34.74 KiB
dist/static/js/promotion/pages/2022traveldes-c8806a11.js      246.64 KiB / gzip: 68.38 KiB
dist/static/js/vender-f755dc6a.js                             299.76 KiB / gzip: 108.17 KiB

second build:

....
dist/static/js/bjuniversalstudios-655dbd3b.js                 69.74 KiB / gzip: 34.62 KiB
dist/static/js/swiper-fc8ca029.js                             122.62 KiB / gzip: 33.25 KiB
dist/static/css/vender-6dc981a5.css                           90.16 KiB / gzip: 34.74 KiB
dist/static/js/promotion/pages/2022traveldes-6ca9070c.js      246.64 KiB / gzip: 68.38 KiB
dist/static/js/vender-f755dc6a.js                             299.76 KiB / gzip: 108.17 KiB

Everything are same , just run build command twice. as you see, chunks's hash has be changed except third packages.

@lukastaegert
Copy link
Member

lukastaegert commented Feb 10, 2022

I think I understand the issue. When using a manual chunks function, the order in which the chunks is generated is arbitrary. While this should not influence hashes too much, it can actually have an effect because the chunk order can also influence other things (the current algorithm is far from perfect). I would propose a slightly different fix, explicitly sort just the aliased modules (and not all modules, which can be quite a lot) by alias:

private assignManualChunks(getManualChunk: GetManualChunk): Map<Module, string> {
	const manualChunkAliasesWithEntry: [alias: string, module: Module][] = [];
	const manualChunksApi = {
		getModuleIds: () => this.graph.modulesById.keys(),
		getModuleInfo: this.graph.getModuleInfo
	};
	for (const module of this.graph.modulesById.values()) {
		if (module instanceof Module) {
			const manualChunkAlias = getManualChunk(module.id, manualChunksApi);
			if (typeof manualChunkAlias === 'string') {
				manualChunkAliasesWithEntry.push([manualChunkAlias, module]);
			}
		}
	}
	manualChunkAliasesWithEntry.sort(([aliasA], [aliasB]) =>
		aliasA > aliasB ? 1 : aliasA < aliasB ? -1 : 0
	);
	const manualChunkAliasByEntry = new Map<Module, string>();
	for (const [alias, module] of manualChunkAliasesWithEntry) {
		addModuleToManualChunk(alias, module, manualChunkAliasByEntry);
	}
	return manualChunkAliasByEntry;
}

Can you try out if this would also solve your issue?

@lukastaegert
Copy link
Member

PR here: #4397

@CaptainLiao
Copy link
Author

CaptainLiao commented Feb 10, 2022

@lukastaegert

bravo! I use your code that could fixed the issue.

waiting for your merge. again, thx.

@hulk-yin
Copy link

I have a similar but somwhat different problem。
I used rollup@2.77.3 and rollup@3.0.9 versions,
On one computer ,build two times,i can get same hash ,but on other computer , chunk's hash has be changed.
The computer os is the same (macos and node 16)。

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

Successfully merging a pull request may close this issue.

4 participants