diff --git a/other/docs/author.md b/other/docs/author.md index abf1a9d..f135600 100644 --- a/other/docs/author.md +++ b/other/docs/author.md @@ -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` @@ -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, { @@ -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 diff --git a/other/docs/user.md b/other/docs/user.md index 060d865..f80a65f 100644 --- a/other/docs/user.md +++ b/other/docs/user.md @@ -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` @@ -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 diff --git a/package.json b/package.json index 093f801..23c641e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 79fe790..1d7a28d 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -343,7 +343,7 @@ configured\`stuff\` ↓ ↓ ↓ ↓ ↓ ↓ -Error: this is a cosmiconfig error +Error: this is a lilconfig error `; diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 0ef561c..89ab10b 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -1,5 +1,5 @@ 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' @@ -7,14 +7,14 @@ 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(() => { @@ -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() { @@ -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 }) diff --git a/src/index.js b/src/index.js index 253f36c..283be9b 100644 --- a/src/index.js +++ b/src/index.js @@ -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) @@ -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', @@ -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, + }, })) }