Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular-devkit/build-angular): remove usage of Webpack Stats.ToJsonOutput type #20016

Merged
merged 1 commit into from Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Expand Up @@ -66,6 +66,7 @@ import { normalizeExtraEntryPoints } from '../webpack/utils/helpers';
import {
BundleStats,
ChunkType,
JsonChunkStats,
generateBundleStats,
statsErrorsToString,
statsHasErrors,
Expand Down Expand Up @@ -781,10 +782,9 @@ function assertNever(input: never): never {
throw new Error(`Unexpected call to assertNever() with input: ${JSON.stringify(input, null /* replacer */, 4 /* tabSize */)}`);
}

type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
function generateBundleInfoStats(
bundle: ProcessBundleFile,
chunk: ArrayElement<webpack.Stats.ToJsonOutput['chunks']> | undefined,
chunk: JsonChunkStats | undefined,
chunkType: ChunkType,
): BundleStats {
return generateBundleStats(
Expand Down
Expand Up @@ -6,10 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import { basename } from 'path';
import * as webpack from 'webpack';
import { Budget, Type } from '../browser/schema';
import { ProcessBundleFile, ProcessBundleResult } from '../utils/process-bundle';
import { formatSize } from '../webpack/utils/stats';
import {
JsonAssetStats,
JsonChunkStats,
JsonCompilationStats,
formatSize,
} from '../webpack/utils/stats';

interface Size {
size: number;
Expand Down Expand Up @@ -105,7 +109,7 @@ export function* calculateThresholds(budget: Budget): IterableIterator<Threshold
*/
function calculateSizes(
budget: Budget,
stats: webpack.Stats.ToJsonOutput,
stats: JsonCompilationStats,
processResults: ProcessBundleResult[],
): Size[] {
if (budget.type === Type.AnyComponentStyle) {
Expand All @@ -115,7 +119,14 @@ function calculateSizes(
}

type NonComponentStyleBudgetTypes = Exclude<Budget['type'], Type.AnyComponentStyle>;
type CalculatorTypes = { new(budget: Budget, chunks: Chunk[], assets: Asset[], processResults: ProcessBundleResult[]): Calculator };
type CalculatorTypes = {
new (
budget: Budget,
chunks: JsonChunkStats[],
assets: JsonAssetStats[],
processResults: ProcessBundleResult[],
): Calculator;
};
const calculatorMap: Record<NonComponentStyleBudgetTypes, CalculatorTypes> = {
all: AllCalculator,
allScript: AllScriptCalculator,
Expand All @@ -139,22 +150,19 @@ function calculateSizes(
return calculator.calculate();
}

type ArrayElement<T> = T extends Array<infer U> ? U : never;
type Chunk = ArrayElement<Exclude<webpack.Stats.ToJsonOutput['chunks'], undefined>>;
type Asset = ArrayElement<Exclude<webpack.Stats.ToJsonOutput['assets'], undefined>>;
abstract class Calculator {
constructor (
protected budget: Budget,
protected chunks: Chunk[],
protected assets: Asset[],
protected chunks: JsonChunkStats[],
protected assets: JsonAssetStats[],
protected processResults: ProcessBundleResult[],
) {}

abstract calculate(): Size[];

/** Calculates the size of the given chunk for the provided build type. */
protected calculateChunkSize(
chunk: Chunk,
chunk: JsonChunkStats,
buildType: DifferentialBuildType,
): number {
// Look for a process result containing different builds for this chunk.
Expand Down Expand Up @@ -183,7 +191,7 @@ abstract class Calculator {
}
}

protected getAssetSize(asset: Asset): number {
protected getAssetSize(asset: JsonAssetStats): number {
if (asset.name.endsWith('.js')) {
const processResult = this.processResults
.find((processResult) => processResult.original && basename(processResult.original.filename) === asset.name);
Expand Down Expand Up @@ -348,7 +356,7 @@ function calculateBytes(

export function* checkBudgets(
budgets: Budget[],
webpackStats: webpack.Stats.ToJsonOutput,
webpackStats: JsonCompilationStats,
processResults: ProcessBundleResult[],
): IterableIterator<{ severity: ThresholdSeverity, message: string }> {
// Ignore AnyComponentStyle budgets as these are handled in `AnyComponentStyleBudgetChecker`.
Expand Down
Expand Up @@ -5,7 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as webpack from 'webpack';
import { Budget, Type } from '../browser/schema';
import { ThresholdSeverity, checkBudgets } from './bundle-calculator';
import { ProcessBundleResult } from './process-bundle';
Expand Down Expand Up @@ -33,7 +32,7 @@ describe('bundle-calculator', () => {
size: 0.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand Down Expand Up @@ -61,7 +60,7 @@ describe('bundle-calculator', () => {
size: 0.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -81,6 +80,7 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
Expand All @@ -95,7 +95,7 @@ describe('bundle-calculator', () => {
size: 0.75 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -121,7 +121,7 @@ describe('bundle-calculator', () => {
},
],
assets: [],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('bundle-calculator', () => {
},
],
assets: [],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -195,7 +195,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
],
Expand All @@ -209,7 +211,7 @@ describe('bundle-calculator', () => {
size: 0.75 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -230,11 +232,12 @@ describe('bundle-calculator', () => {
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
],
assets: [],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -273,11 +276,12 @@ describe('bundle-calculator', () => {
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
],
assets: [],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -308,7 +312,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
],
Expand All @@ -326,7 +332,7 @@ describe('bundle-calculator', () => {
size: 1.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -345,7 +351,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.css' ],
},
],
Expand All @@ -359,7 +367,7 @@ describe('bundle-calculator', () => {
size: 0.75 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -378,7 +386,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.css', 'bar.js' ],
},
],
Expand All @@ -392,7 +402,7 @@ describe('bundle-calculator', () => {
size: 0.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -407,7 +417,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js', 'bar.js' ],
},
],
Expand All @@ -421,7 +433,7 @@ describe('bundle-calculator', () => {
size: 0.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -440,7 +452,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.ext', 'bar.ext' ],
},
],
Expand All @@ -454,7 +468,7 @@ describe('bundle-calculator', () => {
size: 0.5 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};

const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));

Expand All @@ -473,7 +487,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js' ],
},
],
Expand All @@ -483,7 +499,7 @@ describe('bundle-calculator', () => {
size: 1.25 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -514,7 +530,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js' ],
},
],
Expand All @@ -524,7 +542,7 @@ describe('bundle-calculator', () => {
size: 1.25 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -555,7 +573,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js' ],
},
],
Expand All @@ -565,7 +585,7 @@ describe('bundle-calculator', () => {
size: 1.25 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down Expand Up @@ -596,7 +616,9 @@ describe('bundle-calculator', () => {
const stats = {
chunks: [
{
id: 0,
initial: true,
names: [ 'foo' ],
files: [ 'foo.js' ],
},
],
Expand All @@ -606,7 +628,7 @@ describe('bundle-calculator', () => {
size: 1.25 * KB,
},
],
} as unknown as webpack.Stats.ToJsonOutput;
};
const processResults: ProcessBundleResult[] = [
{
name: '0',
Expand Down