packager: remove getPlatformExtension default platforms

Summary: Default arguments are dangerous, and it shows here again. `Server` was calling that function without passing down the platforms, meaning custom platform would not be accounted for, possibly causing all kinds of bugs for OSS use cases. Additional, the typing of `platform` across the stack was wrong: it can be `null`, in which case we resolve the files without extension. The reason Flow didn't detect that issue before is because we use `Object.assign` to re-export the function from `DependencyGraph`, but Flow is not smart enough to propagate the types in that particular case. I'll remove all the other re-export, as it may uncover further type errors.

Reviewed By: davidaurelio

Differential Revision: D5052070

fbshipit-source-id: 7b427ec036ca74b5dcd7c88f7dfa0df541b8eb6b
This commit is contained in:
Jean Lauliac 2017-05-12 10:26:46 -07:00 committed by Facebook Github Bot
parent 73ce956838
commit f7bf4b841b
11 changed files with 48 additions and 54 deletions

View File

@ -62,7 +62,7 @@ export type ExtraTransformOptions = {
export type GetTransformOptionsOpts = {| export type GetTransformOptionsOpts = {|
dev: boolean, dev: boolean,
hot: boolean, hot: boolean,
platform: string, platform: ?string,
|}; |};
export type GetTransformOptions = ( export type GetTransformOptions = (
@ -521,7 +521,7 @@ class Bundler {
generateSourceMaps = false, generateSourceMaps = false,
}: { }: {
entryFile: string, entryFile: string,
platform: string, platform: ?string,
dev?: boolean, dev?: boolean,
minify?: boolean, minify?: boolean,
hot?: boolean, hot?: boolean,
@ -560,7 +560,7 @@ class Bundler {
onProgress, onProgress,
}: { }: {
entryFile: string, entryFile: string,
platform: string, platform: ?string,
dev?: boolean, dev?: boolean,
minify?: boolean, minify?: boolean,
hot?: boolean, hot?: boolean,
@ -784,7 +784,7 @@ class Bundler {
generateSourceMaps: boolean, generateSourceMaps: boolean,
hot: boolean, hot: boolean,
minify: boolean, minify: boolean,
platform: string, platform: ?string,
projectRoots: Array<string>, projectRoots: Array<string>,
|}, |},
): Promise<BundlingOptions> { ): Promise<BundlingOptions> {

View File

@ -170,7 +170,7 @@ type AstResult = {
function inline( function inline(
filename: string, filename: string,
transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap}, transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap},
options: {+dev: boolean, +platform: string}, options: {+dev: boolean, +platform: ?string},
): AstResult { ): AstResult {
const code = transformResult.code; const code = transformResult.code;
const babelOptions = { const babelOptions = {

View File

@ -47,7 +47,7 @@ export type TransformOptionsStrict = {|
+generateSourceMaps: boolean, +generateSourceMaps: boolean,
+hot: boolean, +hot: boolean,
+inlineRequires: {+blacklist: {[string]: true}} | boolean, +inlineRequires: {+blacklist: {[string]: true}} | boolean,
+platform: string, +platform: ?string,
+projectRoot: string, +projectRoot: string,
|}; |};
@ -56,14 +56,14 @@ export type TransformOptions = {
+generateSourceMaps?: boolean, +generateSourceMaps?: boolean,
+hot?: boolean, +hot?: boolean,
+inlineRequires?: {+blacklist: {[string]: true}} | boolean, +inlineRequires?: {+blacklist: {[string]: true}} | boolean,
+platform: string, +platform: ?string,
+projectRoot: string, +projectRoot: string,
}; };
export type Options = {| export type Options = {|
+dev: boolean, +dev: boolean,
+minify: boolean, +minify: boolean,
+platform: string, +platform: ?string,
+transform: TransformOptionsStrict, +transform: TransformOptionsStrict,
|}; |};

View File

@ -98,7 +98,7 @@ class Resolver {
getDependencies<T: ContainsTransformerOptions>( getDependencies<T: ContainsTransformerOptions>(
entryPath: string, entryPath: string,
options: {platform: string, recursive?: boolean}, options: {platform: ?string, recursive?: boolean},
bundlingOptions: T, bundlingOptions: T,
onProgress?: ?(finishedModules: number, totalModules: number) => mixed, onProgress?: ?(finishedModules: number, totalModules: number) => mixed,
getModuleId: mixed, getModuleId: mixed,

View File

@ -155,7 +155,7 @@ describe('processRequest', () => {
isolateModuleIDs: false, isolateModuleIDs: false,
minify: false, minify: false,
onProgress: jasmine.any(Function), onProgress: jasmine.any(Function),
platform: undefined, platform: null,
resolutionResponse: null, resolutionResponse: null,
runBeforeMainModule: ['InitializeCore'], runBeforeMainModule: ['InitializeCore'],
runModule: true, runModule: true,
@ -209,7 +209,7 @@ describe('processRequest', () => {
isolateModuleIDs: false, isolateModuleIDs: false,
minify: false, minify: false,
onProgress: jasmine.any(Function), onProgress: jasmine.any(Function),
platform: undefined, platform: null,
resolutionResponse: null, resolutionResponse: null,
runBeforeMainModule: ['InitializeCore'], runBeforeMainModule: ['InitializeCore'],
runModule: true, runModule: true,
@ -457,7 +457,7 @@ describe('processRequest', () => {
isolateModuleIDs: false, isolateModuleIDs: false,
minify: false, minify: false,
onProgress: null, onProgress: null,
platform: undefined, platform: null,
resolutionResponse: null, resolutionResponse: null,
runBeforeMainModule: ['InitializeCore'], runBeforeMainModule: ['InitializeCore'],
runModule: false, runModule: false,

View File

@ -12,11 +12,11 @@
'use strict'; 'use strict';
const AssetServer = require('../AssetServer'); const AssetServer = require('../AssetServer');
const getPlatformExtension = require('../node-haste/DependencyGraph').getPlatformExtension;
const Bundler = require('../Bundler'); const Bundler = require('../Bundler');
const MultipartResponse = require('./MultipartResponse'); const MultipartResponse = require('./MultipartResponse');
const defaults = require('../../defaults'); const defaults = require('../../defaults');
const getPlatformExtension = require('../node-haste/lib/getPlatformExtension');
const mime = require('mime-types'); const mime = require('mime-types');
const path = require('path'); const path = require('path');
const symbolicate = require('./symbolicate'); const symbolicate = require('./symbolicate');
@ -148,6 +148,7 @@ class Server {
_hmrFileChangeListener: ?(type: string, filePath: string) => mixed; _hmrFileChangeListener: ?(type: string, filePath: string) => mixed;
_reporter: Reporter; _reporter: Reporter;
_symbolicateInWorker: Symbolicate; _symbolicateInWorker: Symbolicate;
_platforms: Set<string>;
constructor(options: Options) { constructor(options: Options) {
this._opts = { this._opts = {
@ -181,6 +182,7 @@ class Server {
this._bundles = Object.create(null); this._bundles = Object.create(null);
this._changeWatchers = []; this._changeWatchers = [];
this._fileChangeListeners = []; this._fileChangeListeners = [];
this._platforms = new Set(this._opts.platforms);
this._assetServer = new AssetServer({ this._assetServer = new AssetServer({
assetExts: this._opts.assetExts, assetExts: this._opts.assetExts,
@ -281,7 +283,7 @@ class Server {
getShallowDependencies(options: DependencyOptions): Promise<Array<Module>> { getShallowDependencies(options: DependencyOptions): Promise<Array<Module>> {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
const platform = options.platform != null const platform = options.platform != null
? options.platform : getPlatformExtension(options.entryFile); ? options.platform : getPlatformExtension(options.entryFile, this._platforms);
const {entryFile, dev, minify, hot} = options; const {entryFile, dev, minify, hot} = options;
return this._bundler.getShallowDependencies( return this._bundler.getShallowDependencies(
{entryFile, platform, dev, minify, hot, generateSourceMaps: false}, {entryFile, platform, dev, minify, hot, generateSourceMaps: false},
@ -296,7 +298,7 @@ class Server {
getDependencies(options: DependencyOptions): Promise<ResolutionResponse<Module, *>> { getDependencies(options: DependencyOptions): Promise<ResolutionResponse<Module, *>> {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
const platform = options.platform != null const platform = options.platform != null
? options.platform : getPlatformExtension(options.entryFile); ? options.platform : getPlatformExtension(options.entryFile, this._platforms);
const {entryFile, dev, minify, hot} = options; const {entryFile, dev, minify, hot} = options;
return this._bundler.getDependencies( return this._bundler.getDependencies(
{entryFile, platform, dev, minify, hot, generateSourceMaps: false}, {entryFile, platform, dev, minify, hot, generateSourceMaps: false},
@ -832,7 +834,7 @@ class Server {
// try to get the platform from the url // try to get the platform from the url
/* $FlowFixMe: `query` could be empty for an invalid URL */ /* $FlowFixMe: `query` could be empty for an invalid URL */
const platform = urlObj.query.platform || const platform = urlObj.query.platform ||
getPlatformExtension(pathname); getPlatformExtension(pathname, this._platforms);
/* $FlowFixMe: `query` could be empty for an invalid URL */ /* $FlowFixMe: `query` could be empty for an invalid URL */
const assetPlugin = urlObj.query.assetPlugin; const assetPlugin = urlObj.query.assetPlugin;

View File

@ -18,7 +18,6 @@ const FetchError = require('node-fetch/lib/fetch-error');
const crypto = require('crypto'); const crypto = require('crypto');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const invariant = require('fbjs/lib/invariant');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const path = require('path'); const path = require('path');
const throat = require('throat'); const throat = require('throat');
@ -134,7 +133,7 @@ class KeyResultStore {
} }
export type TransformProfile = {+dev: boolean, +minify: boolean, +platform: string}; export type TransformProfile = {+dev: boolean, +minify: boolean, +platform: ?string};
function profileKey({dev, minify, platform}: TransformProfile): string { function profileKey({dev, minify, platform}: TransformProfile): string {
return jsonStableStringify({dev, minify, platform}); return jsonStableStringify({dev, minify, platform});

View File

@ -291,7 +291,6 @@ class DependencyGraph extends EventEmitter {
static Module; static Module;
static Polyfill; static Polyfill;
static getAssetDataFromName; static getAssetDataFromName;
static getPlatformExtension;
static replacePatterns; static replacePatterns;
static getInverseDependencies; static getInverseDependencies;
@ -301,7 +300,6 @@ Object.assign(DependencyGraph, {
Module, Module,
Polyfill, Polyfill,
getAssetDataFromName, getAssetDataFromName,
getPlatformExtension,
replacePatterns, replacePatterns,
getInverseDependencies, getInverseDependencies,
}); });

View File

@ -13,9 +13,11 @@ jest.dontMock('../getPlatformExtension')
var getAssetDataFromName = require('../getAssetDataFromName'); var getAssetDataFromName = require('../getAssetDataFromName');
const TEST_PLATFORMS = new Set(['ios', 'android']);
describe('getAssetDataFromName', () => { describe('getAssetDataFromName', () => {
it('should get data from name', () => { it('should get data from name', () => {
expect(getAssetDataFromName('a/b/c.png')).toEqual({ expect(getAssetDataFromName('a/b/c.png', TEST_PLATFORMS)).toEqual({
resolution: 1, resolution: 1,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -23,7 +25,7 @@ describe('getAssetDataFromName', () => {
platform: null, platform: null,
}); });
expect(getAssetDataFromName('a/b/c@1x.png')).toEqual({ expect(getAssetDataFromName('a/b/c@1x.png', TEST_PLATFORMS)).toEqual({
resolution: 1, resolution: 1,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -31,7 +33,7 @@ describe('getAssetDataFromName', () => {
platform: null, platform: null,
}); });
expect(getAssetDataFromName('a/b/c@2.5x.png')).toEqual({ expect(getAssetDataFromName('a/b/c@2.5x.png', TEST_PLATFORMS)).toEqual({
resolution: 2.5, resolution: 2.5,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -39,7 +41,7 @@ describe('getAssetDataFromName', () => {
platform: null, platform: null,
}); });
expect(getAssetDataFromName('a/b/c.ios.png')).toEqual({ expect(getAssetDataFromName('a/b/c.ios.png', TEST_PLATFORMS)).toEqual({
resolution: 1, resolution: 1,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -47,7 +49,7 @@ describe('getAssetDataFromName', () => {
platform: 'ios', platform: 'ios',
}); });
expect(getAssetDataFromName('a/b/c@1x.ios.png')).toEqual({ expect(getAssetDataFromName('a/b/c@1x.ios.png', TEST_PLATFORMS)).toEqual({
resolution: 1, resolution: 1,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -55,7 +57,7 @@ describe('getAssetDataFromName', () => {
platform: 'ios', platform: 'ios',
}); });
expect(getAssetDataFromName('a/b/c@2.5x.ios.png')).toEqual({ expect(getAssetDataFromName('a/b/c@2.5x.ios.png', TEST_PLATFORMS)).toEqual({
resolution: 2.5, resolution: 2.5,
assetName: 'a/b/c.png', assetName: 'a/b/c.png',
type: 'png', type: 'png',
@ -63,7 +65,7 @@ describe('getAssetDataFromName', () => {
platform: 'ios', platform: 'ios',
}); });
expect(getAssetDataFromName('a/b /c.png')).toEqual({ expect(getAssetDataFromName('a/b /c.png', TEST_PLATFORMS)).toEqual({
resolution: 1, resolution: 1,
assetName: 'a/b /c.png', assetName: 'a/b /c.png',
type: 'png', type: 'png',
@ -74,7 +76,7 @@ describe('getAssetDataFromName', () => {
describe('resolution extraction', () => { describe('resolution extraction', () => {
it('should extract resolution simple case', () => { it('should extract resolution simple case', () => {
var data = getAssetDataFromName('test@2x.png'); var data = getAssetDataFromName('test@2x.png', TEST_PLATFORMS);
expect(data).toEqual({ expect(data).toEqual({
assetName: 'test.png', assetName: 'test.png',
resolution: 2, resolution: 2,
@ -85,7 +87,7 @@ describe('getAssetDataFromName', () => {
}); });
it('should default resolution to 1', () => { it('should default resolution to 1', () => {
var data = getAssetDataFromName('test.png'); var data = getAssetDataFromName('test.png', TEST_PLATFORMS);
expect(data).toEqual({ expect(data).toEqual({
assetName: 'test.png', assetName: 'test.png',
resolution: 1, resolution: 1,
@ -96,7 +98,7 @@ describe('getAssetDataFromName', () => {
}); });
it('should support float', () => { it('should support float', () => {
var data = getAssetDataFromName('test@1.1x.png'); var data = getAssetDataFromName('test@1.1x.png', TEST_PLATFORMS);
expect(data).toEqual({ expect(data).toEqual({
assetName: 'test.png', assetName: 'test.png',
resolution: 1.1, resolution: 1.1,
@ -105,7 +107,7 @@ describe('getAssetDataFromName', () => {
platform: null, platform: null,
}); });
data = getAssetDataFromName('test@.1x.png'); data = getAssetDataFromName('test@.1x.png', TEST_PLATFORMS);
expect(data).toEqual({ expect(data).toEqual({
assetName: 'test.png', assetName: 'test.png',
resolution: 0.1, resolution: 0.1,
@ -114,7 +116,7 @@ describe('getAssetDataFromName', () => {
platform: null, platform: null,
}); });
data = getAssetDataFromName('test@0.2x.png'); data = getAssetDataFromName('test@0.2x.png', TEST_PLATFORMS);
expect(data).toEqual({ expect(data).toEqual({
assetName: 'test.png', assetName: 'test.png',
resolution: 0.2, resolution: 0.2,

View File

@ -12,21 +12,20 @@ jest.dontMock('../getPlatformExtension');
var getPlatformExtension = require('../getPlatformExtension'); var getPlatformExtension = require('../getPlatformExtension');
const TEST_PLATFORMS = new Set(['ios', 'android']);
describe('getPlatformExtension', function() { describe('getPlatformExtension', function() {
it('should get platform ext', function() { it('should get platform ext', function() {
expect(getPlatformExtension('a.ios.js')).toBe('ios'); const get = name => getPlatformExtension(name, TEST_PLATFORMS);
expect(getPlatformExtension('a.android.js')).toBe('android'); expect(get('a.ios.js')).toBe('ios');
expect(getPlatformExtension('/b/c/a.ios.js')).toBe('ios'); expect(get('a.android.js')).toBe('android');
expect(getPlatformExtension('/b/c.android/a.ios.js')).toBe('ios'); expect(get('/b/c/a.ios.js')).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.ios.png')).toBe('ios'); expect(get('/b/c.android/a.ios.js')).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.lol.png')).toBe(null); expect(get('/b/c/a@1.5x.ios.png')).toBe('ios');
expect(getPlatformExtension('/b/c/a.lol.png')).toBe(null); expect(get('/b/c/a@1.5x.lol.png')).toBe(null);
}); expect(get('/b/c/a.lol.png')).toBe(null);
it('should optionally accept supported platforms', function() {
expect(getPlatformExtension('a.ios.js', new Set(['ios']))).toBe('ios'); expect(getPlatformExtension('a.ios.js', new Set(['ios']))).toBe('ios');
expect(getPlatformExtension('a.android.js', new Set(['android']))).toBe('android'); expect(getPlatformExtension('a.android.js', new Set(['android']))).toBe('android');
expect(getPlatformExtension('/b/c/a.ios.js', new Set(['ios', 'android']))).toBe('ios');
expect(getPlatformExtension('a.ios.js', new Set(['ubuntu']))).toBe(null); expect(getPlatformExtension('a.ios.js', new Set(['ubuntu']))).toBe(null);
expect(getPlatformExtension('a.ubuntu.js', new Set(['ubuntu']))).toBe('ubuntu'); expect(getPlatformExtension('a.ubuntu.js', new Set(['ubuntu']))).toBe('ubuntu');
}); });

View File

@ -7,21 +7,15 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
const SUPPORTED_PLATFORM_EXTS = new Set([ /**
'android', * Extract platform extension: `index.ios.js` -> `ios`.
'ios', */
'web', function getPlatformExtension(file: string, platforms: Set<string>): ?string {
]);
// Extract platform extension: index.ios.js -> ios
function getPlatformExtension(
file: string,
platforms: Set<string> = SUPPORTED_PLATFORM_EXTS,
): ?string {
const last = file.lastIndexOf('.'); const last = file.lastIndexOf('.');
const secondToLast = file.lastIndexOf('.', last - 1); const secondToLast = file.lastIndexOf('.', last - 1);
if (secondToLast === -1) { if (secondToLast === -1) {