Expose the buildGraph method as a public metro api

Reviewed By: mjesun

Differential Revision: D7320672

fbshipit-source-id: 251874edc28483d5ac6f810dcda77e04e4729049
This commit is contained in:
Rafael Oleza 2018-03-20 06:53:35 -07:00 committed by Facebook Github Bot
parent add2826ebe
commit 2d77ecc6c0
3 changed files with 67 additions and 6 deletions

View File

@ -279,6 +279,25 @@ class Server {
};
}
async buildGraph(options: BundleOptions): Promise<Graph> {
const entryPoint = getAbsolutePath(
options.entryFile,
this._opts.projectRoots,
);
return await this._deltaBundler.buildGraph({
assetPlugins: options.assetPlugins,
customTransformOptions: options.customTransformOptions,
dev: options.dev,
entryPoints: [entryPoint],
hot: options.hot,
minify: options.minify,
onProgress: options.onProgress,
platform: options.platform,
type: 'module',
});
}
async getRamBundleInfo(options: BundleOptions): Promise<RamBundleInfo> {
const {prepend, graph} = await this._buildGraph(options);

View File

@ -32,6 +32,7 @@ const {readFile} = require('fs-extra');
const {Terminal} = require('metro-core');
import type {ConfigT} from './Config';
import type {Graph} from './DeltaBundler';
import type {GlobalTransformCache} from './lib/GlobalTransformCache';
import type {TransformCache} from './lib/TransformCaching';
import type {Reporter} from './lib/reporting';
@ -275,13 +276,19 @@ exports.runServer = async ({
return httpServer;
};
type RunBuildOptions = {|
type BuildGraphOptions = {|
...PublicMetroOptions,
entry: string,
out: string,
dev?: boolean,
onBegin?: () => void,
onProgress?: (transformedFileCount: number, totalFileCount: number) => void,
platform?: string,
|};
type RunBuildOptions = {|
...PublicMetroOptions,
...BuildGraphOptions,
out: string,
onBegin?: () => void,
onComplete?: () => void,
optimize?: boolean,
output?: {
@ -295,7 +302,6 @@ type RunBuildOptions = {|
(...args: Array<string>) => void,
) => Promise<mixed>,
},
platform?: string,
sourceMap?: boolean,
sourceMapUrl?: string,
|};
@ -315,7 +321,7 @@ exports.runBuild = async ({
sourceMapUrl,
...rest
}: RunBuildOptions) => {
// $FlowFixMe Flow doesn't support object spread enough for the following line
// $FlowIssue #16581373 spread of an exact object should be exact
const metroServer = await runMetro({
...rest,
config,
@ -367,6 +373,34 @@ exports.runBuild = async ({
return {metroServer, metroBundle};
};
exports.buildGraph = async function({
config,
dev = false,
entry,
onProgress,
platform = `web`,
...rest
}: BuildGraphOptions): Promise<Graph> {
// $FlowIssue #16581373 spread of an exact object should be exact
const metroServer = await runMetro({
...rest,
config,
});
try {
return await metroServer.buildGraph({
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
bundleType: 'graph',
dev,
entryFile: entry,
onProgress,
platform,
});
} finally {
await metroServer.end();
}
};
type MetroConfigSearchOptions = {|
cwd?: string,
basename?: string,

View File

@ -29,7 +29,15 @@ import type {
MetroSourceMapSegmentTuple,
} from 'metro-source-map';
type BundleType = 'bundle' | 'delta' | 'map' | 'ram' | 'cli' | 'hmr' | 'todo';
type BundleType =
| 'bundle'
| 'delta'
| 'map'
| 'ram'
| 'cli'
| 'hmr'
| 'todo'
| 'graph';
type MetroSourceMapOrMappings =
| MetroSourceMap
| Array<MetroSourceMapSegmentTuple>;