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
This commit is contained in:
Jean Lauliac 2017-05-22 08:57:04 -07:00 committed by Facebook Github Bot
parent 8f31444c60
commit bfc0e8c26f
4 changed files with 38 additions and 11 deletions

View File

@ -22,6 +22,7 @@ const RN_CLI_CONFIG = 'rn-cli.config.js';
import type {GetTransformOptions, PostMinifyProcess, PostProcessModules} from '../../packager/src/Bundler'; import type {GetTransformOptions, PostMinifyProcess, PostProcessModules} from '../../packager/src/Bundler';
import type {HasteImpl} from '../../packager/src/node-haste/Module'; import type {HasteImpl} from '../../packager/src/node-haste/Module';
import type {TransformVariants} from '../../packager/src/ModuleGraph/types.flow'; import type {TransformVariants} from '../../packager/src/ModuleGraph/types.flow';
import type {PostProcessModules as PostProcessModulesForBuck} from '../../packager/src/ModuleGraph/types.flow.js';
/** /**
* Configuration file of the CLI. * Configuration file of the CLI.
@ -89,6 +90,12 @@ export type ConfigT = {
*/ */
postProcessModules: PostProcessModules, postProcessModules: PostProcessModules,
/**
* Same as `postProcessModules` but for the Buck worker. Eventually we do want
* to unify both variants.
*/
postProcessModulesForBuck: PostProcessModulesForBuck,
/** /**
* A module that exports: * A module that exports:
* - a `getHasteName(filePath)` method that returns `hasteName` for module at * - a `getHasteName(filePath)` method that returns `hasteName` for module at
@ -112,6 +119,7 @@ const defaultConfig: ConfigT = {
getTransformOptions: async () => ({}), getTransformOptions: async () => ({}),
postMinifyProcess: x => x, postMinifyProcess: x => x,
postProcessModules: modules => modules, postProcessModules: modules => modules,
postProcessModulesForBuck: modules => modules,
transformVariants: () => ({default: {}}), transformVariants: () => ({default: {}}),
}; };

View File

@ -22,6 +22,7 @@ import type {
GraphFn, GraphFn,
GraphResult, GraphResult,
Module, Module,
PostProcessModules,
} from './types.flow'; } from './types.flow';
type BuildFn = ( type BuildFn = (
@ -37,6 +38,7 @@ type BuildOptions = {|
exports.createBuildSetup = ( exports.createBuildSetup = (
graph: GraphFn, graph: GraphFn,
postProcessModules: PostProcessModules,
translateDefaultsPath: string => string = x => x, translateDefaultsPath: string => string = x => x,
): BuildFn => ): BuildFn =>
(entryPoints, options, callback) => { (entryPoints, options, callback) => {
@ -51,10 +53,16 @@ exports.createBuildSetup = (
const graphOnlyModules = seq(graphWithOptions, getModules); const graphOnlyModules = seq(graphWithOptions, getModules);
parallel({ parallel({
graph: cb => graphWithOptions( graph: cb => graphWithOptions(entryPoints, (error, result) => {
entryPoints, if (error) {
cb, 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( moduleSystem: cb => graphOnlyModules(
[translateDefaultsPath(defaults.moduleSystem)], [translateDefaultsPath(defaults.moduleSystem)],
cb, cb,

View File

@ -16,7 +16,9 @@ const defaults = require('../../../defaults');
const FILE_TYPE = 'module'; const FILE_TYPE = 'module';
describe('build setup', () => { 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 noOptions = {};
const noEntryPoints = []; const noEntryPoints = [];
@ -72,11 +74,11 @@ describe('build setup', () => {
}); });
}); });
it('places all entry points at the end', done => { it('places all entry points and dependencies at the end, post-processed', done => {
const entryPoints = ['a', 'b', 'c']; const entryPoints = ['b', 'c', 'd'];
buildSetup(entryPoints, noOptions, (error, result) => { buildSetup(entryPoints, noOptions, (error, result) => {
expect(Array.from(result.modules).slice(-3)) expect(Array.from(result.modules).slice(-4))
.toEqual(entryPoints.map(moduleFromPath)); .toEqual(['a', 'b', 'c', 'd'].map(moduleFromPath));
done(); done();
}); });
}); });
@ -84,7 +86,7 @@ describe('build setup', () => {
function moduleFromPath(path) { function moduleFromPath(path) {
return { return {
dependencies: [], dependencies: path === 'b' ? ['a'] : [],
file: { file: {
code: '', code: '',
path, path,
@ -95,8 +97,12 @@ function moduleFromPath(path) {
function graph(entryPoints, platform, options, callback) { function graph(entryPoints, platform, options, callback) {
const modules = Array.from(entryPoints, moduleFromPath); const modules = Array.from(entryPoints, moduleFromPath);
const depModules = Array.prototype.concat.apply(
[],
modules.map(x => x.dependencies.map(moduleFromPath)),
);
callback(null, { callback(null, {
entryModules: modules, entryModules: modules,
modules, modules: modules.concat(depModules),
}); });
} }

View File

@ -70,6 +70,11 @@ export type Module = {|
file: File, file: File,
|}; |};
export type PostProcessModules = (
modules: Iterable<Module>,
entryPoints: Array<string>,
) => Iterable<Module>;
export type OutputFn = ({| export type OutputFn = ({|
filename: string, filename: string,
idForPath: IdForPathFn, idForPath: IdForPathFn,