Skip to content

Commit 73a7dc2

Browse files
committed
refactor: replace cosmiconfig with lilconfig
cosmiconfig is a massive package which recently got a small alternative without dependencies lilconfig. https://github.com/antonk52/lilconfig https://packagephobia.com/result?p=cosmiconfig https://packagephobia.com/result?p=lilconfig Though still a big part of cosmiconfig is `yaml` package. https://packagephobia.com/result?p=yaml This package is important piece of [relay](https://github.com/facebook/relay/blob/378104f341c0682dbad646221db34fc5e0ce41c9/packages/babel-plugin-relay/package.json#L16) and [emotion](https://github.com/emotion-js/emotion/blob/242f7d8c9f3ddbba2a69664bcc0fa22501df849f/packages/babel-plugin/package.json#L22). Would be good to make it as small as possible.
1 parent 7f3bdec commit 73a7dc2

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

other/docs/author.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ To specify that your plugin is configurable, you pass a `configName` to
183183
`createMacro`.
184184

185185
A configuration is created from data combined from two sources: We use
186-
[`cosmiconfig`][cosmiconfig] to read a `babel-plugin-macros` configuration which
187-
can be located in any of the following files up the directories from the
188-
importing file:
186+
[`lilconfig`][lilconfig] to read a `babel-plugin-macros` configuration which can
187+
be located in any of the following files up the directories from the importing
188+
file:
189189

190190
- `.babel-plugin-macrosrc`
191191
- `.babel-plugin-macrosrc.json`
@@ -210,15 +210,14 @@ module.exports = {
210210
module.exports = {
211211
plugins: [
212212
[
213-
"macros",
213+
'macros',
214214
{
215-
taggedTranslations: { locale: "en_GB" },
215+
taggedTranslations: {locale: 'en_GB'},
216216
},
217217
],
218218
],
219219
}
220220

221-
222221
// taggedTranslations.macro.js
223222
const {createMacro} = require('babel-plugin-macros')
224223
module.exports = createMacro(taggedTranslationsMacro, {
@@ -343,4 +342,4 @@ function myMacro({references, state, babel}) {
343342
[keyword]: https://docs.npmjs.com/files/package.json#keywords
344343
[npm-babel-plugin-macros]:
345344
https://www.npmjs.com/browse/keyword/babel-plugin-macros
346-
[cosmiconfig]: https://www.npmjs.com/package/cosmiconfig
345+
[lilconfig]: https://www.npmjs.com/package/lilconfig

other/docs/user.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ $ yarn upgrade react-scripts
9797
### config
9898

9999
There is a feature that allows you to configure your macro. We use
100-
[`cosmiconfig`][cosmiconfig] to read a `babel-plugin-macros` configuration which
101-
can be located in any of the following files up the directories from the
102-
importing file:
100+
[`lilconfig`][lilconfig] to read a `babel-plugin-macros` configuration which can
101+
be located in any of the following files up the directories from the importing
102+
file:
103103

104104
- `.babel-plugin-macrosrc`
105105
- `.babel-plugin-macrosrc.json`
@@ -123,5 +123,5 @@ module.exports = {
123123
}
124124
```
125125

126-
[cosmiconfig]: https://www.npmjs.com/package/cosmiconfig
126+
[lilconfig]: https://www.npmjs.com/package/lilconfig
127127
[styled-components]: https://www.styled-components.com/docs/tooling#babel-macro

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"license": "MIT",
2828
"dependencies": {
2929
"@babel/runtime": "^7.12.5",
30-
"cosmiconfig": "^7.0.0",
31-
"resolve": "^1.19.0"
30+
"lilconfig": "^2.0.4",
31+
"resolve": "^1.19.0",
32+
"yaml": "^1.10.2"
3233
},
3334
"devDependencies": {
3435
"@babel/core": "^7.12.9",

src/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ configured\`stuff\`
343343
344344
↓ ↓ ↓ ↓ ↓ ↓
345345
346-
Error: this is a cosmiconfig error
346+
Error: this is a lilconfig error
347347
348348
`;
349349

src/__tests__/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import path from 'path'
2-
import {cosmiconfigSync as cosmiconfigSyncMock} from 'cosmiconfig'
2+
import {lilconfigSync as lilconfigSyncMock} from 'lilconfig'
33
import cpy from 'cpy'
44
import babel from '@babel/core'
55
import pluginTester from 'babel-plugin-tester'
66
import plugin from '../'
77

88
const projectRoot = path.join(__dirname, '../../')
99

10-
jest.mock('cosmiconfig', () => {
11-
const cosmiconfigExports = jest.requireActual('cosmiconfig')
12-
const actualCosmiconfigSync = cosmiconfigExports.cosmiconfigSync
13-
function fakeCosmiconfigSync(...args) {
14-
fakeCosmiconfigSync.explorer = actualCosmiconfigSync(...args)
15-
return fakeCosmiconfigSync.explorer
10+
jest.mock('lilconfig', () => {
11+
const lilconfigExports = jest.requireActual('lilconfig')
12+
const actualLilconfigSync = lilconfigExports.lilconfigSync
13+
function fakeLilconfigSync(...args) {
14+
fakeLilconfigSync.explorer = actualLilconfigSync(...args)
15+
return fakeLilconfigSync.explorer
1616
}
17-
return {...cosmiconfigExports, cosmiconfigSync: fakeCosmiconfigSync}
17+
return {...lilconfigExports, lilconfigSync: fakeLilconfigSync}
1818
})
1919

2020
beforeAll(() => {
@@ -296,9 +296,9 @@ pluginTester({
296296
fixture: path.join(__dirname, 'fixtures/config/code.js'),
297297
setup() {
298298
jest
299-
.spyOn(cosmiconfigSyncMock.explorer, 'search')
299+
.spyOn(lilconfigSyncMock.explorer, 'search')
300300
.mockImplementationOnce(() => {
301-
throw new Error('this is a cosmiconfig error')
301+
throw new Error('this is a lilconfig error')
302302
})
303303
jest.spyOn(console, 'error').mockImplementationOnce(() => {})
304304
return function teardown() {
@@ -319,7 +319,7 @@ pluginTester({
319319
fixture: path.join(__dirname, 'fixtures/config/code.js'),
320320
setup() {
321321
jest
322-
.spyOn(cosmiconfigSyncMock.explorer, 'search')
322+
.spyOn(lilconfigSyncMock.explorer, 'search')
323323
.mockImplementationOnce(() => {
324324
return null
325325
})

src/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const p = require('path')
22
const resolve = require('resolve')
33
// const printAST = require('ast-pretty-print')
44

5+
const {lilconfigSync} = require('lilconfig')
6+
57
const macrosRegex = /[./]macro(\.c?js)?$/
68
const testMacrosRegex = v => macrosRegex.test(v)
79

@@ -19,12 +21,17 @@ class MacroError extends Error {
1921
}
2022
}
2123

24+
function loadYaml(filepath, content) {
25+
// Lazy load yaml since it is a relatively large bundle
26+
const yaml = require('yaml')
27+
return yaml.parse(content)
28+
}
29+
2230
let _configExplorer = null
2331
function getConfigExplorer() {
2432
return (_configExplorer =
2533
_configExplorer ||
26-
// Lazy load cosmiconfig since it is a relatively large bundle
27-
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
34+
lilconfigSync('babel-plugin-macros', {
2835
searchPlaces: [
2936
'package.json',
3037
'.babel-plugin-macrosrc',
@@ -35,6 +42,12 @@ function getConfigExplorer() {
3542
'babel-plugin-macros.config.js',
3643
],
3744
packageProp: 'babelMacros',
45+
loaders: {
46+
'.yaml': loadYaml,
47+
'.yml': loadYaml,
48+
// loader for files with no extension
49+
noExt: loadYaml,
50+
},
3851
}))
3952
}
4053

0 commit comments

Comments
 (0)