Skip to content

runBeforeMainModule is an empty array #78

Closed

Description

@tsyeyuanfeng

Do you want to request a feature or report a bug?
report a bug

What is the current behavior?
runBeforeMainModule is an empty array. I noticed that runBeforeMainModule config was removed from metro-bundler in this commit Make the runBeforeMainModule config param to RN repo and make it absolute. runBeforeMainModule is passed into Server instance in react-native local-cli.

const options = {
      assetExts: defaultAssetExts.concat(assetExts),
      assetRegistryPath: ASSET_REGISTRY_PATH,
      blacklistRE: config.getBlacklistRE(),
      extraNodeModules: config.extraNodeModules,
      getPolyfills: config.getPolyfills,
      getTransformOptions: config.getTransformOptions,
      globalTransformCache: null,
      hasteImpl: config.hasteImpl,
      maxWorkers: args.maxWorkers,
      platforms: defaultPlatforms.concat(platforms),
      postMinifyProcess: config.postMinifyProcess,
      postProcessModules: config.postProcessModules,
      postProcessBundleSourcemap: config.postProcessBundleSourcemap,
      projectRoots: config.getProjectRoots(),
      providesModuleNodeModules: providesModuleNodeModules,
      resetCache: args.resetCache,
      reporter: new TerminalReporter(terminal),
      runBeforeMainModule: config.runBeforeMainModule,
      sourceExts: defaultSourceExts.concat(sourceExts),
      transformCache: TransformCaching.useTempDir(),
      transformModulePath: transformModulePath,
      useDeltaBundler: false,
      watch: false,
      workerPath: config.getWorkerPath && config.getWorkerPath(),
};

packagerInstance = new Server(options);

However, runBeforeMainModule is not passed into bundle options.
In shared/output/bundle.js module, runBeforeMainModule is not included when packagerClient.buildBundle is called.

// shared/output/bundle.js
function buildBundle(packagerClient: Server, requestOptions: RequestOptions) {
  return packagerClient.buildBundle({
    ...Server.DEFAULT_BUNDLE_OPTIONS,
    ...requestOptions,
    isolateModuleIDs: true,
  });
}

In Server/index module, runBeforeMainModule is also not included when this._bundler.bundle(options) is called.

async buildBundle(options: BundleOptions): Promise<Bundle> {
    const bundle = await this._bundler.bundle(options);
    const modules = bundle.getModules();
    const nonVirtual = modules.filter(m => !m.virtual);
    bundleDeps.set(bundle, {
      files: new Map(
        nonVirtual.map(({sourcePath, meta}) => [
          sourcePath,
          meta != null ? meta.dependencies : [],
        ]),
      ),
      idToIndex: new Map(modules.map(({id}, i) => [id, i])),
      dependencyPairs: new Map(
        nonVirtual
          .filter(({meta}) => meta && meta.dependencyPairs)
          .map(m => [m.sourcePath, m.meta.dependencyPairs]),
      ),
      outdated: new Set(),
    });
    return bundle;
  }

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior?
Shouldn't this._opts.runBeforeMainModule merged into options when this._bundler.bundle(options) is called in buildBundle function of Server module?

Please provide your exact metro-bundler configuration and mention your metro-bundler, node, yarn/npm version and operating system.

Activity

  1. rafeca commented on Oct 17, 2017

    @rafeca
    Contributor

    Thanks for reporting!

    The commit that you pointed is in fact causing this issue. I'm going to add a fix for it.

    In the meantime, as a workaround, can you try to add a require('InitializeCore'); call at the beginning of your bundle entry point?

  2. miguelocarvajal commented on Nov 7, 2017

    @miguelocarvajal

    @rafeca Tried to work around the issue but could not figure it out.

    What do you mean by bundle entry point exactly?

  3. miguelocarvajal commented on Nov 7, 2017

    @miguelocarvajal

    Shortly after posting I saw "react-native run-ios --device" runs:

    node /.../node_modules/react-native/local-cli/cli.js bundle --entry-file index.js --platform ios --dev true --reset-cache --bundle-output /.../ios/build/Build/Products/.../main.jsbundle --assets-dest /.../ios/build/...
    

    So bundle entry point is index.js, sorry for that noise.

    Adding require('InitializeCore'); did not work around this issue even though I did see a change in the generated main.jsbundle file.

  4. rafeca commented on Nov 7, 2017

    @rafeca
    Contributor

    Adding require('InitializeCore'); did not work around this issue even though I did see a change in the generated main.jsbundle file.

    Did you add it before any other require in that file? What error message are you getting?

  5. miguelocarvajal commented on Nov 7, 2017

    @miguelocarvajal

    Yes, added it before any other require in the file.

    Error message I am getting is:

    "ReferenceError: Can't find variable: process"

  6. petejkim commented on Nov 8, 2017

    @petejkim

    require('InitializeCore') doesn't appear to work

  7. rafeca commented on Nov 8, 2017

    @rafeca
    Contributor

    oh, just realized that if you're using import syntax, you'll need to also import InitializeCore (import Foo from 'InitializeCore';), since imports are hoisted before the require statements. Is this the case?

  8. miguelocarvajal commented on Nov 8, 2017

    @miguelocarvajal

    That was the case. That resolved it! Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions