Repository: rollup/rollup
File: src/utils/FileEmitter.ts (TypeScript)

Code before:
				errorFailedValidation(
					`Emitted prebuilt chunks need to have a valid string code, received "${emitPrebuiltChunk.code}"`
				)
			);
		}
		if (
			typeof emitPrebuiltChunk.fileName !== 'string' ||
			!(
				typeof emitPrebuiltChunk.fileName === 'string' &&
				!isPathFragment(emitPrebuiltChunk.fileName)
			)
		) {
			return error(
				errorFailedValidation(
					`Emitted prebuilt chunks need to have a valid string fileName and that is neither absolute nor relative paths, received "${emitPrebuiltChunk.fileName}"`
				)
			);
		}
		const consumedPrebuiltChunk: ConsumedPrebuiltChunk = {
			code: emitPrebuiltChunk.code,
			exports: emitPrebuiltChunk.exports,
			fileName: emitPrebuiltChunk.fileName,
			map: emitPrebuiltChunk.map,
			referenceId: '',
			type: 'prebuilt-chunk'
		};
		const referenceId = this.assignReferenceId(
			consumedPrebuiltChunk,
			consumedPrebuiltChunk.fileName
		);
		if (this.output) {
			this.output.bundle[consumedPrebuiltChunk.fileName] =
				this.createPrebuiltChunk(consumedPrebuiltChunk);
		}
		return referenceId;
	}

	private finalizeAdditionalAsset(
		consumedFile: Readonly<ConsumedAsset>,
		source: string | Uint8Array,
		{ bundle, fileNamesBySource, outputOptions }: FileEmitterOutput
	): void {
		let { fileName, needsCodeReference, referenceId } = consumedFile;

		// Deduplicate assets if an explicit fileName is not provided
		if (!fileName) {
			const sourceHash = getSourceHash(source);
			fileName = fileNamesBySource.get(sourceHash);
			if (!fileName) {
				fileName = generateAssetFileName(
					consumedFile.name,

Review comment (suggestion):
This scenario is where a plugin emits a prebuilt-chunk during output generation. To test this, e.g. emit such a chunk in `generateBundle`.

Code after:
			);
		}
		if (
			typeof emitPrebuiltChunk.fileName !== 'string' ||
			isPathFragment(emitPrebuiltChunk.fileName)
		) {
			return error(
				errorFailedValidation(
					`The "fileName" property of emitted prebuilt chunks must be strings that are neither absolute nor relative paths, received "${emitPrebuiltChunk.fileName}".`
				)
			);
		}
		const consumedPrebuiltChunk: ConsumedPrebuiltChunk = {
			code: emitPrebuiltChunk.code,
			exports: emitPrebuiltChunk.exports,
			fileName: emitPrebuiltChunk.fileName,
			map: emitPrebuiltChunk.map,
			referenceId: '',
			type: 'prebuilt-chunk'
		};
		const referenceId = this.assignReferenceId(
			consumedPrebuiltChunk,
			consumedPrebuiltChunk.fileName
		);
		if (this.output) {
			this.output.bundle[consumedPrebuiltChunk.fileName] =
				this.createPrebuiltChunk(consumedPrebuiltChunk);
		}
		return referenceId;
	}

	private finalizeAdditionalAsset(
		consumedFile: Readonly<ConsumedAsset>,
		source: string | Uint8Array,
		{ bundle, fileNamesBySource, outputOptions }: FileEmitterOutput
	): void {
		let { fileName, needsCodeReference, referenceId } = consumedFile;

		// Deduplicate assets if an explicit fileName is not provided
		if (!fileName) {
			const sourceHash = getSourceHash(source);
			fileName = fileNamesBySource.get(sourceHash);
			if (!fileName) {
				fileName = generateAssetFileName(
					consumedFile.name,
					source,
					sourceHash,
					outputOptions,
					bundle
				);
				fileNamesBySource.set(sourceHash, fileName);