1082815dcSEvan Bacon"use strict";
2082815dcSEvan Bacon
3082815dcSEvan BaconObject.defineProperty(exports, "__esModule", {
4082815dcSEvan Bacon  value: true
5082815dcSEvan Bacon});
6082815dcSEvan Baconexports.withStaticPlugin = void 0;
7082815dcSEvan Baconfunction _assert() {
8082815dcSEvan Bacon  const data = _interopRequireDefault(require("assert"));
9082815dcSEvan Bacon  _assert = function () {
10082815dcSEvan Bacon    return data;
11082815dcSEvan Bacon  };
12082815dcSEvan Bacon  return data;
13082815dcSEvan Bacon}
14082815dcSEvan Baconfunction _getenv() {
15082815dcSEvan Bacon  const data = require("getenv");
16082815dcSEvan Bacon  _getenv = function () {
17082815dcSEvan Bacon    return data;
18082815dcSEvan Bacon  };
19082815dcSEvan Bacon  return data;
20082815dcSEvan Bacon}
21082815dcSEvan Baconfunction _errors() {
22082815dcSEvan Bacon  const data = require("../utils/errors");
23082815dcSEvan Bacon  _errors = function () {
24082815dcSEvan Bacon    return data;
25082815dcSEvan Bacon  };
26082815dcSEvan Bacon  return data;
27082815dcSEvan Bacon}
28082815dcSEvan Baconfunction _pluginResolver() {
29082815dcSEvan Bacon  const data = require("../utils/plugin-resolver");
30082815dcSEvan Bacon  _pluginResolver = function () {
31082815dcSEvan Bacon    return data;
32082815dcSEvan Bacon  };
33082815dcSEvan Bacon  return data;
34082815dcSEvan Bacon}
35082815dcSEvan Baconfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36*ee216927STomasz Sapetaconst EXPO_DEBUG = (0, _getenv().boolish)('EXPO_DEBUG', false);
37082815dcSEvan Bacon
38*ee216927STomasz Sapeta// Show all error info related to plugin resolution.
39*ee216927STomasz Sapetaconst EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS = (0, _getenv().boolish)('EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS', false);
40*ee216927STomasz Sapeta// Force using the fallback unversioned plugin instead of a local versioned copy,
41082815dcSEvan Bacon// this should only be used for testing the CLI.
42082815dcSEvan Baconconst EXPO_USE_UNVERSIONED_PLUGINS = (0, _getenv().boolish)('EXPO_USE_UNVERSIONED_PLUGINS', false);
43082815dcSEvan Baconfunction isModuleMissingError(name, error) {
44082815dcSEvan Bacon  // @ts-ignore
45082815dcSEvan Bacon  if (['MODULE_NOT_FOUND', 'PLUGIN_NOT_FOUND'].includes(error.code)) {
46082815dcSEvan Bacon    return true;
47082815dcSEvan Bacon  }
48082815dcSEvan Bacon  return error.message.includes(`Cannot find module '${name}'`);
49082815dcSEvan Bacon}
50082815dcSEvan Baconfunction isUnexpectedTokenError(error) {
51082815dcSEvan Bacon  if (error instanceof SyntaxError || error instanceof _errors().PluginError && error.code === 'INVALID_PLUGIN_IMPORT') {
52*ee216927STomasz Sapeta    return (
53*ee216927STomasz Sapeta      // These are the most common errors that'll be thrown when a package isn't transpiled correctly.
54082815dcSEvan Bacon      !!error.message.match(/Unexpected token/) || !!error.message.match(/Cannot use import statement/)
55082815dcSEvan Bacon    );
56082815dcSEvan Bacon  }
57082815dcSEvan Bacon  return false;
58082815dcSEvan Bacon}
59*ee216927STomasz Sapeta
60082815dcSEvan Bacon/**
61082815dcSEvan Bacon * Resolves static module plugin and potentially falls back on a provided plugin if the module cannot be resolved
62082815dcSEvan Bacon *
63082815dcSEvan Bacon * @param config
64082815dcSEvan Bacon * @param fallback Plugin with `_resolverError` explaining why the module couldn't be used
65082815dcSEvan Bacon * @param projectRoot optional project root, fallback to _internal.projectRoot. Used for testing.
66082815dcSEvan Bacon * @param _isLegacyPlugin Used to suppress errors thrown by plugins that are applied automatically
67082815dcSEvan Bacon */
68082815dcSEvan Baconconst withStaticPlugin = (config, props) => {
69082815dcSEvan Bacon  var _pluginProps;
70082815dcSEvan Bacon  let projectRoot = props.projectRoot;
71082815dcSEvan Bacon  if (!projectRoot) {
72082815dcSEvan Bacon    var _config$_internal;
73082815dcSEvan Bacon    projectRoot = (_config$_internal = config._internal) === null || _config$_internal === void 0 ? void 0 : _config$_internal.projectRoot;
74082815dcSEvan Bacon    (0, _pluginResolver().assertInternalProjectRoot)(projectRoot);
75082815dcSEvan Bacon  }
76*ee216927STomasz Sapeta  let [pluginResolve, pluginProps] = (0, _pluginResolver().normalizeStaticPlugin)(props.plugin);
77*ee216927STomasz Sapeta  // Ensure no one uses this property by accident.
78082815dcSEvan Bacon  (0, _assert().default)(!((_pluginProps = pluginProps) !== null && _pluginProps !== void 0 && _pluginProps._resolverError), `Plugin property '_resolverError' is a reserved property of \`withStaticPlugin\``);
79082815dcSEvan Bacon  let withPlugin;
80*ee216927STomasz Sapeta  if (
81*ee216927STomasz Sapeta  // Function was provided, no need to resolve: [withPlugin, {}]
82082815dcSEvan Bacon  typeof pluginResolve === 'function') {
83082815dcSEvan Bacon    withPlugin = pluginResolve;
84082815dcSEvan Bacon  } else if (typeof pluginResolve === 'string') {
85082815dcSEvan Bacon    try {
86082815dcSEvan Bacon      // Resolve and evaluate plugins.
87*ee216927STomasz Sapeta      withPlugin = (0, _pluginResolver().resolveConfigPluginFunction)(projectRoot, pluginResolve);
88082815dcSEvan Bacon
89*ee216927STomasz Sapeta      // Only force if the project has the versioned plugin, otherwise use default behavior.
90*ee216927STomasz Sapeta      // This helps see which plugins are being skipped.
91082815dcSEvan Bacon      if (EXPO_USE_UNVERSIONED_PLUGINS && !!withPlugin && !!props._isLegacyPlugin && !!props.fallback) {
92082815dcSEvan Bacon        console.log(`Force "${pluginResolve}" to unversioned plugin`);
93082815dcSEvan Bacon        withPlugin = props.fallback;
94082815dcSEvan Bacon      }
95082815dcSEvan Bacon    } catch (error) {
96082815dcSEvan Bacon      if (EXPO_DEBUG) {
97082815dcSEvan Bacon        if (EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS) {
98082815dcSEvan Bacon          // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins).
99082815dcSEvan Bacon          console.log(`Error resolving plugin "${pluginResolve}"`);
100082815dcSEvan Bacon          console.log(error);
101082815dcSEvan Bacon          console.log();
102082815dcSEvan Bacon        } else {
103082815dcSEvan Bacon          const shouldMuteWarning = props._isLegacyPlugin && (isModuleMissingError(pluginResolve, error) || isUnexpectedTokenError(error));
104082815dcSEvan Bacon          if (!shouldMuteWarning) {
105082815dcSEvan Bacon            if (isModuleMissingError(pluginResolve, error)) {
106082815dcSEvan Bacon              // Prevent causing log spew for basic resolution errors.
107082815dcSEvan Bacon              console.log(`Could not find plugin "${pluginResolve}"`);
108082815dcSEvan Bacon            } else {
109082815dcSEvan Bacon              // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins).
110082815dcSEvan Bacon              console.log(`Error resolving plugin "${pluginResolve}"`);
111082815dcSEvan Bacon              console.log(error);
112082815dcSEvan Bacon              console.log();
113082815dcSEvan Bacon            }
114082815dcSEvan Bacon          }
115082815dcSEvan Bacon        }
116*ee216927STomasz Sapeta      }
117*ee216927STomasz Sapeta      // TODO: Maybe allow for `PluginError`s to be thrown so external plugins can assert invalid options.
118*ee216927STomasz Sapeta
119082815dcSEvan Bacon      // If the static module failed to resolve, attempt to use a fallback.
120082815dcSEvan Bacon      // This enables support for built-in plugins with versioned variations living in other packages.
121082815dcSEvan Bacon      if (props.fallback) {
122*ee216927STomasz Sapeta        if (!pluginProps) pluginProps = {};
123*ee216927STomasz Sapeta        // Pass this to the fallback plugin for potential warnings about needing to install a versioned package.
124082815dcSEvan Bacon        pluginProps._resolverError = error;
125082815dcSEvan Bacon        withPlugin = props.fallback;
126082815dcSEvan Bacon      } else {
127082815dcSEvan Bacon        // If no fallback, throw the resolution error.
128082815dcSEvan Bacon        throw error;
129082815dcSEvan Bacon      }
130082815dcSEvan Bacon    }
131082815dcSEvan Bacon  } else {
132082815dcSEvan Bacon    throw new (_errors().PluginError)(`Plugin is an unexpected type: ${typeof pluginResolve}`, 'INVALID_PLUGIN_TYPE');
133*ee216927STomasz Sapeta  }
134082815dcSEvan Bacon
135*ee216927STomasz Sapeta  // Execute the plugin.
136082815dcSEvan Bacon  config = withPlugin(config, pluginProps);
137082815dcSEvan Bacon  return config;
138082815dcSEvan Bacon};
139082815dcSEvan Baconexports.withStaticPlugin = withStaticPlugin;
140082815dcSEvan Bacon//# sourceMappingURL=withStaticPlugin.js.map