Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ Each section in the config can have these options:
* **compareWith**: path to `stats.json` from another build to compare
(when `--why` is using).
* **uiReports**: custom UI reports list (see [Statoscope docs]).
* **disablePlugins**: npm package names of plugins to skip
for this check. For example: `"@size-limit/webpack"`, `"@size-limit/esbuild"`,
or `"@size-limit/time"`.

If you use Size Limit to track the size of CSS files, make sure to set
`webpack: false`. Otherwise, you will get wrong numbers, because webpack
Expand Down
1 change: 1 addition & 0 deletions fixtures/disable-plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 1
14 changes: 14 additions & 0 deletions fixtures/disable-plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"size-limit": [
{
"path": "index.js",
"disablePlugins": ["@size-limit/esbuild"]
}
],
"devDependencies": {
"@size-limit/esbuild": ">= 0.0.0",
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
5 changes: 5 additions & 0 deletions fixtures/non-string-disable-plugins/.size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = [
{
"disablePlugins": 1
}
]
7 changes: 7 additions & 0 deletions fixtures/non-string-disable-plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
6 changes: 6 additions & 0 deletions fixtures/string-disable-plugins/.size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = [
{
"path": "index.js",
"disablePlugins": "@size-limit/esbuild"
}
]
1 change: 1 addition & 0 deletions fixtures/string-disable-plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test'
8 changes: 8 additions & 0 deletions fixtures/string-disable-plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"devDependencies": {
"@size-limit/esbuild": ">= 0.0.0",
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
13 changes: 10 additions & 3 deletions packages/size-limit/calc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
function isPluginDisabled(plugin, check) {
if (!check.disablePlugins) return false
return check.disablePlugins.includes(plugin.name)
}

export default async function calc(plugins, config, createSpinner) {
process.setMaxListeners(config.checks.reduce((a, i) => a + i.files.length, 1))

async function step(number) {
for (let plugin of plugins.list) {
let checks = config.checks.filter(i => !isPluginDisabled(plugin, i))
let spinner
if (plugin['wait' + number] && createSpinner) {
if (checks.length > 0 && plugin['wait' + number] && createSpinner) {
spinner = createSpinner(plugin['wait' + number]).start()
}
if (plugin['step' + number]) {
try {
await Promise.all(
config.checks.map(i => {
checks.map(i => {
return plugin['step' + number](config, i)
})
)
Expand All @@ -26,8 +32,9 @@ export default async function calc(plugins, config, createSpinner) {
async function callMethodForEachPlugin(methodName) {
for (let plugin of plugins.list) {
if (plugin[methodName]) {
let checks = config.checks.filter(i => !isPluginDisabled(plugin, i))
await Promise.all(
config.checks.map(i => {
checks.map(i => {
return plugin[methodName](config, i)
})
)
Expand Down
8 changes: 8 additions & 0 deletions packages/size-limit/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let OPTIONS = {
compareWith: 'webpack',
config: ['webpack', 'esbuild'],
disableModuleConcatenation: 'webpack',
disablePlugins: true,
entry: 'webpack',
gzip: 'file',
hidePassed: false,
Expand Down Expand Up @@ -43,6 +44,10 @@ function isStringsOrUndefined(value) {
return type === 'undefined' || type === 'string' || isStrings(value)
}

function isStringArrayOrUndefined(value) {
return typeof value === 'undefined' || isStrings(value)
}

function endsWithMs(value) {
return / ?ms/i.test(value)
}
Expand All @@ -68,6 +73,9 @@ function checkChecks(plugins, checks) {
if (!isStringsOrUndefined(check.entry)) {
throw new SizeLimitError('entryNotString')
}
if (!isStringArrayOrUndefined(check.disablePlugins)) {
throw new SizeLimitError('disablePluginsNotArray')
}
for (let opt in check) {
let available = OPTIONS[opt]
if (typeof available === 'string') {
Expand Down
6 changes: 6 additions & 0 deletions packages/size-limit/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface Check {

disableModuleConcatenation?: boolean

/**
* Plugin npm package names to skip for this check.
* For example: `["@size-limit/webpack"]`, `["@size-limit/esbuild"]`, or `["@size-limit/time"]`.
*/
disablePlugins?: string[]

/**
* When using a custom webpack config, a webpack entry could be given.
* It could be a string or an array of strings. By default,
Expand Down
3 changes: 3 additions & 0 deletions packages/size-limit/size-limit-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const MESSAGES = {
`The directory *${dir}* is not empty. ` +
'Pass *--clean-dir* if you want to remove it',
cmdError: (cmd, error) => (error ? `${cmd} error: ${error}` : `${cmd} error`),
disablePluginsNotArray: () =>
'The *disablePlugins* in Size Limit config ' +
'must be *an array of strings*',
emptyConfig: () => 'Size Limit config must *not be empty*',
entryNotString: () =>
'The *entry* in Size Limit config ' +
Expand Down
10 changes: 10 additions & 0 deletions packages/size-limit/test/__snapshots__/run.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ exports[`throws on non-array config 1`] = `
"
`;

exports[`throws on non-array disablePlugins 1`] = `
" ERROR  The disablePlugins in Size Limit config must be an array of strings
"
`;

exports[`throws on non-object check 1`] = `
" ERROR  Size Limit config array should contain only objects

Expand Down Expand Up @@ -511,6 +516,11 @@ exports[`throws on running option without time plugin 1`] = `
"
`;

exports[`throws on string disablePlugins 1`] = `
" ERROR  The disablePlugins in Size Limit config must be an array of strings
"
`;

exports[`throws on time limit without time plugin 1`] = `
" ERROR  Add @size-limit/time plugin to use time limit
"
Expand Down
15 changes: 15 additions & 0 deletions packages/size-limit/test/get-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ it('uses peerDependencies as ignore option', async () => {
})
})

it('supports disablePlugins option', async () => {
expect(await check('disable-plugins')).toEqual({
checks: [
{
disablePlugins: ['@size-limit/esbuild'],
files: [fixture('disable-plugins', 'index.js')],
name: 'index.js',
path: 'index.js'
}
],
configPath: 'package.json',
cwd: fixture('disable-plugins')
})
})

it('normalizes time limits', async () => {
expect(await check('time')).toEqual({
checks: [
Expand Down
17 changes: 17 additions & 0 deletions packages/size-limit/test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,23 @@ it('throws on non-string entry', async () => {
expect(await error('non-string-entry')).toMatchSnapshot()
})

it('throws on non-array disablePlugins', async () => {
expect(await error('non-string-disable-plugins')).toMatchSnapshot()
})

it('throws on string disablePlugins', async () => {
expect(await error('string-disable-plugins')).toMatchSnapshot()
})

it('respects disablePlugins option', async () => {
await checkJson('disable-plugins', [
{
name: 'index.js',
size: 20
}
])
})

it('throws on webpack option without webpack plugin', async () => {
expect(await error('non-webpack')).toMatchSnapshot()
expect(await error('non-webpack-ignore')).toMatchSnapshot()
Expand Down
Loading