Skip to content

Commit 549350a

Browse files
authored
feat: implement new --config arg to specify a custom path to configuration file when running CLI (#382)
* feat: implement new --config arg to specify a custom path to configuration file when running CLI * test: extend tests for --config property if the path has invalid or unsupported extension
1 parent 3542c73 commit 549350a

File tree

10 files changed

+104
-3
lines changed

10 files changed

+104
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ npm install --save-dev @size-limit/file
591591
npx size-limit --limit "10 kB" dist/bundle.js
592592
```
593593
594+
Additionally, you can specify a custom path to your configuration file when running the CLI:
595+
596+
```sh
597+
npx size-limit --config src/configs/your-config.{m,c}?{js,ts,json}
598+
```
599+
594600
[Statoscope docs]: https://github.com/statoscope/statoscope/tree/master/packages/webpack-plugin#optionsreports-report
595601
[pattern]: https://github.com/SuperchupuDev/tinyglobby
596602
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const first = 'first'
2+
3+
const second = 'second'
4+
5+
export { first, second }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "config-file-from-arg",
4+
"devDependencies": {
5+
"@size-limit/file": ">= 0.0.0",
6+
"size-limit": ">= 0.0.0"
7+
},
8+
"size-limit": [
9+
{
10+
"path": [
11+
"a.js",
12+
"b.js"
13+
],
14+
"import": "a"
15+
}
16+
]
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default [
2+
{
3+
path: '../../index.js',
4+
limit: 10,
5+
name: 'index'
6+
},
7+
{
8+
path: '../main.js',
9+
limit: 20,
10+
name: 'main'
11+
}
12+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello')

packages/size-limit/get-config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bytes from 'bytes-iec'
22
import { lilconfig } from 'lilconfig'
33
import { createRequire } from 'node:module'
4-
import { dirname, isAbsolute, join, relative } from 'node:path'
4+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
55
import { fileURLToPath, pathToFileURL } from 'node:url'
66
import { glob } from 'tinyglobby'
77

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

152154
if (result === null) throw new SizeLimitError('noConfig')
153155
checkChecks(plugins, result.config)

packages/size-limit/parse-args.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export default function parseArgs(plugins, argv) {
6363
throw new SizeLimitError('argWithoutParameter', 'compare-with', 'FILE')
6464
}
6565
args.compareWith = nextArg
66+
} else if (arg === '--config') {
67+
let nextArg = argv[++i]
68+
if (!nextArg || nextArg.startsWith('--')) {
69+
throw new SizeLimitError('argWithoutParameter', 'config', 'FILE')
70+
}
71+
args.config = nextArg
6672
} else if (arg === '--watch') {
6773
/* c8 ignore next */
6874
args.watch = true

packages/size-limit/test/__snapshots__/run.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ exports[`throws on --compare-with argument without webpack 1`] = `
397397
"
398398
`;
399399

400+
exports[`throws on --config argument without FILE parameter 1`] = `
401+
" ERROR  Missing parameter FILE for --config argument
402+
"
403+
`;
404+
405+
exports[`throws on --config argument without FILE parameter 2`] = `
406+
" ERROR  Missing parameter FILE for --config argument
407+
"
408+
`;
409+
400410
exports[`throws on --save-bundle argument without DIR parameter 1`] = `
401411
" ERROR  Missing parameter DIR for --save-bundle argument
402412
"

packages/size-limit/test/get-config.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'node:path'
1+
import { dirname, join } from 'node:path'
22
import { beforeEach, describe, expect, it, vi } from 'vitest'
33

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

432+
it('takes config from CLI config argument', async () => {
433+
let cwd = 'config-file-from-arg'
434+
let configPath = 'src/configs/my-size-limit.config.js'
435+
expect(await check(cwd, ['--config', fixture(cwd, configPath)])).toEqual({
436+
checks: [
437+
{
438+
files: [fixture(cwd, 'index.js')],
439+
limit: 10,
440+
name: 'index',
441+
path: '../../index.js',
442+
sizeLimit: 10
443+
},
444+
{
445+
files: [fixture(cwd, 'src/main.js')],
446+
limit: 20,
447+
name: 'main',
448+
path: '../main.js',
449+
sizeLimit: 20
450+
}
451+
],
452+
configPath,
453+
cwd: fixture(cwd, dirname(configPath))
454+
})
455+
})
456+
432457
const allConfigFileExtensions = ['mjs', 'js', 'cjs', 'ts', 'mts', 'cts']
433458
const exportTypes = [
434459
{ exportSyntax: 'export default', moduleType: 'esm' },

packages/size-limit/test/run.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,23 @@ it('throws on --compare-with argument without value', async () => {
200200
expect(await error('webpack', ['--why', '--compare-with'])).toMatchSnapshot()
201201
})
202202

203+
it('throws on --config argument without FILE parameter', async () => {
204+
expect(await error('file', ['--config'])).toMatchSnapshot()
205+
expect(await error('file', ['--config', '--why'])).toMatchSnapshot()
206+
})
207+
208+
it('throws on --config argument with invalid FILE parameter', async () => {
209+
expect(await error('file', ['--config', 'invalid/config/path'])).toContain(
210+
'no such file or directory'
211+
)
212+
})
213+
214+
it('throws on --config argument with invalid FILE extension', async () => {
215+
expect(
216+
await error('file', ['--config', 'invalid/config/path.extension'])
217+
).toContain('No loader specified for extension')
218+
})
219+
203220
it('throws on no config', async () => {
204221
expect(await error('file')).toMatchSnapshot()
205222
})

0 commit comments

Comments
 (0)