Skip to content

Commit

Permalink
Rewrite config following Babel best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalduez committed Aug 14, 2024
1 parent e2887bf commit 3b3eee0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
* [Hermes Parser]
* https://github.com/facebook/hermes/blob/main/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser
*
* [Other]
* [Ponyfills]
* https://babeljs.io/docs/en/babel-plugin-transform-runtime
*/

const { declarePreset } = require('@babel/helper-plugin-utils');
const presetEnv = require('@babel/preset-env');
const presetFlow = require('@babel/preset-flow');
const presetReact = require('@babel/preset-react');
const transformRuntime = require('@babel/plugin-transform-runtime');
const syntaxHermesParser = require('babel-plugin-syntax-hermes-parser');

/**
* Notes
*
* [Notes]
* Presets are run in the reverse order they are defined.
* Plugins are run in the order they are defined below,
* but they are run *before* presets.
Expand All @@ -28,7 +34,9 @@
* See https://github.com/babel/babel/issues/9297#issuecomment-453750049
*/

module.exports = (context, options = {}) => {
module.exports = declarePreset((api, options = {}) => {
api.assertVersion(7);

let { parser = 'babel', ...envOptions } = options;

let envOpts = {
Expand All @@ -49,19 +57,15 @@ module.exports = (context, options = {}) => {
version: '7.25.0', // 1
};

let presets = [
['@babel/preset-env', envOpts],
['@babel/preset-react', reactOpts],
'@babel/preset-flow',
];
let presets = [[presetEnv, envOpts], [presetReact, reactOpts], presetFlow];

let plugins = [
...(parser === 'hermes' ? ['babel-plugin-syntax-hermes-parser'] : []),
['@babel/plugin-transform-runtime', runtimeOpts],
...(parser === 'hermes' ? [syntaxHermesParser] : []),
[transformRuntime, runtimeOpts],
];

return {
presets,
plugins,
};
};
});
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"browserslist": "$browserslist"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.24.8",
"@babel/plugin-transform-runtime": "^7.24.7",
"@babel/preset-env": "^7.25.3",
"@babel/preset-flow": "^7.24.7",
Expand Down

0 comments on commit 3b3eee0

Please sign in to comment.