Skip to content

Commit

Permalink
fix: make file name deterministic in parallel emits (fix #4909) (#4912)
Browse files Browse the repository at this point in the history
* fix: make file name deterministic in parallel emits (fix #4909)

* refactor: use > determine emit file name

* test: emit file in a random order
  • Loading branch information
sun0day committed Mar 20, 2023
1 parent 680912e commit 3682f30
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
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 > newFileName)
) {
if (fileName) {
delete bundle[fileName];
}
fileName = newFileName;
fileNamesBySource.set(sourceHash, fileName);
}
}
Expand Down
Expand Up @@ -4,13 +4,16 @@ module.exports = {
input: ['main.js'],
plugins: {
buildStart() {
this.emitFile({ type: 'asset', name: 'string.txt', source: 'string' });
// emit 'string' source in a random order
this.emitFile({ type: 'asset', name: 'stringSameSource.txt', source: 'string' });
this.emitFile({ type: 'asset', name: 'string2.txt', source: 'string' });
this.emitFile({ type: 'asset', name: 'string1.txt', source: 'string' });
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

0 comments on commit 3682f30

Please sign in to comment.