Skip to content

Commit

Permalink
Store plugin driver on class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 6, 2020
1 parent 9785b46 commit 0ae1f35
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 56 deletions.
16 changes: 6 additions & 10 deletions src/Bundle.ts
Expand Up @@ -82,17 +82,12 @@ export default class Bundle {
): Promise<void> {
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));
})
);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -203,6 +198,7 @@ export default class Bundle {
this.inputOptions,
this.outputOptions,
this.unsetOptions,
this.pluginDriver,
this.graph.modulesById,
chunkByModule,
this.facadeChunkByModule,
Expand All @@ -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);
}
}
}
Expand Down
79 changes: 33 additions & 46 deletions src/Chunk.ts
Expand Up @@ -118,6 +118,7 @@ export default class Chunk {
inputOptions: NormalizedInputOptions,
outputOptions: NormalizedOutputOptions,
unsetOptions: Set<string>,
pluginDriver: PluginDriver,
modulesById: Map<string, Module | ExternalModule>,
chunkByModule: Map<Module, Chunk>,
facadeChunkByModule: Map<Module, Chunk>,
Expand All @@ -129,6 +130,7 @@ export default class Chunk {
inputOptions,
outputOptions,
unsetOptions,
pluginDriver,
modulesById,
chunkByModule,
facadeChunkByModule,
Expand Down Expand Up @@ -197,6 +199,7 @@ export default class Chunk {
private readonly inputOptions: NormalizedInputOptions,
private readonly outputOptions: NormalizedOutputOptions,
private readonly unsetOptions: Set<string>,
private readonly pluginDriver: PluginDriver,
private readonly modulesById: Map<string, Module | ExternalModule>,
private readonly chunkByModule: Map<Module, Chunk>,
private readonly facadeChunkByModule: Map<Module, Chunk>,
Expand Down Expand Up @@ -342,6 +345,7 @@ export default class Chunk {
this.inputOptions,
this.outputOptions,
this.unsetOptions,
this.pluginDriver,
this.modulesById,
this.chunkByModule,
this.facadeChunkByModule,
Expand Down Expand Up @@ -376,8 +380,7 @@ export default class Chunk {
addons: Addons,
options: NormalizedOutputOptions,
existingNames: Record<string, any>,
includeHash: boolean,
outputPluginDriver: PluginDriver
includeHash: boolean
): string {
if (this.fileName !== null) {
return this.fileName;
Expand All @@ -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()
},
Expand Down Expand Up @@ -462,15 +460,15 @@ export default class Chunk {
};
}

getChunkInfoWithFileNames(outputPluginDriver: PluginDriver): RenderedChunk {
getChunkInfoWithFileNames(): RenderedChunk {
return Object.assign(this.getChunkInfo(), {
code: undefined,
dynamicImports: Array.from(this.dynamicDependencies, getId),
fileName: this.id!,
implicitlyLoadedBefore: Array.from(this.implicitlyLoadedBefore, getId),
imports: Array.from(this.dependencies, getId),
map: undefined,
referencedFiles: this.getReferencedFiles(outputPluginDriver)
referencedFiles: this.getReferencedFiles()
});
}

Expand All @@ -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()],
Expand Down Expand Up @@ -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);
Expand All @@ -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'
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -676,7 +654,7 @@ export default class Chunk {
}

this.finaliseDynamicImports(options);
this.finaliseImportMetas(format, outputPluginDriver);
this.finaliseImportMetas(format);

const hasExports =
this.renderedExports!.length !== 0 ||
Expand Down Expand Up @@ -739,7 +717,7 @@ export default class Chunk {
let code = await renderChunk({
code: prevCode,
options,
outputPluginDriver,
outputPluginDriver: this.pluginDriver,
renderChunk: outputChunk,
sourcemapChain: chunkSourcemapChain
});
Expand Down Expand Up @@ -813,8 +791,7 @@ export default class Chunk {
private computeContentHashWithDependencies(
addons: Addons,
options: NormalizedOutputOptions,
existingNames: Record<string, any>,
outputPluginDriver: PluginDriver
existingNames: Record<string, any>
): string {
const hash = createHash();
hash.update(
Expand All @@ -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]) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit 0ae1f35

Please sign in to comment.