Make the runBeforeMainModule config param to RN repo and make it absolute

Reviewed By: davidaurelio

Differential Revision: D5880700

fbshipit-source-id: 5df6781026030395900388c561283abadefa6511
This commit is contained in:
Rafael Oleza 2017-09-26 12:54:44 -07:00 committed by Facebook Github Bot
parent f9246bcbb3
commit eefb8e9fc5
8 changed files with 30 additions and 48 deletions

View File

@ -384,7 +384,7 @@ class Bundler {
onProgress?: () => void,
platform?: ?string,
resolutionResponse?: ResolutionResponse<Module, BundlingOptions>,
runBeforeMainModule?: boolean,
runBeforeMainModule?: Array<string>,
runModule?: boolean,
unbundle?: boolean,
}) {
@ -406,12 +406,10 @@ class Bundler {
bundle: finalBundle,
transformedModules,
response,
modulesByName,
}: {
bundle: Bundle,
transformedModules: Array<{module: Module, transformed: ModuleTransport}>,
response: ResolutionResponse<Module, BundlingOptions>,
modulesByName: {[name: string]: Module},
}) =>
this._resolverPromise
.then(resolver =>
@ -422,16 +420,20 @@ class Bundler {
),
)
.then(() => {
const runBeforeMainModuleIds = Array.isArray(runBeforeMainModule)
? runBeforeMainModule
.map(name => modulesByName[name])
.filter(Boolean)
.map(response.getModuleId)
: undefined;
return Promise.all(
runBeforeMainModule
? runBeforeMainModule.map(path => this.getModuleForPath(path))
: [],
);
})
.then(runBeforeMainModules => {
finalBundle.finalize({
runModule,
runBeforeMainModule: runBeforeMainModuleIds,
runBeforeMainModule: runBeforeMainModules.map(module =>
/* $FlowFixMe: looks like ResolutionResponse is monkey-patched
* with `getModuleId`. */
response.getModuleId(module),
),
allowUpdates: this._opts.allowBundleUpdates,
});
return finalBundle;
@ -480,8 +482,6 @@ class Bundler {
}),
);
const modulesByName = Object.create(null);
if (!resolutionResponse) {
resolutionResponse = this.getDependencies({
entryFile,
@ -539,7 +539,6 @@ class Bundler {
dependencyPairs: response.getResolvedDependencyPairs(module),
}).then(transformed => {
modulesByTransport.set(transformed, module);
modulesByName[transformed.name] = module;
onModuleTransformed({
module,
response,
@ -565,7 +564,6 @@ class Bundler {
bundle,
transformedModules,
response,
modulesByName,
});
})
.then(() => bundle);

View File

@ -128,6 +128,12 @@ export type ConfigT = {
hasteImpl?: HasteImpl,
transformVariants: () => TransformVariants,
/**
* An array of modules to be required before the entry point. It should
* contain the absolute path of each module.
*/
runBeforeMainModule: Array<string>,
};
const DEFAULT = ({
@ -152,6 +158,7 @@ const DEFAULT = ({
postProcessModules: modules => modules,
postProcessModulesForBuck: modules => modules,
postProcessBundleSourcemap: ({code, map, outFileName}) => ({code, map}),
runBeforeMainModule: [],
transformVariants: () => ({default: {}}),
getWorkerPath: () => null,
}: ConfigT);

View File

@ -142,14 +142,6 @@ class DeltaCalculator extends EventEmitter {
return this._shallowDependencies;
}
/**
* Returns a map of module names to Module objects (module name being the
* result of calling `Module.getName()`).
*/
getModulesByName(): Map<string, Module> {
return this._modulesByName;
}
getInverseDependencies(): Map<string, Set<string>> {
return this._inverseDependencies;
}
@ -316,16 +308,6 @@ class DeltaCalculator extends EventEmitter {
});
}
// Last iteration through all dependencies to populate the modulesByName
// cache (we could get rid of this if the `runBeforeMainModule` option was
// an asbsolute path).
await Promise.all(
currentDependencies.map(async module => {
const name = await module.getName();
this._modulesByName.set(name, module);
}),
);
// Yet another iteration through all the dependencies. This one is to
// calculate the inverse dependencies. Right now we cannot do a faster
// iteration to only calculate this for changed files since

View File

@ -194,10 +194,7 @@ class DeltaTransformer extends EventEmitter {
// Return the source code that gets appended to all the modules. This
// contains the require() calls to startup the execution of the modules.
const appendSources = reset
? await this._getAppend(
dependencyPairs,
this._deltaCalculator.getModulesByName(),
)
? await this._getAppend(dependencyPairs)
: new Map();
// Inverse dependencies are needed for HMR.
@ -249,7 +246,6 @@ class DeltaTransformer extends EventEmitter {
async _getAppend(
dependencyPairs: ShallowDependencies,
modulesByName: Map<string, Module>,
): Promise<DeltaEntries> {
// Get the absolute path of the entry file, in order to be able to get the
// actual correspondant module (and its moduleId) to be able to add the
@ -264,9 +260,8 @@ class DeltaTransformer extends EventEmitter {
// module so the last thing that gets required is the entry point.
return new Map(
this._bundleOptions.runBeforeMainModule
.map(name => modulesByName.get(name))
.map(path => this._resolver.getModuleForPath(path))
.concat(entryPointModule)
.filter(Boolean)
.map(this._getModuleId)
.map(moduleId => {
const code = `;require(${JSON.stringify(moduleId)});`;

View File

@ -44,6 +44,7 @@ describe('processRequest', () => {
cacheVersion: null,
polyfillModuleNames: null,
reporter: require('../../lib/reporting').nullReporter,
runBeforeMainModule: ['InitializeCore'],
};
const makeRequest = (reqHandler, requrl, reqOptions) => new Promise(resolve =>
@ -470,7 +471,7 @@ describe('processRequest', () => {
onProgress: null,
platform: undefined,
resolutionResponse: null,
runBeforeMainModule: ['InitializeCore'],
runBeforeMainModule: [],
runModule: true,
sourceMapUrl: null,
unbundle: false,

View File

@ -89,6 +89,7 @@ export type Options = {|
providesModuleNodeModules?: Array<string>,
reporter?: Reporter,
resetCache?: boolean,
+runBeforeMainModule: Array<string>,
silent?: boolean,
+sourceExts: ?Array<string>,
+transformCache: TransformCache,
@ -160,6 +161,7 @@ class Server {
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
resetCache: boolean,
+runBeforeMainModule: Array<string>,
silent: boolean,
+sourceExts: Array<string>,
+transformCache: TransformCache,
@ -215,6 +217,7 @@ class Server {
providesModuleNodeModules: options.providesModuleNodeModules,
reporter,
resetCache: options.resetCache || false,
runBeforeMainModule: options.runBeforeMainModule,
silent: options.silent || false,
sourceExts: options.sourceExts || defaults.sourceExts,
transformCache: options.transformCache,
@ -1182,7 +1185,7 @@ class Server {
minify,
excludeSource,
hot: true,
runBeforeMainModule: defaults.runBeforeMainModule,
runBeforeMainModule: this._opts.runBeforeMainModule,
runModule: this._getBoolOptionFromQuery(urlObj.query, 'runModule', true),
inlineSourceMap: this._getBoolOptionFromQuery(
urlObj.query,
@ -1236,7 +1239,7 @@ Server.DEFAULT_BUNDLE_OPTIONS = {
minify: false,
onProgress: null,
resolutionResponse: null,
runBeforeMainModule: defaults.runBeforeMainModule,
runBeforeMainModule: [],
runModule: true,
sourceMapUrl: null,
unbundle: false,

View File

@ -52,8 +52,3 @@ exports.platforms = ['ios', 'android', 'windows', 'web'];
exports.providesModuleNodeModules = ['react-native', 'react-native-windows'];
exports.transformModulePath = require.resolve('./defaultTransform.js');
exports.runBeforeMainModule = [
// Ensures essential globals are available and are patched correctly.
'InitializeCore',
];

View File

@ -193,6 +193,7 @@ function toServerOptions(options: Options): ServerOptions {
providesModuleNodeModules: options.providesModuleNodeModules,
reporter: options.reporter,
resetCache: options.resetCache,
runBeforeMainModule: options.runBeforeMainModule,
silent: options.silent,
sourceExts: options.sourceExts,
transformCache: options.transformCache || TransformCaching.useTempDir(),