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

Provide hashed file name when using this.getFileName in generateBundle #4747

Merged
merged 1 commit into from Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 27 additions & 27 deletions src/Chunk.ts
Expand Up @@ -163,7 +163,6 @@ export default class Chunk {
execIndex: number;
exportMode: 'none' | 'named' | 'default' = 'named';
facadeModule: Module | null = null;
id: string | null = null;
namespaceVariableName = '';
suggestedVariableName: string;
variableName = '';
Expand Down Expand Up @@ -318,6 +317,32 @@ export default class Chunk {
return true;
}

finalizeChunk(
code: string,
map: SourceMap | null,
hashesByPlaceholder: Map<string, string>
): OutputChunk {
const renderedChunkInfo = this.getRenderedChunkInfo();
const finalize = (code: string) => replacePlaceholders(code, hashesByPlaceholder);
const fileName = (this.fileName = finalize(renderedChunkInfo.fileName));
return {
...renderedChunkInfo,
code,
dynamicImports: renderedChunkInfo.dynamicImports.map(finalize),
fileName,
implicitlyLoadedBefore: renderedChunkInfo.implicitlyLoadedBefore.map(finalize),
importedBindings: Object.fromEntries(
Object.entries(renderedChunkInfo.importedBindings).map(([fileName, bindings]) => [
finalize(fileName),
bindings
])
),
imports: renderedChunkInfo.imports.map(finalize),
map,
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize)
};
}

generateExports(): void {
this.sortedExportNames = null;
const remainingExports = new Set(this.exports);
Expand Down Expand Up @@ -449,31 +474,6 @@ export default class Chunk {
return facades;
}

generateOutputChunk(
code: string,
map: SourceMap | null,
hashesByPlaceholder: Map<string, string>
): OutputChunk {
const renderedChunkInfo = this.getRenderedChunkInfo();
const finalize = (code: string) => replacePlaceholders(code, hashesByPlaceholder);
return {
...renderedChunkInfo,
code,
dynamicImports: renderedChunkInfo.dynamicImports.map(finalize),
fileName: finalize(renderedChunkInfo.fileName),
implicitlyLoadedBefore: renderedChunkInfo.implicitlyLoadedBefore.map(finalize),
importedBindings: Object.fromEntries(
Object.entries(renderedChunkInfo.importedBindings).map(([fileName, bindings]) => [
finalize(fileName),
bindings
])
),
imports: renderedChunkInfo.imports.map(finalize),
map,
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize)
};
}

getChunkName(): string {
return (this.name ??= this.outputOptions.sanitizeFileName(this.getFallbackChunkName()));
}
Expand All @@ -483,7 +483,7 @@ export default class Chunk {
}

getFileName(): string {
return this.preliminaryFileName?.fileName || this.getPreliminaryFileName().fileName;
return this.fileName || this.getPreliminaryFileName().fileName;
}

getImportPath(importer: string): string {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/FileEmitter.ts
Expand Up @@ -144,8 +144,7 @@ function getChunkFileName(
return file.fileName;
}
if (facadeChunkByModule) {
const chunk = facadeChunkByModule.get(file.module!)!;
return chunk.id || chunk.getFileName();
return facadeChunkByModule.get(file.module!)!.getFileName();
}
return error(errorChunkNotGeneratedForFileName(file.fileName || file.name));
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/renderChunks.ts
Expand Up @@ -309,15 +309,15 @@ function addChunksToBundle(
map.file = replacePlaceholders(map.file, hashesByPlaceholder);
updatedCode += emitSourceMapAndGetComment(finalFileName, map, pluginDriver, options);
}
bundle[finalFileName] = chunk.generateOutputChunk(updatedCode, map, hashesByPlaceholder);
bundle[finalFileName] = chunk.finalizeChunk(updatedCode, map, hashesByPlaceholder);
}
for (const { chunk, code, fileName, map } of nonHashedChunksWithPlaceholders) {
let updatedCode =
hashesByPlaceholder.size > 0 ? replacePlaceholders(code, hashesByPlaceholder) : code;
if (map) {
updatedCode += emitSourceMapAndGetComment(fileName, map, pluginDriver, options);
}
bundle[fileName] = chunk.generateOutputChunk(updatedCode, map, hashesByPlaceholder);
bundle[fileName] = chunk.finalizeChunk(updatedCode, map, hashesByPlaceholder);
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/function/samples/emit-chunk-hash/_config.js
@@ -0,0 +1,20 @@
const assert = require('node:assert');
let referenceId;

module.exports = {
description: 'gives access to the hashed filed name via this.getFileName in generateBundle',
options: {
input: 'main',
output: {
chunkFileNames: '[name]-[hash].js'
},
plugins: {
buildStart() {
referenceId = this.emitFile({ type: 'chunk', id: 'emitted' });
},
generateBundle() {
assert.strictEqual(this.getFileName(referenceId), 'emitted-38bdd9b2.js');
}
}
}
};
1 change: 1 addition & 0 deletions test/function/samples/emit-chunk-hash/emitted.js
@@ -0,0 +1 @@
assert.ok(true);
1 change: 1 addition & 0 deletions test/function/samples/emit-chunk-hash/main.js
@@ -0,0 +1 @@
assert.ok(true);