From 0ae1f3552d7c6b4b40676b10642835ca9f216931 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 6 Jul 2020 20:56:33 +0200 Subject: [PATCH] Store plugin driver on class --- src/Bundle.ts | 16 ++++------- src/Chunk.ts | 79 +++++++++++++++++++++------------------------------ 2 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/Bundle.ts b/src/Bundle.ts index f79c0d95fae..d139e9627c4 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -82,17 +82,12 @@ export default class Bundle { ): Promise { this.assignChunkIds(chunks, inputBase, addons, outputBundle); for (const chunk of chunks) { - outputBundle[chunk.id!] = chunk.getChunkInfoWithFileNames(this.pluginDriver) as OutputChunk; + outputBundle[chunk.id!] = chunk.getChunkInfoWithFileNames() as OutputChunk; } await Promise.all( - chunks.map(chunk => { + chunks.map(async 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; - }); + Object.assign(outputChunk, await chunk.render(this.outputOptions, addons, outputChunk)); }) ); } @@ -143,7 +138,7 @@ export default class Bundle { this.unsetOptions ); } else { - chunk.id = chunk.generateId(addons, this.outputOptions, bundle, true, this.pluginDriver); + chunk.id = chunk.generateId(addons, this.outputOptions, bundle, true); } bundle[chunk.id] = FILE_PLACEHOLDER; } @@ -203,6 +198,7 @@ export default class Bundle { this.inputOptions, this.outputOptions, this.unsetOptions, + this.pluginDriver, this.graph.modulesById, chunkByModule, this.facadeChunkByModule, @@ -228,7 +224,7 @@ export default class Bundle { chunk.generateExports(); } for (const chunk of chunks) { - chunk.preRender(this.outputOptions, inputBase, this.pluginDriver); + chunk.preRender(this.outputOptions, inputBase); } } } diff --git a/src/Chunk.ts b/src/Chunk.ts index 371e3f2aba2..9fd9e1a5bd5 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -118,6 +118,7 @@ export default class Chunk { inputOptions: NormalizedInputOptions, outputOptions: NormalizedOutputOptions, unsetOptions: Set, + pluginDriver: PluginDriver, modulesById: Map, chunkByModule: Map, facadeChunkByModule: Map, @@ -129,6 +130,7 @@ export default class Chunk { inputOptions, outputOptions, unsetOptions, + pluginDriver, modulesById, chunkByModule, facadeChunkByModule, @@ -197,6 +199,7 @@ export default class Chunk { private readonly inputOptions: NormalizedInputOptions, private readonly outputOptions: NormalizedOutputOptions, private readonly unsetOptions: Set, + private readonly pluginDriver: PluginDriver, private readonly modulesById: Map, private readonly chunkByModule: Map, private readonly facadeChunkByModule: Map, @@ -342,6 +345,7 @@ export default class Chunk { this.inputOptions, this.outputOptions, this.unsetOptions, + this.pluginDriver, this.modulesById, this.chunkByModule, this.facadeChunkByModule, @@ -376,8 +380,7 @@ export default class Chunk { addons: Addons, options: NormalizedOutputOptions, existingNames: Record, - includeHash: boolean, - outputPluginDriver: PluginDriver + includeHash: boolean ): string { if (this.fileName !== null) { return this.fileName; @@ -394,12 +397,7 @@ export default class Chunk { format: () => options.format, hash: () => includeHash - ? this.computeContentHashWithDependencies( - addons, - options, - existingNames, - outputPluginDriver - ) + ? this.computeContentHashWithDependencies(addons, options, existingNames) : '[hash]', name: () => this.getChunkName() }, @@ -462,7 +460,7 @@ export default class Chunk { }; } - getChunkInfoWithFileNames(outputPluginDriver: PluginDriver): RenderedChunk { + getChunkInfoWithFileNames(): RenderedChunk { return Object.assign(this.getChunkInfo(), { code: undefined, dynamicImports: Array.from(this.dynamicDependencies, getId), @@ -470,7 +468,7 @@ export default class Chunk { implicitlyLoadedBefore: Array.from(this.implicitlyLoadedBefore, getId), imports: Array.from(this.dependencies, getId), map: undefined, - referencedFiles: this.getReferencedFiles(outputPluginDriver) + referencedFiles: this.getReferencedFiles() }); } @@ -484,25 +482,10 @@ export default class Chunk { ); } - // TODO Lukas private? - // TODO Lukas move plugin driver to class? - getReferencedFiles(outputPluginDriver: PluginDriver): string[] { - const referencedFiles: string[] = []; - for (const module of this.orderedModules) { - for (const meta of module.importMetas) { - const fileName = meta.getReferencedFileName(outputPluginDriver); - if (fileName) { - referencedFiles.push(fileName); - } - } - } - return referencedFiles; - } - - getRenderedHash(outputPluginDriver: PluginDriver): string { + getRenderedHash(): string { if (this.renderedHash) return this.renderedHash; const hash = createHash(); - const hashAugmentation = outputPluginDriver.hookReduceValueSync( + const hashAugmentation = this.pluginDriver.hookReduceValueSync( 'augmentChunkHash', '', [this.getChunkInfo()], @@ -545,7 +528,7 @@ export default class Chunk { } // prerender allows chunk hashes and names to be generated before finalizing - preRender(options: NormalizedOutputOptions, inputBase: string, outputPluginDriver: PluginDriver) { + preRender(options: NormalizedOutputOptions, inputBase: string) { const magicString = new MagicStringBundle({ separator: options.compact ? '' : '\n\n' }); this.usedModules = []; this.indentString = getIndentString(this.orderedModules, options); @@ -561,7 +544,7 @@ export default class Chunk { freeze: options.freeze, indent: this.indentString, namespaceToStringTag: options.namespaceToStringTag, - outputPluginDriver, + outputPluginDriver: this.pluginDriver, varOrConst: options.preferConst ? 'const' : 'var' }; @@ -643,12 +626,7 @@ export default class Chunk { this.exportMode === 'none' ? [] : this.getChunkExportDeclarations(options.format); } - async render( - options: NormalizedOutputOptions, - addons: Addons, - outputChunk: RenderedChunk, - outputPluginDriver: PluginDriver - ) { + async render(options: NormalizedOutputOptions, addons: Addons, outputChunk: RenderedChunk) { timeStart('render format', 2); const format = options.format; @@ -676,7 +654,7 @@ export default class Chunk { } this.finaliseDynamicImports(options); - this.finaliseImportMetas(format, outputPluginDriver); + this.finaliseImportMetas(format); const hasExports = this.renderedExports!.length !== 0 || @@ -739,7 +717,7 @@ export default class Chunk { let code = await renderChunk({ code: prevCode, options, - outputPluginDriver, + outputPluginDriver: this.pluginDriver, renderChunk: outputChunk, sourcemapChain: chunkSourcemapChain }); @@ -813,8 +791,7 @@ export default class Chunk { private computeContentHashWithDependencies( addons: Addons, options: NormalizedOutputOptions, - existingNames: Record, - outputPluginDriver: PluginDriver + existingNames: Record ): string { const hash = createHash(); hash.update( @@ -826,8 +803,8 @@ export default class Chunk { if (current instanceof ExternalModule) { hash.update(':' + current.renderPath); } else { - hash.update(current.getRenderedHash(outputPluginDriver)); - hash.update(current.generateId(addons, options, existingNames, false, outputPluginDriver)); + hash.update(current.getRenderedHash()); + hash.update(current.generateId(addons, options, existingNames, false)); } if (current instanceof ExternalModule) continue; for (const dependency of [...current.dependencies, ...current.dynamicDependencies]) { @@ -890,13 +867,10 @@ export default class Chunk { } } - private finaliseImportMetas( - format: InternalModuleFormat, - outputPluginDriver: PluginDriver - ): void { + private finaliseImportMetas(format: InternalModuleFormat): void { for (const [module, code] of this.renderedModuleSources) { for (const importMeta of module.importMetas) { - importMeta.renderFinalMechanism(code, this.id!, format, outputPluginDriver); + importMeta.renderFinalMechanism(code, this.id!, format, this.pluginDriver); } } } @@ -1064,6 +1038,19 @@ export default class Chunk { return getAliasName(this.orderedModules[this.orderedModules.length - 1].id); } + private getReferencedFiles(): string[] { + const referencedFiles: string[] = []; + for (const module of this.orderedModules) { + for (const meta of module.importMetas) { + const fileName = meta.getReferencedFileName(this.pluginDriver); + if (fileName) { + referencedFiles.push(fileName); + } + } + } + return referencedFiles; + } + private getRelativePath(targetPath: string, stripJsExtension: boolean): string { let relativePath = normalize(relative(dirname(this.id!), targetPath)); if (stripJsExtension && relativePath.endsWith('.js')) {