diff --git a/src/utils/FileEmitter.ts b/src/utils/FileEmitter.ts index 5b7ceab90be..327ee54397f 100644 --- a/src/utils/FileEmitter.ts +++ b/src/utils/FileEmitter.ts @@ -21,6 +21,7 @@ import { errorInvalidRollupPhaseForChunkEmission, errorNoAssetSourceSet } from './error'; +import { getOrCreate } from './getOrCreate'; import { defaultHashSize } from './hashPlaceholders'; import type { OutputBundleWithPlaceholders } from './outputBundle'; import { FILE_PLACEHOLDER, lowercaseBundleKeys } from './outputBundle'; @@ -74,6 +75,7 @@ interface ConsumedChunk { fileName: string | undefined; module: null | Module; name: string; + referenceId: string; type: 'chunk'; } @@ -81,6 +83,7 @@ interface ConsumedAsset { fileName: string | undefined; name: string | undefined; needsCodeReference: boolean; + referenceId: string; source: string | Uint8Array | undefined; type: 'asset'; } @@ -231,11 +234,11 @@ export class FileEmitter { } const source = getValidSource(requestedSource, consumedFile, referenceId); if (this.output) { - this.finalizeAsset(consumedFile, source, referenceId, this.output); + this.finalizeAdditionalAsset(consumedFile, source, this.output); } else { consumedFile.source = source; for (const emitter of this.outputFileEmitters) { - emitter.finalizeAsset(consumedFile, source, referenceId, emitter.output!); + emitter.finalizeAdditionalAsset(consumedFile, source, emitter.output!); } } }; @@ -258,11 +261,20 @@ export class FileEmitter { reserveFileNameInBundle(emittedFile.fileName, output, this.options.onwarn); } } - for (const [referenceId, consumedFile] of this.filesByReferenceId) { + const consumedAssetsByHash = new Map(); + for (const consumedFile of this.filesByReferenceId.values()) { if (consumedFile.type === 'asset' && consumedFile.source !== undefined) { - this.finalizeAsset(consumedFile, consumedFile.source, referenceId, output); + if (consumedFile.fileName) { + this.finalizeAdditionalAsset(consumedFile, consumedFile.source, output); + } else { + const sourceHash = getSourceHash(consumedFile.source); + getOrCreate(consumedAssetsByHash, sourceHash, () => []).push(consumedFile); + } } } + for (const [sourceHash, consumedFiles] of consumedAssetsByHash) { + this.finalizeAssetsWithSameSource(consumedFiles, sourceHash, output); + } }; private addOutputFileEmitter(outputFileEmitter: FileEmitter) { @@ -278,6 +290,7 @@ export class FileEmitter { this.filesByReferenceId.has(referenceId) || this.outputFileEmitters.some(({ filesByReferenceId }) => filesByReferenceId.has(referenceId)) ); + file.referenceId = referenceId; this.filesByReferenceId.set(referenceId, file); for (const { filesByReferenceId } of this.outputFileEmitters) { filesByReferenceId.set(referenceId, file); @@ -294,6 +307,7 @@ export class FileEmitter { fileName: emittedAsset.fileName, name: emittedAsset.name, needsCodeReference: !!emittedAsset.needsCodeReference, + referenceId: '', source, type: 'asset' }; @@ -302,10 +316,10 @@ export class FileEmitter { emittedAsset.fileName || emittedAsset.name || String(this.nextIdBase++) ); if (this.output) { - this.emitAssetWithReferenceId(consumedAsset, referenceId, this.output); + this.emitAssetWithReferenceId(consumedAsset, this.output); } else { for (const fileEmitter of this.outputFileEmitters) { - fileEmitter.emitAssetWithReferenceId(consumedAsset, referenceId, fileEmitter.output!); + fileEmitter.emitAssetWithReferenceId(consumedAsset, fileEmitter.output!); } } return referenceId; @@ -313,7 +327,6 @@ export class FileEmitter { private emitAssetWithReferenceId( consumedAsset: Readonly, - referenceId: string, output: FileEmitterOutput ) { const { fileName, source } = consumedAsset; @@ -321,7 +334,7 @@ export class FileEmitter { reserveFileNameInBundle(fileName, output, this.options.onwarn); } if (source !== undefined) { - this.finalizeAsset(consumedAsset, source, referenceId, output); + this.finalizeAdditionalAsset(consumedAsset, source, output); } } @@ -340,6 +353,7 @@ export class FileEmitter { fileName: emittedChunk.fileName, module: null, name: emittedChunk.name || emittedChunk.id, + referenceId: '', type: 'chunk' }; this.graph.moduleLoader @@ -353,48 +367,86 @@ export class FileEmitter { return this.assignReferenceId(consumedChunk, emittedChunk.id); } - private finalizeAsset( + private finalizeAdditionalAsset( consumedFile: Readonly, source: string | Uint8Array, - referenceId: string, { bundle, fileNamesBySource, outputOptions }: FileEmitterOutput ): void { - let fileName = consumedFile.fileName; + let { fileName, needsCodeReference, referenceId } = consumedFile; // Deduplicate assets if an explicit fileName is not provided if (!fileName) { const sourceHash = getSourceHash(source); fileName = fileNamesBySource.get(sourceHash); - const newFileName = generateAssetFileName( - consumedFile.name, + if (!fileName) { + fileName = generateAssetFileName( + consumedFile.name, + source, + sourceHash, + outputOptions, + bundle + ); + fileNamesBySource.set(sourceHash, fileName); + } + } + + // We must not modify the original assets to avoid interaction between outputs + const assetWithFileName = { ...consumedFile, fileName, source }; + this.filesByReferenceId.set(referenceId, assetWithFileName); + + const existingAsset = bundle[fileName]; + if (existingAsset?.type === 'asset') { + existingAsset.needsCodeReference &&= needsCodeReference; + } else { + bundle[fileName] = { + fileName, + name: consumedFile.name, + needsCodeReference, source, + type: 'asset' + }; + } + } + + private finalizeAssetsWithSameSource( + consumedFiles: ReadonlyArray, + sourceHash: string, + { bundle, fileNamesBySource, outputOptions }: FileEmitterOutput + ): void { + let fileName = ''; + let usedConsumedFile: ConsumedAsset; + let needsCodeReference = true; + for (const consumedFile of consumedFiles) { + needsCodeReference &&= consumedFile.needsCodeReference; + const assetFileName = generateAssetFileName( + consumedFile.name, + consumedFile.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) + assetFileName.length < fileName.length || + (assetFileName.length === fileName.length && assetFileName < fileName) ) { - if (fileName) { - delete bundle[fileName]; - } - fileName = newFileName; - fileNamesBySource.set(sourceHash, fileName); + fileName = assetFileName; + usedConsumedFile = consumedFile; } } + fileNamesBySource.set(sourceHash, fileName); - // We must not modify the original assets to avoid interaction between outputs - const assetWithFileName = { ...consumedFile, fileName, source }; - this.filesByReferenceId.set(referenceId, assetWithFileName); + for (const consumedFile of consumedFiles) { + // We must not modify the original assets to avoid interaction between outputs + const assetWithFileName = { ...consumedFile, fileName }; + this.filesByReferenceId.set(consumedFile.referenceId, assetWithFileName); + } bundle[fileName] = { fileName, - name: consumedFile.name, - needsCodeReference: consumedFile.needsCodeReference, - source, + name: usedConsumedFile!.name, + needsCodeReference, + source: usedConsumedFile!.source!, type: 'asset' }; } diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_config.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/_config.js new file mode 100644 index 00000000000..158c7510a84 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_config.js @@ -0,0 +1,98 @@ +const assert = require('node:assert'); + +module.exports = { + description: + 'emits unreferenced assets if needsCodeReference is true if they are also emitted without that flag', + options: { + output: { + assetFileNames: '[name][extname]' + }, + plugins: [ + { + name: 'test', + buildStart() { + this.emitFile({ + type: 'asset', + name: 'needs-reference1.txt', + needsCodeReference: true, + source: 'source1' + }); + this.emitFile({ + type: 'asset', + name: 'file1.txt', + source: 'source1' + }); + this.emitFile({ + type: 'asset', + name: 'file2.txt', + source: 'source2' + }); + this.emitFile({ + type: 'asset', + name: 'needs-reference2.txt', + needsCodeReference: true, + source: 'source2' + }); + this.emitFile({ + type: 'asset', + name: 'needs-reference3.txt', + needsCodeReference: true, + source: 'source3' + }); + this.emitFile({ + type: 'asset', + name: 'file4.txt', + source: 'source4' + }); + this.emitFile({ + type: 'asset', + name: 'needs-reference5.txt', + needsCodeReference: true, + source: 'source5' + }); + this.emitFile({ + type: 'asset', + name: 'file6.txt', + source: 'source6' + }); + }, + renderStart() { + this.emitFile({ + type: 'asset', + name: 'file3.txt', + source: 'source3' + }); + this.emitFile({ + type: 'asset', + name: 'needs-reference4.txt', + needsCodeReference: true, + source: 'source4' + }); + }, + generateBundle(_, bundle) { + this.emitFile({ + type: 'asset', + name: 'file5.txt', + source: 'source5' + }); + this.emitFile({ + type: 'asset', + name: 'needs-reference6.txt', + needsCodeReference: true, + source: 'source6' + }); + + assert.deepEqual(Object.keys(bundle).sort(), [ + 'file1.txt', + 'file2.txt', + 'file4.txt', + 'file6.txt', + 'main.js', + 'needs-reference3.txt', + 'needs-reference5.txt' + ]); + } + } + ] + } +}; diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file1.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file1.txt new file mode 100644 index 00000000000..90607bb217b --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file1.txt @@ -0,0 +1 @@ +source1 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file2.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file2.txt new file mode 100644 index 00000000000..a1d2704e103 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file2.txt @@ -0,0 +1 @@ +source2 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file4.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file4.txt new file mode 100644 index 00000000000..253dd032578 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file4.txt @@ -0,0 +1 @@ +source4 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file6.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file6.txt new file mode 100644 index 00000000000..bbc84df5fe1 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/file6.txt @@ -0,0 +1 @@ +source6 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/main.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/main.js new file mode 100644 index 00000000000..49540b44d78 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/main.js @@ -0,0 +1,5 @@ +define((function () { 'use strict'; + + console.log('main'); + +})); diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference3.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference3.txt new file mode 100644 index 00000000000..ab3070242e6 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference3.txt @@ -0,0 +1 @@ +source3 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference5.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference5.txt new file mode 100644 index 00000000000..cfd9aaa7945 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/amd/needs-reference5.txt @@ -0,0 +1 @@ +source5 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file1.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file1.txt new file mode 100644 index 00000000000..90607bb217b --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file1.txt @@ -0,0 +1 @@ +source1 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file2.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file2.txt new file mode 100644 index 00000000000..a1d2704e103 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file2.txt @@ -0,0 +1 @@ +source2 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file4.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file4.txt new file mode 100644 index 00000000000..253dd032578 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file4.txt @@ -0,0 +1 @@ +source4 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file6.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file6.txt new file mode 100644 index 00000000000..bbc84df5fe1 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/file6.txt @@ -0,0 +1 @@ +source6 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/main.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/main.js new file mode 100644 index 00000000000..d0ed06d8c90 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/main.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('main'); diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference3.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference3.txt new file mode 100644 index 00000000000..ab3070242e6 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference3.txt @@ -0,0 +1 @@ +source3 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference5.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference5.txt new file mode 100644 index 00000000000..cfd9aaa7945 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/cjs/needs-reference5.txt @@ -0,0 +1 @@ +source5 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file1.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file1.txt new file mode 100644 index 00000000000..90607bb217b --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file1.txt @@ -0,0 +1 @@ +source1 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file2.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file2.txt new file mode 100644 index 00000000000..a1d2704e103 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file2.txt @@ -0,0 +1 @@ +source2 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file4.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file4.txt new file mode 100644 index 00000000000..253dd032578 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file4.txt @@ -0,0 +1 @@ +source4 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file6.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file6.txt new file mode 100644 index 00000000000..bbc84df5fe1 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/file6.txt @@ -0,0 +1 @@ +source6 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/main.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference3.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference3.txt new file mode 100644 index 00000000000..ab3070242e6 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference3.txt @@ -0,0 +1 @@ +source3 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference5.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference5.txt new file mode 100644 index 00000000000..cfd9aaa7945 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/es/needs-reference5.txt @@ -0,0 +1 @@ +source5 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file1.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file1.txt new file mode 100644 index 00000000000..90607bb217b --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file1.txt @@ -0,0 +1 @@ +source1 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file2.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file2.txt new file mode 100644 index 00000000000..a1d2704e103 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file2.txt @@ -0,0 +1 @@ +source2 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file4.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file4.txt new file mode 100644 index 00000000000..253dd032578 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file4.txt @@ -0,0 +1 @@ +source4 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file6.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file6.txt new file mode 100644 index 00000000000..bbc84df5fe1 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/file6.txt @@ -0,0 +1 @@ +source6 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/main.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/main.js new file mode 100644 index 00000000000..35e2693371a --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/main.js @@ -0,0 +1,10 @@ +System.register([], (function () { + 'use strict'; + return { + execute: (function () { + + console.log('main'); + + }) + }; +})); diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference3.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference3.txt new file mode 100644 index 00000000000..ab3070242e6 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference3.txt @@ -0,0 +1 @@ +source3 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference5.txt b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference5.txt new file mode 100644 index 00000000000..cfd9aaa7945 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/_expected/system/needs-reference5.txt @@ -0,0 +1 @@ +source5 \ No newline at end of file diff --git a/test/chunking-form/samples/asset-needs-code-reference-same-source/main.js b/test/chunking-form/samples/asset-needs-code-reference-same-source/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/chunking-form/samples/asset-needs-code-reference-same-source/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/chunking-form/samples/emit-file/deduplicate-assets/_config.js b/test/chunking-form/samples/emit-file/deduplicate-assets/_config.js index 7fdfc9c6913..747fbbadf41 100644 --- a/test/chunking-form/samples/emit-file/deduplicate-assets/_config.js +++ b/test/chunking-form/samples/emit-file/deduplicate-assets/_config.js @@ -1,3 +1,14 @@ +const assert = require('node:assert'); +let string1Id, + string2Id, + stringSameSourceId, + sameStringAsBufferId, + otherStringId, + bufferId, + bufferSameSourceId, + sameBufferAsStringId, + otherBufferId; + module.exports = { description: 'deduplicates asset that have the same source', options: { @@ -5,36 +16,44 @@ module.exports = { plugins: { buildStart() { // 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({ + stringSameSourceId = this.emitFile({ + type: 'asset', + name: 'stringSameSource.txt', + source: 'string' + }); + string2Id = this.emitFile({ type: 'asset', name: 'string2.txt', source: 'string' }); + string1Id = this.emitFile({ type: 'asset', name: 'string1.txt', source: 'string' }); + sameStringAsBufferId = 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' }); + otherStringId = this.emitFile({ + type: 'asset', + name: 'otherString.txt', + source: 'otherString' + }); const bufferSource = () => Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); - this.emitFile({ + bufferId = this.emitFile({ type: 'asset', name: 'buffer.txt', source: bufferSource() }); - this.emitFile({ + bufferSameSourceId = this.emitFile({ type: 'asset', name: 'bufferSameSource.txt', source: bufferSource() }); - this.emitFile({ + sameBufferAsStringId = this.emitFile({ type: 'asset', name: 'sameBufferAsString.txt', source: bufferSource().toString() // Test cross Buffer/string deduplication }); // Different buffer source - this.emitFile({ + otherBufferId = this.emitFile({ type: 'asset', name: 'otherBuffer.txt', source: Buffer.from('otherBuffer') @@ -48,6 +67,41 @@ module.exports = { source: bufferSource() }); return null; + }, + generateBundle() { + assert.strictEqual(this.getFileName(string1Id), 'assets/string1-473287f8.txt', 'string1'); + assert.strictEqual(this.getFileName(string2Id), 'assets/string1-473287f8.txt', 'string2'); + assert.strictEqual( + this.getFileName(stringSameSourceId), + 'assets/string1-473287f8.txt', + 'stringSameSource' + ); + assert.strictEqual( + this.getFileName(sameStringAsBufferId), + 'assets/string1-473287f8.txt', + 'sameStringAsBuffer' + ); + assert.strictEqual( + this.getFileName(otherStringId), + 'assets/otherString-e296c1ca.txt', + 'otherString' + ); + assert.strictEqual(this.getFileName(bufferId), 'assets/buffer-d0ca8c2a.txt', 'buffer'); + assert.strictEqual( + this.getFileName(bufferSameSourceId), + 'assets/buffer-d0ca8c2a.txt', + 'bufferSameSource' + ); + assert.strictEqual( + this.getFileName(sameBufferAsStringId), + 'assets/buffer-d0ca8c2a.txt', + 'sameBufferAsString' + ); + assert.strictEqual( + this.getFileName(otherBufferId), + 'assets/otherBuffer-e8d9b528.txt', + 'otherBuffer' + ); } } }