UNPKG

1.26 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9'use strict';
10
11/**
12 * Manually resolve all default Babel plugins.
13 * `babel.transform` will attempt to resolve all base plugins relative to
14 * the file it's compiling. This makes sure that we're using the plugins
15 * installed in the react-native package.
16 */
17function resolvePlugins(plugins, prefix) {
18 return plugins.map(plugin => resolvePlugin(plugin, prefix));
19}
20
21/**
22 * Manually resolve a single Babel plugin.
23 */
24function resolvePlugin(plugin, prefix = '@babel/plugin-') {
25 // Normalise plugin to an array.
26 if (!Array.isArray(plugin)) {
27 plugin = [plugin];
28 }
29 // Only resolve the plugin if it's a string reference.
30 if (typeof plugin[0] === 'string') {
31 plugin[0] = require(prefix + plugin[0]);
32 plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
33 }
34 return plugin;
35}
36
37module.exports = resolvePlugins;
38module.exports.resolvePlugin = resolvePlugin;
39module.exports.resolvePluginAs = (prefix, plugin) =>
40 resolvePlugin(plugin, prefix);
41module.exports.resolvePluginsAs = (prefix, plugins) =>
42 resolvePlugins(plugins, prefix);