Merge branch 'oss-sync/master' into HEAD

This commit is contained in:
Christopher Chedeau 2015-04-14 18:16:23 -07:00
commit cfb38b1ece
11 changed files with 411 additions and 26 deletions

View File

@ -18,26 +18,35 @@ var sharedBlacklist = [
'node_modules/react-tools/src/event/EventPropagators.js'
];
var webBlacklist = [
'.ios.js'
];
var iosBlacklist = [
'node_modules/react-tools/src/browser/ui/React.js',
'node_modules/react-tools/src/browser/eventPlugins/ResponderEventPlugin.js',
// 'node_modules/react-tools/src/vendor/core/ExecutionEnvironment.js',
'.web.js',
'.android.js',
];
var platformBlacklists = {
web: [
'.ios.js'
],
ios: [
'node_modules/react-tools/src/browser/ui/React.js',
'node_modules/react-tools/src/browser/eventPlugins/ResponderEventPlugin.js',
// 'node_modules/react-tools/src/vendor/core/ExecutionEnvironment.js',
'.web.js',
'.android.js',
],
android: [
'node_modules/react-tools/src/browser/ui/React.js',
'node_modules/react-tools/src/browser/eventPlugins/ResponderEventPlugin.js',
'node_modules/react-tools/src/browser/ReactTextComponent.js',
// 'node_modules/react-tools/src/vendor/core/ExecutionEnvironment.js',
'.web.js',
'.ios.js',
],
};
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
function blacklist(isWeb, additionalBlacklist) {
function blacklist(platform, additionalBlacklist) {
return new RegExp('(' +
(additionalBlacklist || []).concat(sharedBlacklist)
.concat(isWeb ? webBlacklist : iosBlacklist)
.concat(platformBlacklists[platform] || [])
.map(escapeRegExp)
.join('|') +
')$'

View File

@ -42,6 +42,10 @@ var options = parseCommandLine([{
}, {
command: 'assetRoots',
description: 'specify the root directories of app assets'
}, {
command: 'platform',
default: 'ios',
description: 'Specify the platform-specific blacklist (ios, android, web).'
}, {
command: 'skipflow',
description: 'Disable flow checks'
@ -192,7 +196,7 @@ function statusPageMiddleware(req, res, next) {
function getAppMiddleware(options) {
return ReactPackager.middleware({
projectRoots: options.projectRoots,
blacklistRE: blacklist(false),
blacklistRE: blacklist(options.platform),
cacheVersion: '2',
transformModulePath: require.resolve('./transformer.js'),
assetRoots: options.assetRoots,
@ -200,7 +204,7 @@ function getAppMiddleware(options) {
}
function runServer(
options, /* {[]string projectRoot, bool web} */
options,
readyCallback
) {
var app = connect()

View File

@ -37,6 +37,12 @@ function ModuleDescriptor(fields) {
throw new Error('Cannot be an asset and a deprecated asset');
}
this.resolution = fields.resolution;
if (this.isAsset && isNaN(this.resolution)) {
throw new Error('Expected resolution to be a number for asset modules');
}
this.altId = fields.altId;
this._fields = fields;

View File

@ -169,7 +169,74 @@ describe('DependencyGraph', function() {
{ id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a.png',
dependencies: [],
isAsset: true
isAsset: true,
resolution: 1,
},
]);
});
});
pit('should get dependencies with assets and resolution', function() {
var root = '/root';
fs.__setMockFilesystem({
'root': {
'index.js': [
'/**',
' * @providesModule index',
' */',
'require("./imgs/a.png");',
'require("./imgs/b.png");',
'require("./imgs/c.png");',
].join('\n'),
'imgs': {
'a@1.5x.png': '',
'b@.7x.png': '',
'c.png': '',
'c@2x.png': '',
},
'package.json': JSON.stringify({
name: 'rootPackage'
}),
}
});
var dgraph = new DependencyGraph({
roots: [root],
fileWatcher: fileWatcher,
});
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{
id: 'index',
altId: 'rootPackage/index',
path: '/root/index.js',
dependencies: [
'./imgs/a.png',
'./imgs/b.png',
'./imgs/c.png',
]
},
{
id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a@1.5x.png',
resolution: 1.5,
dependencies: [],
isAsset: true,
},
{
id: 'rootPackage/imgs/b.png',
path: '/root/imgs/b@.7x.png',
resolution: 0.7,
dependencies: [],
isAsset: true
},
{
id: 'rootPackage/imgs/c.png',
path: '/root/imgs/c.png',
resolution: 1,
dependencies: [],
isAsset: true
},
]);
});
@ -213,7 +280,8 @@ describe('DependencyGraph', function() {
id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a.png',
dependencies: [],
isAsset: true
isAsset: true,
resolution: 1,
},
{
id: 'image!a',
@ -1332,6 +1400,7 @@ describe('DependencyGraph', function() {
path: '/root/foo.png',
dependencies: [],
isAsset: true,
resolution: 1,
},
]);
});

View File

@ -258,7 +258,7 @@ DependecyGraph.prototype.resolveDependency = function(
}
// JS modules can be required without extensios.
if (this._assetExts.indexOf(extname(modulePath)) === -1) {
if (!this._isFileAsset(modulePath)) {
modulePath = withExtJs(modulePath);
}
@ -266,10 +266,14 @@ DependecyGraph.prototype.resolveDependency = function(
// Maybe the dependency is a directory and there is an index.js inside it.
if (dep == null) {
modulePath = path.join(dir, depModuleId, 'index.js');
dep = this._graph[path.join(dir, depModuleId, 'index.js')];
}
dep = this._graph[modulePath];
// Maybe it's an asset with @n.nx resolution and the path doesn't map
// to the id
if (dep == null && this._isFileAsset(modulePath)) {
dep = this._moduleById[this._lookupName(modulePath)];
}
if (dep == null) {
debug(
@ -417,11 +421,14 @@ DependecyGraph.prototype._processModule = function(modulePath) {
var module;
if (this._assetExts.indexOf(extname(modulePath)) > -1) {
moduleData.id = this._lookupName(modulePath);
var assetData = extractResolutionPostfix(this._lookupName(modulePath));
moduleData.id = assetData.assetName;
moduleData.resolution = assetData.resolution;
moduleData.isAsset = true;
moduleData.dependencies = [];
module = Promise.resolve(new ModuleDescriptor(moduleData));
module = new ModuleDescriptor(moduleData);
this._updateGraphWithModule(module);
return Promise.resolve(module);
}
var self = this;
@ -652,6 +659,10 @@ DependecyGraph.prototype._processAssetChange_DEPRECATED = function(eventType, fi
}
};
DependecyGraph.prototype._isFileAsset = function(file) {
return this._assetExts.indexOf(extname(file)) !== -1;
};
/**
* Extract all required modules from a `code` string.
*/
@ -761,6 +772,27 @@ function extname(name) {
return path.extname(name).replace(/^\./, '');
}
function extractResolutionPostfix(filename) {
var ext = extname(filename);
var re = new RegExp('@([\\d\\.]+)x\\.' + ext + '$');
var match = filename.match(re);
var resolution;
if (!(match && match[1])) {
resolution = 1;
} else {
resolution = parseFloat(match[1], 10);
if (isNaN(resolution)) {
resolution = 1;
}
}
return {
resolution: resolution,
assetName: match ? filename.replace(re, '.' + ext) : filename,
};
}
function NotFoundError() {
Error.call(this);

View File

@ -82,6 +82,29 @@ describe('HasteDependencyResolver', function() {
'polyfills/console.js'
],
},
{ id: 'polyfills/String.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/String.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
],
},
module
]);
});
@ -142,6 +165,29 @@ describe('HasteDependencyResolver', function() {
'polyfills/console.js'
],
},
{ id: 'polyfills/String.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/String.prototype.es6.js',
dependencies: [
'polyfills/prelude_dev.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude_dev.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js'
],
},
module
]);
});
@ -203,6 +249,29 @@ describe('HasteDependencyResolver', function() {
'polyfills/console.js'
],
},
{ id: 'polyfills/String.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/String.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
],
},
{ path: 'some module',
id: 'some module',
isPolyfill: true,
@ -212,6 +281,8 @@ describe('HasteDependencyResolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js'
]
},
module

View File

@ -112,6 +112,8 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
path.join(__dirname, 'polyfills/polyfills.js'),
path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'),
path.join(__dirname, 'polyfills/String.prototype.es6.js'),
path.join(__dirname, 'polyfills/Array.prototype.es6.js'),
].concat(this._polyfillModuleNames);
var polyfillModules = polyfillModuleNames.map(

View File

@ -0,0 +1,106 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @provides Array.prototype.es6
* @polyfill
* @requires __DEV__
*/
/*eslint-disable */
/*jslint bitwise: true */
(function (undefined) {
if (__DEV__) {
// Define DEV-only setter that blows up when someone incorrectly
// iterates over arrays.
try {
Object.defineProperty && Object.defineProperty(
Array.prototype,
'__ARRAY_ENUMERATION_GUARD__',
{
configurable: true,
enumerable: true,
get: function() {
console.error(
'Your code is broken! Do not iterate over arrays with ' +
'for...in. See https://fburl.com/31944000 for more information.'
);
}
}
);
} catch (e) {
// Nothing
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
function findIndex(predicate, context) {
/**
* Why am I seeing this "findIndex" method as a value in my array!?
*
* We polyfill the "findIndex" method -- called like
* `[1, 2, 3].findIndex(1)` -- for older browsers. A side effect of the way
* we do that is that the method is enumerable. If you were incorrectly
* iterating over your array using the object property iterator syntax
* `for (key in obj)` you will see the method name "findIndex" as a key.
*
* To fix your code please do one of the following:
*
* - Use a regular for loop with index.
* - Use one of the array methods: a.forEach, a.map, etc.
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
*
* More info:
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
*/
if (this == null) {
throw new TypeError(
'Array.prototype.findIndex called on null or undefined'
);
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
for (var i = 0; i < length; i++) {
if (predicate.call(context, list[i], i, list)) {
return i;
}
}
return -1;
}
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = findIndex;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
if (!Array.prototype.find) {
Array.prototype.find = function(predicate, context) {
/**
* Why am I seeing this "find" method as a value in my array!?
*
* We polyfill the "find" method -- called like
* `[1, 2, 3].find(1)` -- for older browsers. A side effect of the way
* we do that is that the method is enumerable. If you were incorrectly
* iterating over your array using the object property iterator syntax
* `for (key in obj)` you will see the method name "find" as a key.
*
* To fix your code please do one of the following:
*
* - Use a regular for loop with index.
* - Use one of the array methods: a.forEach, a.map, etc.
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
*
* More info:
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
*/
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
var index = findIndex.call(this, predicate, context);
return index === -1 ? undefined : this[index];
};
}
})();

View File

@ -0,0 +1,85 @@
/**
* @provides String.prototype.es6
* @polyfill
*/
/*eslint global-strict:0, no-extend-native:0, no-bitwise:0 */
/*jshint bitwise:false*/
/*
* NOTE: We use (Number(x) || 0) to replace NaN values with zero.
*/
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(search) {
'use strict';
if (this == null) {
throw TypeError();
}
var string = String(this);
var pos = arguments.length > 1 ?
(Number(arguments[1]) || 0) : 0;
var start = Math.min(Math.max(pos, 0), string.length);
return string.indexOf(String(search), pos) === start;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search) {
'use strict';
if (this == null) {
throw TypeError();
}
var string = String(this);
var stringLength = string.length;
var searchString = String(search);
var pos = arguments.length > 1 ?
(Number(arguments[1]) || 0) : stringLength;
var end = Math.min(Math.max(pos, 0), stringLength);
var start = end - searchString.length;
if (start < 0) {
return false;
}
return string.lastIndexOf(searchString, start) === start;
};
}
if (!String.prototype.contains) {
String.prototype.contains = function(search) {
'use strict';
if (this == null) {
throw TypeError();
}
var string = String(this);
var pos = arguments.length > 1 ?
(Number(arguments[1]) || 0) : 0;
return string.indexOf(String(search), pos) !== -1;
};
}
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw TypeError();
}
var string = String(this);
count = Number(count) || 0;
if (count < 0 || count === Infinity) {
throw RangeError();
}
if (count === 1) {
return string;
}
var result = '';
while (count) {
if (count & 1) {
result += string;
}
if ((count >>= 1)) {
string += string;
}
}
return result;
};
}

View File

@ -57,6 +57,7 @@ describe('Packager', function() {
id: 'new_image.png',
path: '/root/img/new_image.png',
isAsset: true,
resolution: 2,
dependencies: []
}
];
@ -111,8 +112,8 @@ describe('Packager', function() {
isStatic: true,
path: '/root/img/new_image.png',
uri: 'img/new_image.png',
width: 50,
height: 100,
width: 25,
height: 50,
};
expect(p.addModule.mock.calls[3]).toEqual([

View File

@ -195,8 +195,8 @@ function generateAssetModule(module, relPath) {
isStatic: true,
path: module.path, //TODO(amasad): this should be path inside tar file.
uri: relPath,
width: dimensions.width,
height: dimensions.height,
width: dimensions.width / module.resolution,
height: dimensions.height / module.resolution,
};
var code = 'module.exports = ' + JSON.stringify(img) + ';';