Skip to content

Commit 04529e2

Browse files
authoredOct 23, 2021
perf: replace cosmiconfig with lilconfig (#981)
Replace 'cosmiconfig' with 'lilconfig' due to the smaller size and dependency tree of the latter package. This also installs 'js-yaml', since 'lilconfig' doesn't provide YAML parsing.
1 parent f861d8d commit 04529e2

8 files changed

+229
-170
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Starting with v3.1 you can now use different ways of configuring lint-staged:
105105
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in JS format
106106
- Pass a configuration file using the `--config` or `-c` flag
107107
108-
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
108+
See [lilconfig](https://github.com/antonk52/lilconfig) for more details on what formats are supported.
109109
110110
Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns.
111111

‎lib/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const { cosmiconfig } = require('cosmiconfig')
3+
const { lilconfig } = require('lilconfig')
4+
const { yaml } = require('js-yaml')
45
const debugLog = require('debug')('lint-staged')
56
const stringifyObject = require('stringify-object')
67

@@ -24,8 +25,10 @@ const resolveConfig = (configPath) => {
2425
}
2526
}
2627

28+
const jsYamlLoad = (filepath, content) => yaml.load(content)
29+
2730
const loadConfig = (configPath) => {
28-
const explorer = cosmiconfig('lint-staged', {
31+
const explorer = lilconfig('lint-staged', {
2932
searchPlaces: [
3033
'package.json',
3134
'.lintstagedrc',
@@ -37,6 +40,11 @@ const loadConfig = (configPath) => {
3740
'lint-staged.config.js',
3841
'lint-staged.config.cjs',
3942
],
43+
loaders: {
44+
'.yml': jsYamlLoad,
45+
'.yaml': jsYamlLoad,
46+
noExt: jsYamlLoad,
47+
},
4048
})
4149

4250
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
@@ -84,7 +92,7 @@ const lintStaged = async (
8492
) => {
8593
await validateOptions({ shell }, logger)
8694

87-
debugLog('Loading config using `cosmiconfig`')
95+
debugLog('Loading config using `lilconfig`')
8896

8997
const resolved = configObject
9098
? { config: configObject, filepath: '(input)' }

‎package-lock.json

+197-147
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
"cli-truncate": "2.1.0",
3131
"colorette": "^1.4.0",
3232
"commander": "^8.2.0",
33-
"cosmiconfig": "^7.0.1",
3433
"debug": "^4.3.2",
3534
"enquirer": "^2.3.6",
3635
"execa": "^5.1.1",
36+
"js-yaml": "^4.1.0",
37+
"lilconfig": "^2.0.3",
3738
"listr2": "^3.12.2",
3839
"micromatch": "^4.0.4",
3940
"normalize-path": "^3.0.0",

‎test/__mocks__/cosmiconfig.js

-7
This file was deleted.

‎test/__mocks__/lilconfig.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const actual = jest.requireActual('lilconfig')
2+
3+
function lilconfig(name, options) {
4+
return actual.lilconfig(name, options)
5+
}
6+
7+
module.exports.lilconfig = jest.fn(lilconfig)

‎test/index.spec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cosmiconfig } from 'cosmiconfig'
1+
import { lilconfig } from 'lilconfig'
22
import makeConsoleMock from 'consolemock'
33
import path from 'path'
44

@@ -14,8 +14,8 @@ import { InvalidOptionsError } from '../lib/symbols'
1414
import validateOptions from '../lib/validateOptions'
1515
import { replaceSerializer } from './utils/replaceSerializer'
1616

17-
const mockCosmiconfigWith = (result) => {
18-
cosmiconfig.mockImplementationOnce(() => ({
17+
const mockLilconfigWith = (result) => {
18+
lilconfig.mockImplementationOnce(() => ({
1919
search: () => Promise.resolve(result),
2020
}))
2121
}
@@ -33,11 +33,11 @@ describe('lintStaged', () => {
3333
logger.clearHistory()
3434
})
3535

36-
it('should use cosmiconfig if no params are passed', async () => {
36+
it('should use lilconfig if no params are passed', async () => {
3737
expect.assertions(1)
3838

3939
const config = { '*': 'mytask' }
40-
mockCosmiconfigWith({ config })
40+
mockLilconfigWith({ config })
4141

4242
await lintStaged(undefined, logger)
4343

@@ -60,7 +60,7 @@ describe('lintStaged', () => {
6060
it('should use use the console if no logger is passed', async () => {
6161
expect.assertions(2)
6262

63-
mockCosmiconfigWith({ config: {} })
63+
mockLilconfigWith({ config: {} })
6464

6565
const previousConsole = console
6666
const mockedConsole = makeConsoleMock()
@@ -79,7 +79,7 @@ describe('lintStaged', () => {
7979
expect.assertions(1)
8080

8181
const config = { '*': 'mytask' }
82-
mockCosmiconfigWith({ config })
82+
mockLilconfigWith({ config })
8383

8484
await lintStaged({ debug: true, quiet: true }, logger)
8585

@@ -96,7 +96,7 @@ describe('lintStaged', () => {
9696
expect.assertions(1)
9797

9898
const config = { '*': 'mytask' }
99-
mockCosmiconfigWith({ config })
99+
mockLilconfigWith({ config })
100100

101101
await lintStaged({ quiet: true }, logger)
102102

@@ -119,7 +119,7 @@ describe('lintStaged', () => {
119119

120120
it('should throw when invalid config is provided', async () => {
121121
const config = {}
122-
mockCosmiconfigWith({ config })
122+
mockLilconfigWith({ config })
123123

124124
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
125125
`[Error: Configuration should not be empty!]`
@@ -200,7 +200,7 @@ describe('lintStaged', () => {
200200
it('should print helpful error message when config file is not found', async () => {
201201
expect.assertions(2)
202202

203-
mockCosmiconfigWith(null)
203+
mockLilconfigWith(null)
204204

205205
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
206206
`[Error: Config could not be found]`

‎test/integration.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ansiSerializer from 'jest-snapshot-serializer-ansi'
44
import normalize from 'normalize-path'
55
import path from 'path'
66

7-
jest.unmock('cosmiconfig')
7+
jest.unmock('lilconfig')
88
jest.unmock('execa')
99

1010
import execGitBase from '../lib/execGit'

0 commit comments

Comments
 (0)
Please sign in to comment.