Skip to content

Commit

Permalink
Avoid cycles when merging chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Dec 5, 2022
1 parent ec05ef3 commit ae77794
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 65 deletions.
42 changes: 38 additions & 4 deletions src/utils/chunkAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ function assignEntryToStaticDependencies(
}

interface ChunkDescription {
dependencies: Set<Module>;
modules: Module[];
pure: boolean;
signature: string;
size: number;
transitiveDependencies: Set<Module>;
}

type ChunkPartition = {
Expand Down Expand Up @@ -333,9 +335,24 @@ function getPartitionedChunks(
for (const [signature, modules] of Object.entries(chunkModulesBySignature)) {
let size = 0;
let pure = true;
const dependencies = new Set<Module>();
const transitiveDependencies = new Set<Module>();
for (const module of modules) {
pure &&= !module.hasEffects();
size += module.magicString.toString().length;
for (const dependency of module.getDependenciesToBeIncluded()) {
if (!(dependency instanceof ExternalModule)) {
dependencies.add(dependency);
transitiveDependencies.add(dependency);
}
}
}
for (const module of transitiveDependencies) {
for (const dependency of module.getDependenciesToBeIncluded()) {
if (!(dependency instanceof ExternalModule)) {
transitiveDependencies.add(dependency);
}
}
}
(size < minChunkSize
? pure
Expand All @@ -344,7 +361,7 @@ function getPartitionedChunks(
: pure
? bigPureChunks
: bigSideEffectChunks
).push({ modules, pure, signature, size });
).push({ dependencies, modules, pure, signature, size, transitiveDependencies });
}
for (const chunks of [
bigPureChunks,
Expand Down Expand Up @@ -399,14 +416,31 @@ function mergeChunks(
closestChunk.size += size;
closestChunk.pure &&= pure;
closestChunk.signature = mergeSignatures(signature, closestChunk.signature);
const { dependencies, transitiveDependencies } = closestChunk;
for (const dependency of mergedChunk.dependencies) {
dependencies.add(dependency);
}
for (const dependency of mergedChunk.transitiveDependencies) {
transitiveDependencies.add(dependency);
}
getChunksInPartition(closestChunk, minChunkSize, chunkPartition).add(closestChunk);
}
}
}

// function isValidMerge(mergedChunk: ChunkDescription, targetChunk: ChunkDescription) {
// return false;
// }
// If a module is a transitive but not a direct dependency of the other chunk,
// merging is prohibited as that would create a new cyclic dependency.
function isValidMerge(mergedChunk: ChunkDescription, targetChunk: ChunkDescription) {
for (const module of mergedChunk.modules) {
if (targetChunk.transitiveDependencies.has(module) && !targetChunk.dependencies.has(module))
return false;
}
for (const module of targetChunk.modules) {
if (mergedChunk.transitiveDependencies.has(module) && !mergedChunk.dependencies.has(module))
return false;
}
return true;
}

function getChunksInPartition(
chunk: ChunkDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
solo: true,
description: 'avoids circular dependencies when merging chunks',
options: {
input: ['main1.js', 'main2.js', 'main3.js'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
define(['exports', './generated-small'], (function (exports, small) { 'use strict';

const result = small.small + '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
console.log(result);

const generated = 'generated' + result;

exports.generated = generated;
exports.result = result;

}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(['exports'], (function (exports) { 'use strict';

const small = '1';

exports.small = small;

}));

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
define(['./generated-small2'], (function (small2) { 'use strict';
define(['exports', './generated-main1', './generated-small'], (function (exports, main1, small) { 'use strict';

console.log(small2.small1, small2.small2);


exports.generated = main1.generated;

}));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define(['./generated-small2'], (function (small2) { 'use strict';
define(['./generated-main1', './generated-small'], (function (main1, small) { 'use strict';

console.log(small2.small1);
console.log(main1.result);

}));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define(['./generated-small2'], (function (small2) { 'use strict';
define(['./generated-small'], (function (small) { 'use strict';

console.log(small2.small2);
console.log(small.small);

}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var small = require('./generated-small.js');

const result = small.small + '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
console.log(result);

const generated = 'generated' + result;

exports.generated = generated;
exports.result = result;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const small = '1';

exports.small = small;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

var small2 = require('./generated-small2.js');
var main1 = require('./generated-main1.js');
require('./generated-small.js');

console.log(small2.small1, small2.small2);


exports.generated = main1.generated;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var small2 = require('./generated-small2.js');
var main1 = require('./generated-main1.js');
require('./generated-small.js');

console.log(small2.small1);
console.log(main1.result);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

var small2 = require('./generated-small2.js');
var small = require('./generated-small.js');

console.log(small2.small2);
console.log(small.small);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { s as small } from './generated-small.js';

const result = small + '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
console.log(result);

const generated = 'generated' + result;

export { generated as g, result as r };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const small = '1';

export { small as s };

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import { s as small1, a as small2 } from './generated-small2.js';

console.log(small1, small2);
export { g as generated } from './generated-main1.js';
import './generated-small.js';
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { s as small1 } from './generated-small2.js';
import { r as result } from './generated-main1.js';
import './generated-small.js';

console.log(small1);
console.log(result);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { a as small2 } from './generated-small2.js';
import { s as small } from './generated-small.js';

console.log(small2);
console.log(small);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
System.register(['./generated-small.js'], (function (exports) {
'use strict';
var small;
return {
setters: [function (module) {
small = module.s;
}],
execute: (function () {

const result = exports('r', small + '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789');
console.log(result);

const generated = exports('g', 'generated' + result);

})
};
}));
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ System.register([], (function (exports) {
return {
execute: (function () {

const small1 = exports('s', '1');

const small2 = exports('a', '1');
const small = exports('s', '1');

})
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
System.register(['./generated-small2.js'], (function () {
System.register(['./generated-main1.js', './generated-small.js'], (function (exports) {
'use strict';
var small1, small2;
return {
setters: [function (module) {
small1 = module.s;
small2 = module.a;
}],
exports('generated', module.g);
}, null],
execute: (function () {

console.log(small1, small2);


})
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
System.register(['./generated-small2.js'], (function () {
System.register(['./generated-main1.js', './generated-small.js'], (function () {
'use strict';
var small1;
var result;
return {
setters: [function (module) {
small1 = module.s;
}],
result = module.r;
}, null],
execute: (function () {

console.log(small1);
console.log(result);

})
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
System.register(['./generated-small2.js'], (function () {
System.register(['./generated-small.js'], (function () {
'use strict';
var small2;
var small;
return {
setters: [function (module) {
small2 = module.a;
small = module.s;
}],
execute: (function () {

console.log(small2);
console.log(small);

})
};
Expand Down

0 comments on commit ae77794

Please sign in to comment.