diff --git a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js index 1a9350fa..1794a206 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js @@ -227,6 +227,55 @@ describe('DependencyGraph', function() { }); }); + pit('should get package json as a dep', () => { + var root = '/root'; + fs.__setMockFilesystem({ + 'root': { + 'package.json': JSON.stringify({ + name: 'package' + }), + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("./package.json")', + ].join('\n'), + } + }); + + var dgraph = new DependencyGraph({ + roots: [root], + fileWatcher: fileWatcher, + assetExts: ['png', 'jpg'], + cache: cache, + }); + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { + expect(deps) + .toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./package.json'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'package/package.json', + isJSON: true, + path: '/root/package.json', + dependencies: [], + isAsset: false, + isAsset_DEPRECATED: false, + isPolyfill: false, + resolution: undefined, + }, + ]); + }); + }); + pit('should get dependencies with deprecated assets', function() { var root = '/root'; fs.__setMockFilesystem({ @@ -2619,6 +2668,89 @@ describe('DependencyGraph', function() { ]); }); }); + + pit('should require package.json', () => { + var root = '/root'; + fs.__setMockFilesystem({ + 'root': { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("foo/package.json");', + 'require("bar");', + ].join('\n'), + 'node_modules': { + 'foo': { + 'package.json': JSON.stringify({ + name: 'foo', + main: 'main.js', + }), + }, + 'bar': { + 'package.json': JSON.stringify({ + name: 'bar', + main: 'main.js', + }), + 'main.js': 'require("./package.json")', + }, + }, + } + }); + + var dgraph = new DependencyGraph({ + roots: [root], + fileWatcher: fileWatcher, + assetExts: ['png', 'jpg'], + cache: cache, + }); + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { + expect(deps) + .toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo/package.json', 'bar'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/package.json', + path: '/root/node_modules/foo/package.json', + dependencies: [], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: '/root/node_modules/bar/main.js', + dependencies: ['./package.json'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/package.json', + path: '/root/node_modules/bar/package.json', + dependencies: [], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + + ]); + }); + }); }); describe('file watch updating', function() { diff --git a/react-packager/src/DependencyResolver/Package.js b/react-packager/src/DependencyResolver/Package.js index 5354e588..d45a8923 100644 --- a/react-packager/src/DependencyResolver/Package.js +++ b/react-packager/src/DependencyResolver/Package.js @@ -34,13 +34,13 @@ class Package { } isHaste() { - return this._cache.get(this.path, 'haste', () => + return this._cache.get(this.path, 'package-haste', () => this._read().then(json => !!json.name) ); } getName() { - return this._cache.get(this.path, 'name', () => + return this._cache.get(this.path, 'package-name', () => this._read().then(json => json.name) ); }