Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more Babel configuration files. #6

Merged
merged 9 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
"test",
"infra"
]
},
{
"login": "stephenjang",
"name": "Jiyoon Jang",
"avatar_url": "https://avatars2.githubusercontent.com/u/26544?v=4",
"profile": "http://jiyoonjang.com",
"contributions": [
"code",
"test"
]
}
]
],
"repoType": "github",
"commitConvention": "none"
}
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ sudo: false
node_js:
- '12'
- '10'
- '8'

cache:
yarn: true
Expand Down
20 changes: 10 additions & 10 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
- The use of sexualized language or imagery and unwelcome sexual attention or advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Expand Down
Empty file.
4 changes: 4 additions & 0 deletions fixture/babelconfigjs/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ['es2015', 'react'],
plugins: ['transform-class-properties'],
}
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ const fs = require('fs')
const findUp = require('find-up')
const json5 = require('json5')

const BABELRC = '.babelrc'
const BABELRC_JS = '.babelrc.js'
// https://babeljs.io/docs/en/config-files#supported-file-extensions
const BABELRC = ['.babelrc', '.babelrc.json', 'babel.config.json']
const BABELRC_JS = [
'.babelrc.js',
'.babelrc.cjs',
'.babelrc.mjs',
'babel.config.js',
'babel.config.cjs',
'babel.config.mjs',
]
const PKG = 'package.json'

const readConfigJs = fp => {
Expand All @@ -24,11 +32,13 @@ const readConfigJs = fp => {
const loadConfig = fp => {
const file = path.basename(fp)
const obj = fs.readFileSync(fp, 'utf-8')
const matchBabelRc = /^(?<babelrc>\.babelrc(?:.json|$)|babel\.config\.json)$/giu
const matchBabelRcJs = /^(?<babelrc>\.babelrc|babel\.config)[.](?:js|cjs|mjs)$/giu

switch (file) {
case BABELRC_JS:
case matchBabelRcJs.test(file) ? file : null:
return readConfigJs(fp)
case BABELRC:
case matchBabelRc.test(file) ? file : null:
return json5.parse(obj)
case PKG:
return JSON.parse(obj).babel
Expand All @@ -38,7 +48,7 @@ const loadConfig = fp => {
}

module.exports = opts => {
return findUp([BABELRC, BABELRC_JS, PKG], opts).then(fp => {
return findUp([...BABELRC, ...BABELRC_JS, PKG], opts).then(fp => {
if (!fp) {
return {}
}
Expand All @@ -50,7 +60,7 @@ module.exports = opts => {
}

module.exports.sync = opts => {
const fp = findUp.sync([BABELRC, BABELRC_JS, PKG], opts)
const fp = findUp.sync([...BABELRC, ...BABELRC_JS, PKG], opts)

if (!fp) {
return {}
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "read-babelrc-up",
"version": "1.0.0",
"description": "Read the closet .babelrc file",
"description": "Read the closet Babel configuration file",
"license": "MIT",
"repository": "akameco/read-babelrc-up",
"author": {
"name": "akameco",
"email": "akameco.t@gmail.com",
"url": "akameco.github.io"
"url": "https://akameco.github.io"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"add-contributor": "all-contributors add",
Expand All @@ -36,6 +36,7 @@
"babel",
"babelrc",
".babelrc",
"babel.config.js",
"json",
"file",
"parse",
Expand All @@ -46,16 +47,16 @@
],
"dependencies": {
"find-up": "^4.1.0",
"json5": "^2.1.0"
"json5": "^2.1.1"
},
"devDependencies": {
"all-contributors-cli": "^6.8.2",
"all-contributors-cli": "^6.13.0",
"ava": "^2.3.0",
"eslint": "^6.3.0",
"eslint-config-precure": "^5.0.2",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"prettier": "^1.18.2"
"eslint": "^6.8.0",
"eslint-config-precure": "^5.2.0",
"husky": "^4.2.3",
"lint-staged": "^10.0.7",
"prettier": "^1.19.1"
},
"husky": {
"hooks": {
Expand Down
18 changes: 13 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)

> Read the closet .babelrc or .babelrc.js or babel in package.json.
> Read the closet [Babel configuration file](https://babeljs.io/docs/en/config-files) or `babel` in package.json.

## Install

Expand Down Expand Up @@ -51,16 +51,24 @@ Directory to start looking for .babelrc file.

## Related

* [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
<table><tr><td align="center"><a href="http://akameco.github.io"><img src="https://avatars2.githubusercontent.com/u/4002137?v=4" width="100px;" alt="akameco"/><br /><sub><b>akameco</b></sub></a><br /><a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Code">💻</a> <a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Documentation">📖</a> <a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Tests">⚠️</a> <a href="#infra-akameco" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td></tr></table>

<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="http://akameco.github.io"><img src="https://avatars2.githubusercontent.com/u/4002137?v=4" width="100px;" alt=""/><br /><sub><b>akameco</b></sub></a><br /><a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Code">💻</a> <a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Documentation">📖</a> <a href="https://github.com/akameco/read-babelrc-up/commits?author=akameco" title="Tests">⚠️</a> <a href="#infra-akameco" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
<td align="center"><a href="http://jiyoonjang.com"><img src="https://avatars2.githubusercontent.com/u/26544?v=4" width="100px;" alt=""/><br /><sub><b>Jiyoon Jang</b></sub></a><br /><a href="https://github.com/akameco/read-babelrc-up/commits?author=stephenjang" title="Code">💻</a> <a href="https://github.com/akameco/read-babelrc-up/commits?author=stephenjang" title="Tests">⚠️</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ import fn from '.'

const babelrcPath = path.resolve('fixture', 'babelrc', '.babelrc')
const babelrcJsPath = path.resolve('fixture', 'babelrcjs', '.babelrc.js')
const babelConfigJsPath = path.resolve(
'fixture',
'babelconfigjs',
'babel.config.js'
)
const pkgPath = path.resolve('fixture', 'pkg', 'package.json')

test('parse babel.config.js', async t => {
const cwd = path.join('fixture', 'babelconfigjs')
const { path: p, babel } = await fn({ cwd })
t.is(p, babelConfigJsPath)
t.snapshot(babel)
})

test('parse babel.config.js - sync', t => {
const cwd = path.join('fixture', 'babelconfigjs')
const { path: p, babel } = fn.sync({ cwd })
t.is(p, babelConfigJsPath)
t.snapshot(babel)
})

test('async', async t => {
const cwd = path.join('fixture', 'babelrc', 'app')
const { path: p, babel } = await fn({ cwd })
Expand Down
28 changes: 28 additions & 0 deletions test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,31 @@ Generated by [AVA](https://ava.li).
'react',
],
}

## parse babel.config.js

> Snapshot 1

{
plugins: [
'transform-class-properties',
],
presets: [
'es2015',
'react',
],
}

## parse babel.config.js - sync

> Snapshot 1

{
plugins: [
'transform-class-properties',
],
presets: [
'es2015',
'react',
],
}
Binary file modified test.js.snap
Binary file not shown.