Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runBeforeMainModule is an empty array #78

Closed
tsyeyuanfeng opened this issue Oct 17, 2017 · 8 comments
Closed

runBeforeMainModule is an empty array #78

tsyeyuanfeng opened this issue Oct 17, 2017 · 8 comments

Comments

@tsyeyuanfeng
Copy link

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.

@rafeca
Copy link
Contributor

rafeca commented Oct 17, 2017

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?

@miguelocarvajal
Copy link

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

What do you mean by bundle entry point exactly?

@miguelocarvajal
Copy link

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.

@rafeca
Copy link
Contributor

rafeca commented Nov 7, 2017

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?

@miguelocarvajal
Copy link

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

Error message I am getting is:

"ReferenceError: Can't find variable: process"

@petejkim
Copy link

petejkim commented Nov 8, 2017

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

@rafeca
Copy link
Contributor

rafeca commented Nov 8, 2017

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?

@miguelocarvajal
Copy link

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

rafeca added a commit that referenced this issue Nov 8, 2017
Summary: This diff fixes #78 by adding the `runBeforeMainModule` server parameter to the bundler. This is not the cleanest solution, but will be fixed once we use the Delta Bundler for builds from the CLI

Reviewed By: davidaurelio

Differential Revision: D6074469

fbshipit-source-id: 068926ef671d9f897ad9f1bd0540036a97340c00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants