All files / src/commands add-babel.js

71.79% Statements 28/39
46.34% Branches 19/41
60.87% Functions 14/23
69.44% Lines 25/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                      9x         9x       9x           9x     9x     9x         9x                     9x       7x       7x                                           7x           13x 10x 9x 2x         9x 2x         2x       7x           7x       7x       7x         7x           7x           7x        
import {
    BabelConfigModuleEditor,
    BabelConfigJsonEditor,
    PackageJsonEditor,
    SnowpackConfigEditor,
    allDoExist,
    allDoNotExist,
    install,
    someDoExist
} from '../api';
 
const BABEL_CORE = [
    '@babel/cli',
    '@babel/core',
    '@babel/runtime'
];
const BABEL_PRESETS = [
    '@babel/preset-env',
    'babel-preset-minify'
];
const BABEL_PLUGINS = [
    '@babel/plugin-transform-runtime',
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-export-default-from',
    '@babel/plugin-proposal-optional-chaining'
];
const BABEL_REACT_PLUGINS = [
    'react-hot-loader'
];
const BABEL_REACT_PRESETS = [
    '@babel/preset-react'
];
const BABEL_DEPENDENCIES = [
    ...BABEL_CORE,
    ...BABEL_PRESETS,
    ...BABEL_PLUGINS
];
const SNOWPACK_DEPENDENCIES = [
    'snowpack',
    '@snowpack/app-scripts-react',
    '@snowpack/plugin-react-refresh',
    '@snowpack/plugin-postcss',
    '@snowpack/plugin-optimize'
];
/**
 * @type {task[]}
 * @see https://babeljs.io/
 */
export const addBabel = [
    {
        text: 'Create Babel config file',
        task: async ({useParcel}) => {
            await (useParcel ? new BabelConfigJsonEditor() : new BabelConfigModuleEditor())
                .create()
                .commit();
        },
        condition: () => allDoNotExist('babel.config.js', 'babel.config.json', '.babelrc', '.babelrc.js')
    },
    {
        text: 'Create Snowpack config and Configure Babel for Snowpack',
        task: async ({useSnowpack}) => {
            const snowpackConfig = {
                extends: `'@snowpack/app-scripts-react/babel.config.json'`,
                plugins: `[]`,
                presets: `[]`,
                env: {
                    development: {
                        plugins: `['react-refresh/babel']`
                    }
                }
            };
            await (new SnowpackConfigEditor())
                .create()
                .commit();
            await (new BabelConfigModuleEditor())
                .extend(useSnowpack ? snowpackConfig : {})
                .commit();
        },
        condition: ({useSnowpack}) => useSnowpack && allDoExist('babel.config.js'),
        optional: ({useSnowpack}) => useSnowpack
    },
    {
        text: 'Add React support to Babel configuration file',
        task: async ({useParcel, useRollup}) => {
            const addQuotes = str => useParcel ? str : `'${str}'`;
            const maybeRemove = name => (!useRollup || name !== 'react-hot-loader');
            const maybeRename = name => (name === 'react-hot-loader') ? 'react-hot-loader/babel' : name;
            const plugins = [
                ...(useParcel ? [] : BABEL_REACT_PLUGINS),
                ...BABEL_PLUGINS
            ]
                .filter(maybeRemove)
                .map(name => name |> maybeRename |> addQuotes);
            const presets = [...BABEL_PRESETS]
                .map(addQuotes)
                .concat([useParcel ?
                    ['@babel/preset-react', {runtime: 'automatic'}] :
                    [`'@babel/preset-react'`, {runtime: `'automatic'`}]]);
            await (useParcel ? new BabelConfigJsonEditor() : new BabelConfigModuleEditor())
                .extend({plugins, presets})
                .commit();
        },
        condition: ({useReact, useSnowpack}) => useReact && !useSnowpack && someDoExist('babel.config.js', 'babel.config.json'),
        optional: ({useReact, useSnowpack}) => useReact && !useSnowpack
    },
    {
        text: 'Add Babel build task to package.json',
        task: async ({outputDirectory, sourceDirectory, useSnowpack}) => {
            const scripts = {
                'build:es': `babel ${sourceDirectory} --out-dir ${outputDirectory}`,
                'watch:es': `watch 'npm run build:es' ${sourceDirectory}`
            };
            await (new PackageJsonEditor())
                .extend(useSnowpack ? {scripts: {build: 'snowpack build'}} : {scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Install Babel core, CLI, presets, and plugins',
        task: ({skipInstall}) => install(BABEL_DEPENDENCIES, {dev: true, skipInstall}),
        condition: ({skipInstall, useSnowpack}) => !useSnowpack && !skipInstall && (!(new PackageJsonEditor()).hasAll(...BABEL_DEPENDENCIES) && allDoExist('package.json')), // eslint-disable-line max-len
        optional: ({useSnowpack}) => !useSnowpack
    },
    {
        text: 'Install Babel React presets and plugins',
        task: ({skipInstall, useParcel}) => install([...BABEL_REACT_PRESETS, ...(useParcel ? [] : BABEL_REACT_PLUGINS)], {dev: true, skipInstall}),
        condition: ({skipInstall, useReact, useSnowpack}) => !useSnowpack && !skipInstall && useReact && allDoExist('package.json'), // eslint-disable-line max-len
        optional: ({useReact, useSnowpack}) => useReact && !useSnowpack
    },
    {
        text: 'Install Snowpack dependencies',
        task: ({skipInstall}) => install(SNOWPACK_DEPENDENCIES, {dev: true, skipInstall}),
        condition: ({skipInstall, useSnowpack}) => !skipInstall && useSnowpack && (!(new PackageJsonEditor()).hasAll(...SNOWPACK_DEPENDENCIES) && allDoExist('package.json')), // eslint-disable-line max-len
        optional: ({useSnowpack}) => useSnowpack
    }
];
export default addBabel;