1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.withStaticPlugin = void 0; 7function _assert() { 8 const data = _interopRequireDefault(require("assert")); 9 _assert = function () { 10 return data; 11 }; 12 return data; 13} 14function _getenv() { 15 const data = require("getenv"); 16 _getenv = function () { 17 return data; 18 }; 19 return data; 20} 21function _errors() { 22 const data = require("../utils/errors"); 23 _errors = function () { 24 return data; 25 }; 26 return data; 27} 28function _pluginResolver() { 29 const data = require("../utils/plugin-resolver"); 30 _pluginResolver = function () { 31 return data; 32 }; 33 return data; 34} 35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 36const EXPO_DEBUG = (0, _getenv().boolish)('EXPO_DEBUG', false); 37 38// Show all error info related to plugin resolution. 39const EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS = (0, _getenv().boolish)('EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS', false); 40// Force using the fallback unversioned plugin instead of a local versioned copy, 41// this should only be used for testing the CLI. 42const EXPO_USE_UNVERSIONED_PLUGINS = (0, _getenv().boolish)('EXPO_USE_UNVERSIONED_PLUGINS', false); 43function isModuleMissingError(name, error) { 44 // @ts-ignore 45 if (['MODULE_NOT_FOUND', 'PLUGIN_NOT_FOUND'].includes(error.code)) { 46 return true; 47 } 48 return error.message.includes(`Cannot find module '${name}'`); 49} 50function isUnexpectedTokenError(error) { 51 if (error instanceof SyntaxError || error instanceof _errors().PluginError && error.code === 'INVALID_PLUGIN_IMPORT') { 52 return ( 53 // These are the most common errors that'll be thrown when a package isn't transpiled correctly. 54 !!error.message.match(/Unexpected token/) || !!error.message.match(/Cannot use import statement/) 55 ); 56 } 57 return false; 58} 59 60/** 61 * Resolves static module plugin and potentially falls back on a provided plugin if the module cannot be resolved 62 * 63 * @param config 64 * @param fallback Plugin with `_resolverError` explaining why the module couldn't be used 65 * @param projectRoot optional project root, fallback to _internal.projectRoot. Used for testing. 66 * @param _isLegacyPlugin Used to suppress errors thrown by plugins that are applied automatically 67 */ 68const withStaticPlugin = (config, props) => { 69 var _pluginProps; 70 let projectRoot = props.projectRoot; 71 if (!projectRoot) { 72 var _config$_internal; 73 projectRoot = (_config$_internal = config._internal) === null || _config$_internal === void 0 ? void 0 : _config$_internal.projectRoot; 74 (0, _pluginResolver().assertInternalProjectRoot)(projectRoot); 75 } 76 let [pluginResolve, pluginProps] = (0, _pluginResolver().normalizeStaticPlugin)(props.plugin); 77 // Ensure no one uses this property by accident. 78 (0, _assert().default)(!((_pluginProps = pluginProps) !== null && _pluginProps !== void 0 && _pluginProps._resolverError), `Plugin property '_resolverError' is a reserved property of \`withStaticPlugin\``); 79 let withPlugin; 80 if ( 81 // Function was provided, no need to resolve: [withPlugin, {}] 82 typeof pluginResolve === 'function') { 83 withPlugin = pluginResolve; 84 } else if (typeof pluginResolve === 'string') { 85 try { 86 // Resolve and evaluate plugins. 87 withPlugin = (0, _pluginResolver().resolveConfigPluginFunction)(projectRoot, pluginResolve); 88 89 // Only force if the project has the versioned plugin, otherwise use default behavior. 90 // This helps see which plugins are being skipped. 91 if (EXPO_USE_UNVERSIONED_PLUGINS && !!withPlugin && !!props._isLegacyPlugin && !!props.fallback) { 92 console.log(`Force "${pluginResolve}" to unversioned plugin`); 93 withPlugin = props.fallback; 94 } 95 } catch (error) { 96 if (EXPO_DEBUG) { 97 if (EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS) { 98 // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins). 99 console.log(`Error resolving plugin "${pluginResolve}"`); 100 console.log(error); 101 console.log(); 102 } else { 103 const shouldMuteWarning = props._isLegacyPlugin && (isModuleMissingError(pluginResolve, error) || isUnexpectedTokenError(error)); 104 if (!shouldMuteWarning) { 105 if (isModuleMissingError(pluginResolve, error)) { 106 // Prevent causing log spew for basic resolution errors. 107 console.log(`Could not find plugin "${pluginResolve}"`); 108 } else { 109 // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins). 110 console.log(`Error resolving plugin "${pluginResolve}"`); 111 console.log(error); 112 console.log(); 113 } 114 } 115 } 116 } 117 // TODO: Maybe allow for `PluginError`s to be thrown so external plugins can assert invalid options. 118 119 // If the static module failed to resolve, attempt to use a fallback. 120 // This enables support for built-in plugins with versioned variations living in other packages. 121 if (props.fallback) { 122 if (!pluginProps) pluginProps = {}; 123 // Pass this to the fallback plugin for potential warnings about needing to install a versioned package. 124 pluginProps._resolverError = error; 125 withPlugin = props.fallback; 126 } else { 127 // If no fallback, throw the resolution error. 128 throw error; 129 } 130 } 131 } else { 132 throw new (_errors().PluginError)(`Plugin is an unexpected type: ${typeof pluginResolve}`, 'INVALID_PLUGIN_TYPE'); 133 } 134 135 // Execute the plugin. 136 config = withPlugin(config, pluginProps); 137 return config; 138}; 139exports.withStaticPlugin = withStaticPlugin; 140//# sourceMappingURL=withStaticPlugin.js.map