Skip to content

Commit

Permalink
Get rid of colouring hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 4, 2020
1 parent 7c82858 commit bd20782
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 95 deletions.
30 changes: 7 additions & 23 deletions src/Chunk.ts
Expand Up @@ -352,7 +352,7 @@ export default class Chunk {
}

getDynamicImportIds(): string[] {
return Array.from(this.dynamicDependencies).map(chunk => chunk.id as string);
return [...this.dynamicDependencies].map(chunk => chunk.id as string);
}

getExportNames(): string[] {
Expand All @@ -362,7 +362,7 @@ export default class Chunk {
}

getImportIds(): string[] {
return Array.from(this.dependencies).map(chunk => chunk.id as string);
return [...this.dependencies].map(chunk => chunk.id as string);
}

getRenderedHash(outputPluginDriver: PluginDriver): string {
Expand Down Expand Up @@ -552,7 +552,7 @@ export default class Chunk {
if (dep instanceof Chunk) this.inlineChunkDependencies(dep, true);
}
}
const sortedDependencies = Array.from(this.dependencies);
const sortedDependencies = [...this.dependencies];
sortByExecutionOrder(sortedDependencies);
this.dependencies = new Set(sortedDependencies);

Expand Down Expand Up @@ -656,7 +656,7 @@ export default class Chunk {

const hasExports =
this.renderedExports!.length !== 0 ||
Array.from(this.renderedDependencies!.values()).some(
[...this.renderedDependencies!.values()].some(
dep => (dep.reexports && dep.reexports.length !== 0)!
);

Expand Down Expand Up @@ -687,7 +687,7 @@ export default class Chunk {
this.renderedSource!,
{
accessedGlobals,
dependencies: Array.from(this.renderedDependencies!.values()),
dependencies: [...this.renderedDependencies!.values()],
exports: this.renderedExports!,
hasExports,
indentString: this.indentString,
Expand Down Expand Up @@ -841,10 +841,7 @@ export default class Chunk {
hash.update(current.generateId(addons, options, existingNames, false, outputPluginDriver));
}
if (current instanceof ExternalModule) continue;
for (const dependency of current.dependencies) {
dependenciesForHashing.add(dependency);
}
for (const dependency of current.dynamicDependencies) {
for (const dependency of [...current.dependencies, ...current.dynamicDependencies]) {
dependenciesForHashing.add(dependency);
}
}
Expand Down Expand Up @@ -1077,12 +1074,7 @@ export default class Chunk {
}

private setExternalRenderPaths(options: OutputOptions, inputBase: string) {
for (const dependency of this.dependencies) {
if (dependency instanceof ExternalModule) {
dependency.setRenderPath(options, inputBase);
}
}
for (const dependency of this.dynamicDependencies) {
for (const dependency of [...this.dependencies, ...this.dynamicDependencies]) {
if (dependency instanceof ExternalModule) {
dependency.setRenderPath(options, inputBase);
}
Expand Down Expand Up @@ -1154,14 +1146,6 @@ export default class Chunk {
const map = module.getExportNamesByVariable();
for (const exportedVariable of map.keys()) {
this.exports.add(exportedVariable);
const exportSourceModule = exportedVariable.module!;
const exportChunk =
exportSourceModule instanceof ExternalModule
? exportSourceModule
: exportSourceModule.chunk!;
if (exportChunk !== this) {
this.dependencies.add(exportChunk);
}
const exportingModule = exportedVariable.module;
if (exportingModule && exportingModule.chunk && exportingModule.chunk !== this) {
exportingModule.chunk.exports.add(exportedVariable);
Expand Down
2 changes: 1 addition & 1 deletion src/Graph.ts
Expand Up @@ -278,7 +278,7 @@ export default class Graph {
timeEnd('generate chunks', 2);

this.phase = BuildPhase.GENERATE;
return chunks.concat(facades);
return [...chunks, ...facades];
});
}

Expand Down
7 changes: 3 additions & 4 deletions src/Module.ts
Expand Up @@ -205,7 +205,6 @@ export default class Module {
node: ImportExpression;
resolution: Module | ExternalModule | string | null;
}[] = [];
entryPointsHash: Uint8Array = new Uint8Array(10);
excludeFromSourcemap: boolean;
execIndex = Infinity;
exportAllModules: (Module | ExternalModule)[] = [];
Expand Down Expand Up @@ -353,7 +352,7 @@ export default class Module {
relevantDependencies.add(variable.module!);
}
if (this.isEntryPoint || this.dynamicallyImportedBy.length > 0 || this.graph.preserveModules) {
for (const exportName of this.getReexports().concat(this.getExports())) {
for (const exportName of [...this.getReexports(), ...this.getExports()]) {
relevantDependencies.add(this.getVariableForExportName(exportName).module as Module);
}
}
Expand Down Expand Up @@ -444,7 +443,7 @@ export default class Module {
if (module instanceof ExternalModule) {
reexports.add(`*${module.id}`);
} else {
for (const name of module.getExports().concat(module.getReexports())) {
for (const name of [...module.getReexports(), ...module.getExports()]) {
if (name !== 'default') reexports.add(name);
}
}
Expand Down Expand Up @@ -613,7 +612,7 @@ export default class Module {
module
);
}
this.exportAllModules = this.exportAllModules.concat(externalExportAllModules);
this.exportAllModules = [...this.exportAllModules, ...externalExportAllModules];
}

render(options: RenderOptions): MagicString {
Expand Down
79 changes: 44 additions & 35 deletions src/utils/chunkAssignment.ts
@@ -1,33 +1,32 @@
import ExternalModule from '../ExternalModule';
import Module from '../Module';
import { randomUint8Array, Uint8ArrayToHexString, Uint8ArrayXor } from './entryHashing';

type DependentModuleMap = Map<Module, Set<Module>>;
type DependentModuleMap<T = Module> = Map<Module, Set<T>>;

export function getChunkAssignments(
entryModules: Module[],
manualChunkModules: Record<string, Module[]>
): Module[][] {
const includedModules = new Set<Module>();
const assignedEntryPointsByModule: DependentModuleMap<Module | string> = new Map();
const { dependentEntryPointsByModule, dynamicEntryModules } = analyzeModuleGraph(entryModules);
const dynamicDependentEntryPointsByDynamicEntry: DependentModuleMap = getDynamicDependentEntryPoints(
const dynamicallyDependentEntryPointsByDynamicEntry: DependentModuleMap = getDynamicDependentEntryPoints(
dependentEntryPointsByModule,
dynamicEntryModules
);
const staticEntries = new Set(entryModules);

function addColourToModuleDependencies(
function assignEntryToStaticDependencies(
entry: Module,
colour: Uint8Array,
dynamicDependentEntryPoints: Set<Module> | null
dynamicDependentEntryPoints: Set<Module> | null,
assignedEntry: Module | string = entry
) {
const manualChunkAlias = entry.manualChunkAlias;
const modulesToHandle = new Set([entry]);
for (const module of modulesToHandle) {
includedModules.add(module);
const assignedEntryPoints = getDependentModules(assignedEntryPointsByModule, module);
if (manualChunkAlias) {
module.manualChunkAlias = manualChunkAlias;
module.entryPointsHash = colour;
assignedEntryPoints.add(assignedEntry);
} else if (
dynamicDependentEntryPoints &&
areEntryPointsContainedOrDynamicallyDependent(
Expand All @@ -37,7 +36,7 @@ export function getChunkAssignments(
) {
continue;
} else {
Uint8ArrayXor(module.entryPointsHash, colour);
assignedEntryPoints.add(assignedEntry);
}
for (const dependency of module.getDependenciesToBeIncluded()) {
if (!(dependency instanceof ExternalModule || dependency.manualChunkAlias)) {
Expand All @@ -55,7 +54,9 @@ export function getChunkAssignments(
for (const entry of entriesToCheck) {
if (!superSet.has(entry)) {
if (staticEntries.has(entry)) return false;
const dynamicDependentEntryPoints = dynamicDependentEntryPointsByDynamicEntry.get(entry)!;
const dynamicDependentEntryPoints = dynamicallyDependentEntryPointsByDynamicEntry.get(
entry
)!;
for (const dependentEntry of dynamicDependentEntryPoints) {
entriesToCheck.add(dependentEntry);
}
Expand All @@ -66,43 +67,31 @@ export function getChunkAssignments(

if (manualChunkModules) {
for (const chunkName of Object.keys(manualChunkModules)) {
const entryHash = randomUint8Array(10);

for (const entry of manualChunkModules[chunkName]) {
addColourToModuleDependencies(entry, entryHash, null);
assignEntryToStaticDependencies(entry, null, chunkName);
}
}
}

for (const entry of entryModules) {
if (!entry.manualChunkAlias) {
const entryHash = randomUint8Array(10);
addColourToModuleDependencies(entry, entryHash, null);
assignEntryToStaticDependencies(entry, null);
}
}

for (const entry of dynamicEntryModules) {
if (!entry.manualChunkAlias) {
const entryHash = randomUint8Array(10);
addColourToModuleDependencies(
assignEntryToStaticDependencies(
entry,
entryHash,
dynamicDependentEntryPointsByDynamicEntry.get(entry)!
dynamicallyDependentEntryPointsByDynamicEntry.get(entry)!
);
}
}

const chunkModules: { [entryHashSum: string]: Module[] } = {};
for (const module of includedModules) {
const entryPointsHashStr = Uint8ArrayToHexString(module.entryPointsHash);
const curChunk = chunkModules[entryPointsHashStr];
if (curChunk) {
curChunk.push(module);
} else {
chunkModules[entryPointsHashStr] = [module];
}
}
return Object.keys(chunkModules).map(entryHashSum => chunkModules[entryHashSum]);
return createChunks(
[...Object.keys(manualChunkModules), ...entryModules, ...dynamicEntryModules],
assignedEntryPointsByModule
);
}

function analyzeModuleGraph(
Expand Down Expand Up @@ -138,7 +127,7 @@ function analyzeModuleGraph(
return { dependentEntryPointsByModule, dynamicEntryModules };
}

function getDependentModules(moduleMap: DependentModuleMap, module: Module): Set<Module> {
function getDependentModules<T>(moduleMap: DependentModuleMap<T>, module: Module): Set<T> {
const dependentModules = moduleMap.get(module) || new Set();
moduleMap.set(module, dependentModules);
return dependentModules;
Expand All @@ -148,10 +137,10 @@ function getDynamicDependentEntryPoints(
dependentEntryPointsByModule: DependentModuleMap,
dynamicEntryModules: Set<Module>
): DependentModuleMap {
const dynamicDependentEntryPointsByDynamicEntry: DependentModuleMap = new Map();
const dynamicallyDependentEntryPointsByDynamicEntry: DependentModuleMap = new Map();
for (const dynamicEntry of dynamicEntryModules) {
const dynamicDependentEntryPoints = getDependentModules(
dynamicDependentEntryPointsByDynamicEntry,
dynamicallyDependentEntryPointsByDynamicEntry,
dynamicEntry
);
for (const importer of dynamicEntry.dynamicallyImportedBy) {
Expand All @@ -160,5 +149,25 @@ function getDynamicDependentEntryPoints(
}
}
}
return dynamicDependentEntryPointsByDynamicEntry;
return dynamicallyDependentEntryPointsByDynamicEntry;
}

function createChunks(
allEntryPoints: (Module | string)[],
assignedEntryPointsByModule: DependentModuleMap<Module | string>
) {
const chunkModules: { [chunkSignature: string]: Module[] } = Object.create(null);
for (const [module, assignedEntryPoints] of assignedEntryPointsByModule) {
let chunkSignature = '';
for (const entry of allEntryPoints) {
chunkSignature += assignedEntryPoints.has(entry) ? 'X' : '_';
}
const chunk = chunkModules[chunkSignature];
if (chunk) {
chunk.push(module);
} else {
chunkModules[chunkSignature] = [module];
}
}
return Object.keys(chunkModules).map(chunkSignature => chunkModules[chunkSignature]);
}
31 changes: 0 additions & 31 deletions src/utils/entryHashing.ts

This file was deleted.

@@ -1,4 +1,3 @@
// TODO Lukas at the moment, there is probably an "empty" catch-all chunk that is pruned
module.exports = {
description: 'avoids empty imports if moduleSideEffects are false',
options: {
Expand Down

0 comments on commit bd20782

Please sign in to comment.