Skip to content

Commit aceaf75

Browse files
committed
feat: use a modern config for babel
1 parent 546e38c commit aceaf75

File tree

6 files changed

+981
-1275
lines changed

6 files changed

+981
-1275
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ To configure your project manually, follow these steps:
4242
"output": "lib",
4343
"targets": [
4444
["aar", {"reverseJetify": true}],
45-
["commonjs", {"flow": true}],
45+
["commonjs", {"copyFlow": true}],
4646
"module",
4747
"typescript",
4848
]
4949
}
5050
```
5151

52+
See options below for more details.
53+
5254
1. Add `bob` to your `prepare` step:
5355

5456
```js
@@ -62,7 +64,7 @@ To configure your project manually, follow these steps:
6264
```json
6365
"main": "lib/commonjs/index.js",
6466
"module": "lib/module/index.js",
65-
"react-native": "src/index.js",
67+
"react-native": "src/index.ts",
6668
"types": "lib/typescript/src/index.d.ts",
6769
"files": [
6870
"lib/",

package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,28 @@
2626
"release": "release-it"
2727
},
2828
"dependencies": {
29-
"@babel/core": "^7.7.7",
29+
"@babel/core": "^7.8.4",
30+
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
31+
"@babel/preset-env": "^7.8.4",
32+
"@babel/preset-flow": "^7.8.3",
33+
"@babel/preset-react": "^7.8.3",
34+
"@babel/preset-typescript": "^7.8.3",
3035
"chalk": "^3.0.0",
3136
"cosmiconfig": "^6.0.0",
3237
"del": "^5.1.0",
3338
"fs-extra": "^8.1.0",
3439
"glob": "^7.1.6",
35-
"inquirer": "^7.0.1",
40+
"inquirer": "^7.0.4",
3641
"is-git-dirty": "^1.0.0",
3742
"json5": "^2.1.1",
38-
"metro-react-native-babel-preset": "^0.57.0",
39-
"yargs": "^15.0.2"
43+
"yargs": "^15.1.0"
4044
},
4145
"optionalDependencies": {
4246
"jetifier": "^1.6.5"
4347
},
4448
"devDependencies": {
45-
"@babel/cli": "^7.7.7",
46-
"@babel/plugin-proposal-optional-chaining": "^7.7.5",
47-
"@babel/preset-env": "^7.7.7",
48-
"@babel/preset-typescript": "^7.7.7",
49-
"@commitlint/config-conventional": "^8.2.0",
49+
"@babel/cli": "^7.8.4",
50+
"@commitlint/config-conventional": "^8.3.4",
5051
"@release-it/conventional-changelog": "^1.1.0",
5152
"@types/babel__core": "^7.1.3",
5253
"@types/chalk": "^2.2.0",
@@ -55,14 +56,14 @@
5556
"@types/glob": "^7.1.1",
5657
"@types/inquirer": "^6.5.0",
5758
"@types/json5": "^0.0.30",
58-
"@types/yargs": "^13.0.3",
59-
"commitlint": "^8.2.0",
60-
"eslint": "^6.7.2",
59+
"@types/yargs": "^15.0.3",
60+
"commitlint": "^8.3.5",
61+
"eslint": "^6.8.0",
6162
"eslint-config-satya164": "^3.1.5",
62-
"husky": "^3.1.0",
63+
"husky": "^4.2.1",
6364
"prettier": "^1.19.1",
6465
"release-it": "^12.4.3",
65-
"typescript": "^3.7.3"
66+
"typescript": "^3.7.5"
6667
},
6768
"husky": {
6869
"hooks": {

src/targets/commonjs.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import compile from '../utils/compile';
55
import { Input } from '../types';
66

77
type Options = Input & {
8-
options?: { flow?: boolean };
8+
options?: {
9+
babelrc?: boolean | null;
10+
configFile?: string | false | null;
11+
copyFlow?: boolean;
12+
};
913
};
1014

1115
export default async function build({
@@ -25,10 +29,10 @@ export default async function build({
2529
root,
2630
source,
2731
output,
28-
options: {
29-
presets: [[require.resolve('metro-react-native-babel-preset')]],
30-
},
31-
flow: options?.flow ? true : false,
32+
modules: 'commonjs',
33+
babelrc: options?.babelrc,
34+
configFile: options?.configFile,
35+
copyFlow: options?.copyFlow ? true : false,
3236
report,
3337
});
3438
}

src/targets/module.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import compile from '../utils/compile';
55
import { Input } from '../types';
66

77
type Options = Input & {
8-
options?: { flow?: boolean };
8+
options?: {
9+
babelrc?: boolean | null;
10+
configFile?: string | false | null;
11+
copyFlow?: boolean;
12+
};
913
};
1014

1115
export default async function build({
@@ -25,15 +29,10 @@ export default async function build({
2529
root,
2630
source,
2731
output,
28-
options: {
29-
presets: [
30-
[
31-
require.resolve('metro-react-native-babel-preset'),
32-
{ disableImportExportTransform: true },
33-
],
34-
],
35-
},
36-
flow: options?.flow ? true : false,
32+
modules: false,
33+
babelrc: options?.babelrc,
34+
configFile: options?.configFile,
35+
copyFlow: options?.copyFlow ? true : false,
3736
report,
3837
});
3938
}

src/utils/compile.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import glob from 'glob';
66
import { Input } from '../types';
77

88
type Options = Input & {
9-
options: babel.TransformOptions;
10-
flow: boolean;
9+
babelrc?: boolean | null;
10+
configFile?: string | false | null;
11+
modules: 'commonjs' | false;
12+
copyFlow: boolean;
1113
};
1214

1315
export default async function compile({
1416
root,
1517
source,
1618
output,
17-
options,
18-
flow,
19+
babelrc,
20+
configFile,
21+
modules,
22+
copyFlow,
1923
report,
2024
}: Options) {
2125
const files = glob.sync('**/*', {
@@ -47,11 +51,42 @@ export default async function compile({
4751

4852
const content = await fs.readFile(filepath, 'utf-8');
4953
const result = await babel.transformAsync(content, {
50-
babelrc: false,
51-
configFile: false,
54+
babelrc: babelrc,
55+
configFile: configFile,
5256
sourceMaps: true,
5357
filename: filepath,
54-
...options,
58+
...(babelrc || configFile
59+
? null
60+
: {
61+
presets: [
62+
[
63+
'@babel/preset-env',
64+
{
65+
targets: {
66+
browsers: [
67+
'>1%',
68+
'last 2 chrome versions',
69+
'last 2 edge versions',
70+
'last 2 firefox versions',
71+
'last 2 safari versions',
72+
'not dead',
73+
'not ie <= 11',
74+
'not op_mini all',
75+
'not android <= 4.4',
76+
'not samsung <= 4',
77+
],
78+
node: '10',
79+
},
80+
useBuiltIns: false,
81+
modules,
82+
},
83+
],
84+
'@babel/preset-react',
85+
'@babel/preset-typescript',
86+
'@babel/preset-flow',
87+
],
88+
plugins: ['@babel/plugin-proposal-class-properties'],
89+
}),
5590
});
5691

5792
if (result == null) {
@@ -69,7 +104,7 @@ export default async function compile({
69104

70105
await fs.writeFile(outputFilename, code);
71106

72-
if (flow) {
107+
if (copyFlow) {
73108
fs.copy(filepath, outputFilename + '.flow');
74109
}
75110
})

0 commit comments

Comments
 (0)