How to use metro-resolver - 10 common examples

To help you get started, we’ve selected a few metro-resolver examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebook / metro / packages / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
this._options.moduleMap.getPackage(name, platform, true),
          getPackageMainPath: this._getPackageMainPath,
        },
        moduleName,
        platform,
      );
      return this._getFileResolvedModule(result);
    } catch (error) {
      if (error instanceof Resolver.FailedToResolvePathError) {
        const {candidates} = error;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            '\n\nNone of these files exist:',
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file),
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir),
            )}`,
          ].join('\n'),
        );
      }
      if (error instanceof Resolver.FailedToResolveNameError) {
        const {
          dirPaths,
          extraPaths,
        }: {
          // $flowfixme these types are defined explicitly in FailedToResolveNameError but Flow refuses to recognize them here
          dirPaths: $ReadOnlyArray,
          extraPaths: $ReadOnlyArray,
github facebook / metro / packages / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
moduleName,
        platform,
      );
      return this._getFileResolvedModule(result);
    } catch (error) {
      if (error instanceof Resolver.FailedToResolvePathError) {
        const {candidates} = error;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            '\n\nNone of these files exist:',
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file),
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir),
            )}`,
          ].join('\n'),
        );
      }
      if (error instanceof Resolver.FailedToResolveNameError) {
        const {
          dirPaths,
          extraPaths,
        }: {
          // $flowfixme these types are defined explicitly in FailedToResolveNameError but Flow refuses to recognize them here
          dirPaths: $ReadOnlyArray,
          extraPaths: $ReadOnlyArray,
          ...
        } = error;
        const displayDirPaths = dirPaths
github godaddy / ekke / api / metro / configure.js View on Github external
// give request and point it to a completely different, unknown file. The
    // last thing we've wanted to do the actual resolving, so we're going to
    // rely on the `metro-resolver` module for that..
    //
    // Unfortunately, there's an issue, as we've set a `resolveRequest`
    // function the resolver function thinks it needs to use that resolver to
    // resolve the file, leading to an infinite loop, pain, suffering. So
    // before we can continue, we need to check if we should remove our
    // self from the context so the resolver works as intended again.
    //
    if (context.resolveRequest === resolveRequest) {
      context.resolveRequest = null;
    }

    debug(`resovling file(${file})`);
    return resolve(context, file, platform);
  };
github facebook / metro / packages / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
const {candidates} = error;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            '\n\nNone of these files exist:',
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file),
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir),
            )}`,
          ].join('\n'),
        );
      }
      if (error instanceof Resolver.FailedToResolveNameError) {
        const {
          dirPaths,
          extraPaths,
        }: {
          // $flowfixme these types are defined explicitly in FailedToResolveNameError but Flow refuses to recognize them here
          dirPaths: $ReadOnlyArray,
          extraPaths: $ReadOnlyArray,
          ...
        } = error;
        const displayDirPaths = dirPaths
          .filter((dirPath: string) => this._options.dirExists(dirPath))
          .map(dirPath => path.relative(this._options.projectRoot, dirPath))
          .concat(extraPaths);

        const hint = displayDirPaths.length ? ' or in these directories:' : '';
        throw new UnableToResolveError(
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
moduleName,
        platform
      );
      return this._getFileResolvedModule(result);
    } catch (error) {
      if (error instanceof Resolver.FailedToResolvePathError) {
        const candidates = error.candidates;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            "\n\nNone of these files exist:",
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file)
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir)
            )}`
          ].join("\n")
        );
      }

      if (error instanceof Resolver.FailedToResolveNameError) {
        const dirPaths = error.dirPaths,
          extraPaths = error.extraPaths;
        const displayDirPaths = dirPaths
          .filter(dirPath => this._options.dirExists(dirPath))
          .map(dirPath => path.relative(this._options.projectRoot, dirPath))
          .concat(extraPaths);
        const hint = displayDirPaths.length ? " or in these directories:" : "";
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
this._options.moduleMap.getPackage(name, platform, true),
          getPackageMainPath: this._getPackageMainPath
        }),
        moduleName,
        platform
      );
      return this._getFileResolvedModule(result);
    } catch (error) {
      if (error instanceof Resolver.FailedToResolvePathError) {
        const candidates = error.candidates;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            "\n\nNone of these files exist:",
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file)
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir)
            )}`
          ].join("\n")
        );
      }

      if (error instanceof Resolver.FailedToResolveNameError) {
        const dirPaths = error.dirPaths,
          extraPaths = error.extraPaths;
        const displayDirPaths = dirPaths
          .filter(dirPath => this._options.dirExists(dirPath))
          .map(dirPath => path.relative(this._options.projectRoot, dirPath))
          .concat(extraPaths);
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
resolveDependency(fromModule, moduleName, allowHaste, platform) {
    try {
      const result = Resolver.resolve(
        _objectSpread({}, this._options, {
          originModulePath: fromModule.path,
          redirectModulePath: modulePath =>
            this._redirectRequire(fromModule, modulePath),
          allowHaste,
          platform,
          resolveHasteModule: name =>
            this._options.moduleMap.getModule(name, platform, true),
          resolveHastePackage: name =>
            this._options.moduleMap.getPackage(name, platform, true),
          getPackageMainPath: this._getPackageMainPath
        }),
        moduleName,
        platform
      );
      return this._getFileResolvedModule(result);
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            "\n\nNone of these files exist:",
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file)
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir)
            )}`
          ].join("\n")
        );
      }

      if (error instanceof Resolver.FailedToResolveNameError) {
        const dirPaths = error.dirPaths,
          extraPaths = error.extraPaths;
        const displayDirPaths = dirPaths
          .filter(dirPath => this._options.dirExists(dirPath))
          .map(dirPath => path.relative(this._options.projectRoot, dirPath))
          .concat(extraPaths);
        const hint = displayDirPaths.length ? " or in these directories:" : "";
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [`${moduleName} could not be found within the project${hint || "."}`]
            .concat(
              _toConsumableArray(
                displayDirPaths.map(dirPath => `  ${path.dirname(dirPath)}`)
              ),
              [
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / node-haste / DependencyGraph / ModuleResolution.js View on Github external
redirectModulePath: modulePath =>
            this._redirectRequire(fromModule, modulePath),
          allowHaste,
          platform,
          resolveHasteModule: name =>
            this._options.moduleMap.getModule(name, platform, true),
          resolveHastePackage: name =>
            this._options.moduleMap.getPackage(name, platform, true),
          getPackageMainPath: this._getPackageMainPath
        }),
        moduleName,
        platform
      );
      return this._getFileResolvedModule(result);
    } catch (error) {
      if (error instanceof Resolver.FailedToResolvePathError) {
        const candidates = error.candidates;
        throw new UnableToResolveError(
          path.relative(this._options.projectRoot, fromModule.path),
          moduleName,
          [
            "\n\nNone of these files exist:",
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.file)
            )}`,
            `  * ${Resolver.formatFileCandidates(
              this._removeRoot(candidates.dir)
            )}`
          ].join("\n")
        );
      }
github facebook / metro / packages / metro-core / src / errors / PackageResolutionError.js View on Github external
constructor(opts: {|
    +originModulePath: string,
    +packageError: InvalidPackageError,
    +targetModuleName: string,
  |}) {
    const perr = opts.packageError;
    super(
      `While trying to resolve module \`${opts.targetModuleName}\` from file ` +
        `\`${opts.originModulePath}\`, the package ` +
        `\`${perr.packageJsonPath}\` was successfully found. However, ` +
        'this package itself specifies ' +
        'a `main` module field that could not be resolved (' +
        `\`${perr.mainPrefixPath}\`. Indeed, none of these files exist:\n\n` +
        `  * ${formatFileCandidates(perr.fileCandidates)}\n` +
        `  * ${formatFileCandidates(perr.indexCandidates)}`,
    );
    Object.assign(this, opts);
  }
}

metro-resolver

🚇 Implementation of Metro's resolution logic.

MIT
Latest version published 1 month ago

Package Health Score

95 / 100
Full package analysis

Similar packages