Skip to content

Commit

Permalink
Allow custom platforms for the RN Packager on a per-project basis
Browse files Browse the repository at this point in the history
Reviewed By: cpojer

Differential Revision: D4255979

fbshipit-source-id: bf900b67ee30e2f994e96c9a6103ed2e53a87f88
  • Loading branch information
andrewimm authored and Martin Konicek committed Dec 12, 2016
1 parent cd8cfc3 commit 5b37c3e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
9 changes: 9 additions & 0 deletions local-cli/default.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ var config = {
return [];
},

/**
* Specify any additional platforms to be used by the packager.
* For example, if you want to add a "custom" platform, and use modules
* ending in .custom.js, you would return ['custom'] here.
*/
getPlatforms() {
return [];
},

/**
* Returns a regular expression for modules that should be ignored by the
* packager on a given platform.
Expand Down
2 changes: 2 additions & 0 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const connect = require('connect');
const copyToClipBoardMiddleware = require('./middleware/copyToClipBoardMiddleware');
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
const defaultAssetExts = require('../../packager/defaults').assetExts;
const defaultPlatforms = require('../../packager/defaults').platforms;
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const heapCaptureMiddleware = require('./middleware/heapCaptureMiddleware.js');
const http = require('http');
Expand Down Expand Up @@ -91,6 +92,7 @@ function getPackagerServer(args, config) {
cacheVersion: '3',
extraNodeModules: config.extraNodeModules,
getTransformOptions: config.getTransformOptions,
platforms: defaultPlatforms.concat(args.platforms),
projectRoots: args.projectRoots,
resetCache: args.resetCache,
transformModulePath: transformModulePath,
Expand Down
5 changes: 5 additions & 0 deletions local-cli/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ module.exports = {
description: 'Specify any additional asset extentions to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getAssetExts(),
}, {
command: '--platforms [list]',
description: 'Specify any additional platforms to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getPlatforms(),
}, {
command: '--skipflow',
description: 'Disable flow checks'
Expand Down
10 changes: 10 additions & 0 deletions packager/react-packager/src/Bundler/__tests__/Bundler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ describe('Bundler', function() {
);
});

it('allows overriding the platforms array', () => {
expect(bundler._opts.platforms).toEqual(['ios', 'android', 'windows', 'web']);
const b = new Bundler({
projectRoots,
assetServer: assetServer,
platforms: ['android', 'vr'],
});
expect(b._opts.platforms).toEqual(['android', 'vr']);
});

describe('getOrderedDependencyPaths', () => {
beforeEach(() => {
assetServer.getAssetData.mockImpl(function(relPath) {
Expand Down
7 changes: 7 additions & 0 deletions packager/react-packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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,
Expand Down Expand Up @@ -91,6 +92,10 @@ const validateOpts = declareOpts({
type: 'array',
default: ['png'],
},
platforms: {
type: 'array',
default: defaults.platforms,
},
watch: {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -126,6 +131,7 @@ type Options = {
getTransformOptions?: GetTransformOptions<*>,
extraNodeModules: {},
assetExts: Array<string>,
platforms: Array<string>,
watch: boolean,
assetServer: AssetServer,
transformTimeoutInterval: ?number,
Expand Down Expand Up @@ -200,6 +206,7 @@ class Bundler {
watch: opts.watch,
minifyCode: this._transformer.minify,
moduleFormat: opts.moduleFormat,
platforms: opts.platforms,
polyfillModuleNames: opts.polyfillModuleNames,
projectRoots: opts.projectRoots,
resetCache: opts.resetCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ describe('Resolver', function() {
});
});

it('passes custom platforms to the dependency graph', function() {
new Resolver({ // eslint-disable-line no-new
projectRoot: '/root',
platforms: ['ios', 'windows', 'vr'],
});
const platforms = DependencyGraph.mock.calls[0][0].platforms;
expect(platforms).toEqual(['ios', 'windows', 'vr']);
});

pit('should get dependencies with polyfills', function() {
var module = createModule('index');
var deps = [module];
Expand Down
6 changes: 5 additions & 1 deletion packager/react-packager/src/Resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const validateOpts = declareOpts({
type: 'array',
required: true,
},
platforms: {
type: 'array',
required: true,
},
cache: {
type: 'object',
required: true,
Expand Down Expand Up @@ -94,7 +98,7 @@ class Resolver {
(opts.blacklistRE && opts.blacklistRE.test(filepath));
},
providesModuleNodeModules: defaults.providesModuleNodeModules,
platforms: defaults.platforms,
platforms: opts.platforms,
preferNativePlatform: true,
watch: opts.watch,
cache: opts.cache,
Expand Down
4 changes: 4 additions & 0 deletions packager/react-packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ const validateOpts = declareOpts({
type: 'array',
default: defaults.assetExts,
},
platforms: {
type: 'array',
default: defaults.platforms,
},
transformTimeoutInterval: {
type: 'number',
required: false,
Expand Down

0 comments on commit 5b37c3e

Please sign in to comment.