mirror of https://github.com/status-im/metro.git
Remove getAssetData() method from AssetServer
Reviewed By: mjesun Differential Revision: D6488537 fbshipit-source-id: 48c27e9cf5802c62fb25aac592ac1c5bcd6cc87f
This commit is contained in:
parent
3721464107
commit
5bff35f09e
|
@ -16,7 +16,6 @@ jest.mock('fs');
|
|||
jest.mock('image-size');
|
||||
|
||||
const AssetServer = require('../');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
|
||||
require('image-size').mockReturnValue({
|
||||
|
@ -24,8 +23,6 @@ require('image-size').mockReturnValue({
|
|||
height: 200,
|
||||
});
|
||||
|
||||
const {objectContaining} = jasmine;
|
||||
|
||||
describe('AssetServer', () => {
|
||||
describe('assetServer.get', () => {
|
||||
it('should work for the simple case', () => {
|
||||
|
@ -177,123 +174,4 @@ describe('AssetServer', () => {
|
|||
.then(data => expect(data).toBe('b1 image'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('assetServer.getAssetData', () => {
|
||||
it('should get assetData', () => {
|
||||
const server = new AssetServer({
|
||||
projectRoots: ['/root'],
|
||||
});
|
||||
|
||||
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('/root/imgs/b.png').then(data => {
|
||||
expect(data).toEqual(
|
||||
objectContaining({
|
||||
__packager_asset: true,
|
||||
type: 'png',
|
||||
name: 'b',
|
||||
scales: [1, 2, 4, 4.5],
|
||||
fileSystemLocation: '/root/imgs',
|
||||
httpServerLocation: '/assets/imgs',
|
||||
files: [
|
||||
'/root/imgs/b@1x.png',
|
||||
'/root/imgs/b@2x.png',
|
||||
'/root/imgs/b@4x.png',
|
||||
'/root/imgs/b@4.5x.png',
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get assetData for non-png images', () => {
|
||||
const server = new AssetServer({
|
||||
projectRoots: ['/root'],
|
||||
});
|
||||
|
||||
fs.__setMockFilesystem({
|
||||
root: {
|
||||
imgs: {
|
||||
'b@1x.jpg': 'b1 image',
|
||||
'b@2x.jpg': 'b2 image',
|
||||
'b@4x.jpg': 'b4 image',
|
||||
'b@4.5x.jpg': 'b4.5 image',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return server.getAssetData('/root/imgs/b.jpg').then(data => {
|
||||
expect(data).toEqual(
|
||||
objectContaining({
|
||||
__packager_asset: true,
|
||||
type: 'jpg',
|
||||
name: 'b',
|
||||
scales: [1, 2, 4, 4.5],
|
||||
fileSystemLocation: '/root/imgs',
|
||||
httpServerLocation: '/assets/imgs',
|
||||
files: [
|
||||
'/root/imgs/b@1x.jpg',
|
||||
'/root/imgs/b@2x.jpg',
|
||||
'/root/imgs/b@4x.jpg',
|
||||
'/root/imgs/b@4.5x.jpg',
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hash:', () => {
|
||||
let server, mockFS;
|
||||
beforeEach(() => {
|
||||
server = new AssetServer({
|
||||
projectRoots: ['/root'],
|
||||
});
|
||||
|
||||
mockFS = {
|
||||
root: {
|
||||
imgs: {
|
||||
'b@1x.jpg': 'b1 image',
|
||||
'b@2x.jpg': 'b2 image',
|
||||
'b@4x.jpg': 'b4 image',
|
||||
'b@4.5x.jpg': 'b4.5 image',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
fs.__setMockFilesystem(mockFS);
|
||||
});
|
||||
|
||||
it('uses the file contents to build the hash', () => {
|
||||
const hash = crypto.createHash('md5');
|
||||
for (const name in mockFS.root.imgs) {
|
||||
hash.update(mockFS.root.imgs[name]);
|
||||
}
|
||||
|
||||
return server
|
||||
.getAssetData('/root/imgs/b.jpg')
|
||||
.then(data =>
|
||||
expect(data).toEqual(objectContaining({hash: hash.digest('hex')})),
|
||||
);
|
||||
});
|
||||
|
||||
it('changes the hash when the passed-in file watcher emits an `all` event', () => {
|
||||
return server.getAssetData('/root/imgs/b.jpg').then(initialData => {
|
||||
mockFS.root.imgs['b@4x.jpg'] = 'updated data';
|
||||
|
||||
return server
|
||||
.getAssetData('/root/imgs/b.jpg')
|
||||
.then(data => expect(data.hash).not.toEqual(initialData.hash));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
/**
|
||||
* Copyright (c) 2013-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.
|
||||
*
|
||||
* @emails oncall+javascript_foundation
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
jest.mock('fs');
|
||||
jest.mock('image-size');
|
||||
|
||||
const {getAssetData} = require('../util');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
|
||||
require('image-size').mockReturnValue({
|
||||
width: 300,
|
||||
height: 200,
|
||||
});
|
||||
|
||||
describe('getAssetData', () => {
|
||||
it('should get assetData', () => {
|
||||
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 getAssetData('/root/imgs/b.png', 'imgs/b.png').then(data => {
|
||||
expect(data).toEqual(
|
||||
expect.objectContaining({
|
||||
__packager_asset: true,
|
||||
type: 'png',
|
||||
name: 'b',
|
||||
scales: [1, 2, 4, 4.5],
|
||||
fileSystemLocation: '/root/imgs',
|
||||
httpServerLocation: '/assets/imgs',
|
||||
files: [
|
||||
'/root/imgs/b@1x.png',
|
||||
'/root/imgs/b@2x.png',
|
||||
'/root/imgs/b@4x.png',
|
||||
'/root/imgs/b@4.5x.png',
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get assetData for non-png images', async () => {
|
||||
fs.__setMockFilesystem({
|
||||
root: {
|
||||
imgs: {
|
||||
'b@1x.jpg': 'b1 image',
|
||||
'b@2x.jpg': 'b2 image',
|
||||
'b@4x.jpg': 'b4 image',
|
||||
'b@4.5x.jpg': 'b4.5 image',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
|
||||
|
||||
expect(data).toEqual(
|
||||
expect.objectContaining({
|
||||
__packager_asset: true,
|
||||
type: 'jpg',
|
||||
name: 'b',
|
||||
scales: [1, 2, 4, 4.5],
|
||||
fileSystemLocation: '/root/imgs',
|
||||
httpServerLocation: '/assets/imgs',
|
||||
files: [
|
||||
'/root/imgs/b@1x.jpg',
|
||||
'/root/imgs/b@2x.jpg',
|
||||
'/root/imgs/b@4x.jpg',
|
||||
'/root/imgs/b@4.5x.jpg',
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe('hash:', () => {
|
||||
let mockFS;
|
||||
|
||||
beforeEach(() => {
|
||||
mockFS = {
|
||||
root: {
|
||||
imgs: {
|
||||
'b@1x.jpg': 'b1 image',
|
||||
'b@2x.jpg': 'b2 image',
|
||||
'b@4x.jpg': 'b4 image',
|
||||
'b@4.5x.jpg': 'b4.5 image',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
fs.__setMockFilesystem(mockFS);
|
||||
});
|
||||
|
||||
it('uses the file contents to build the hash', async () => {
|
||||
const hash = crypto.createHash('md5');
|
||||
|
||||
for (const name in mockFS.root.imgs) {
|
||||
hash.update(mockFS.root.imgs[name]);
|
||||
}
|
||||
|
||||
expect(await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg')).toEqual(
|
||||
expect.objectContaining({hash: hash.digest('hex')}),
|
||||
);
|
||||
});
|
||||
|
||||
it('changes the hash when the passed-in file watcher emits an `all` event', async () => {
|
||||
const initialData = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
|
||||
|
||||
mockFS.root.imgs['b@4x.jpg'] = 'updated data';
|
||||
|
||||
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
|
||||
expect(data.hash).not.toEqual(initialData.hash);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -17,9 +17,8 @@ const AssetPaths = require('../node-haste/lib/AssetPaths');
|
|||
const denodeify = require('denodeify');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const toLocalPath = require('../node-haste/lib/toLocalPath');
|
||||
|
||||
const {findRoot, getAbsoluteAssetRecord, getAssetData} = require('./util');
|
||||
const {findRoot, getAbsoluteAssetRecord} = require('./util');
|
||||
|
||||
export type AssetData = {|
|
||||
__packager_asset: boolean,
|
||||
|
@ -59,15 +58,6 @@ class AssetServer {
|
|||
});
|
||||
}
|
||||
|
||||
async getAssetData(
|
||||
assetPath: string,
|
||||
platform: ?string = null,
|
||||
): Promise<AssetData> {
|
||||
const localPath = toLocalPath(this._roots, assetPath);
|
||||
|
||||
return getAssetData(assetPath, localPath, platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a request for an image by path. That could contain a resolution
|
||||
* postfix, we need to find that image (or the closest one to it's resolution)
|
||||
|
|
|
@ -235,9 +235,18 @@ async function getAssetData(
|
|||
};
|
||||
}
|
||||
|
||||
async function getAssetFiles(
|
||||
assetPath: string,
|
||||
platform: ?string = null,
|
||||
): Promise<Array<string>> {
|
||||
const assetData = await getAbsoluteAssetInfo(assetPath, platform);
|
||||
|
||||
return assetData.files;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findRoot,
|
||||
getAbsoluteAssetInfo,
|
||||
getAbsoluteAssetRecord,
|
||||
getAssetData,
|
||||
getAssetFiles,
|
||||
};
|
||||
|
|
|
@ -89,7 +89,7 @@ export type PostProcessBundleSourcemap = ({
|
|||
outFileName: string,
|
||||
}) => {code: Buffer | string, map: SourceMap | string};
|
||||
|
||||
type Options = {|
|
||||
export type Options = {|
|
||||
+assetExts: Array<string>,
|
||||
+assetRegistryPath: string,
|
||||
+assetServer: AssetServer,
|
||||
|
@ -226,8 +226,8 @@ class Bundler {
|
|||
this._getTransformOptions = opts.getTransformOptions;
|
||||
}
|
||||
|
||||
getAssetServer(): AssetServer {
|
||||
return this._assetServer;
|
||||
getOptions(): Options {
|
||||
return this._opts;
|
||||
}
|
||||
|
||||
async end() {
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
|
||||
const DeltaPatcher = require('./DeltaPatcher');
|
||||
|
||||
const toLocalPath = require('../node-haste/lib/toLocalPath');
|
||||
|
||||
const {getAssetData} = require('../AssetServer/util');
|
||||
const {fromRawMappings} = require('../Bundler/source-map');
|
||||
const {createRamBundleGroups} = require('../Bundler/util');
|
||||
|
||||
|
@ -230,9 +233,12 @@ async function getAssets(
|
|||
const assets = await Promise.all(
|
||||
modules.map(async module => {
|
||||
if (module.type === 'asset') {
|
||||
return await deltaBundler
|
||||
.getAssetServer()
|
||||
.getAssetData(module.path, options.platform);
|
||||
const localPath = toLocalPath(
|
||||
deltaBundler.getOptions().projectRoots,
|
||||
module.path,
|
||||
);
|
||||
|
||||
return getAssetData(module.path, localPath, options.platform);
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
|
|
|
@ -12,6 +12,12 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
jest.mock('../../node-haste/lib/toLocalPath');
|
||||
jest.mock('../../AssetServer/util');
|
||||
|
||||
const {getAssetData} = require('../../AssetServer/util');
|
||||
const toLocalPath = require('../../node-haste/lib/toLocalPath');
|
||||
|
||||
const CURRENT_TIME = 1482363367000;
|
||||
|
||||
describe('Serializers', () => {
|
||||
|
@ -62,18 +68,24 @@ describe('Serializers', () => {
|
|||
},
|
||||
};
|
||||
},
|
||||
getAssetServer() {
|
||||
return {
|
||||
async getAssetData(path, platform) {
|
||||
return {path, platform, assetData: true};
|
||||
},
|
||||
};
|
||||
},
|
||||
getPostProcessModulesFn() {
|
||||
return postProcessModules;
|
||||
},
|
||||
getOptions() {
|
||||
return {
|
||||
projectRoots: ['/foo'],
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
getAssetData.mockImplementation((path, localPath, platform) => ({
|
||||
path,
|
||||
platform,
|
||||
assetData: true,
|
||||
}));
|
||||
|
||||
toLocalPath.mockImplementation((roots, path) => path.replace(roots[0], ''));
|
||||
|
||||
setCurrentTime(CURRENT_TIME);
|
||||
});
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
const DeltaTransformer = require('./DeltaTransformer');
|
||||
|
||||
import type Bundler from '../Bundler';
|
||||
import type Bundler, {Options as ServerOptions} from '../Bundler';
|
||||
import type {BundleOptions} from '../shared/types.flow';
|
||||
import type {DeltaEntry} from './DeltaTransformer';
|
||||
|
||||
|
@ -56,8 +56,8 @@ class DeltaBundler {
|
|||
this._deltaTransformers = new Map();
|
||||
}
|
||||
|
||||
getAssetServer() {
|
||||
return this._bundler.getAssetServer();
|
||||
getOptions(): ServerOptions {
|
||||
return this._bundler.getOptions();
|
||||
}
|
||||
|
||||
async getDeltaTransformer(
|
||||
|
|
|
@ -268,11 +268,7 @@ class Server {
|
|||
).platform;
|
||||
}
|
||||
|
||||
return await getOrderedDependencyPaths(
|
||||
this._deltaBundler,
|
||||
this._assetServer,
|
||||
bundleOptions,
|
||||
);
|
||||
return await getOrderedDependencyPaths(this._deltaBundler, bundleOptions);
|
||||
}
|
||||
|
||||
onFileChange(type: string, filePath: string) {
|
||||
|
|
|
@ -13,20 +13,21 @@
|
|||
'use strict';
|
||||
|
||||
jest.mock('../../DeltaBundler/Serializers');
|
||||
jest.mock('../../AssetServer/util');
|
||||
|
||||
const getOrderedDependencyPaths = require('../getOrderedDependencyPaths');
|
||||
const Serializers = require('../../DeltaBundler/Serializers');
|
||||
|
||||
const {getAssetFiles} = require('../../AssetServer/util');
|
||||
|
||||
describe('getOrderedDependencyPaths', () => {
|
||||
const assetsServer = {
|
||||
getAssetData: jest.fn(),
|
||||
};
|
||||
const deltaBundler = {};
|
||||
|
||||
beforeEach(() => {
|
||||
assetsServer.getAssetData.mockImplementation(async path => ({
|
||||
files: [`${path}@2x`, `${path}@3x`],
|
||||
}));
|
||||
getAssetFiles.mockImplementation(async path => [
|
||||
`${path}@2x`,
|
||||
`${path}@3x`,
|
||||
]);
|
||||
});
|
||||
|
||||
it('Should return all module dependencies correctly', async () => {
|
||||
|
@ -42,11 +43,13 @@ describe('getOrderedDependencyPaths', () => {
|
|||
);
|
||||
|
||||
expect(
|
||||
await getOrderedDependencyPaths(deltaBundler, assetsServer, ['/tmp'], {}),
|
||||
await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {}),
|
||||
).toEqual(['/tmp/1.js', '/tmp/2.js', '/tmp/3.js', '/tmp/4.js']);
|
||||
});
|
||||
|
||||
it('Should add assets data dependencies correctly', async () => {
|
||||
deltaBundler.getOptions = () => ({projectRoots: ['/root']});
|
||||
|
||||
Serializers.getAllModules.mockReturnValue(
|
||||
Promise.resolve(
|
||||
new Map([
|
||||
|
@ -60,7 +63,7 @@ describe('getOrderedDependencyPaths', () => {
|
|||
);
|
||||
|
||||
expect(
|
||||
await getOrderedDependencyPaths(deltaBundler, assetsServer, ['/tmp'], {}),
|
||||
await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {}),
|
||||
).toEqual([
|
||||
'/tmp/1.js',
|
||||
'/tmp/2.png@2x',
|
||||
|
@ -70,5 +73,10 @@ describe('getOrderedDependencyPaths', () => {
|
|||
'/tmp/4.png@3x',
|
||||
'/tmp/5.js',
|
||||
]);
|
||||
|
||||
expect(getAssetFiles.mock.calls).toEqual([
|
||||
['/tmp/2.png', undefined],
|
||||
['/tmp/4.png', undefined],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -14,13 +14,13 @@
|
|||
|
||||
const Serializers = require('../DeltaBundler/Serializers');
|
||||
|
||||
import type AssetsServer from '../AssetServer';
|
||||
const {getAssetFiles} = require('../AssetServer/util');
|
||||
|
||||
import type {Options} from '../DeltaBundler/Serializers';
|
||||
import type DeltaBundler from '../DeltaBundler';
|
||||
|
||||
async function getOrderedDependencyPaths(
|
||||
deltaBundler: DeltaBundler,
|
||||
assetsServer: AssetsServer,
|
||||
options: Options,
|
||||
): Promise<Array<string>> {
|
||||
const modules = await Serializers.getAllModules(deltaBundler, options);
|
||||
|
@ -30,12 +30,7 @@ async function getOrderedDependencyPaths(
|
|||
if (module.type !== 'asset') {
|
||||
return [module.path];
|
||||
} else {
|
||||
const assetData = await assetsServer.getAssetData(
|
||||
module.path,
|
||||
options.platform,
|
||||
);
|
||||
|
||||
return assetData.files;
|
||||
return await getAssetFiles(module.path, options.platform);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue