Skip to content

Commit

Permalink
Structure Bundle.generate
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 21, 2020
1 parent 97f51ce commit bca9052
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions src/Bundle.ts
Expand Up @@ -31,7 +31,6 @@ export default class Bundle {
private readonly graph: Graph
) {}

// TODO Lukas make this one nicer by structuring it
async generate(isWrite: boolean): Promise<OutputBundle> {
timeStart('GENERATE', 1);
const outputBundle: OutputBundleWithPlaceholders = Object.create(null);
Expand All @@ -45,36 +44,17 @@ export default class Bundle {

timeStart('generate chunks', 2);
const chunks = await this.generateChunks();
timeEnd('generate chunks', 2);

timeStart('render modules', 2);
if (chunks.length > 1) {
validateOptionsForMultiChunkOutput(this.outputOptions);
}
const addons = await createAddons(this.outputOptions, this.pluginDriver);
for (const chunk of chunks) {
chunk.generateExports();
}
const inputBase = commondir(getAbsoluteEntryModulePaths(chunks));
for (const chunk of chunks) {
chunk.preRender(this.outputOptions, inputBase, this.pluginDriver);
}
timeEnd('render modules', 2);
timeEnd('generate chunks', 2);

this.assignChunkIds(chunks, inputBase, addons, outputBundle);
assignChunksToBundle(chunks, outputBundle);
timeStart('render modules', 2);
this.prerenderChunks(chunks, inputBase);
timeEnd('render modules', 2);

await Promise.all(
chunks.map(chunk => {
const outputChunk = outputBundle[chunk.id!] as OutputChunk;
return chunk
.render(this.outputOptions, addons, outputChunk, this.pluginDriver)
.then(rendered => {
outputChunk.code = rendered.code;
outputChunk.map = rendered.map;
});
})
);
await this.addFinalizedChunksToBundle(chunks, inputBase, outputBundle);
} catch (error) {
await this.pluginDriver.hookParallel('renderError', [error]);
throw error;
Expand All @@ -84,23 +64,38 @@ export default class Bundle {
outputBundle as OutputBundle,
isWrite
]);
for (const key of Object.keys(outputBundle)) {
const file = outputBundle[key] as any;
if (!file.type) {
warnDeprecation(
'A plugin is directly adding properties to the bundle object in the "generateBundle" hook. This is deprecated and will be removed in a future Rollup version, please use "this.emitFile" instead.',
true,
this.inputOptions
);
file.type = 'asset';
}
}
this.pluginDriver.finaliseAssets();
this.finaliseAssets(outputBundle);

timeEnd('GENERATE', 1);
return outputBundle as OutputBundle;
}

private async addFinalizedChunksToBundle(
chunks: Chunk[],
inputBase: string,
outputBundle: OutputBundleWithPlaceholders
): Promise<void> {
const addons = await createAddons(this.outputOptions, this.pluginDriver);
this.assignChunkIds(chunks, inputBase, addons, outputBundle);
for (const chunk of chunks) {
const chunkDescription = (outputBundle[
chunk.id!
] = chunk.getPrerenderedChunk() as OutputChunk);
chunkDescription.fileName = chunk.id!;
}
await Promise.all(
chunks.map(chunk => {
const outputChunk = outputBundle[chunk.id!] as OutputChunk;
return chunk
.render(this.outputOptions, addons, outputChunk, this.pluginDriver)
.then(rendered => {
outputChunk.code = rendered.code;
outputChunk.map = rendered.map;
});
})
);
}

private async addManualChunks(
manualChunks: Record<string, string[]>
): Promise<Map<Module, string>> {
Expand Down Expand Up @@ -170,6 +165,21 @@ export default class Bundle {
return manualChunkAliasByEntry;
}

private finaliseAssets(outputBundle: OutputBundleWithPlaceholders): void {
for (const key of Object.keys(outputBundle)) {
const file = outputBundle[key] as any;
if (!file.type) {
warnDeprecation(
'A plugin is directly adding properties to the bundle object in the "generateBundle" hook. This is deprecated and will be removed in a future Rollup version, please use "this.emitFile" instead.',
true,
this.inputOptions
);
file.type = 'asset';
}
}
this.pluginDriver.finaliseAssets();
}

private async generateChunks(): Promise<Chunk[]> {
const { manualChunks } = this.outputOptions;
const manualChunkAliasByEntry =
Expand Down Expand Up @@ -211,6 +221,15 @@ export default class Bundle {
}
return [...chunks, ...facades];
}

private prerenderChunks(chunks: Chunk[], inputBase: string): void {
for (const chunk of chunks) {
chunk.generateExports();
}
for (const chunk of chunks) {
chunk.preRender(this.outputOptions, inputBase, this.pluginDriver);
}
}
}

function getAbsoluteEntryModulePaths(chunks: Chunk[]): string[] {
Expand Down Expand Up @@ -245,19 +264,6 @@ function validateOptionsForMultiChunkOutput(outputOptions: NormalizedOutputOptio
});
}

function assignChunksToBundle(
chunks: Chunk[],
outputBundle: OutputBundleWithPlaceholders
): OutputBundle {
for (const chunk of chunks) {
const chunkdDescription = (outputBundle[
chunk.id!
] = chunk.getPrerenderedChunk() as OutputChunk);
chunkdDescription.fileName = chunk.id!;
}
return outputBundle as OutputBundle;
}

function getIncludedModules(modulesById: Map<string, Module | ExternalModule>): Module[] {
return [...modulesById.values()].filter(
module =>
Expand Down

0 comments on commit bca9052

Please sign in to comment.