Skip to content

Commit 5d99117

Browse files
weyertKent C. Dodds
authored andcommitted
feat: add support for flow in babel (#77)
Detects whether `flow-bin` is a dependency on the project, if so it will load the `@babel/preset-flow`-module. **What**: In `babelrc.js` we should check if `flow-bin` is a dependency and then load the `@babel/preset-flow` **Why**: We have copied the same away `react` is being detected by using `ifAnyDep`-utility **How**: Small change to `babelrc.js` **Checklist**: - [X] Documentation - [ ] Tests - [X] Ready to be merged - [ ] Added myself to contributors table <!-- feel free to add additional comments -->
1 parent f67adf9 commit 5d99117

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ for linting, testing, building, and more.
3838
- [Installation](#installation)
3939
- [Usage](#usage)
4040
- [Overriding Config](#overriding-config)
41+
- [Flow support](#flow-support)
4142
- [Inspiration](#inspiration)
4243
- [Other Solutions](#other-solutions)
4344
- [Contributors](#contributors)
@@ -111,6 +112,10 @@ module.exports = Object.assign(jestConfig, {
111112
> configuring things to make it less magical and more straightforward. Extending
112113
> can take place on your terms. I think this is actually a great way to do this.
113114
115+
### Flow support
116+
117+
If the `flow-bin` is a dependency on the project the `@babel/preset-flow` will automatically get loaded when you use the default babel config that comes with `kcd-scripts`. If you customised your `.babelrc`-file you might need to manually add `@babel/preset-flow` to the `presets`-section.
118+
114119
## Inspiration
115120

116121
This is inspired by `react-scripts`.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@babel/plugin-transform-runtime": "^7.2.0",
4343
"@babel/preset-env": "^7.2.0",
4444
"@babel/preset-react": "^7.0.0",
45+
"@babel/preset-flow": "^7.0.0",
4546
"@babel/runtime": "^7.2.0",
4647
"all-contributors-cli": "^5.4.1",
4748
"arrify": "^1.0.1",

src/config/babelrc.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ const isWebpack = parseEnv('BUILD_WEBPACK', false)
1313
const treeshake = parseEnv('BUILD_TREESHAKE', isRollup || isWebpack)
1414
const alias = parseEnv('BUILD_ALIAS', isPreact ? {react: 'preact'} : null)
1515

16-
const hasBabelRuntimeDep = Boolean(pkg.dependencies && pkg.dependencies['@babel/runtime'])
17-
const RUNTIME_HELPERS_WARN = 'You should add @babel/runtime as dependency to your package. It will allow reusing so-called babel helpers from npm rather than bundling their copies into your files.'
16+
const hasBabelRuntimeDep = Boolean(
17+
pkg.dependencies && pkg.dependencies['@babel/runtime'],
18+
)
19+
const RUNTIME_HELPERS_WARN =
20+
'You should add @babel/runtime as dependency to your package. It will allow reusing so-called babel helpers from npm rather than bundling their copies into your files.'
1821

1922
if (!treeshake && !hasBabelRuntimeDep) {
2023
throw new Error(RUNTIME_HELPERS_WARN)
@@ -35,8 +38,8 @@ const browsersConfig = browserslist.loadConfig({path: appDirectory}) || [
3538
const envTargets = isTest
3639
? {node: 'current'}
3740
: isWebpack || isRollup
38-
? {browsers: browsersConfig}
39-
: {node: getNodeVersion(pkg)}
41+
? {browsers: browsersConfig}
42+
: {node: getNodeVersion(pkg)}
4043
const envOptions = {modules: false, loose: true, targets: envTargets}
4144

4245
module.exports = () => ({
@@ -49,9 +52,13 @@ module.exports = () => ({
4952
{pragma: isPreact ? 'React.h' : undefined},
5053
],
5154
),
55+
ifAnyDep(['flow-bin'], [require.resolve('@babel/preset-flow')]),
5256
].filter(Boolean),
5357
plugins: [
54-
[require.resolve('@babel/plugin-transform-runtime'), { useESModules: treeshake && !isCJS }],
58+
[
59+
require.resolve('@babel/plugin-transform-runtime'),
60+
{useESModules: treeshake && !isCJS},
61+
],
5562
require.resolve('babel-plugin-macros'),
5663
alias
5764
? [

0 commit comments

Comments
 (0)