import assert from 'assert' ; import { boolish } from 'getenv' ; import { ConfigPlugin , StaticPlugin } from '../Plugin.types' ; import { PluginError } from '../utils/errors' ; import { assertInternalProjectRoot , normalizeStaticPlugin , resolveConfigPluginFunction , } from '../utils/plugin-resolver' ; const EXPO_DEBUG = boolish ( 'EXPO_DEBUG' , false ) ; const EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS = boolish ( 'EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS' , false ) ; const EXPO_USE_UNVERSIONED_PLUGINS = boolish ( 'EXPO_USE_UNVERSIONED_PLUGINS' , false ) ; function isModuleMissingError ( name , error ) { if ( [ 'MODULE_NOT_FOUND' , 'PLUGIN_NOT_FOUND' ] . includes ( error . code ) ) { return true ; } return error . message . includes ( `Cannot find module '${ name }'` ) ; } function isUnexpectedTokenError ( error ) { if ( error instanceof SyntaxError || ( error instanceof PluginError && error . code === 'INVALID_PLUGIN_IMPORT' ) ) { return ( ! ! error . message . match ( /Unexpected token/ ) || ! ! error . message . match ( /Cannot use import statement/ ) ) ; } return false ; } param config param fallback param projectRoot param _isLegacyPlugin export const withStaticPlugin = ( config , props ) => { let projectRoot = props . projectRoot ; if ( ! projectRoot ) { projectRoot = config . _internal ?. projectRoot ; assertInternalProjectRoot ( projectRoot ) ; } let [ pluginResolve , pluginProps ] = normalizeStaticPlugin ( props . plugin ) ; assert ( ! pluginProps ?. _resolverError , `Plugin property '_resolverError' is a reserved property of \`withStaticPlugin\`` ) ; let withPlugin ; if ( typeof pluginResolve === 'function' ) { withPlugin = pluginResolve ; } else if ( typeof pluginResolve === 'string' ) { try { withPlugin = resolveConfigPluginFunction ( projectRoot , pluginResolve ) ; if ( EXPO_USE_UNVERSIONED_PLUGINS && ! ! withPlugin && ! ! props . _isLegacyPlugin && ! ! props . fallback ) { console . log ( `Force "${ pluginResolve }" to unversioned plugin` ) ; withPlugin = props . fallback ; } } catch ( error ) { if ( EXPO_DEBUG ) { if ( EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS ) { console . log ( `Error resolving plugin "${ pluginResolve }"` ) ; console . log ( error ) ; console . log ( ) ; } else { const shouldMuteWarning = props . _isLegacyPlugin && ( isModuleMissingError ( pluginResolve , error ) || isUnexpectedTokenError ( error ) ) ; if ( ! shouldMuteWarning ) { if ( isModuleMissingError ( pluginResolve , error ) ) { console . log ( `Could not find plugin "${ pluginResolve }"` ) ; } else { console . log ( `Error resolving plugin "${ pluginResolve }"` ) ; console . log ( error ) ; console . log ( ) ; } } } } if ( props . fallback ) { if ( ! pluginProps ) pluginProps = { } ; pluginProps . _resolverError = error ; withPlugin = props . fallback ; } else { throw error ; } } } else { throw new PluginError ( `Plugin is an unexpected type: ${ typeof pluginResolve }` , 'INVALID_PLUGIN_TYPE' ) ; } config = withPlugin ( config , pluginProps ) ; return config ; } ;