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

View File

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

View File

@ -18,7 +18,7 @@ const invariant = require('invariant');
const minify = require('./minify');
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) {
if (filename.endsWith('.json')) {
@ -46,13 +46,15 @@ type Transform = (
) => mixed,
) => void;
export type TransformOptions = {
platform: string,
preloadedModules?: Array<string>,
projectRoots: Array<string>,
ramGroups?: Array<string>,
} & BabelTransformOptions;
export type Options = {
transform: {
projectRoots: Array<string>,
ramGroups: Array<string>,
platform: string,
preloadedModules: Array<string>,
} & TransformOptions,
transform: TransformOptions,
platform: string,
};

View File

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