Skip to content

Commit

Permalink
Merge pull request #37 from abouthiroppy/feature/convert-comma-to-array
Browse files Browse the repository at this point in the history
 Convert comma separated presets/plugins into an array
  • Loading branch information
hzoo authored Feb 26, 2018
2 parents 0486d24 + 3ad2f31 commit d8e4263
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
22 changes: 22 additions & 0 deletions __tests__/__snapshots__/upgradeConfig.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`convert comma separated presets/plugins into an array 1`] = `
Object {
"env": Object {
"development": Object {
"plugins": Array [
"babel-plugin-istanbul",
"@babel/plugin-transform-react-jsx-source",
"@babel/plugin-transform-react-inline-elements",
],
},
},
"plugins": Array [
"@babel/plugin-proposal-function-bind",
"@babel/plugin-proposal-class-properties",
],
"presets": Array [
"@babel/preset-env",
"@babel/preset-react",
],
}
`;

exports[`package that is removed 1`] = `
Object {
"plugins": Array [
Expand Down
5 changes: 5 additions & 0 deletions __tests__/upgradeConfig.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const upgradeConfig = require('../src/upgradeConfig');
const babelrcFixture = require('../fixtures/babelrc');
const optionParsingFixture = require('../fixtures/option-parsing');
const { readBabelRC } = require('../src');
const JSON5_PATH = path.resolve(__dirname, '../fixtures/babelrc.json5');

Expand Down Expand Up @@ -38,3 +39,7 @@ test('packages (json5)', async () => {
const json5Data = await readBabelRC(JSON5_PATH);
expect(upgradeConfig(json5Data)).toMatchSnapshot();
});

test('convert comma separated presets/plugins into an array', () => {
expect(upgradeConfig(optionParsingFixture)).toMatchSnapshot();
});
9 changes: 9 additions & 0 deletions fixtures/option-parsing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": "@babel/preset-env, @babel/preset-react",
"plugins": "babel-plugin-transform-function-bind, transform-class-properties",
"env": {
"development": {
"plugins": "babel-plugin-istanbul, transform-react-jsx-source, babel-plugin-transform-react-inline-elements"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ npx babel-upgrade --install
- [ ] Figure out how to change nested .babelrcs into using "overrides" instead
- [ ] Monorepo support
- [ ] `.babelrc.js` and other js files with a config like presets, `webpack.config.js`
- [ ] Convert comma separated presets/plugins into an array
- [x] Convert comma separated presets/plugins into an array
- [x] handle react + flow preset being split. Read if `.flowconfig` and add it?
- [ ] convert `only`/`ignore` if necessary
- [ ] remove `typeof-symbol` if using `@babel/preset-env` + loose
Expand Down
12 changes: 10 additions & 2 deletions src/upgradeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const { presets: oldPresets, plugins: oldPlugins } = require('./packageData');

// TODO: fix all of this
function changePresets(config) {
const presets = config.presets;
let presets = config.presets;

if (!Array.isArray(presets) && typeof presets === 'string') {
presets = config.presets = config.presets.split(',').map((preset) => preset.trim());
}

// check if presets are there
if (presets) {
Expand Down Expand Up @@ -42,7 +46,11 @@ function changePresets(config) {
}

function changePlugins(config) {
const plugins = config.plugins;
let plugins = config.plugins;

if (!Array.isArray(plugins) && typeof plugins === 'string') {
plugins = config.plugins = config.plugins.split(',').map((plugin) => plugin.trim());
}

// check if plugins are there
if (plugins) {
Expand Down

0 comments on commit d8e4263

Please sign in to comment.