forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GranularChunks conformance check (vercel#11710)
Adding a conformance plugin the make sure users don't undo the benefits of the granularChunks config. The plugin makes sure that minSize, maxInitialRequests values aren't overridden. Also ensures the cacheGroups - vendors, framework, libs, common, shared are maintained. The warning and error messages do not break the build with this change. They only display a message. cc - @prateekbh, @atcastle
- Loading branch information
1 parent
03b4e28
commit 84bb360
Showing
6 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...xt/build/webpack/plugins/webpack-conformance-plugin/checks/granular-chunks-conformance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import chalk from 'chalk' | ||
import { | ||
IWebpackConformanceTest, | ||
IConformanceTestResult, | ||
IConformanceTestStatus, | ||
} from '../TestInterface' | ||
import { | ||
CONFORMANCE_ERROR_PREFIX, | ||
CONFORMANCE_WARNING_PREFIX, | ||
} from '../constants' | ||
import { deepEqual } from '../utils/utils' | ||
|
||
export interface GranularChunksConformanceCheck | ||
extends IWebpackConformanceTest { | ||
granularChunksConfig: any | ||
} | ||
|
||
function getWarningMessage(modifiedProp: string) { | ||
return ( | ||
`${CONFORMANCE_WARNING_PREFIX}: The splitChunks config as part of the granularChunks flag has ` + | ||
`been carefully crafted to optimize build size and build times. Modifying - ${chalk.bold( | ||
modifiedProp | ||
)} could result in slower builds and increased code duplication` | ||
) | ||
} | ||
|
||
function getErrorMessage(message: string) { | ||
return ( | ||
`${CONFORMANCE_ERROR_PREFIX}: The splitChunks config as part of the granularChunks flag has ` + | ||
`been carefully crafted to optimize build size and build times. Please avoid changes to ${chalk.bold( | ||
message | ||
)}` | ||
) | ||
} | ||
|
||
export class GranularChunksConformanceCheck { | ||
constructor(granularChunksConfig: any) { | ||
this.granularChunksConfig = granularChunksConfig | ||
} | ||
|
||
public buildStared(options: any): IConformanceTestResult { | ||
const userSplitChunks = options.optimization.splitChunks | ||
const warnings = [] | ||
const errors = [] | ||
|
||
if ( | ||
userSplitChunks.maxInitialRequests !== | ||
this.granularChunksConfig.maxInitialRequests | ||
) { | ||
warnings.push('splitChunks.maxInitialRequests') | ||
} | ||
|
||
if (userSplitChunks.minSize !== this.granularChunksConfig.minSize) { | ||
warnings.push('splitChunks.minSize') | ||
} | ||
|
||
const userCacheGroup = userSplitChunks.cacheGroups | ||
const originalCacheGroup = this.granularChunksConfig.cacheGroups | ||
|
||
if (userCacheGroup.vendors !== false) { | ||
errors.push('splitChunks.cacheGroups.vendors') | ||
} | ||
|
||
if (!deepEqual(userCacheGroup.framework, originalCacheGroup.framework)) { | ||
errors.push('splitChunks.cacheGroups.framework') | ||
} | ||
|
||
if (!deepEqual(userCacheGroup.lib, originalCacheGroup.lib)) { | ||
errors.push('splitChunks.cacheGroups.lib') | ||
} | ||
|
||
if (!deepEqual(userCacheGroup.commons, originalCacheGroup.commons)) { | ||
errors.push('splitChunks.cacheGroups.commons') | ||
} | ||
|
||
if (!deepEqual(userCacheGroup.shared, originalCacheGroup.shared)) { | ||
errors.push('splitChunks.cacheGroups.shared') | ||
} | ||
|
||
if (!warnings.length && !errors.length) { | ||
return { | ||
result: IConformanceTestStatus.SUCCESS, | ||
} | ||
} | ||
|
||
const failedResult: IConformanceTestResult = { | ||
result: IConformanceTestStatus.FAILED, | ||
} | ||
|
||
if (warnings.length) { | ||
failedResult.warnings = warnings.map((warning) => ({ | ||
message: getWarningMessage(warning), | ||
})) | ||
} | ||
|
||
if (errors.length) { | ||
failedResult.warnings = errors.map((error) => ({ | ||
message: getErrorMessage(error), | ||
})) | ||
} | ||
|
||
return failedResult | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/next/build/webpack/plugins/webpack-conformance-plugin/utils/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const assert = require('assert').strict | ||
|
||
export function deepEqual(a: any, b: any) { | ||
try { | ||
assert.deepStrictEqual(a, b) | ||
return true | ||
} catch (_) { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters