diff --git a/react-packager/src/Bundler/__tests__/Bundler-test.js b/react-packager/src/Bundler/__tests__/Bundler-test.js index dfb2ddcb..5b735de9 100644 --- a/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -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'], diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 59fcfcbf..aa3576b3 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -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 = ( - string, - Object, - string => Promise>, -) => T | Promise; +export type GetTransformOptions = ( + mainModuleName: string, + options: {}, + getDependencies: string => Promise>, +) => {} | 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, polyfillModuleNames: Array, @@ -153,15 +90,16 @@ class Bundler { _resolver: Resolver; _projectRoots: Array; _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, @@ -761,11 +699,12 @@ class Bundler { mainModuleName: string, options: { dev?: boolean, - platform: string, - hot?: boolean, generateSourceMaps?: boolean, + hot?: boolean, + platform: string, + projectRoots: Array, }, - ) { + ): Promise { 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() { diff --git a/react-packager/src/JSTransformer/worker/worker.js b/react-packager/src/JSTransformer/worker/worker.js index a81edf9e..9b443b84 100644 --- a/react-packager/src/JSTransformer/worker/worker.js +++ b/react-packager/src/JSTransformer/worker/worker.js @@ -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, + projectRoots: Array, + ramGroups?: Array, +} & BabelTransformOptions; + export type Options = { - transform: { - projectRoots: Array, - ramGroups: Array, - platform: string, - preloadedModules: Array, - } & TransformOptions, + transform: TransformOptions, platform: string, }; diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 478fe825..8a0dde2f 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -58,7 +58,7 @@ type Options = { blacklistRE?: RegExp, cacheVersion?: string, extraNodeModules?: {}, - getTransformOptions?: GetTransformOptions<*>, + getTransformOptions?: GetTransformOptions, moduleFormat?: string, platforms?: Array, polyfillModuleNames?: Array, @@ -173,7 +173,7 @@ class Server { blacklistRE: ?RegExp, cacheVersion: string, extraNodeModules: {}, - getTransformOptions?: GetTransformOptions<*>, + getTransformOptions?: GetTransformOptions, moduleFormat: string, platforms: Array, polyfillModuleNames: Array,