From 307eede076018a109b9fc9ff709deeb393e71da1 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 22 May 2017 08:57:04 -0700 Subject: [PATCH] packager: add preprocess for buck worker Summary: For the Buck integration (work-in-progress), we want to add the ability to do some custom preprocessing similar to the packager server. The signature is different so I prefer to have a separate function for that. Also we don't need the transform options right now, I suggest we don't add them for now and add them later if necessary. Reviewed By: davidaurelio Differential Revision: D5094632 fbshipit-source-id: 1775ddef90b331deabc5be3e57a67436bce06c82 --- .../src/ModuleGraph/ModuleGraph.js | 16 +++++++++++---- .../ModuleGraph/__tests__/ModuleGraph-test.js | 20 ++++++++++++------- .../src/ModuleGraph/types.flow.js | 5 +++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js b/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js index ac660b5e..e0018609 100644 --- a/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js +++ b/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js @@ -22,6 +22,7 @@ import type { GraphFn, GraphResult, Module, + PostProcessModules, } from './types.flow'; type BuildFn = ( @@ -37,6 +38,7 @@ type BuildOptions = {| exports.createBuildSetup = ( graph: GraphFn, + postProcessModules: PostProcessModules, translateDefaultsPath: string => string = x => x, ): BuildFn => (entryPoints, options, callback) => { @@ -51,10 +53,16 @@ exports.createBuildSetup = ( const graphOnlyModules = seq(graphWithOptions, getModules); parallel({ - graph: cb => graphWithOptions( - entryPoints, - cb, - ), + graph: cb => graphWithOptions(entryPoints, (error, result) => { + if (error) { + cb(error); + return; + } + /* $FlowFixMe: not undefined if there is no error */ + const {modules, entryModules} = result; + const prModules = postProcessModules(modules, [...entryPoints]); + cb(null, {modules: prModules, entryModules}); + }), moduleSystem: cb => graphOnlyModules( [translateDefaultsPath(defaults.moduleSystem)], cb, diff --git a/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js b/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js index bc91a412..334ea931 100644 --- a/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js +++ b/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js @@ -16,7 +16,9 @@ const defaults = require('../../../defaults'); const FILE_TYPE = 'module'; describe('build setup', () => { - const buildSetup = ModuleGraph.createBuildSetup(graph); + const buildSetup = ModuleGraph.createBuildSetup(graph, mds => { + return [...mds].sort((l, r) => l.file.path > r.file.path); + }); const noOptions = {}; const noEntryPoints = []; @@ -72,11 +74,11 @@ describe('build setup', () => { }); }); - it('places all entry points at the end', done => { - const entryPoints = ['a', 'b', 'c']; + it('places all entry points and dependencies at the end, post-processed', done => { + const entryPoints = ['b', 'c', 'd']; buildSetup(entryPoints, noOptions, (error, result) => { - expect(Array.from(result.modules).slice(-3)) - .toEqual(entryPoints.map(moduleFromPath)); + expect(Array.from(result.modules).slice(-4)) + .toEqual(['a', 'b', 'c', 'd'].map(moduleFromPath)); done(); }); }); @@ -84,7 +86,7 @@ describe('build setup', () => { function moduleFromPath(path) { return { - dependencies: [], + dependencies: path === 'b' ? ['a'] : [], file: { code: '', path, @@ -95,8 +97,12 @@ function moduleFromPath(path) { function graph(entryPoints, platform, options, callback) { const modules = Array.from(entryPoints, moduleFromPath); + const depModules = Array.prototype.concat.apply( + [], + modules.map(x => x.dependencies.map(moduleFromPath)), + ); callback(null, { entryModules: modules, - modules, + modules: modules.concat(depModules), }); } diff --git a/packages/metro-bundler/src/ModuleGraph/types.flow.js b/packages/metro-bundler/src/ModuleGraph/types.flow.js index da283a7a..e2a8eea8 100644 --- a/packages/metro-bundler/src/ModuleGraph/types.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/types.flow.js @@ -70,6 +70,11 @@ export type Module = {| file: File, |}; +export type PostProcessModules = ( + modules: Iterable, + entryPoints: Array, +) => Iterable; + export type OutputFn = ({| filename: string, idForPath: IdForPathFn,