Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

get-babel-options

v1.0.3arrow_drop_down
v1.0.3
v1.0.2
v1.0.1
v1.0.0
v0.0.1
STATUS
Passing
DOWNLOADS
6,767
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
4 months ago
SIZE
35 KB
Retrieves the babel options from the `.babelrc` file as JSON from the specified directory.
4 contributors
Install get-babel-options as a package?
Copied
npm i @bit/bit.envs.internal.get-babel-options
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
index.js
74 Lines(56 sloc)
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
import fs from 'fs-extra';
import path from 'path';

const getBabelRc = (pathToLook) => {
    const babelRcPath = `${pathToLook}${path.sep}.babelrc`;
    const file = fs.readFileSync(babelRcPath, 'utf8');
    return JSON.parse(file);
}

const moduleIsAvailable = (modulePath, compilerNodeModules) => {
    try {
        return require.resolve(modulePath, {paths: [compilerNodeModules]});
    } catch (e) {
        return null;
    }
}

const addBabelPrefixAndResolve = (prefixType, obj, compilerRootFolder) => {
    const compilerNodeModules = path.join(compilerRootFolder, 'node_modules');
    if(obj.startsWith('module:')) {
        obj = obj.substring(7);
    }
    const resolvedModule = moduleIsAvailable(obj, compilerNodeModules);
    if (resolvedModule) {
        return resolvedModule;
    }
    if (obj.startsWith('@babel/')) {
        if (!obj.startsWith(`@babel/${prefixType}`)) {
            obj = `@babel/${prefixType}-${obj}`;
        }
    } else if (!obj.startsWith(`babel-${prefixType}`)) {
        obj = `babel-${prefixType}-${obj}`;
    }
    return require.resolve(obj, {paths: [compilerNodeModules]});
}

/**
 * @name getBabelOptions
 * @description Retrieves the babel options from the `.babelrc` file as JSON  from the specified directory.
 * @param {string} pathToLook 
 * @example
 * const babelOptions = getBabelOptions();
 * babel.transform(code, babelOptions, distPath);
 */
const getBabelOptions = (pathToLook) => {
    const options = getBabelRc(pathToLook);
    options.sourceMaps = true;

    if(options.plugins) {
        options.plugins = options.plugins.map(plugin => {
            if (Array.isArray(plugin)) {
                plugin[0] = addBabelPrefixAndResolve('plugin', plugin[0], pathToLook);
                return plugin;
            }
    
            return addBabelPrefixAndResolve('plugin', plugin, pathToLook);
        });
    }

    if(options.presets) {
        options.presets = options.presets.map(preset => {
            if (Array.isArray(preset)) {
                preset[0] = addBabelPrefixAndResolve('preset', preset[0], pathToLook);
                return preset;
            }
    
            return addBabelPrefixAndResolve('preset', preset, pathToLook);
        });
    }

    return options;
}

export default getBabelOptions;