mirror of
https://github.com/status-im/metro.git
synced 2025-02-28 02:30:29 +00:00
Updates from Fri 24 Apr
This commit is contained in:
commit
02aba9adee
@ -41,13 +41,13 @@ var messageHandlers = {
|
|||||||
window.localStorage.setItem('sessionID', message.id);
|
window.localStorage.setItem('sessionID', message.id);
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
},
|
},
|
||||||
'executeApplicationScript:sourceURL:onComplete:': function(message, sendReply) {
|
'executeApplicationScript': function(message, sendReply) {
|
||||||
for (var key in message.inject) {
|
for (var key in message.inject) {
|
||||||
window[key] = JSON.parse(message.inject[key]);
|
window[key] = JSON.parse(message.inject[key]);
|
||||||
}
|
}
|
||||||
loadScript(message.url, sendReply.bind(null, null));
|
loadScript(message.url, sendReply.bind(null, null));
|
||||||
},
|
},
|
||||||
'executeJSCall:method:arguments:callback:': function(message, sendReply) {
|
'executeJSCall': function(message, sendReply) {
|
||||||
var returnValue = [[], [], [], [], []];
|
var returnValue = [[], [], [], [], []];
|
||||||
try {
|
try {
|
||||||
if (window && window.require) {
|
if (window && window.require) {
|
||||||
|
@ -10,12 +10,14 @@
|
|||||||
|
|
||||||
var chalk = require('chalk');
|
var chalk = require('chalk');
|
||||||
var exec = require('child_process').exec;
|
var exec = require('child_process').exec;
|
||||||
|
var Activity = require('./react-packager/src/Activity');
|
||||||
|
|
||||||
var hasWarned = {};
|
var hasWarned = {};
|
||||||
|
|
||||||
function getFlowTypeCheckMiddleware(options) {
|
function getFlowTypeCheckMiddleware(options) {
|
||||||
return function(req, res, next) {
|
return function(req, res, next) {
|
||||||
if (options.skipflow) {
|
var isBundle = req.url.indexOf('.bundle') !== -1;
|
||||||
|
if (options.skipflow || !isBundle) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
if (options.flowroot || options.projectRoots.length === 1) {
|
if (options.flowroot || options.projectRoots.length === 1) {
|
||||||
@ -44,20 +46,10 @@ function getFlowTypeCheckMiddleware(options) {
|
|||||||
|
|
||||||
function doFlowTypecheck(res, flowroot, next) {
|
function doFlowTypecheck(res, flowroot, next) {
|
||||||
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
||||||
var start = Date.now();
|
var eventId = Activity.startEvent('flow static typechecks');
|
||||||
// Log start message if flow is slow to let user know something is happening.
|
|
||||||
var flowSlow = setTimeout(
|
|
||||||
function() {
|
|
||||||
console.log(chalk.gray('flow: Running static typechecks.'));
|
|
||||||
},
|
|
||||||
500
|
|
||||||
);
|
|
||||||
exec(flowCmd, function(flowError, stdout, stderr) {
|
exec(flowCmd, function(flowError, stdout, stderr) {
|
||||||
clearTimeout(flowSlow);
|
Activity.endEvent(eventId);
|
||||||
if (!flowError) {
|
if (!flowError) {
|
||||||
console.log(chalk.gray(
|
|
||||||
'flow: Typechecks passed (' + (Date.now() - start) + 'ms).')
|
|
||||||
);
|
|
||||||
return next();
|
return next();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
@ -8,23 +8,21 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var chalk = require('chalk');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var spawn = require('child_process').spawn;
|
var exec = require('child_process').exec;
|
||||||
|
|
||||||
var firstLaunch = true;
|
function printInstructions(title) {
|
||||||
|
console.log([
|
||||||
function guessEditor() {
|
'',
|
||||||
if (firstLaunch) {
|
chalk.bgBlue.white.bold(' ' + title + ' '),
|
||||||
console.log('When you see Red Box with stack trace, you can click any ' +
|
' When you see Red Box with stack trace, you can click any ',
|
||||||
'stack frame to jump to the source file. The packager will launch your ' +
|
' stack frame to jump to the source file. The packager will launch your ',
|
||||||
'editor of choice. It will first look at REACT_EDITOR environment ' +
|
' editor of choice. It will first look at REACT_EDITOR environment ',
|
||||||
'variable, then at EDITOR. To set it up, you can add something like ' +
|
' variable, then at EDITOR. To set it up, you can add something like ',
|
||||||
'REACT_EDITOR=atom to your .bashrc.');
|
' REACT_EDITOR=atom to your .bashrc.',
|
||||||
firstLaunch = false;
|
''
|
||||||
}
|
].join('\n'));
|
||||||
|
|
||||||
var editor = process.env.REACT_EDITOR || process.env.EDITOR || 'subl';
|
|
||||||
return editor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function launchEditor(fileName, lineNumber) {
|
function launchEditor(fileName, lineNumber) {
|
||||||
@ -37,9 +35,18 @@ function launchEditor(fileName, lineNumber) {
|
|||||||
argument += ':' + lineNumber;
|
argument += ':' + lineNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
var editor = guessEditor();
|
var editor = process.env.REACT_EDITOR || process.env.EDITOR;
|
||||||
console.log('Opening ' + fileName + ' with ' + editor);
|
if (editor) {
|
||||||
spawn(editor, [argument], { stdio: ['pipe', 'pipe', process.stderr] });
|
console.log('Opening ' + chalk.underline(fileName) + ' with ' + chalk.bold(editor));
|
||||||
|
exec(editor + ' ' + argument, function(error) {
|
||||||
|
if (error) {
|
||||||
|
console.log(chalk.red(error.message));
|
||||||
|
printInstructions('How to fix');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
printInstructions('PRO TIP');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = launchEditor;
|
module.exports = launchEditor;
|
||||||
|
@ -200,6 +200,7 @@ function getAppMiddleware(options) {
|
|||||||
cacheVersion: '2',
|
cacheVersion: '2',
|
||||||
transformModulePath: require.resolve('./transformer.js'),
|
transformModulePath: require.resolve('./transformer.js'),
|
||||||
assetRoots: options.assetRoots,
|
assetRoots: options.assetRoots,
|
||||||
|
assetExts: ['png', 'jpeg', 'jpg']
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
react-packager/src/Activity/__mocks__/chalk.js
vendored
Normal file
13
react-packager/src/Activity/__mocks__/chalk.js
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
dim: function(s) { return s; },
|
||||||
|
};
|
12
react-packager/src/Activity/index.js
vendored
12
react-packager/src/Activity/index.js
vendored
@ -8,6 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var chalk = require('chalk');
|
||||||
|
|
||||||
var COLLECTION_PERIOD = 1000;
|
var COLLECTION_PERIOD = 1000;
|
||||||
|
|
||||||
var _endedEvents = Object.create(null);
|
var _endedEvents = Object.create(null);
|
||||||
@ -132,22 +134,22 @@ function _writeAction(action) {
|
|||||||
|
|
||||||
switch (action.action) {
|
switch (action.action) {
|
||||||
case 'startEvent':
|
case 'startEvent':
|
||||||
console.log(
|
console.log(chalk.dim(
|
||||||
'[' + fmtTime + '] ' +
|
'[' + fmtTime + '] ' +
|
||||||
'<START> ' + action.eventName +
|
'<START> ' + action.eventName +
|
||||||
data
|
data
|
||||||
);
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'endEvent':
|
case 'endEvent':
|
||||||
var startAction = _eventStarts[action.eventId];
|
var startAction = _eventStarts[action.eventId];
|
||||||
var startData = startAction.data ? ': ' + JSON.stringify(startAction.data) : '';
|
var startData = startAction.data ? ': ' + JSON.stringify(startAction.data) : '';
|
||||||
console.log(
|
console.log(chalk.dim(
|
||||||
'[' + fmtTime + '] ' +
|
'[' + fmtTime + '] ' +
|
||||||
'<END> ' + startAction.eventName +
|
'<END> ' + startAction.eventName +
|
||||||
'(' + (action.tstamp - startAction.tstamp) + 'ms)' +
|
' (' + (action.tstamp - startAction.tstamp) + 'ms)' +
|
||||||
startData
|
startData
|
||||||
);
|
));
|
||||||
delete _eventStarts[action.eventId];
|
delete _eventStarts[action.eventId];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1,72 +1,82 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.autoMockOff()
|
.dontMock('path')
|
||||||
.mock('../../lib/declareOpts')
|
.dontMock('../../lib/getAssetDataFromName')
|
||||||
.mock('fs');
|
.dontMock('../');
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
var AssetServer = require('../');
|
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
|
|
||||||
describe('AssetServer', function() {
|
describe('AssetServer', function() {
|
||||||
pit('should work for the simple case', function() {
|
var AssetServer;
|
||||||
var server = new AssetServer({
|
var crypto;
|
||||||
projectRoots: ['/root'],
|
var fs;
|
||||||
assetExts: ['png'],
|
|
||||||
});
|
|
||||||
|
|
||||||
fs.__setMockFilesystem({
|
beforeEach(function() {
|
||||||
'root': {
|
AssetServer = require('../');
|
||||||
imgs: {
|
crypto = require('crypto');
|
||||||
'b.png': 'b image',
|
fs = require('fs');
|
||||||
'b@2x.png': 'b2 image',
|
});
|
||||||
|
|
||||||
|
describe('assetServer.get', function() {
|
||||||
|
pit('should work for the simple case', function() {
|
||||||
|
var server = new AssetServer({
|
||||||
|
projectRoots: ['/root'],
|
||||||
|
assetExts: ['png'],
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.__setMockFilesystem({
|
||||||
|
'root': {
|
||||||
|
imgs: {
|
||||||
|
'b.png': 'b image',
|
||||||
|
'b@2x.png': 'b2 image',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
server.get('imgs/b.png'),
|
server.get('imgs/b.png'),
|
||||||
server.get('imgs/b@1x.png'),
|
server.get('imgs/b@1x.png'),
|
||||||
]).then(function(resp) {
|
]).then(function(resp) {
|
||||||
resp.forEach(function(data) {
|
resp.forEach(function(data) {
|
||||||
expect(data).toBe('b image');
|
expect(data).toBe('b image');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
pit.only('should pick the bigger one', function() {
|
pit('should pick the bigger one', function() {
|
||||||
var server = new AssetServer({
|
var server = new AssetServer({
|
||||||
projectRoots: ['/root'],
|
projectRoots: ['/root'],
|
||||||
assetExts: ['png'],
|
assetExts: ['png'],
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.__setMockFilesystem({
|
fs.__setMockFilesystem({
|
||||||
'root': {
|
'root': {
|
||||||
imgs: {
|
imgs: {
|
||||||
'b@1x.png': 'b1 image',
|
'b@1x.png': 'b1 image',
|
||||||
'b@2x.png': 'b2 image',
|
'b@2x.png': 'b2 image',
|
||||||
'b@4x.png': 'b4 image',
|
'b@4x.png': 'b4 image',
|
||||||
'b@4.5x.png': 'b4.5 image',
|
'b@4.5x.png': 'b4.5 image',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
return server.get('imgs/b@3x.png').then(function(data) {
|
||||||
|
expect(data).toBe('b4 image');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return server.get('imgs/b@3x.png').then(function(data) {
|
pit('should support multiple project roots', function() {
|
||||||
expect(data).toBe('b4 image');
|
var server = new AssetServer({
|
||||||
});
|
projectRoots: ['/root', '/root2'],
|
||||||
});
|
assetExts: ['png'],
|
||||||
|
});
|
||||||
|
|
||||||
pit('should support multiple project roots', function() {
|
fs.__setMockFilesystem({
|
||||||
var server = new AssetServer({
|
'root': {
|
||||||
projectRoots: ['/root'],
|
imgs: {
|
||||||
assetExts: ['png'],
|
'b.png': 'b image',
|
||||||
});
|
},
|
||||||
|
|
||||||
fs.__setMockFilesystem({
|
|
||||||
'root': {
|
|
||||||
imgs: {
|
|
||||||
'b.png': 'b image',
|
|
||||||
},
|
},
|
||||||
'root2': {
|
'root2': {
|
||||||
'newImages': {
|
'newImages': {
|
||||||
@ -75,11 +85,53 @@ describe('AssetServer', function() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return server.get('newImages/imgs/b.png').then(function(data) {
|
return server.get('newImages/imgs/b.png').then(function(data) {
|
||||||
expect(data).toBe('b1 image');
|
expect(data).toBe('b1 image');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('assetSerer.getAssetData', function() {
|
||||||
|
pit('should get assetData', function() {
|
||||||
|
var hash = {
|
||||||
|
update: jest.genMockFn(),
|
||||||
|
digest: jest.genMockFn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
hash.digest.mockImpl(function() {
|
||||||
|
return 'wow such hash';
|
||||||
|
});
|
||||||
|
crypto.createHash.mockImpl(function() {
|
||||||
|
return hash;
|
||||||
|
});
|
||||||
|
|
||||||
|
var server = new AssetServer({
|
||||||
|
projectRoots: ['/root'],
|
||||||
|
assetExts: ['png'],
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.__setMockFilesystem({
|
||||||
|
'root': {
|
||||||
|
imgs: {
|
||||||
|
'b@1x.png': 'b1 image',
|
||||||
|
'b@2x.png': 'b2 image',
|
||||||
|
'b@4x.png': 'b4 image',
|
||||||
|
'b@4.5x.png': 'b4.5 image',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return server.getAssetData('imgs/b.png').then(function(data) {
|
||||||
|
expect(hash.update.mock.calls.length).toBe(4);
|
||||||
|
expect(data).toEqual({
|
||||||
|
type: 'png',
|
||||||
|
name: 'b',
|
||||||
|
scales: [1, 2, 4, 4.5],
|
||||||
|
hash: 'wow such hash',
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
63
react-packager/src/AssetServer/index.js
vendored
63
react-packager/src/AssetServer/index.js
vendored
@ -9,10 +9,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var declareOpts = require('../lib/declareOpts');
|
var declareOpts = require('../lib/declareOpts');
|
||||||
var extractAssetResolution = require('../lib/extractAssetResolution');
|
var getAssetDataFromName = require('../lib/getAssetDataFromName');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var crypto = require('crypto');
|
||||||
|
|
||||||
var lstat = Promise.promisify(fs.lstat);
|
var lstat = Promise.promisify(fs.lstat);
|
||||||
var readDir = Promise.promisify(fs.readdir);
|
var readDir = Promise.promisify(fs.readdir);
|
||||||
@ -44,11 +45,11 @@ function AssetServer(options) {
|
|||||||
*
|
*
|
||||||
* 1. We first parse the directory of the asset
|
* 1. We first parse the directory of the asset
|
||||||
* 2. We check to find a matching directory in one of the project roots
|
* 2. We check to find a matching directory in one of the project roots
|
||||||
* 3. We then build a map of all assets and their resolutions in this directory
|
* 3. We then build a map of all assets and their scales in this directory
|
||||||
* 4. Then pick the closest resolution (rounding up) to the requested one
|
* 4. Then pick the closest resolution (rounding up) to the requested one
|
||||||
*/
|
*/
|
||||||
|
|
||||||
AssetServer.prototype.get = function(assetPath) {
|
AssetServer.prototype._getAssetRecord = function(assetPath) {
|
||||||
var filename = path.basename(assetPath);
|
var filename = path.basename(assetPath);
|
||||||
|
|
||||||
return findRoot(
|
return findRoot(
|
||||||
@ -60,13 +61,7 @@ AssetServer.prototype.get = function(assetPath) {
|
|||||||
readDir(dir),
|
readDir(dir),
|
||||||
];
|
];
|
||||||
}).spread(function(dir, files) {
|
}).spread(function(dir, files) {
|
||||||
// Easy case. File exactly what the client requested.
|
var assetData = getAssetDataFromName(filename);
|
||||||
var index = files.indexOf(filename);
|
|
||||||
if (index > -1) {
|
|
||||||
return readFile(path.join(dir, filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
var assetData = extractAssetResolution(filename);
|
|
||||||
var map = buildAssetMap(dir, files);
|
var map = buildAssetMap(dir, files);
|
||||||
var record = map[assetData.assetName];
|
var record = map[assetData.assetName];
|
||||||
|
|
||||||
@ -74,8 +69,15 @@ AssetServer.prototype.get = function(assetPath) {
|
|||||||
throw new Error('Asset not found');
|
throw new Error('Asset not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < record.resolutions.length; i++) {
|
return record;
|
||||||
if (record.resolutions[i] >= assetData.resolution) {
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
AssetServer.prototype.get = function(assetPath) {
|
||||||
|
var assetData = getAssetDataFromName(assetPath);
|
||||||
|
return this._getAssetRecord(assetPath).then(function(record) {
|
||||||
|
for (var i = 0; i < record.scales.length; i++) {
|
||||||
|
if (record.scales[i] >= assetData.resolution) {
|
||||||
return readFile(record.files[i]);
|
return readFile(record.files[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +86,33 @@ AssetServer.prototype.get = function(assetPath) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AssetServer.prototype.getAssetData = function(assetPath) {
|
||||||
|
var nameData = getAssetDataFromName(assetPath);
|
||||||
|
var data = {
|
||||||
|
name: nameData.name,
|
||||||
|
type: 'png',
|
||||||
|
};
|
||||||
|
|
||||||
|
return this._getAssetRecord(assetPath).then(function(record) {
|
||||||
|
data.scales = record.scales;
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
record.files.map(function(file) {
|
||||||
|
return lstat(file);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}).then(function(stats) {
|
||||||
|
var hash = crypto.createHash('md5');
|
||||||
|
|
||||||
|
stats.forEach(function(stat) {
|
||||||
|
hash.update(stat.mtime.getTime().toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
data.hash = hash.digest('hex');
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
function findRoot(roots, dir) {
|
function findRoot(roots, dir) {
|
||||||
return Promise.some(
|
return Promise.some(
|
||||||
roots.map(function(root) {
|
roots.map(function(root) {
|
||||||
@ -105,26 +134,26 @@ function findRoot(roots, dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildAssetMap(dir, files) {
|
function buildAssetMap(dir, files) {
|
||||||
var assets = files.map(extractAssetResolution);
|
var assets = files.map(getAssetDataFromName);
|
||||||
var map = Object.create(null);
|
var map = Object.create(null);
|
||||||
assets.forEach(function(asset, i) {
|
assets.forEach(function(asset, i) {
|
||||||
var file = files[i];
|
var file = files[i];
|
||||||
var record = map[asset.assetName];
|
var record = map[asset.assetName];
|
||||||
if (!record) {
|
if (!record) {
|
||||||
record = map[asset.assetName] = {
|
record = map[asset.assetName] = {
|
||||||
resolutions: [],
|
scales: [],
|
||||||
files: [],
|
files: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var insertIndex;
|
var insertIndex;
|
||||||
var length = record.resolutions.length;
|
var length = record.scales.length;
|
||||||
for (insertIndex = 0; insertIndex < length; insertIndex++) {
|
for (insertIndex = 0; insertIndex < length; insertIndex++) {
|
||||||
if (asset.resolution < record.resolutions[insertIndex]) {
|
if (asset.resolution < record.scales[insertIndex]) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
record.resolutions.splice(insertIndex, 0, asset.resolution);
|
record.scales.splice(insertIndex, 0, asset.resolution);
|
||||||
record.files.splice(insertIndex, 0, path.join(dir, file));
|
record.files.splice(insertIndex, 0, path.join(dir, file));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -45,6 +45,8 @@ function ModuleDescriptor(fields) {
|
|||||||
|
|
||||||
this.altId = fields.altId;
|
this.altId = fields.altId;
|
||||||
|
|
||||||
|
this.isJSON = fields.isJSON;
|
||||||
|
|
||||||
this._fields = fields;
|
this._fields = fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ jest
|
|||||||
.dontMock('absolute-path')
|
.dontMock('absolute-path')
|
||||||
.dontMock('../docblock')
|
.dontMock('../docblock')
|
||||||
.dontMock('../../replacePatterns')
|
.dontMock('../../replacePatterns')
|
||||||
.dontMock('../../../../lib/extractAssetResolution')
|
.dontMock('../../../../lib/getAssetDataFromName')
|
||||||
.setMock('../../../ModuleDescriptor', function(data) {return data;});
|
.setMock('../../../ModuleDescriptor', function(data) {return data;});
|
||||||
|
|
||||||
describe('DependencyGraph', function() {
|
describe('DependencyGraph', function() {
|
||||||
@ -101,6 +101,46 @@ describe('DependencyGraph', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pit('should get json dependencies', function() {
|
||||||
|
var root = '/root';
|
||||||
|
fs.__setMockFilesystem({
|
||||||
|
'root': {
|
||||||
|
'package.json': JSON.stringify({
|
||||||
|
name: 'package'
|
||||||
|
}),
|
||||||
|
'index.js': [
|
||||||
|
'/**',
|
||||||
|
' * @providesModule index',
|
||||||
|
' */',
|
||||||
|
'require("./a.json")'
|
||||||
|
].join('\n'),
|
||||||
|
'a.json': JSON.stringify({}),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var dgraph = new DependencyGraph({
|
||||||
|
roots: [root],
|
||||||
|
fileWatcher: fileWatcher
|
||||||
|
});
|
||||||
|
return dgraph.load().then(function() {
|
||||||
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
|
.toEqual([
|
||||||
|
{
|
||||||
|
id: 'index',
|
||||||
|
altId: 'package/index',
|
||||||
|
path: '/root/index.js',
|
||||||
|
dependencies: ['./a.json']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'package/a.json',
|
||||||
|
isJSON: true,
|
||||||
|
path: '/root/a.json',
|
||||||
|
dependencies: []
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
pit('should get dependencies with deprecated assets', function() {
|
pit('should get dependencies with deprecated assets', function() {
|
||||||
var root = '/root';
|
var root = '/root';
|
||||||
fs.__setMockFilesystem({
|
fs.__setMockFilesystem({
|
||||||
@ -129,7 +169,8 @@ describe('DependencyGraph', function() {
|
|||||||
{ id: 'image!a',
|
{ id: 'image!a',
|
||||||
path: '/root/imgs/a.png',
|
path: '/root/imgs/a.png',
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
isAsset_DEPRECATED: true
|
isAsset_DEPRECATED: true,
|
||||||
|
resolution: 1,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -288,7 +329,8 @@ describe('DependencyGraph', function() {
|
|||||||
id: 'image!a',
|
id: 'image!a',
|
||||||
path: '/root/imgs/a.png',
|
path: '/root/imgs/a.png',
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
isAsset_DEPRECATED: true
|
isAsset_DEPRECATED: true,
|
||||||
|
resolution: 1,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -1350,6 +1392,7 @@ describe('DependencyGraph', function() {
|
|||||||
path: '/root/foo.png',
|
path: '/root/foo.png',
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
isAsset_DEPRECATED: true,
|
isAsset_DEPRECATED: true,
|
||||||
|
resolution: 1,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
@ -18,7 +18,7 @@ var isAbsolutePath = require('absolute-path');
|
|||||||
var debug = require('debug')('DependecyGraph');
|
var debug = require('debug')('DependecyGraph');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var declareOpts = require('../../../lib/declareOpts');
|
var declareOpts = require('../../../lib/declareOpts');
|
||||||
var extractAssetResolution = require('../../../lib/extractAssetResolution');
|
var getAssetDataFromName = require('../../../lib/getAssetDataFromName');
|
||||||
|
|
||||||
var readFile = Promise.promisify(fs.readFile);
|
var readFile = Promise.promisify(fs.readFile);
|
||||||
var readDir = Promise.promisify(fs.readdir);
|
var readDir = Promise.promisify(fs.readdir);
|
||||||
@ -66,7 +66,7 @@ function DependecyGraph(options) {
|
|||||||
this._debugUpdateEvents = [];
|
this._debugUpdateEvents = [];
|
||||||
|
|
||||||
this._moduleExtPattern = new RegExp(
|
this._moduleExtPattern = new RegExp(
|
||||||
'\.(' + ['js'].concat(this._assetExts).join('|') + ')$'
|
'\.(' + ['js', 'json'].concat(this._assetExts).join('|') + ')$'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Kick off the search process to precompute the dependency graph.
|
// Kick off the search process to precompute the dependency graph.
|
||||||
@ -259,7 +259,7 @@ DependecyGraph.prototype.resolveDependency = function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JS modules can be required without extensios.
|
// JS modules can be required without extensios.
|
||||||
if (!this._isFileAsset(modulePath)) {
|
if (!this._isFileAsset(modulePath) && !modulePath.match(/\.json$/)) {
|
||||||
modulePath = withExtJs(modulePath);
|
modulePath = withExtJs(modulePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -422,7 +422,7 @@ DependecyGraph.prototype._processModule = function(modulePath) {
|
|||||||
var module;
|
var module;
|
||||||
|
|
||||||
if (this._assetExts.indexOf(extname(modulePath)) > -1) {
|
if (this._assetExts.indexOf(extname(modulePath)) > -1) {
|
||||||
var assetData = extractAssetResolution(this._lookupName(modulePath));
|
var assetData = getAssetDataFromName(this._lookupName(modulePath));
|
||||||
moduleData.id = assetData.assetName;
|
moduleData.id = assetData.assetName;
|
||||||
moduleData.resolution = assetData.resolution;
|
moduleData.resolution = assetData.resolution;
|
||||||
moduleData.isAsset = true;
|
moduleData.isAsset = true;
|
||||||
@ -432,13 +432,23 @@ DependecyGraph.prototype._processModule = function(modulePath) {
|
|||||||
return Promise.resolve(module);
|
return Promise.resolve(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (extname(modulePath) === 'json') {
|
||||||
|
moduleData.id = this._lookupName(modulePath);
|
||||||
|
moduleData.isJSON = true;
|
||||||
|
moduleData.dependencies = [];
|
||||||
|
module = new ModuleDescriptor(moduleData);
|
||||||
|
this._updateGraphWithModule(module);
|
||||||
|
return Promise.resolve(module);
|
||||||
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
return readFile(modulePath, 'utf8')
|
return readFile(modulePath, 'utf8')
|
||||||
.then(function(content) {
|
.then(function(content) {
|
||||||
var moduleDocBlock = docblock.parseAsObject(content);
|
var moduleDocBlock = docblock.parseAsObject(content);
|
||||||
if (moduleDocBlock.providesModule || moduleDocBlock.provides) {
|
if (moduleDocBlock.providesModule || moduleDocBlock.provides) {
|
||||||
moduleData.id =
|
moduleData.id = /^(\S*)/.exec(
|
||||||
moduleDocBlock.providesModule || moduleDocBlock.provides;
|
moduleDocBlock.providesModule || moduleDocBlock.provides
|
||||||
|
)[1];
|
||||||
|
|
||||||
// Incase someone wants to require this module via
|
// Incase someone wants to require this module via
|
||||||
// packageName/path/to/module
|
// packageName/path/to/module
|
||||||
@ -641,6 +651,7 @@ DependecyGraph.prototype._processAsset_DEPRECATED = function(file) {
|
|||||||
path: path.resolve(file),
|
path: path.resolve(file),
|
||||||
isAsset_DEPRECATED: true,
|
isAsset_DEPRECATED: true,
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
|
resolution: getAssetDataFromName(file).resolution,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
4
react-packager/src/FileWatcher/index.js
vendored
4
react-packager/src/FileWatcher/index.js
vendored
@ -26,7 +26,7 @@ var detectingWatcherClass = new Promise(function(resolve) {
|
|||||||
|
|
||||||
module.exports = FileWatcher;
|
module.exports = FileWatcher;
|
||||||
|
|
||||||
var MAX_WAIT_TIME = 3000;
|
var MAX_WAIT_TIME = 10000;
|
||||||
|
|
||||||
// Singleton
|
// Singleton
|
||||||
var fileWatcher = null;
|
var fileWatcher = null;
|
||||||
@ -73,7 +73,7 @@ function createWatcher(rootConfig) {
|
|||||||
var rejectTimeout = setTimeout(function() {
|
var rejectTimeout = setTimeout(function() {
|
||||||
reject(new Error([
|
reject(new Error([
|
||||||
'Watcher took too long to load',
|
'Watcher took too long to load',
|
||||||
'Try running `watchman` from your terminal',
|
'Try running `watchman version` from your terminal',
|
||||||
'https://facebook.github.io/watchman/docs/troubleshooting.html',
|
'https://facebook.github.io/watchman/docs/troubleshooting.html',
|
||||||
].join('\n')));
|
].join('\n')));
|
||||||
}, MAX_WAIT_TIME);
|
}, MAX_WAIT_TIME);
|
||||||
|
11
react-packager/src/Packager/Package.js
vendored
11
react-packager/src/Packager/Package.js
vendored
@ -17,6 +17,7 @@ module.exports = Package;
|
|||||||
function Package(sourceMapUrl) {
|
function Package(sourceMapUrl) {
|
||||||
this._finalized = false;
|
this._finalized = false;
|
||||||
this._modules = [];
|
this._modules = [];
|
||||||
|
this._assets = [];
|
||||||
this._sourceMapUrl = sourceMapUrl;
|
this._sourceMapUrl = sourceMapUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +37,10 @@ Package.prototype.addModule = function(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Package.prototype.addAsset = function(asset) {
|
||||||
|
this._assets.push(asset);
|
||||||
|
};
|
||||||
|
|
||||||
Package.prototype.finalize = function(options) {
|
Package.prototype.finalize = function(options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
if (options.runMainModule) {
|
if (options.runMainModule) {
|
||||||
@ -49,6 +54,8 @@ Package.prototype.finalize = function(options) {
|
|||||||
|
|
||||||
Object.freeze(this._modules);
|
Object.freeze(this._modules);
|
||||||
Object.seal(this._modules);
|
Object.seal(this._modules);
|
||||||
|
Object.freeze(this._assets);
|
||||||
|
Object.seal(this._assets);
|
||||||
this._finalized = true;
|
this._finalized = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -146,6 +153,10 @@ Package.prototype.getSourceMap = function(options) {
|
|||||||
return map;
|
return map;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Package.prototype.getAssets = function() {
|
||||||
|
return this._assets;
|
||||||
|
};
|
||||||
|
|
||||||
Package.prototype._getMappings = function() {
|
Package.prototype._getMappings = function() {
|
||||||
var modules = this._modules;
|
var modules = this._modules;
|
||||||
|
|
||||||
|
@ -76,6 +76,18 @@ describe('Package', function() {
|
|||||||
expect(s).toEqual(genSourceMap(p._modules));
|
expect(s).toEqual(genSourceMap(p._modules));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getAssets()', function() {
|
||||||
|
it('should save and return asset objects', function() {
|
||||||
|
var p = new Package('test_url');
|
||||||
|
var asset1 = {};
|
||||||
|
var asset2 = {};
|
||||||
|
p.addAsset(asset1);
|
||||||
|
p.addAsset(asset2);
|
||||||
|
p.finalize();
|
||||||
|
expect(p.getAssets()).toEqual([asset1, asset2]);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function genSourceMap(modules) {
|
function genSourceMap(modules) {
|
||||||
|
@ -43,7 +43,20 @@ describe('Packager', function() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
var packager = new Packager({projectRoots: ['/root']});
|
|
||||||
|
require('fs').readFile.mockImpl(function(file, callback) {
|
||||||
|
callback(null, '{"json":true}');
|
||||||
|
});
|
||||||
|
|
||||||
|
var assetServer = {
|
||||||
|
getAssetData: jest.genMockFn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
var packager = new Packager({
|
||||||
|
projectRoots: ['/root'],
|
||||||
|
assetServer: assetServer,
|
||||||
|
});
|
||||||
|
|
||||||
var modules = [
|
var modules = [
|
||||||
{id: 'foo', path: '/root/foo.js', dependencies: []},
|
{id: 'foo', path: '/root/foo.js', dependencies: []},
|
||||||
{id: 'bar', path: '/root/bar.js', dependencies: []},
|
{id: 'bar', path: '/root/bar.js', dependencies: []},
|
||||||
@ -52,6 +65,7 @@ describe('Packager', function() {
|
|||||||
path: '/root/img/img.png',
|
path: '/root/img/img.png',
|
||||||
isAsset_DEPRECATED: true,
|
isAsset_DEPRECATED: true,
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
|
resolution: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'new_image.png',
|
id: 'new_image.png',
|
||||||
@ -59,7 +73,13 @@ describe('Packager', function() {
|
|||||||
isAsset: true,
|
isAsset: true,
|
||||||
resolution: 2,
|
resolution: 2,
|
||||||
dependencies: []
|
dependencies: []
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
id: 'package/file.json',
|
||||||
|
path: '/root/file.json',
|
||||||
|
isJSON: true,
|
||||||
|
dependencies: [],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
getDependencies.mockImpl(function() {
|
getDependencies.mockImpl(function() {
|
||||||
@ -86,6 +106,15 @@ describe('Packager', function() {
|
|||||||
cb(null, { width: 50, height: 100 });
|
cb(null, { width: 50, height: 100 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assetServer.getAssetData.mockImpl(function() {
|
||||||
|
return {
|
||||||
|
scales: [1,2,3],
|
||||||
|
hash: 'i am a hash',
|
||||||
|
name: 'img',
|
||||||
|
type: 'png',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
return packager.package('/root/foo.js', true, 'source_map_url')
|
return packager.package('/root/foo.js', true, 'source_map_url')
|
||||||
.then(function(p) {
|
.then(function(p) {
|
||||||
expect(p.addModule.mock.calls[0]).toEqual([
|
expect(p.addModule.mock.calls[0]).toEqual([
|
||||||
@ -98,22 +127,37 @@ describe('Packager', function() {
|
|||||||
'source /root/bar.js',
|
'source /root/bar.js',
|
||||||
'/root/bar.js'
|
'/root/bar.js'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
var imgModule_DEPRECATED = {
|
||||||
|
__packager_asset: true,
|
||||||
|
isStatic: true,
|
||||||
|
path: '/root/img/img.png',
|
||||||
|
uri: 'img',
|
||||||
|
width: 25,
|
||||||
|
height: 50,
|
||||||
|
deprecated: true,
|
||||||
|
};
|
||||||
|
|
||||||
expect(p.addModule.mock.calls[2]).toEqual([
|
expect(p.addModule.mock.calls[2]).toEqual([
|
||||||
'lol module.exports = ' +
|
'lol module.exports = ' +
|
||||||
JSON.stringify({ uri: 'img', isStatic: true}) +
|
JSON.stringify(imgModule_DEPRECATED) +
|
||||||
'; lol',
|
'; lol',
|
||||||
'module.exports = ' +
|
'module.exports = ' +
|
||||||
JSON.stringify({ uri: 'img', isStatic: true}) +
|
JSON.stringify(imgModule_DEPRECATED) +
|
||||||
';',
|
';',
|
||||||
'/root/img/img.png'
|
'/root/img/img.png'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var imgModule = {
|
var imgModule = {
|
||||||
isStatic: true,
|
__packager_asset: true,
|
||||||
path: '/root/img/new_image.png',
|
fileSystemLocation: '/root/img',
|
||||||
uri: 'assets/img/new_image.png',
|
httpServerLocation: '/assets/img',
|
||||||
width: 25,
|
width: 25,
|
||||||
height: 50,
|
height: 50,
|
||||||
|
scales: [1, 2, 3],
|
||||||
|
hash: 'i am a hash',
|
||||||
|
name: 'img',
|
||||||
|
type: 'png',
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(p.addModule.mock.calls[3]).toEqual([
|
expect(p.addModule.mock.calls[3]).toEqual([
|
||||||
@ -126,9 +170,23 @@ describe('Packager', function() {
|
|||||||
'/root/img/new_image.png'
|
'/root/img/new_image.png'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
expect(p.addModule.mock.calls[4]).toEqual([
|
||||||
|
'lol module.exports = {"json":true}; lol',
|
||||||
|
'module.exports = {"json":true};',
|
||||||
|
'/root/file.json'
|
||||||
|
]);
|
||||||
|
|
||||||
expect(p.finalize.mock.calls[0]).toEqual([
|
expect(p.finalize.mock.calls[0]).toEqual([
|
||||||
{runMainModule: true}
|
{runMainModule: true}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
expect(p.addAsset.mock.calls[0]).toEqual([
|
||||||
|
imgModule_DEPRECATED
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(p.addAsset.mock.calls[1]).toEqual([
|
||||||
|
imgModule
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
100
react-packager/src/Packager/index.js
vendored
100
react-packager/src/Packager/index.js
vendored
@ -20,6 +20,9 @@ var Activity = require('../Activity');
|
|||||||
var declareOpts = require('../lib/declareOpts');
|
var declareOpts = require('../lib/declareOpts');
|
||||||
var imageSize = require('image-size');
|
var imageSize = require('image-size');
|
||||||
|
|
||||||
|
var sizeOf = Promise.promisify(imageSize);
|
||||||
|
var readFile = Promise.promisify(fs.readFile);
|
||||||
|
|
||||||
var validateOpts = declareOpts({
|
var validateOpts = declareOpts({
|
||||||
projectRoots: {
|
projectRoots: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
@ -64,6 +67,10 @@ var validateOpts = declareOpts({
|
|||||||
type: 'object',
|
type: 'object',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
assetServer: {
|
||||||
|
type: 'object',
|
||||||
|
required: true,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function Packager(options) {
|
function Packager(options) {
|
||||||
@ -91,6 +98,7 @@ function Packager(options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._projectRoots = opts.projectRoots;
|
this._projectRoots = opts.projectRoots;
|
||||||
|
this._assetServer = opts.assetServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
Packager.prototype.kill = function() {
|
Packager.prototype.kill = function() {
|
||||||
@ -98,9 +106,9 @@ Packager.prototype.kill = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Packager.prototype.package = function(main, runModule, sourceMapUrl, isDev) {
|
Packager.prototype.package = function(main, runModule, sourceMapUrl, isDev) {
|
||||||
var transformModule = this._transformModule.bind(this);
|
|
||||||
var ppackage = new Package(sourceMapUrl);
|
var ppackage = new Package(sourceMapUrl);
|
||||||
|
|
||||||
|
var transformModule = this._transformModule.bind(this, ppackage);
|
||||||
var findEventId = Activity.startEvent('find dependencies');
|
var findEventId = Activity.startEvent('find dependencies');
|
||||||
var transformEventId;
|
var transformEventId;
|
||||||
|
|
||||||
@ -138,16 +146,15 @@ Packager.prototype.getDependencies = function(main, isDev) {
|
|||||||
return this._resolver.getDependencies(main, { dev: isDev });
|
return this._resolver.getDependencies(main, { dev: isDev });
|
||||||
};
|
};
|
||||||
|
|
||||||
Packager.prototype._transformModule = function(module) {
|
Packager.prototype._transformModule = function(ppackage, module) {
|
||||||
var transform;
|
var transform;
|
||||||
|
|
||||||
if (module.isAsset_DEPRECATED) {
|
if (module.isAsset_DEPRECATED) {
|
||||||
transform = Promise.resolve(generateAssetModule_DEPRECATED(module));
|
transform = this.generateAssetModule_DEPRECATED(ppackage, module);
|
||||||
} else if (module.isAsset) {
|
} else if (module.isAsset) {
|
||||||
transform = generateAssetModule(
|
transform = this.generateAssetModule(ppackage, module);
|
||||||
module,
|
} else if (module.isJSON) {
|
||||||
getPathRelativeToRoot(this._projectRoots, module.path)
|
transform = generateJSONModule(module);
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
transform = this._transformer.loadFileAndTransform(
|
transform = this._transformer.loadFileAndTransform(
|
||||||
path.resolve(module.path)
|
path.resolve(module.path)
|
||||||
@ -164,43 +171,69 @@ Packager.prototype._transformModule = function(module) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function verifyRootExists(root) {
|
|
||||||
// Verify that the root exists.
|
|
||||||
assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory');
|
|
||||||
}
|
|
||||||
|
|
||||||
Packager.prototype.getGraphDebugInfo = function() {
|
Packager.prototype.getGraphDebugInfo = function() {
|
||||||
return this._resolver.getDebugInfo();
|
return this._resolver.getDebugInfo();
|
||||||
};
|
};
|
||||||
|
|
||||||
function generateAssetModule_DEPRECATED(module) {
|
Packager.prototype.generateAssetModule_DEPRECATED = function(ppackage, module) {
|
||||||
var code = 'module.exports = ' + JSON.stringify({
|
|
||||||
uri: module.id.replace(/^[^!]+!/, ''),
|
|
||||||
isStatic: true,
|
|
||||||
}) + ';';
|
|
||||||
|
|
||||||
return {
|
|
||||||
code: code,
|
|
||||||
sourceCode: code,
|
|
||||||
sourcePath: module.path,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
var sizeOf = Promise.promisify(imageSize);
|
|
||||||
|
|
||||||
function generateAssetModule(module, relPath) {
|
|
||||||
return sizeOf(module.path).then(function(dimensions) {
|
return sizeOf(module.path).then(function(dimensions) {
|
||||||
var img = {
|
var img = {
|
||||||
|
__packager_asset: true,
|
||||||
isStatic: true,
|
isStatic: true,
|
||||||
path: module.path, //TODO(amasad): this should be path inside tar file.
|
path: module.path,
|
||||||
uri: path.join('assets', relPath),
|
uri: module.id.replace(/^[^!]+!/, ''),
|
||||||
width: dimensions.width / module.resolution,
|
width: dimensions.width / module.resolution,
|
||||||
height: dimensions.height / module.resolution,
|
height: dimensions.height / module.resolution,
|
||||||
|
deprecated: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ppackage.addAsset(img);
|
||||||
|
|
||||||
var code = 'module.exports = ' + JSON.stringify(img) + ';';
|
var code = 'module.exports = ' + JSON.stringify(img) + ';';
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: code,
|
||||||
|
sourceCode: code,
|
||||||
|
sourcePath: module.path,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Packager.prototype.generateAssetModule = function(ppackage, module) {
|
||||||
|
var relPath = getPathRelativeToRoot(this._projectRoots, module.path);
|
||||||
|
|
||||||
|
return Promise.all([
|
||||||
|
sizeOf(module.path),
|
||||||
|
this._assetServer.getAssetData(relPath),
|
||||||
|
]).spread(function(dimensions, assetData) {
|
||||||
|
var img = {
|
||||||
|
__packager_asset: true,
|
||||||
|
fileSystemLocation: path.dirname(module.path),
|
||||||
|
httpServerLocation: path.join('/assets', path.dirname(relPath)),
|
||||||
|
width: dimensions.width / module.resolution,
|
||||||
|
height: dimensions.height / module.resolution,
|
||||||
|
scales: assetData.scales,
|
||||||
|
hash: assetData.hash,
|
||||||
|
name: assetData.name,
|
||||||
|
type: assetData.type,
|
||||||
|
};
|
||||||
|
|
||||||
|
ppackage.addAsset(img);
|
||||||
|
|
||||||
|
var code = 'module.exports = ' + JSON.stringify(img) + ';';
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: code,
|
||||||
|
sourceCode: code,
|
||||||
|
sourcePath: module.path,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function generateJSONModule(module) {
|
||||||
|
return readFile(module.path).then(function(data) {
|
||||||
|
var code = 'module.exports = ' + data.toString('utf8') + ';';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: code,
|
code: code,
|
||||||
sourceCode: code,
|
sourceCode: code,
|
||||||
@ -222,4 +255,9 @@ function getPathRelativeToRoot(roots, absPath) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function verifyRootExists(root) {
|
||||||
|
// Verify that the root exists.
|
||||||
|
assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory');
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = Packager;
|
module.exports = Packager;
|
||||||
|
21
react-packager/src/Server/index.js
vendored
21
react-packager/src/Server/index.js
vendored
@ -71,13 +71,17 @@ function Server(options) {
|
|||||||
this._packages = Object.create(null);
|
this._packages = Object.create(null);
|
||||||
this._changeWatchers = [];
|
this._changeWatchers = [];
|
||||||
|
|
||||||
|
var assetGlobs = opts.assetExts.map(function(ext) {
|
||||||
|
return '**/*.' + ext;
|
||||||
|
});
|
||||||
|
|
||||||
var watchRootConfigs = opts.projectRoots.map(function(dir) {
|
var watchRootConfigs = opts.projectRoots.map(function(dir) {
|
||||||
return {
|
return {
|
||||||
dir: dir,
|
dir: dir,
|
||||||
globs: [
|
globs: [
|
||||||
'**/*.js',
|
'**/*.js',
|
||||||
'**/package.json',
|
'**/*.json',
|
||||||
]
|
].concat(assetGlobs),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -86,9 +90,7 @@ function Server(options) {
|
|||||||
opts.assetRoots.map(function(dir) {
|
opts.assetRoots.map(function(dir) {
|
||||||
return {
|
return {
|
||||||
dir: dir,
|
dir: dir,
|
||||||
globs: opts.assetExts.map(function(ext) {
|
globs: assetGlobs,
|
||||||
return '**/*.' + ext;
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -98,15 +100,16 @@ function Server(options) {
|
|||||||
? FileWatcher.createDummyWatcher()
|
? FileWatcher.createDummyWatcher()
|
||||||
: new FileWatcher(watchRootConfigs);
|
: new FileWatcher(watchRootConfigs);
|
||||||
|
|
||||||
var packagerOpts = Object.create(opts);
|
|
||||||
packagerOpts.fileWatcher = this._fileWatcher;
|
|
||||||
this._packager = new Packager(packagerOpts);
|
|
||||||
|
|
||||||
this._assetServer = new AssetServer({
|
this._assetServer = new AssetServer({
|
||||||
projectRoots: opts.projectRoots,
|
projectRoots: opts.projectRoots,
|
||||||
assetExts: opts.assetExts,
|
assetExts: opts.assetExts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var packagerOpts = Object.create(opts);
|
||||||
|
packagerOpts.fileWatcher = this._fileWatcher;
|
||||||
|
packagerOpts.assetServer = this._assetServer;
|
||||||
|
this._packager = new Packager(packagerOpts);
|
||||||
|
|
||||||
var onFileChange = this._onFileChange.bind(this);
|
var onFileChange = this._onFileChange.bind(this);
|
||||||
this._fileWatcher.on('all', onFileChange);
|
this._fileWatcher.on('all', onFileChange);
|
||||||
|
|
||||||
|
12
react-packager/src/__mocks__/fs.js
vendored
12
react-packager/src/__mocks__/fs.js
vendored
@ -67,6 +67,12 @@ fs.lstat.mockImpl(function(filepath, callback) {
|
|||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mtime = {
|
||||||
|
getTime: function() {
|
||||||
|
return Math.ceil(Math.random() * 10000000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (node && typeof node === 'object' && node.SYMLINK == null) {
|
if (node && typeof node === 'object' && node.SYMLINK == null) {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
isDirectory: function() {
|
isDirectory: function() {
|
||||||
@ -74,7 +80,8 @@ fs.lstat.mockImpl(function(filepath, callback) {
|
|||||||
},
|
},
|
||||||
isSymbolicLink: function() {
|
isSymbolicLink: function() {
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
|
mtime: mtime,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
@ -86,7 +93,8 @@ fs.lstat.mockImpl(function(filepath, callback) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
|
mtime: mtime,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,42 +1,52 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
jest.autoMockOff();
|
jest.autoMockOff();
|
||||||
var extractAssetResolution = require('../extractAssetResolution');
|
var getAssetDataFromName = require('../getAssetDataFromName');
|
||||||
|
|
||||||
describe('extractAssetResolution', function() {
|
describe('getAssetDataFromName', function() {
|
||||||
it('should extract resolution simple case', function() {
|
it('should extract resolution simple case', function() {
|
||||||
var data = extractAssetResolution('test@2x.png');
|
var data = getAssetDataFromName('test@2x.png');
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
assetName: 'test.png',
|
assetName: 'test.png',
|
||||||
resolution: 2,
|
resolution: 2,
|
||||||
|
type: 'png',
|
||||||
|
name: 'test',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should default resolution to 1', function() {
|
it('should default resolution to 1', function() {
|
||||||
var data = extractAssetResolution('test.png');
|
var data = getAssetDataFromName('test.png');
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
assetName: 'test.png',
|
assetName: 'test.png',
|
||||||
resolution: 1,
|
resolution: 1,
|
||||||
|
type: 'png',
|
||||||
|
name: 'test',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support float', function() {
|
it('should support float', function() {
|
||||||
var data = extractAssetResolution('test@1.1x.png');
|
var data = getAssetDataFromName('test@1.1x.png');
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
assetName: 'test.png',
|
assetName: 'test.png',
|
||||||
resolution: 1.1,
|
resolution: 1.1,
|
||||||
|
type: 'png',
|
||||||
|
name: 'test',
|
||||||
});
|
});
|
||||||
|
|
||||||
data = extractAssetResolution('test@.1x.png');
|
data = getAssetDataFromName('test@.1x.png');
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
assetName: 'test.png',
|
assetName: 'test.png',
|
||||||
resolution: 0.1,
|
resolution: 0.1,
|
||||||
|
type: 'png',
|
||||||
|
name: 'test',
|
||||||
});
|
});
|
||||||
|
|
||||||
data = extractAssetResolution('test@0.2x.png');
|
data = getAssetDataFromName('test@0.2x.png');
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
assetName: 'test.png',
|
assetName: 'test.png',
|
||||||
resolution: 0.2,
|
resolution: 0.2,
|
||||||
|
type: 'png',
|
||||||
|
name: 'test',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
function extractAssetResolution(filename) {
|
function getAssetDataFromName(filename) {
|
||||||
var ext = path.extname(filename);
|
var ext = path.extname(filename);
|
||||||
|
|
||||||
var re = new RegExp('@([\\d\\.]+)x\\' + ext + '$');
|
var re = new RegExp('@([\\d\\.]+)x\\' + ext + '$');
|
||||||
@ -19,10 +19,13 @@ function extractAssetResolution(filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var assetName = match ? filename.replace(re, ext) : filename;
|
||||||
return {
|
return {
|
||||||
resolution: resolution,
|
resolution: resolution,
|
||||||
assetName: match ? filename.replace(re, ext) : filename,
|
assetName: assetName,
|
||||||
|
type: ext.slice(1),
|
||||||
|
name: path.basename(assetName, ext)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = extractAssetResolution;
|
module.exports = getAssetDataFromName;
|
Loading…
x
Reference in New Issue
Block a user