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: make file name deterministic in parallel emits (fix #4909) #4912

Merged
merged 3 commits into from Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/utils/FileEmitter.ts
Expand Up @@ -365,14 +365,23 @@ export class FileEmitter {
if (!fileName) {
const sourceHash = getSourceHash(source);
fileName = fileNamesBySource.get(sourceHash);
if (!fileName) {
fileName = generateAssetFileName(
consumedFile.name,
source,
sourceHash,
outputOptions,
bundle
);
const newFileName = generateAssetFileName(
consumedFile.name,
source,
sourceHash,
outputOptions,
bundle
);
// make sure file name deterministic in parallel emits, always use the shorter and smaller file name
if (
!fileName ||
fileName.length > newFileName.length ||
(fileName.length === newFileName.length && fileName.localeCompare(newFileName) > 0)
sun0day marked this conversation as resolved.
Show resolved Hide resolved
) {
if (fileName) {
delete bundle[fileName];
}
fileName = newFileName;
fileNamesBySource.set(sourceHash, fileName);
}
}
Expand Down
35 changes: 27 additions & 8 deletions test/chunking-form/samples/emit-file/deduplicate-assets/_config.js
@@ -1,16 +1,35 @@
function asyncify(callback) {
sun0day marked this conversation as resolved.
Show resolved Hide resolved
const time = Math.round(Math.random() * 100);

return new Promise(resolve => {
setTimeout(() => {
callback();
resolve();
}, time);
});
}
module.exports = {
description: 'deduplicates asset that have the same source',
solo: true,
options: {
input: ['main.js'],
plugins: {
buildStart() {
this.emitFile({ type: 'asset', name: 'string.txt', source: 'string' });
this.emitFile({ type: 'asset', name: 'stringSameSource.txt', source: 'string' });
this.emitFile({
type: 'asset',
name: 'sameStringAsBuffer.txt',
source: Buffer.from('string') // Test cross Buffer/string deduplication
});
async buildStart() {
await Promise.all([
asyncify(() => this.emitFile({ type: 'asset', name: 'string1.txt', source: 'string' })),
asyncify(() => this.emitFile({ type: 'asset', name: 'string2.txt', source: 'string' })),
asyncify(() =>
this.emitFile({ type: 'asset', name: 'stringSameSource.txt', source: 'string' })
),
asyncify(() =>
this.emitFile({
type: 'asset',
name: 'sameStringAsBuffer.txt',
source: Buffer.from('string') // Test cross Buffer/string deduplication
})
)
]);

// Different string source
this.emitFile({ type: 'asset', name: 'otherString.txt', source: 'otherString' });

Expand Down