Skip to content

Commit

Permalink
Expose a modulesOnly option for the bundle endpoint
Browse files Browse the repository at this point in the history
Reviewed By: rubennorte

Differential Revision: D15898570

fbshipit-source-id: c8b11eac6cd616b7fb3adedcce6205a647a38da2
  • Loading branch information
cpojer authored and facebook-github-bot committed Jun 24, 2019
1 parent beb3d1a commit 79904d8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,46 @@ it('should add an inline source map to a very simple bundle', () => {
version: 3,
});
});

it('does not add polyfills when `modulesOnly` is used', () => {
expect(
baseJSBundle(
'/root/foo',
[polyfill],
{
dependencies: new Map([
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
},
{
processModuleFilter: () => true,
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
modulesOnly: true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
sourceMapUrl: 'http://localhost/bundle.map',
},
),
).toMatchInlineSnapshot(`
Object {
"modules": Array [
Array [
"foo",
"__d(function() {/* code for foo */},\\"foo\\",[\\"bar\\"],\\"foo\\");",
],
Array [
"bar",
"__d(function() {/* code for bar */},\\"bar\\",[],\\"bar\\");",
],
],
"post": "require(\\"foo\\");
//# sourceMappingURL=http://localhost/bundle.map",
"pre": "",
}
`);
});
5 changes: 5 additions & 0 deletions packages/metro/src/DeltaBundler/Serializers/baseJSBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function baseJSBundle(
projectRoot: options.projectRoot,
};

// Do not prepend polyfills or the require runtime when only modules are requested
if (options.modulesOnly) {
preModules = [];
}

const preCode = processModules(preModules, processModulesOptions)
.map(([_, code]) => code)
.join('\n');
Expand Down
5 changes: 3 additions & 2 deletions packages/metro/src/DeltaBundler/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ export type DeltaResult<T = MixedOutput> = {|
|};

export type SerializerOptions = {|
+processModuleFilter: (module: Module<>) => boolean,
+createModuleId: string => number,
+dev: boolean,
+getRunModuleStatement: (number | string) => string,
+inlineSourceMap: ?boolean,
+modulesOnly: boolean,
+processModuleFilter: (module: Module<>) => boolean,
+projectRoot: string,
+runBeforeMainModule: $ReadOnlyArray<string>,
+runModule: boolean,
+sourceMapUrl: ?string,
+inlineSourceMap: ?boolean,
|};
6 changes: 6 additions & 0 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Server {
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
dev: transformOptions.dev,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
runBeforeMainModule: this._config.serializer.getModulesRunBeforeMainModule(
path.relative(this._config.projectRoot, entryPoint),
),
Expand Down Expand Up @@ -241,6 +242,7 @@ class Server {
getTransformOptions: this._config.transformer.getTransformOptions,
platform: transformOptions.platform,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
runBeforeMainModule: this._config.serializer.getModulesRunBeforeMainModule(
path.relative(this._config.projectRoot, entryPoint),
),
Expand Down Expand Up @@ -655,6 +657,7 @@ class Server {
dev: transformOptions.dev,
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
runBeforeMainModule: this._config.serializer.getModulesRunBeforeMainModule(
path.relative(this._config.projectRoot, entryFile),
),
Expand Down Expand Up @@ -733,6 +736,7 @@ class Server {
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
dev: transformOptions.dev,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
runBeforeMainModule: this._config.serializer.getModulesRunBeforeMainModule(
path.relative(this._config.projectRoot, entryFile),
),
Expand Down Expand Up @@ -874,6 +878,7 @@ class Server {
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
dev: transformOptions.dev,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
runBeforeMainModule: this._config.serializer.getModulesRunBeforeMainModule(
path.relative(this._config.projectRoot, entryFile),
),
Expand Down Expand Up @@ -1057,6 +1062,7 @@ class Server {
...Server.DEFAULT_GRAPH_OPTIONS,
excludeSource: false,
inlineSourceMap: false,
modulesOnly: false,
onProgress: null,
runModule: true,
sourceMapUrl: null,
Expand Down
2 changes: 2 additions & 0 deletions packages/metro/src/lib/parseOptionsFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function parseOptionsFromUrl(
false,
);
const runModule = getBoolOptionFromQuery(urlQuery, 'runModule', true);
const modulesOnly = getBoolOptionFromQuery(urlQuery, 'modulesOnly', false);

const customTransformOptions = parseCustomTransformOptions(urlObj);

Expand All @@ -96,6 +97,7 @@ function parseOptionsFromUrl(
dev,
hot: true,
minify,
modulesOnly,
platform,
onProgress: null,
entryFile: entryFileRelativeToProjectRoot,
Expand Down
5 changes: 3 additions & 2 deletions packages/metro/src/lib/splitBundleOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ function splitBundleOptions(options: BundleOptions): SplitBundleOptions {
type: 'module',
},
serializerOptions: {
sourceMapUrl: options.sourceMapUrl,
runModule: options.runModule,
excludeSource: options.excludeSource,
inlineSourceMap: options.inlineSourceMap,
modulesOnly: options.modulesOnly,
runModule: options.runModule,
sourceMapUrl: options.sourceMapUrl,
},
onProgress: options.onProgress,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/metro/src/shared/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type BundleOptions = {
+hot: boolean,
+inlineSourceMap: boolean,
minify: boolean,
+modulesOnly: boolean,
onProgress: ?(doneCont: number, totalCount: number) => mixed,
+platform: ?string,
+runModule: boolean,
Expand All @@ -57,6 +58,7 @@ export type SerializerOptions = {|
+runModule: boolean,
+excludeSource: boolean,
+inlineSourceMap: boolean,
+modulesOnly: boolean,
|};

// Stricter representation of BundleOptions.
Expand Down

0 comments on commit 79904d8

Please sign in to comment.