Skip to content

Commit 368b72f

Browse files
committed
feat(): add excludeUnusedDependencies
1 parent 88d37a1 commit 368b72f

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/compiler/entries/component-bundles.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,43 @@ export function generateComponentBundles(
77
config: d.Config,
88
buildCtx: d.BuildCtx,
99
): d.ComponentCompilerMeta[][] {
10-
let cmps = buildCtx.components;
10+
const cmps = sortBy(buildCtx.components, cmp => cmp.dependants.length);
1111
if (config.devMode) {
12-
return cmps.map(cmp => [cmp]);
12+
const devCmps = (config.excludeUnusedDependencies)
13+
? filterUnusedDependencies(cmps, cmps)
14+
: cmps;
15+
16+
return devCmps.map(cmp => [cmp]);
1317
}
1418

1519
const defaultBundles = getDefaultBundles(config, buildCtx, cmps);
1620

17-
cmps = sortBy(cmps, cmp => cmp.dependants.length);
18-
1921
// Visit components that are already in one of the default bundlers
2022
const visited = new Set();
2123
defaultBundles.forEach(entry => {
2224
entry.forEach(cmp => visited.add(cmp));
2325
});
2426

25-
const bundlers: d.ComponentCompilerMeta[][] = cmps
26-
.filter(cmp => !visited.has(cmp))
27-
.map(cmp => [cmp]);
27+
let remainingComponents: d.ComponentCompilerMeta[] = cmps
28+
.filter(cmp => !visited.has(cmp));
2829

30+
if (config.excludeUnusedDependencies) {
31+
remainingComponents = filterUnusedDependencies(remainingComponents, cmps);
32+
}
33+
34+
const bundlers = remainingComponents.map(c => [c]);
2935
return [
3036
...defaultBundles,
3137
...optimizeBundlers(bundlers, 0.6)
3238
].filter(b => b.length > 0);
3339
}
3440

41+
function filterUnusedDependencies(remainingComponents: d.ComponentCompilerMeta[], allCmps: d.ComponentCompilerMeta[]) {
42+
return remainingComponents.filter(c => (
43+
!c.isCollectionDependency ||
44+
c.dependants.some(dep => allCmps.some(c => c.tagName === dep && !c.isCollectionDependency))
45+
));
46+
}
3547

3648
function optimizeBundlers(bundles: d.ComponentCompilerMeta[][], threshold: number) {
3749
const cmpIndexMap = new Map<string, number>();

src/compiler/entries/resolve-component-dependencies.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function resolveTransitiveDependencies(cmp: d.ComponentCompilerMeta, cmps: d.Com
2828

2929
const dependencies = cmp.potentialCmpRefs.filter(tagName => cmps.some(c => c.tagName === tagName));
3030
cmp.dependencies = cmp.directDependencies = dependencies;
31-
3231
const transitiveDeps = flatOne(
3332
dependencies
3433
.map(tagName => cmps.find(c => c.tagName === tagName))

src/declarations/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export interface StencilConfig {
182182
tsconfig?: string;
183183
validateTypes?: boolean;
184184
watchIgnoredRegex?: RegExp;
185+
excludeUnusedDependencies?: boolean;
185186
}
186187

187188
export interface Config extends StencilConfig {

0 commit comments

Comments
 (0)