require InitializeJavaScriptAppEngine in bundle

Reviewed By: martinbigio

Differential Revision: D2582794

fb-gh-sync-id: d6cfdda81f556ef0cefb2e9dc58f5978b651bd5b
This commit is contained in:
Bhuwan Khattar 2015-10-28 21:05:15 -07:00 committed by facebook-github-bot-4
parent c5c4dc94b1
commit 02c3c465f8
6 changed files with 109 additions and 55 deletions

View File

@ -53,13 +53,8 @@ class Bundle {
finalize(options) {
options = options || {};
if (options.runMainModule) {
const runCode = ';require("' + this._mainModuleId + '");';
this.addModule(new ModuleTransport({
code: runCode,
virtual: true,
sourceCode: runCode,
sourcePath: 'RunMainModule.js'
}));
options.runBeforeMainModule.forEach(this._addRequireCall, this);
this._addRequireCall(this._mainModuleId);
}
Object.freeze(this._modules);
@ -69,6 +64,18 @@ class Bundle {
this._finalized = true;
}
_addRequireCall(moduleId) {
const code = ';require("' + moduleId + '");';
const name = 'require-' + moduleId;
this.addModule(new ModuleTransport({
name,
code,
virtual: true,
sourceCode: code,
sourcePath: name + '.js',
}));
}
_assertFinalized() {
if (!this._finalized) {
throw new Error('Bundle needs to be finalized before getting any source');

View File

@ -81,10 +81,14 @@ describe('Bundle', function() {
}));
bundle.setMainModuleId('foo');
bundle.finalize({runMainModule: true});
bundle.finalize({
runBeforeMainModule: ['bar'],
runMainModule: true,
});
expect(bundle.getSource()).toBe([
'transformed foo;',
'transformed bar;',
';require("bar");',
';require("foo");',
'\/\/@ sourceMappingURL=test_url',
].join('\n'));
@ -141,7 +145,10 @@ describe('Bundle', function() {
}));
p.setMainModuleId('foo');
p.finalize({runMainModule: true});
p.finalize({
runBeforeMainModule: [],
runMainModule: true,
});
var s = p.getSourceMap();
expect(s).toEqual(genSourceMap(p.getModules()));
});
@ -171,7 +178,10 @@ describe('Bundle', function() {
}));
p.setMainModuleId('foo');
p.finalize({runMainModule: true});
p.finalize({
runBeforeMainModule: ['InitializeJavaScriptAppEngine'],
runMainModule: true,
});
var s = p.getSourceMap();
expect(s).toEqual({
@ -200,14 +210,28 @@ describe('Bundle', function() {
line: 6
},
map: {
file: 'RunMainModule.js',
file: 'require-InitializeJavaScriptAppEngine.js',
mappings: 'AAAA;',
names: [],
sources: [ 'RunMainModule.js' ],
sources: [ 'require-InitializeJavaScriptAppEngine.js' ],
sourcesContent: [';require("InitializeJavaScriptAppEngine");'],
version: 3,
}
},
{
offset: {
column: 0,
line: 7
},
map: {
file: 'require-foo.js',
mappings: 'AAAA;',
names: [],
sources: [ 'require-foo.js' ],
sourcesContent: [';require("foo");'],
version: 3,
}
}
},
],
});
});

View File

@ -146,8 +146,12 @@ describe('Bundler', function() {
};
});
return bundler.bundle('/root/foo.js', true, 'source_map_url')
.then(function(p) {
return bundler.bundle({
entryFile: '/root/foo.js',
runBeforeMainModule: [],
runModule: true,
sourceMapUrl: 'source_map_url',
}).then(function(p) {
expect(p.addModule.mock.calls[0][0]).toEqual({
code: 'lol transformed /root/foo.js lol',
map: 'sourcemap /root/foo.js',
@ -221,7 +225,7 @@ describe('Bundler', function() {
});
expect(p.finalize.mock.calls[0]).toEqual([
{runMainModule: true}
{runMainModule: true, runBeforeMainModule: []}
]);
expect(p.addAsset.mock.calls).toContain([

View File

@ -132,12 +132,19 @@ class Bundler {
return this._bundlesLayout.generateLayout(main, isDev);
}
bundle(main, runModule, sourceMapUrl, isDev, platform) {
bundle({
entryFile,
runModule: runMainModule,
runBeforeMainModule,
sourceMapUrl,
dev: isDev,
platform,
}) {
const bundle = new Bundle(sourceMapUrl);
const findEventId = Activity.startEvent('find dependencies');
let transformEventId;
return this.getDependencies(main, isDev, platform).then((response) => {
return this.getDependencies(entryFile, isDev, platform).then((response) => {
Activity.endEvent(findEventId);
transformEventId = Activity.startEvent('transform');
@ -174,7 +181,7 @@ class Bundler {
bundle.addModule(moduleTransport);
});
bundle.finalize({ runMainModule: runModule });
bundle.finalize({runBeforeMainModule, runMainModule});
return bundle;
});
}

View File

@ -106,13 +106,16 @@ describe('processRequest', () => {
'index.ios.includeRequire.bundle'
).then(response => {
expect(response).toEqual('this is the source');
expect(Bundler.prototype.bundle).toBeCalledWith(
'index.ios.js',
true,
'index.ios.includeRequire.map',
true,
undefined
);
expect(Bundler.prototype.bundle).toBeCalledWith({
entryFile: 'index.ios.js',
inlineSourceMap: false,
minify: false,
runModule: true,
sourceMapUrl: 'index.ios.includeRequire.map',
dev: true,
platform: undefined,
runBeforeMainModule: ['InitializeJavaScriptAppEngine'],
});
});
});
@ -122,13 +125,16 @@ describe('processRequest', () => {
'index.bundle?platform=ios'
).then(function(response) {
expect(response).toEqual('this is the source');
expect(Bundler.prototype.bundle).toBeCalledWith(
'index.js',
true,
'index.map?platform=ios',
true,
'ios',
);
expect(Bundler.prototype.bundle).toBeCalledWith({
entryFile: 'index.js',
inlineSourceMap: false,
minify: false,
runModule: true,
sourceMapUrl: 'index.map?platform=ios',
dev: true,
platform: 'ios',
runBeforeMainModule: ['InitializeJavaScriptAppEngine'],
});
});
});
@ -260,13 +266,15 @@ describe('processRequest', () => {
return server.buildBundle({
entryFile: 'foo file'
}).then(() =>
expect(Bundler.prototype.bundle).toBeCalledWith(
'foo file',
true,
undefined,
true,
undefined
)
expect(Bundler.prototype.bundle).toBeCalledWith({
entryFile: 'foo file',
inlineSourceMap: false,
minify: false,
runModule: true,
dev: true,
platform: undefined,
runBeforeMainModule: ['InitializeJavaScriptAppEngine'],
})
);
});
});
@ -275,13 +283,16 @@ describe('processRequest', () => {
pit('Calls the bundler with the correct args', () => {
return server.buildBundleFromUrl('/path/to/foo.bundle?dev=false&runModule=false')
.then(() =>
expect(Bundler.prototype.bundle).toBeCalledWith(
'path/to/foo.js',
false,
'/path/to/foo.map?dev=false&runModule=false',
false,
undefined
)
expect(Bundler.prototype.bundle).toBeCalledWith({
entryFile: 'path/to/foo.js',
inlineSourceMap: false,
minify: false,
runModule: false,
sourceMapUrl: '/path/to/foo.map?dev=false&runModule=false',
dev: false,
platform: undefined,
runBeforeMainModule: ['InitializeJavaScriptAppEngine'],
})
);
});
});

View File

@ -94,7 +94,14 @@ const bundleOpts = declareOpts({
platform: {
type: 'string',
required: true,
}
},
runBeforeMainModule: {
type: 'array',
default: [
// Ensures essential globals are available and are patched correctly.
'InitializeJavaScriptAppEngine'
],
},
});
const dependencyOpts = declareOpts({
@ -179,13 +186,7 @@ class Server {
}
const opts = bundleOpts(options);
return this._bundler.bundle(
opts.entryFile,
opts.runModule,
opts.sourceMapUrl,
opts.dev,
opts.platform
);
return this._bundler.bundle(opts);
});
}