Repository navigation
runBeforeMainModule is an empty array #78
Description
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
rafeca commentedon 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 commentedon Nov 7, 2017
@rafeca Tried to work around the issue but could not figure it out.
What do you mean by bundle entry point exactly?
miguelocarvajal commentedon Nov 7, 2017
Shortly after posting I saw "react-native run-ios --device" runs:
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 commentedon Nov 7, 2017
Did you add it before any other require in that file? What error message are you getting?
miguelocarvajal commentedon Nov 7, 2017
Yes, added it before any other require in the file.
Error message I am getting is:
"ReferenceError: Can't find variable: process"
petejkim commentedon Nov 8, 2017
require('InitializeCore')doesn't appear to workrafeca commentedon 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 commentedon Nov 8, 2017
That was the case. That resolved it! Thanks so much!
Always set the runBeforeMainModule bundle param on buildBundle()