Skip to content

Commit

Permalink
refactor: replace cosmiconfig with lilconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Jan 7, 2022
1 parent 6801972 commit 235fdef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
13 changes: 6 additions & 7 deletions other/docs/author.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ To specify that your plugin is configurable, you pass a `configName` to
`createMacro`.

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

- `.babel-plugin-macrosrc`
- `.babel-plugin-macrosrc.json`
Expand All @@ -210,15 +210,14 @@ module.exports = {
module.exports = {
plugins: [
[
"macros",
'macros',
{
taggedTranslations: { locale: "en_GB" },
taggedTranslations: {locale: 'en_GB'},
},
],
],
}


// taggedTranslations.macro.js
const {createMacro} = require('babel-plugin-macros')
module.exports = createMacro(taggedTranslationsMacro, {
Expand Down Expand Up @@ -343,4 +342,4 @@ function myMacro({references, state, babel}) {
[keyword]: https://docs.npmjs.com/files/package.json#keywords
[npm-babel-plugin-macros]:
https://www.npmjs.com/browse/keyword/babel-plugin-macros
[cosmiconfig]: https://www.npmjs.com/package/cosmiconfig
[lilconfig]: https://www.npmjs.com/package/lilconfig
8 changes: 4 additions & 4 deletions other/docs/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ $ yarn upgrade react-scripts
### config

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

- `.babel-plugin-macrosrc`
- `.babel-plugin-macrosrc.json`
Expand All @@ -123,5 +123,5 @@ module.exports = {
}
```

[cosmiconfig]: https://www.npmjs.com/package/cosmiconfig
[lilconfig]: https://www.npmjs.com/package/lilconfig
[styled-components]: https://www.styled-components.com/docs/tooling#babel-macro
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.12.5",
"cosmiconfig": "^7.0.0",
"resolve": "^1.19.0"
"lilconfig": "^2.0.4",
"resolve": "^1.19.0",
"yaml": "^1.10.2"
},
"devDependencies": {
"@babel/core": "^7.12.9",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ configured\`stuff\`
↓ ↓ ↓ ↓ ↓ ↓
Error: this is a cosmiconfig error
Error: this is a lilconfig error
`;
Expand Down
22 changes: 11 additions & 11 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import path from 'path'
import {cosmiconfigSync as cosmiconfigSyncMock} from 'cosmiconfig'
import {lilconfigSync as lilconfigSyncMock} from 'lilconfig'
import cpy from 'cpy'
import babel from '@babel/core'
import pluginTester from 'babel-plugin-tester'
import plugin from '../'

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

jest.mock('cosmiconfig', () => {
const cosmiconfigExports = jest.requireActual('cosmiconfig')
const actualCosmiconfigSync = cosmiconfigExports.cosmiconfigSync
function fakeCosmiconfigSync(...args) {
fakeCosmiconfigSync.explorer = actualCosmiconfigSync(...args)
return fakeCosmiconfigSync.explorer
jest.mock('lilconfig', () => {
const lilconfigExports = jest.requireActual('lilconfig')
const actualLilconfigSync = lilconfigExports.lilconfigSync
function fakeLilconfigSync(...args) {
fakeLilconfigSync.explorer = actualLilconfigSync(...args)
return fakeLilconfigSync.explorer
}
return {...cosmiconfigExports, cosmiconfigSync: fakeCosmiconfigSync}
return {...lilconfigExports, lilconfigSync: fakeLilconfigSync}
})

beforeAll(() => {
Expand Down Expand Up @@ -296,9 +296,9 @@ pluginTester({
fixture: path.join(__dirname, 'fixtures/config/code.js'),
setup() {
jest
.spyOn(cosmiconfigSyncMock.explorer, 'search')
.spyOn(lilconfigSyncMock.explorer, 'search')
.mockImplementationOnce(() => {
throw new Error('this is a cosmiconfig error')
throw new Error('this is a lilconfig error')
})
jest.spyOn(console, 'error').mockImplementationOnce(() => {})
return function teardown() {
Expand All @@ -319,7 +319,7 @@ pluginTester({
fixture: path.join(__dirname, 'fixtures/config/code.js'),
setup() {
jest
.spyOn(cosmiconfigSyncMock.explorer, 'search')
.spyOn(lilconfigSyncMock.explorer, 'search')
.mockImplementationOnce(() => {
return null
})
Expand Down
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const p = require('path')
const resolve = require('resolve')
// const printAST = require('ast-pretty-print')

const {lilconfigSync} = require('lilconfig')

const macrosRegex = /[./]macro(\.c?js)?$/
const testMacrosRegex = v => macrosRegex.test(v)

Expand All @@ -19,12 +21,17 @@ class MacroError extends Error {
}
}

function loadYaml(filepath, content) {
// Lazy load yaml since it is a relatively large bundle
const yaml = require('yaml')
return yaml.parse(content)
}

let _configExplorer = null
function getConfigExplorer() {
return (_configExplorer =
_configExplorer ||
// Lazy load cosmiconfig since it is a relatively large bundle
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
lilconfigSync('babel-plugin-macros', {
searchPlaces: [
'package.json',
'.babel-plugin-macrosrc',
Expand All @@ -35,6 +42,12 @@ function getConfigExplorer() {
'babel-plugin-macros.config.js',
],
packageProp: 'babelMacros',
loaders: {
'.yaml': loadYaml,
'.yml': loadYaml,
// loader for files with no extension
noExt: loadYaml,
},
}))
}

Expand Down

0 comments on commit 235fdef

Please sign in to comment.