-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): 支持构建产物中初始资源的全部检查规则 (#15)
- Loading branch information
Showing
7 changed files
with
163 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
import {Stats} from 'webpack'; | ||
import {BuildInspectSettings} from '@reskript/settings'; | ||
import {run} from './utils'; | ||
import initialResources from './initialResources'; | ||
|
||
|
||
export default (stats: Stats, settings: BuildInspectSettings) => { | ||
const {children = []} = stats.toJson('normal'); | ||
const processors = [ | ||
...initialResources(children, settings.initialResources), | ||
]; | ||
run(processors); | ||
}; | ||
|
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,83 @@ | ||
import {StatsCompilation} from 'webpack'; | ||
import {flatMap, uniqBy, sumBy, meanBy} from 'lodash'; | ||
import prettyBytes from 'pretty-bytes'; | ||
import {BuildInspectInitialResource} from '@reskript/settings'; | ||
import {RuleProcessor} from './utils'; | ||
|
||
const extractInitialChunks = (compilations: StatsCompilation[]) => { | ||
const chunks = uniqBy(flatMap(compilations, child => child.chunks ?? []), chunk => chunk.id); | ||
const initialChunks = chunks.filter(chunk => chunk.initial); | ||
return initialChunks; | ||
}; | ||
|
||
type StatsChunk = Exclude<StatsCompilation['chunks'], undefined>[0]; | ||
|
||
const findDisallowedImportsInChunks = (chunks: StatsChunk[], imports: string[]) => { | ||
const matchImportInChunks = (disallowed: string) => { | ||
const match = `node_modules/${disallowed}/`; | ||
const matchedChunks = chunks.filter(chunk => chunk.modules?.some(m => m.nameForCondition?.includes(match))); | ||
const toChunkMatch = (chunk: StatsChunk) => { | ||
const file = chunk.files?.[0] ?? '(unknown)'; | ||
return { | ||
file, | ||
moduleName: disallowed, | ||
}; | ||
}; | ||
return matchedChunks.map(toChunkMatch); | ||
}; | ||
|
||
return flatMap(imports, matchImportInChunks); | ||
}; | ||
|
||
export default (compilations: StatsCompilation[], settings: BuildInspectInitialResource) => { | ||
const initialChunks = extractInitialChunks(compilations); | ||
|
||
const count: RuleProcessor<number> = { | ||
config: settings.count, | ||
defaultConfigValue: Infinity, | ||
check: (max, {notice, report}) => { | ||
notice(`Initial resource count: ${initialChunks.length}`); | ||
if (initialChunks.length > max) { | ||
report(`Too many initial resoures, max allowed is ${max}`); | ||
} | ||
return initialChunks.length <= max; | ||
}, | ||
}; | ||
const totalSize: RuleProcessor<number> = { | ||
config: settings.totalSize, | ||
defaultConfigValue: Infinity, | ||
check: (max, {notice, report}) => { | ||
const totalSize = sumBy(initialChunks, chunk => chunk.size); | ||
notice(`Initial resource size: ${prettyBytes(totalSize)} (not gzipped)`); | ||
if (totalSize > max) { | ||
report(`Initial size is too large, max allowed is is ${prettyBytes(max)}`); | ||
} | ||
return totalSize <= max; | ||
}, | ||
}; | ||
const sizeDeviation: RuleProcessor<number> = { | ||
config: settings.sizeDeviation, | ||
defaultConfigValue: Infinity, | ||
check: (max, {report}) => { | ||
const average = meanBy(initialChunks, chunk => chunk.size); | ||
const abnormalChunks = initialChunks.filter(chunk => (chunk.size - average) / average > max); | ||
for (const chunk of abnormalChunks) { | ||
report(`Resource ${chunk.files?.[0]} has unbalanced size to other resources`); | ||
} | ||
return !abnormalChunks.length; | ||
}, | ||
}; | ||
const disallowImports: RuleProcessor<string[]> = { | ||
config: settings.disallowImports, | ||
defaultConfigValue: [], | ||
check: (disallowImports, {report}) => { | ||
const unwantedChunkImports = findDisallowedImportsInChunks(initialChunks, disallowImports); | ||
for (const {file, moduleName} of unwantedChunkImports) { | ||
report(`Initial chunk ${file} includes disallowed module ${moduleName}`); | ||
} | ||
return !unwantedChunkImports.length; | ||
}, | ||
}; | ||
|
||
return [count, totalSize, sizeDeviation, disallowImports]; | ||
}; |
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,63 @@ | ||
import chalk from 'chalk'; | ||
import {RuleConfig, Severity} from '@reskript/settings'; | ||
|
||
const SEVERITY_PREFIX: Record<Severity, string> = { | ||
'off': ' ', | ||
'print': chalk.bgWhite.black(' I '), | ||
'warn': chalk.bgYellow.white(' W '), | ||
'error': chalk.bgRed.white(' E '), | ||
}; | ||
|
||
export const createPrint = (severity: Severity) => (message: string) => { | ||
console.log(`${SEVERITY_PREFIX[severity]} ${message}`); | ||
}; | ||
|
||
export const normalizeRuleConfig = <T>(config: RuleConfig<T>, defaultConfigValue: T): [Severity, T] => { | ||
if (typeof config === 'string') { | ||
return [config, defaultConfigValue]; | ||
} | ||
|
||
return config; | ||
}; | ||
|
||
export interface CheckHelper { | ||
notice: (message: string) => void; | ||
report: (message: string) => void; | ||
} | ||
|
||
export type Check<T> = (configValue: T, helpers: CheckHelper) => boolean; | ||
|
||
export interface RuleProcessor<T> { | ||
config: RuleConfig<T>; | ||
defaultConfigValue: T; | ||
check: Check<T>; | ||
} | ||
|
||
export const run = (processors: Array<RuleProcessor<any>>): void => { | ||
const results = processors.reduce( | ||
(results, processor) => { | ||
const [severity, configValue] = normalizeRuleConfig(processor.config, processor.defaultConfigValue); | ||
|
||
if (severity === 'off') { | ||
return results; | ||
} | ||
|
||
const helpers = { | ||
report: createPrint(severity), | ||
notice: createPrint('print'), | ||
}; | ||
const result = processor.check(configValue, helpers); | ||
|
||
if (!result) { | ||
results.add(severity); | ||
} | ||
|
||
return results; | ||
}, | ||
new Set<Severity>() | ||
); | ||
|
||
if (results.has('error')) { | ||
process.exit(10); | ||
} | ||
}; |
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
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