packager: remove validateOpts for Bundler class

Reviewed By: davidaurelio, cpojer

Differential Revision: D4380831

fbshipit-source-id: e3b5b2c39e83bf0b49028820e0f17daef27d7b3c
This commit is contained in:
Jean Lauliac 2017-01-06 06:02:40 -08:00 committed by Facebook Github Bot
parent 6eb417e69d
commit 7b5d796953
4 changed files with 44 additions and 88 deletions

View File

@ -28,9 +28,20 @@ jest
var Bundler = require('../'); var Bundler = require('../');
var Resolver = require('../../Resolver'); var Resolver = require('../../Resolver');
var defaults = require('../../../../defaults');
var sizeOf = require('image-size'); var sizeOf = require('image-size');
var fs = require('fs'); var fs = require('fs');
var commonOptions = {
allowBundleUpdates: false,
assetExts: defaults.assetExts,
cacheVersion: 'smth',
extraNodeModules: {},
platforms: defaults.platforms,
resetCache: false,
watch: false,
};
describe('Bundler', function() { describe('Bundler', function() {
function createModule({ function createModule({
@ -91,6 +102,7 @@ describe('Bundler', function() {
}; };
bundler = new Bundler({ bundler = new Bundler({
...commonOptions,
projectRoots, projectRoots,
assetServer: assetServer, assetServer: assetServer,
}); });
@ -270,6 +282,7 @@ describe('Bundler', function() {
it('allows overriding the platforms array', () => { it('allows overriding the platforms array', () => {
expect(bundler._opts.platforms).toEqual(['ios', 'android', 'windows', 'web']); expect(bundler._opts.platforms).toEqual(['ios', 'android', 'windows', 'web']);
const b = new Bundler({ const b = new Bundler({
...commonOptions,
projectRoots, projectRoots,
assetServer: assetServer, assetServer: assetServer,
platforms: ['android', 'vr'], platforms: ['android', 'vr'],

View File

@ -21,12 +21,10 @@ const Resolver = require('../Resolver');
const Bundle = require('./Bundle'); const Bundle = require('./Bundle');
const HMRBundle = require('./HMRBundle'); const HMRBundle = require('./HMRBundle');
const ModuleTransport = require('../lib/ModuleTransport'); const ModuleTransport = require('../lib/ModuleTransport');
const declareOpts = require('../lib/declareOpts');
const imageSize = require('image-size'); const imageSize = require('image-size');
const path = require('path'); const path = require('path');
const version = require('../../../package.json').version; const version = require('../../../package.json').version;
const denodeify = require('denodeify'); const denodeify = require('denodeify');
const defaults = require('../../../defaults');
const { const {
sep: pathSeparator, sep: pathSeparator,
@ -39,14 +37,14 @@ const {
import type AssetServer from '../AssetServer'; import type AssetServer from '../AssetServer';
import type Module from '../node-haste/Module'; import type Module from '../node-haste/Module';
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
import type {Options as TransformOptions} from '../JSTransformer/worker/worker'; import type {Options as JSTransformerOptions, TransformOptions} from '../JSTransformer/worker/worker';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
export type GetTransformOptions<T> = ( export type GetTransformOptions = (
string, mainModuleName: string,
Object, options: {},
string => Promise<Array<string>>, getDependencies: string => Promise<Array<string>>,
) => T | Promise<T>; ) => {} | Promise<{}>;
const sizeOf = denodeify(imageSize); const sizeOf = denodeify(imageSize);
@ -58,67 +56,6 @@ const {
log, log,
} = require('../Logger'); } = require('../Logger');
const validateOpts = declareOpts({
projectRoots: {
type: 'array',
required: true,
},
blacklistRE: {
type: 'object', // typeof regex is object
},
moduleFormat: {
type: 'string',
default: 'haste',
},
polyfillModuleNames: {
type: 'array',
default: [],
},
cacheVersion: {
type: 'string',
default: '1.0',
},
resetCache: {
type: 'boolean',
default: false,
},
transformModulePath: {
type:'string',
required: false,
},
extraNodeModules: {
type: 'object',
required: false,
},
assetExts: {
type: 'array',
default: ['png'],
},
platforms: {
type: 'array',
default: defaults.platforms,
},
watch: {
type: 'boolean',
default: false,
},
assetServer: {
type: 'object',
required: true,
},
transformTimeoutInterval: {
type: 'number',
required: false,
},
allowBundleUpdates: {
type: 'boolean',
default: false,
},
reporter: {
type: 'object',
},
});
const assetPropertyBlacklist = new Set([ const assetPropertyBlacklist = new Set([
'files', 'files',
'fileSystemLocation', 'fileSystemLocation',
@ -132,7 +69,7 @@ type Options = {
blacklistRE?: RegExp, blacklistRE?: RegExp,
cacheVersion: string, cacheVersion: string,
extraNodeModules: {}, extraNodeModules: {},
getTransformOptions?: GetTransformOptions<*>, getTransformOptions?: GetTransformOptions,
moduleFormat: string, moduleFormat: string,
platforms: Array<string>, platforms: Array<string>,
polyfillModuleNames: Array<string>, polyfillModuleNames: Array<string>,
@ -153,15 +90,16 @@ class Bundler {
_resolver: Resolver; _resolver: Resolver;
_projectRoots: Array<string>; _projectRoots: Array<string>;
_assetServer: AssetServer; _assetServer: AssetServer;
_getTransformOptions: void | GetTransformOptions<*>; _getTransformOptions: void | GetTransformOptions;
constructor(options: Options) { constructor(opts: Options) {
const opts = this._opts = validateOpts(options); this._opts = opts;
opts.projectRoots.forEach(verifyRootExists); opts.projectRoots.forEach(verifyRootExists);
let transformModuleHash; let transformModuleHash;
try { try {
/* $FlowFixMe: if transformModulePath is null it'll just be caught */
const transformModuleStr = fs.readFileSync(opts.transformModulePath); const transformModuleStr = fs.readFileSync(opts.transformModulePath);
transformModuleHash = transformModuleHash =
crypto.createHash('sha1').update(transformModuleStr).digest('hex'); crypto.createHash('sha1').update(transformModuleStr).digest('hex');
@ -216,7 +154,7 @@ class Bundler {
platforms: opts.platforms, platforms: opts.platforms,
polyfillModuleNames: opts.polyfillModuleNames, polyfillModuleNames: opts.polyfillModuleNames,
projectRoots: opts.projectRoots, projectRoots: opts.projectRoots,
reporter: options.reporter, reporter: opts.reporter,
resetCache: opts.resetCache, resetCache: opts.resetCache,
transformCacheKey, transformCacheKey,
transformCode: transformCode:
@ -620,7 +558,7 @@ class Bundler {
module: Module, module: Module,
bundle: Bundle, bundle: Bundle,
entryFilePath: string, entryFilePath: string,
transformOptions: TransformOptions, transformOptions: JSTransformerOptions,
getModuleId: () => number, getModuleId: () => number,
dependencyPairs: Array<[mixed, {path: string}]>, dependencyPairs: Array<[mixed, {path: string}]>,
assetPlugins: Array<string>, assetPlugins: Array<string>,
@ -761,11 +699,12 @@ class Bundler {
mainModuleName: string, mainModuleName: string,
options: { options: {
dev?: boolean, dev?: boolean,
platform: string,
hot?: boolean,
generateSourceMaps?: boolean, generateSourceMaps?: boolean,
hot?: boolean,
platform: string,
projectRoots: Array<string>,
}, },
) { ): Promise<TransformOptions> {
const getDependencies = (entryFile: string) => const getDependencies = (entryFile: string) =>
this.getDependencies({...options, entryFile}) this.getDependencies({...options, entryFile})
.then(r => r.dependencies.map(d => d.path)); .then(r => r.dependencies.map(d => d.path));
@ -773,7 +712,9 @@ class Bundler {
? this._getTransformOptions(mainModuleName, options, getDependencies) ? this._getTransformOptions(mainModuleName, options, getDependencies)
: null; : null;
return Promise.resolve(extraOptions) return Promise.resolve(extraOptions)
.then(extraOpts => Object.assign(options, extraOpts)); .then(extraOpts => {
return {...options, ...extraOpts};
});
} }
getResolver() { getResolver() {

View File

@ -18,7 +18,7 @@ const invariant = require('invariant');
const minify = require('./minify'); const minify = require('./minify');
import type {LogEntry} from '../../Logger/Types'; import type {LogEntry} from '../../Logger/Types';
import type {Ast, SourceMap, TransformOptions} from 'babel-core'; import type {Ast, SourceMap, TransformOptions as BabelTransformOptions} from 'babel-core';
function makeTransformParams(filename, sourceCode, options) { function makeTransformParams(filename, sourceCode, options) {
if (filename.endsWith('.json')) { if (filename.endsWith('.json')) {
@ -46,13 +46,15 @@ type Transform = (
) => mixed, ) => mixed,
) => void; ) => void;
export type TransformOptions = {
platform: string,
preloadedModules?: Array<string>,
projectRoots: Array<string>,
ramGroups?: Array<string>,
} & BabelTransformOptions;
export type Options = { export type Options = {
transform: { transform: TransformOptions,
projectRoots: Array<string>,
ramGroups: Array<string>,
platform: string,
preloadedModules: Array<string>,
} & TransformOptions,
platform: string, platform: string,
}; };

View File

@ -58,7 +58,7 @@ type Options = {
blacklistRE?: RegExp, blacklistRE?: RegExp,
cacheVersion?: string, cacheVersion?: string,
extraNodeModules?: {}, extraNodeModules?: {},
getTransformOptions?: GetTransformOptions<*>, getTransformOptions?: GetTransformOptions,
moduleFormat?: string, moduleFormat?: string,
platforms?: Array<string>, platforms?: Array<string>,
polyfillModuleNames?: Array<string>, polyfillModuleNames?: Array<string>,
@ -173,7 +173,7 @@ class Server {
blacklistRE: ?RegExp, blacklistRE: ?RegExp,
cacheVersion: string, cacheVersion: string,
extraNodeModules: {}, extraNodeModules: {},
getTransformOptions?: GetTransformOptions<*>, getTransformOptions?: GetTransformOptions,
moduleFormat: string, moduleFormat: string,
platforms: Array<string>, platforms: Array<string>,
polyfillModuleNames: Array<string>, polyfillModuleNames: Array<string>,