BREAKING kill deprecated asset support

Summary:
This removes support for `require('image!…')`, which has been deprecated for a long time.

It is still possible to use images that are already bundled by the native app using the `nativeImageSource` module.
Check http://facebook.github.io/react-native/docs/images.html for detailed documentation.

Reviewed By: matryoshcow

Differential Revision: D4231208

fbshipit-source-id: 05ec4c1ca0fabdc3fbb652f8ad1acdf240a67955
This commit is contained in:
David Aurelio 2016-11-24 05:28:29 -08:00 committed by Facebook Github Bot
parent 1b8582a2d1
commit a810087624
14 changed files with 15 additions and 672 deletions

View File

@ -117,7 +117,7 @@ Builds a bundle according to the provided options.
#### `bundleOptions`
* `entryFile` string (required): the entry file of the bundle, relative to one
of the asset roots.
of the roots.
* `dev` boolean (defaults to `true`): sets a global `__DEV__` variable
which will effect how the React Native core libraries behave.
* `minify` boolean: Whether to minify code and apply production optimizations.

View File

@ -38,7 +38,6 @@ describe('Bundler', function() {
id,
dependencies,
isAsset,
isAsset_DEPRECATED,
isJSON,
isPolyfill,
resolution,
@ -50,7 +49,6 @@ describe('Bundler', function() {
getName: () => Promise.resolve(id),
isJSON: () => isJSON,
isAsset: () => isAsset,
isAsset_DEPRECATED: () => isAsset_DEPRECATED,
isPolyfill: () => isPolyfill,
read: () => ({
code: 'arbitrary',
@ -100,13 +98,6 @@ describe('Bundler', function() {
modules = [
createModule({id: 'foo', path: '/root/foo.js', dependencies: []}),
createModule({id: 'bar', path: '/root/bar.js', dependencies: []}),
createModule({
path: '/root/img/img.png',
id: 'image!img',
isAsset_DEPRECATED: true,
dependencies: [],
resolution: 2,
}),
createModule({
id: 'new_image.png',
path: '/root/img/new_image.png',
@ -166,9 +157,8 @@ describe('Bundler', function() {
expect(ithAddedModule(0)).toEqual('/root/foo.js');
expect(ithAddedModule(1)).toEqual('/root/bar.js');
expect(ithAddedModule(2)).toEqual('/root/img/img.png');
expect(ithAddedModule(3)).toEqual('/root/img/new_image.png');
expect(ithAddedModule(4)).toEqual('/root/file.json');
expect(ithAddedModule(2)).toEqual('/root/img/new_image.png');
expect(ithAddedModule(3)).toEqual('/root/file.json');
expect(bundle.finalize.mock.calls[0]).toEqual([{
runMainModule: true,
@ -177,15 +167,6 @@ describe('Bundler', function() {
}]);
expect(bundle.addAsset.mock.calls[0]).toEqual([{
__packager_asset: true,
path: '/root/img/img.png',
uri: 'img',
width: 25,
height: 50,
deprecated: true,
}]);
expect(bundle.addAsset.mock.calls[1]).toEqual([{
__packager_asset: true,
fileSystemLocation: '/root/img',
httpServerLocation: '/assets/img',
@ -245,7 +226,7 @@ describe('Bundler', function() {
sourceMapUrl: 'source_map_url',
assetPlugins: ['mockPlugin1', 'asyncMockPlugin2'],
}).then(bundle => {
expect(bundle.addAsset.mock.calls[1]).toEqual([{
expect(bundle.addAsset.mock.calls[0]).toEqual([{
__packager_asset: true,
fileSystemLocation: '/root/img',
httpServerLocation: '/assets/img',
@ -334,7 +315,6 @@ describe('Bundler', function() {
.then((paths) => expect(paths).toEqual([
'/root/foo.js',
'/root/bar.js',
'/root/img/img.png',
'/root/img/new_image.png',
'/root/img/new_image@2x.png',
'/root/img/new_image@3x.png',

View File

@ -79,10 +79,6 @@ const validateOpts = declareOpts({
type: 'object',
required: false,
},
assetRoots: {
type: 'array',
required: false,
},
assetExts: {
type: 'array',
default: ['png'],
@ -120,7 +116,6 @@ type Options = {
resetCache: boolean,
transformModulePath: string,
extraNodeModules: {},
assetRoots: Array<string>,
assetExts: Array<string>,
watch: boolean,
assetServer: AssetServer,
@ -182,7 +177,6 @@ class Bundler {
this._resolver = new Resolver({
assetExts: opts.assetExts,
assetRoots: opts.assetRoots,
blacklistRE: opts.blacklistRE,
cache: this._cache,
extraNodeModules: opts.extraNodeModules,
@ -603,10 +597,7 @@ class Bundler {
let moduleTransport;
const moduleId = getModuleId(module);
if (module.isAsset_DEPRECATED()) {
moduleTransport =
this._generateAssetModule_DEPRECATED(bundle, module, moduleId);
} else if (module.isAsset()) {
if (module.isAsset()) {
moduleTransport = this._generateAssetModule(
bundle, module, moduleId, assetPlugins, transformOptions.platform);
}
@ -639,38 +630,6 @@ class Bundler {
});
}
_generateAssetModule_DEPRECATED(bundle, module, moduleId) {
return Promise.all([
sizeOf(module.path),
module.getName(),
]).then(([dimensions, id]) => {
const img = {
__packager_asset: true,
path: module.path,
uri: id.replace(/^[^!]+!/, ''),
width: dimensions.width / module.resolution,
height: dimensions.height / module.resolution,
deprecated: true,
};
bundle.addAsset(img);
const code =
'module.exports=' +
JSON.stringify(filterObject(img, assetPropertyBlacklist))
+ ';';
return new ModuleTransport({
name: id,
id: moduleId,
code: code,
sourceCode: code,
sourcePath: module.path,
virtual: true,
});
});
}
_generateAssetObjAndCode(module, assetPlugins, platform: mixed = null) {
const relPath = getPathRelativeToRoot(this._projectRoots, module.path);
var assetUrlPath = joinPath('/assets', pathDirname(relPath));

View File

@ -54,19 +54,6 @@ export type FastFS = {
matches(directory: Path, pattern: RegExp): Array<Path>,
};
type DeprecatedAssetMapOptions = {|
assetExts: Extensions,
files: Array<Path>,
helpers: DependencyGraphHelpers,
platforms: Platforms,
|};
declare class DeprecatedAssetMap {
// node-haste/DependencyGraph/DeprecatedAssetMap.js
constructor(options: DeprecatedAssetMapOptions): void,
}
export type DeprecatedAssetMapT = DeprecatedAssetMap;
type HasteMapOptions = {|
allowRelativePaths: boolean,
extensions: Extensions,
@ -89,7 +76,6 @@ type ResolutionRequestOptions = {|
platforms: Platforms,
preferNativePlatform: true,
hasteMap: HasteMap,
deprecatedAssetMap: DeprecatedAssetMap,
helpers: DependencyGraphHelpers,
moduleCache: ModuleCache,
fastfs: FastFS,

View File

@ -12,7 +12,6 @@
'use strict';
import type { // eslint-disable-line sort-requires
DeprecatedAssetMapT,
Extensions,
HasteMapT,
Path,
@ -25,7 +24,6 @@ import type {
} from '../types.flow';
const DependencyGraphHelpers = require('../../node-haste/DependencyGraph/DependencyGraphHelpers');
const DeprecatedAssetMap: Class<DeprecatedAssetMapT> = require('../../node-haste/DependencyGraph/DeprecatedAssetMap');
const FastFS = require('./FastFS');
const HasteMap: Class<HasteMapT> = require('../../node-haste/DependencyGraph/HasteMap');
const Module = require('./Module');
@ -58,12 +56,6 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
assetExts,
providesModuleNodeModules: defaults.providesModuleNodeModules,
});
const deprecatedAssetMap = new DeprecatedAssetMap({
assetExts,
files,
helpers,
platforms,
});
const fastfs = new FastFS(files);
const moduleCache = new ModuleCache(fastfs, getTransformedFile);
@ -83,7 +75,6 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
let resolutionRequest = resolutionRequests[platform];
if (!resolutionRequest) {
resolutionRequest = resolutionRequests[platform] = new ResolutionRequest({
deprecatedAssetMap,
extraNodeModules,
fastfs,
hasteMap,

View File

@ -31,10 +31,6 @@ const validateOpts = declareOpts({
type: 'string',
default: 'haste',
},
assetRoots: {
type: 'array',
default: [],
},
watch: {
type: 'boolean',
default: false,
@ -92,7 +88,6 @@ class Resolver {
this._depGraph = new DependencyGraph({
roots: opts.projectRoots,
assetRoots_DEPRECATED: opts.assetRoots,
assetExts: opts.assetExts,
ignoreFilePath: function(filepath) {
return filepath.indexOf('__tests__') !== -1 ||

View File

@ -79,10 +79,6 @@ const validateOpts = declareOpts({
type: 'boolean',
default: false,
},
assetRoots: {
type: 'array',
required: false,
},
assetExts: {
type: 'array',
default: defaults.assetExts,

View File

@ -1,45 +0,0 @@
'use strict';
const Module = require('./Module');
const getAssetDataFromName = require('./lib/getAssetDataFromName');
class AssetModule_DEPRECATED extends Module {
constructor(args, platforms) {
super(args);
const {resolution, name} = getAssetDataFromName(this.path, platforms);
this.resolution = resolution;
this.name = name;
this.platforms = platforms;
}
isHaste() {
return Promise.resolve(false);
}
getName() {
return Promise.resolve(`image!${this.name}`);
}
getDependencies() {
return Promise.resolve([]);
}
hash() {
return `AssetModule_DEPRECATED : ${this.path}`;
}
isJSON() {
return false;
}
isAsset_DEPRECATED() {
return true;
}
resolution() {
return getAssetDataFromName(this.path, this.platforms).resolution;
}
}
module.exports = AssetModule_DEPRECATED;

View File

@ -1,73 +0,0 @@
/**
* 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';
const AssetModule_DEPRECATED = require('../AssetModule_DEPRECATED');
const debug = require('debug')('ReactNativePackager:DependencyGraph');
const path = require('path');
class DeprecatedAssetMap {
constructor({
assetExts,
helpers,
platforms,
files,
}) {
this._helpers = helpers;
this._map = Object.create(null);
this._assetExts = assetExts;
this._platforms = platforms;
files.forEach(file => this._processAsset(file));
}
resolve(fromModule, toModuleName) {
if (this._disabled) {
return null;
}
const assetMatch = toModuleName.match(/^image!(.+)/);
if (assetMatch && assetMatch[1]) {
if (!this._map[assetMatch[1]]) {
debug('WARINING: Cannot find asset:', assetMatch[1]);
return null;
}
return this._map[assetMatch[1]];
}
}
_processAsset(file) {
const ext = this._helpers.extname(file);
if (this._assetExts.indexOf(ext) !== -1) {
const name = assetName(file, ext);
if (this._map[name] != null) {
debug('Conflicting assets', name);
}
this._map[name] = new AssetModule_DEPRECATED({ file }, this._platforms);
}
}
processFileChange(type, filePath, fstat) {
const name = assetName(filePath);
if (type === 'change' || type === 'delete') {
delete this._map[name];
}
if (type === 'change' || type === 'add') {
this._processAsset(filePath);
}
}
}
function assetName(file, ext) {
return path.basename(file, '.' + ext).replace(/@[\d\.]+x/, '');
}
module.exports = DeprecatedAssetMap;

View File

@ -26,7 +26,6 @@ class ResolutionRequest {
preferNativePlatform,
entryPath,
hasteMap,
deprecatedAssetMap,
helpers,
moduleCache,
fastfs,
@ -38,7 +37,6 @@ class ResolutionRequest {
this._preferNativePlatform = preferNativePlatform;
this._entryPath = entryPath;
this._hasteMap = hasteMap;
this._deprecatedAssetMap = deprecatedAssetMap;
this._helpers = helpers;
this._moduleCache = moduleCache;
this._fastfs = fastfs;
@ -63,14 +61,6 @@ class ResolutionRequest {
return Promise.resolve(this._immediateResolutionCache[resHash]);
}
const asset_DEPRECATED = this._deprecatedAssetMap.resolve(
fromModule,
toModuleName
);
if (asset_DEPRECATED) {
return Promise.resolve(asset_DEPRECATED);
}
const cacheResult = (result) => {
this._immediateResolutionCache[resHash] = result;
return result;
@ -206,8 +196,8 @@ class ResolutionRequest {
function collect(module) {
collectionsInProgress.start(module);
const result = resolveDependencies(module)
.then(result => addMockDependencies(module, result))
.then(result => crawlDependencies(module, result));
.then(deps => addMockDependencies(module, deps))
.then(deps => crawlDependencies(module, deps));
const end = () => collectionsInProgress.end(module);
result.then(end, end);
return result;
@ -252,9 +242,9 @@ class ResolutionRequest {
let mocks = null;
if (pattern) {
mocks = Object.create(null);
this._fastfs.matchFilesByPattern(pattern).forEach(file =>
mocks[path.basename(file, path.extname(file))] = file
);
this._fastfs.matchFilesByPattern(pattern).forEach(file => {
mocks[path.basename(file, path.extname(file))] = file;
});
}
return Promise.resolve(mocks);
}
@ -532,8 +522,8 @@ function resolveKeyWithPromise([key, promise]) {
return promise.then(value => [key, value]);
}
function isRelativeImport(path) {
return /^[.][.]?(?:[/]|$)/.test(path);
function isRelativeImport(filePath) {
return /^[.][.]?(?:[/]|$)/.test(filePath);
}
module.exports = ResolutionRequest;

View File

@ -219,8 +219,6 @@ class Module {
callback: (error: ?Error, result: ?TransformedCode) => void,
) {
const {_transformCode, _transformCacheKey} = this;
// AssetModule_DEPRECATED doesn't provide transformCode, but these should
// never be transformed anyway.
invariant(_transformCode != null, 'missing code transform funtion');
invariant(_transformCacheKey != null, 'missing cache key');
this._readSourceCode()
@ -311,16 +309,11 @@ class Module {
return false;
}
isAsset_DEPRECATED() {
return false;
}
toJSON() {
return {
hash: this.hash(),
isJSON: this.isJSON(),
isAsset: this.isAsset(),
isAsset_DEPRECATED: this.isAsset_DEPRECATED(),
type: this.type,
path: this.path,
};

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,6 @@
const Cache = require('./Cache');
const DependencyGraphHelpers = require('./DependencyGraph/DependencyGraphHelpers');
const DeprecatedAssetMap = require('./DependencyGraph/DeprecatedAssetMap');
const Fastfs = require('./fastfs');
const HasteMap = require('./DependencyGraph/HasteMap');
const JestHasteMap = require('jest-haste-map');
@ -46,18 +45,13 @@ const {
print,
} = require('../Logger');
const escapePath = (p: string) => {
return (path.sep === '\\') ? p.replace(/(\/|\\(?!\.))/g, '\\\\') : p;
};
class DependencyGraph {
_opts: {
_opts: {|
roots: Array<string>,
ignoreFilePath: (filePath: string) => boolean,
watch: boolean,
forceNodeFilesystemAPI: boolean,
assetRoots_DEPRECATED: Array<string>,
assetExts: Array<string>,
providesModuleNodeModules: Array<string>,
platforms: Set<mixed>,
@ -67,17 +61,14 @@ class DependencyGraph {
transformCode: TransformCode,
transformCacheKey: string,
shouldThrowOnUnresolvedErrors: () => boolean,
enableAssetMap: boolean,
moduleOptions: ModuleOptions,
extraNodeModules: mixed,
useWatchman: boolean,
maxWorkers: number,
resetCache: boolean,
};
|};
_assetDependencies: mixed;
_assetPattern: RegExp;
_cache: Cache;
_deprecatedAssetMap: DeprecatedAssetMap;
_fastfs: Fastfs;
_haste: JestHasteMap;
_hasteMap: HasteMap;
@ -92,7 +83,6 @@ class DependencyGraph {
ignoreFilePath,
watch,
forceNodeFilesystemAPI,
assetRoots_DEPRECATED,
assetExts,
providesModuleNodeModules,
platforms,
@ -103,7 +93,6 @@ class DependencyGraph {
transformCode,
transformCacheKey,
shouldThrowOnUnresolvedErrors = () => true,
enableAssetMap,
assetDependencies,
moduleOptions,
extraNodeModules,
@ -116,7 +105,6 @@ class DependencyGraph {
ignoreFilePath: (filePath: string) => boolean,
watch: boolean,
forceNodeFilesystemAPI?: boolean,
assetRoots_DEPRECATED: Array<string>,
assetExts: Array<string>,
providesModuleNodeModules: Array<string>,
platforms: mixed,
@ -127,7 +115,6 @@ class DependencyGraph {
transformCode: TransformCode,
transformCacheKey: string,
shouldThrowOnUnresolvedErrors: () => boolean,
enableAssetMap: boolean,
assetDependencies: mixed,
moduleOptions: ?ModuleOptions,
extraNodeModules: mixed,
@ -140,7 +127,6 @@ class DependencyGraph {
ignoreFilePath: ignoreFilePath || (() => {}),
watch: !!watch,
forceNodeFilesystemAPI: !!forceNodeFilesystemAPI,
assetRoots_DEPRECATED: assetRoots_DEPRECATED || [],
assetExts: assetExts || [],
providesModuleNodeModules,
platforms: new Set(platforms || []),
@ -150,7 +136,6 @@ class DependencyGraph {
transformCode,
transformCacheKey,
shouldThrowOnUnresolvedErrors,
enableAssetMap: enableAssetMap || true,
moduleOptions: moduleOptions || {
cacheTransformResults: true,
},
@ -160,8 +145,6 @@ class DependencyGraph {
maxWorkers,
resetCache,
};
this._assetPattern =
new RegExp('^' + this._opts.assetRoots_DEPRECATED.map(escapePath).join('|'));
this._cache = cache;
this._assetDependencies = assetDependencies;
@ -186,7 +169,7 @@ class DependencyGraph {
providesModuleNodeModules: this._opts.providesModuleNodeModules,
resetCache: this._opts.resetCache,
retainAllFiles: true,
roots: this._opts.roots.concat(this._opts.assetRoots_DEPRECATED),
roots: this._opts.roots,
useWatchman: this._opts.useWatchman,
watch: this._opts.watch,
});
@ -225,15 +208,6 @@ class DependencyGraph {
platforms: this._opts.platforms,
});
const assetFiles = hasteMap.hasteFS.matchFiles(this._assetPattern);
this._deprecatedAssetMap = new DeprecatedAssetMap({
helpers: this._helpers,
assetExts: this._opts.assetExts,
platforms: this._opts.platforms,
files: assetFiles,
});
this._haste.on('change', ({eventsQueue}) =>
eventsQueue.forEach(({type, filePath, stat}) =>
this.processFileChange(type, filePath, stat)
@ -314,7 +288,6 @@ class DependencyGraph {
platforms: this._opts.platforms,
preferNativePlatform: this._opts.preferNativePlatform,
entryPath: absPath,
deprecatedAssetMap: this._deprecatedAssetMap,
hasteMap: this._hasteMap,
helpers: this._helpers,
moduleCache: this._moduleCache,
@ -371,9 +344,6 @@ class DependencyGraph {
processFileChange(type: string, filePath: string, stat: Object) {
this._fastfs.processFileChange(type, filePath, stat);
this._moduleCache.processFileChange(type, filePath, stat);
if (this._assetPattern.test(filePath)) {
this._deprecatedAssetMap.processFileChange(type, filePath, stat);
}
// This code reports failures but doesn't block recovery in the dev server
// mode. When the hasteMap is left in an incorrect state, we'll rebuild when

View File

@ -18,10 +18,6 @@ module.exports = {
return this._getRoots();
},
getAssetRoots() {
return this._getRoots();
},
getAssetExts() {
return [];
},