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

feat: implement new --config arg to specify a custom path to configuration file when running CLI #382

Merged
merged 2 commits into from
Feb 18, 2025
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -591,6 +591,12 @@ npm install --save-dev @size-limit/file
npx size-limit --limit "10 kB" dist/bundle.js
```

Additionally, you can specify a custom path to your configuration file when running the CLI:

```sh
npx size-limit --config src/configs/your-config.{m,c}?{js,ts,json}
```

[Statoscope docs]: https://github.com/statoscope/statoscope/tree/master/packages/webpack-plugin#optionsreports-report
[pattern]: https://github.com/SuperchupuDev/tinyglobby

5 changes: 5 additions & 0 deletions fixtures/config-file-from-arg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const first = 'first'

const second = 'second'

export { first, second }
17 changes: 17 additions & 0 deletions fixtures/config-file-from-arg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"name": "config-file-from-arg",
"devDependencies": {
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
},
"size-limit": [
{
"path": [
"a.js",
"b.js"
],
"import": "a"
}
]
}
12 changes: 12 additions & 0 deletions fixtures/config-file-from-arg/src/configs/my-size-limit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default [
{
path: '../../index.js',
limit: 10,
name: 'index'
},
{
path: '../main.js',
limit: 20,
name: 'main'
}
]
1 change: 1 addition & 0 deletions fixtures/config-file-from-arg/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello')
6 changes: 4 additions & 2 deletions packages/size-limit/get-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bytes from 'bytes-iec'
import { lilconfig } from 'lilconfig'
import { createRequire } from 'node:module'
import { dirname, isAbsolute, join, relative } from 'node:path'
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { glob } from 'tinyglobby'

@@ -147,7 +147,9 @@ export default async function getConfig(plugins, process, args, pkg) {
'.size-limit.cts'
]
})
let result = await explorer.search(process.cwd())
let result = args.config?.trim()
? await explorer.load(resolve(args.config.trim()))
: await explorer.search(process.cwd())

if (result === null) throw new SizeLimitError('noConfig')
checkChecks(plugins, result.config)
6 changes: 6 additions & 0 deletions packages/size-limit/parse-args.js
Original file line number Diff line number Diff line change
@@ -63,6 +63,12 @@ export default function parseArgs(plugins, argv) {
throw new SizeLimitError('argWithoutParameter', 'compare-with', 'FILE')
}
args.compareWith = nextArg
} else if (arg === '--config') {
let nextArg = argv[++i]
if (!nextArg || nextArg.startsWith('--')) {
throw new SizeLimitError('argWithoutParameter', 'config', 'FILE')
}
args.config = nextArg
} else if (arg === '--watch') {
/* c8 ignore next */
args.watch = true
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
@@ -397,6 +397,16 @@ exports[`throws on --compare-with argument without webpack 1`] = `
"
`;

exports[`throws on --config argument without FILE parameter 1`] = `
" ERROR  Missing parameter FILE for --config argument
"
`;

exports[`throws on --config argument without FILE parameter 2`] = `
" ERROR  Missing parameter FILE for --config argument
"
`;

exports[`throws on --save-bundle argument without DIR parameter 1`] = `
" ERROR  Missing parameter DIR for --save-bundle argument
"
27 changes: 26 additions & 1 deletion packages/size-limit/test/get-config.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'node:path'
import { dirname, join } from 'node:path'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import calc from '../calc'
@@ -429,6 +429,31 @@ it('normalizes import', async () => {
})
})

it('takes config from CLI config argument', async () => {
let cwd = 'config-file-from-arg'
let configPath = 'src/configs/my-size-limit.config.js'
expect(await check(cwd, ['--config', fixture(cwd, configPath)])).toEqual({
checks: [
{
files: [fixture(cwd, 'index.js')],
limit: 10,
name: 'index',
path: '../../index.js',
sizeLimit: 10
},
{
files: [fixture(cwd, 'src/main.js')],
limit: 20,
name: 'main',
path: '../main.js',
sizeLimit: 20
}
],
configPath,
cwd: fixture(cwd, dirname(configPath))
})
})

const allConfigFileExtensions = ['mjs', 'js', 'cjs', 'ts', 'mts', 'cts']
const exportTypes = [
{ exportSyntax: 'export default', moduleType: 'esm' },
17 changes: 17 additions & 0 deletions packages/size-limit/test/run.test.js
Original file line number Diff line number Diff line change
@@ -200,6 +200,23 @@ it('throws on --compare-with argument without value', async () => {
expect(await error('webpack', ['--why', '--compare-with'])).toMatchSnapshot()
})

it('throws on --config argument without FILE parameter', async () => {
expect(await error('file', ['--config'])).toMatchSnapshot()
expect(await error('file', ['--config', '--why'])).toMatchSnapshot()
})

it('throws on --config argument with invalid FILE parameter', async () => {
expect(await error('file', ['--config', 'invalid/config/path'])).toContain(
'no such file or directory'
)
})

it('throws on --config argument with invalid FILE extension', async () => {
expect(
await error('file', ['--config', 'invalid/config/path.extension'])
).toContain('No loader specified for extension')
})

it('throws on no config', async () => {
expect(await error('file')).toMatchSnapshot()
})