[react-packager] Use module objects across the codebase (rid of getPlainObject etc)

Summary:
Instead of using plain objects and having to convert to and from them we just use the `Module` class across the codebase.
This seems cleaner and can enforce the type as opposed to fuzzy objects.
This commit is contained in:
Amjad Masad 2015-08-13 15:30:02 -07:00
parent a372af9956
commit 7664d5fb76
11 changed files with 310 additions and 396 deletions

View File

@ -58,29 +58,50 @@ describe('Bundler', function() {
assetServer: assetServer,
});
function createModule({
path,
id,
dependencies,
isAsset,
isAsset_DEPRECATED,
isJSON,
resolution,
}) {
return {
path,
resolution,
getDependencies() { return Promise.resolve(dependencies); },
getName() { return Promise.resolve(id); },
isJSON() { return isJSON; },
isAsset() { return isAsset; },
isAsset_DEPRECATED() { return isAsset_DEPRECATED; },
};
}
modules = [
{id: 'foo', path: '/root/foo.js', dependencies: []},
{id: 'bar', path: '/root/bar.js', dependencies: []},
{
id: 'image!img',
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',
isAsset: true,
resolution: 2,
dependencies: []
},
{
}),
createModule({
id: 'package/file.json',
path: '/root/file.json',
isJSON: true,
dependencies: [],
},
}),
];
getDependencies.mockImpl(function() {
@ -203,41 +224,11 @@ describe('Bundler', function() {
});
});
pit('gets the list of dependencies', function() {
pit('gets the list of dependencies from the resolver', function() {
return bundler.getDependencies('/root/foo.js', true)
.then(({dependencies}) => {
expect(dependencies).toEqual([
{
dependencies: [],
id: 'foo',
path: '/root/foo.js',
},
{
dependencies: [],
id: 'bar',
path: '/root/bar.js',
},
{
dependencies: [],
id: 'image!img',
isAsset_DEPRECATED: true,
path: '/root/img/img.png',
resolution: 2,
},
{
dependencies: [],
id: 'new_image.png',
isAsset: true,
path: '/root/img/new_image.png',
resolution: 2,
},
{
dependencies: [],
id: 'package/file.json',
isJSON: true,
path: '/root/file.json',
},
]);
});
.then(
() => expect(getDependencies)
.toBeCalledWith('/root/foo.js', { dev: true })
);
});
});

View File

@ -125,8 +125,7 @@ class Bundler {
const findEventId = Activity.startEvent('find dependencies');
let transformEventId;
return this.getDependencies(main, isDev)
.then(function(result) {
return this.getDependencies(main, isDev).then((result) => {
Activity.endEvent(findEventId);
transformEventId = Activity.startEvent('transform');
@ -134,8 +133,7 @@ class Bundler {
return Promise.all(
result.dependencies.map(transformModule)
);
})
.then(function(transformedModules) {
}).then((transformedModules) => {
Activity.endEvent(transformEventId);
transformedModules.forEach(function(moduleTransport) {
@ -158,11 +156,11 @@ class Bundler {
_transformModule(bundle, module) {
let transform;
if (module.isAsset_DEPRECATED) {
if (module.isAsset_DEPRECATED()) {
transform = this.generateAssetModule_DEPRECATED(bundle, module);
} else if (module.isAsset) {
} else if (module.isAsset()) {
transform = this.generateAssetModule(bundle, module);
} else if (module.isJSON) {
} else if (module.isJSON()) {
transform = generateJSONModule(module);
} else {
transform = this._transformer.loadFileAndTransform(
@ -189,12 +187,15 @@ class Bundler {
}
generateAssetModule_DEPRECATED(bundle, module) {
return sizeOf(module.path).then(function(dimensions) {
return Promise.all([
sizeOf(module.path),
module.getName(),
]).then(([dimensions, id]) => {
const img = {
__packager_asset: true,
isStatic: true,
path: module.path,
uri: module.id.replace(/^[^!]+!/, ''),
uri: id.replace(/^[^!]+!/, ''),
width: dimensions.width / module.resolution,
height: dimensions.height / module.resolution,
deprecated: true,

View File

@ -25,6 +25,7 @@ jest
.dontMock('../../DependencyResolver/AssetModule')
.dontMock('../../DependencyResolver/Module')
.dontMock('../../DependencyResolver/Package')
.dontMock('../../DependencyResolver/Polyfill')
.dontMock('../../DependencyResolver/ModuleCache');
const Promise = require('promise');
@ -51,17 +52,6 @@ describe('BundlesLayout', () => {
});
describe('generate', () => {
const polyfills = [
'polyfills/prelude_dev.js',
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js',
];
function newBundlesLayout() {
const resolver = new DependencyResolver({
projectRoots: ['/root'],
@ -79,18 +69,10 @@ describe('BundlesLayout', () => {
return null;
}
return bundles.map(bundle => {
return bundle
.filter(module => { // filter polyfills
for (let p of polyfills) {
if (module.id.indexOf(p) !== -1) {
return false;
}
}
return true;
})
.map(module => module.path);
});
return bundles.map(
bundle => bundle.filter(module => !module.isPolyfill())
.map(module => module.path)
);
}
pit('should bundle dependant modules', () => {

View File

@ -5,6 +5,13 @@ const Promise = require('promise');
const getAssetDataFromName = require('../lib/getAssetDataFromName');
class AssetModule extends Module {
constructor(...args) {
super(...args);
const { resolution, name, type } = getAssetDataFromName(this.path);
this.resolution = resolution;
this._name = name;
this._type = type;
}
isHaste() {
return Promise.resolve(false);
@ -23,28 +30,22 @@ class AssetModule extends Module {
}
getName() {
return super.getName().then(id => {
const {name, type} = getAssetDataFromName(this.path);
return id.replace(/\/[^\/]+$/, `/${name}.${type}`);
});
}
getPlainObject() {
return this.getName().then(name => this.addReference({
path: this.path,
isJSON: false,
isAsset: true,
isAsset_DEPRECATED: false,
isPolyfill: false,
resolution: getAssetDataFromName(this.path).resolution,
id: name,
dependencies: [],
}));
return super.getName().then(
id => id.replace(/\/[^\/]+$/, `/${this._name}.${this._type}`)
);
}
hash() {
return `AssetModule : ${this.path}`;
}
isJSON() {
return false;
}
isAsset() {
return true;
}
}
module.exports = AssetModule;

View File

@ -5,12 +5,19 @@ const Promise = require('promise');
const getAssetDataFromName = require('../lib/getAssetDataFromName');
class AssetModule_DEPRECATED extends Module {
constructor(...args) {
super(...args);
const {resolution, name} = getAssetDataFromName(this.path);
this.resolution = resolution;
this.name = name;
}
isHaste() {
return Promise.resolve(false);
}
getName() {
return Promise.resolve(this.name);
return Promise.resolve(`image!${this.name}`);
}
getDependencies() {
@ -21,24 +28,22 @@ class AssetModule_DEPRECATED extends Module {
return Promise.resolve([]);
}
getPlainObject() {
const {name, resolution} = getAssetDataFromName(this.path);
return Promise.resolve(this.addReference({
path: this.path,
id: `image!${name}`,
resolution,
isAsset_DEPRECATED: true,
dependencies: [],
isJSON: false,
isPolyfill: false,
isAsset: false,
}));
}
hash() {
return `AssetModule_DEPRECATED : ${this.path}`;
}
isJSON() {
return false;
}
isAsset_DEPRECATED() {
return true;
}
resolution() {
return getAssetDataFromName(this.path).resolution;
}
}
module.exports = AssetModule_DEPRECATED;

View File

@ -35,6 +35,24 @@ describe('DependencyGraph', function() {
var fileWatcher;
var fs;
function getOrderedDependenciesAsJSON(dgraph, entry) {
return dgraph.getOrderedDependencies(entry).then(
deps => Promise.all(deps.map(dep => Promise.all([
dep.getName(),
dep.getDependencies(),
]).then(([name, dependencies]) => ({
path: dep.path,
isJSON: dep.isJSON(),
isAsset: dep.isAsset(),
isAsset_DEPRECATED: dep.isAsset_DEPRECATED(),
isPolyfill: dep.isPolyfill(),
resolution: dep.resolution,
id: name,
dependencies
})))
));
}
beforeEach(function() {
fs = require('fs');
Cache = require('../../../Cache');
@ -75,7 +93,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -133,7 +151,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -185,7 +203,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -245,7 +263,7 @@ describe('DependencyGraph', function() {
assetRoots_DEPRECATED: ['/root/imgs'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -297,7 +315,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -354,7 +372,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -432,7 +450,7 @@ describe('DependencyGraph', function() {
assetRoots_DEPRECATED: ['/root/imgs'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -494,7 +512,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -547,7 +565,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -600,7 +618,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -661,7 +679,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -718,7 +736,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -769,7 +787,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -819,7 +837,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -866,7 +884,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -917,7 +935,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -966,7 +984,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1020,7 +1038,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/somedir/somefile.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/somedir/somefile.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1078,7 +1096,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1126,7 +1144,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1173,7 +1191,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1232,7 +1250,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1291,7 +1309,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1370,7 +1388,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1427,7 +1445,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1484,7 +1502,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1541,7 +1559,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1613,7 +1631,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{ id: 'index',
@ -1716,7 +1734,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{ id: 'index',
@ -1797,7 +1815,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1879,7 +1897,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.ios.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -1962,7 +1980,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2059,7 +2077,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2145,7 +2163,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2250,7 +2268,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2319,7 +2337,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/react-tools/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/react-tools/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2376,7 +2394,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2421,7 +2439,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2482,7 +2500,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.ios.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2538,7 +2556,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.ios.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2587,7 +2605,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.ios.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2671,11 +2689,11 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', '');
triggerFileChange('change', 'index.js', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2736,11 +2754,11 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', '');
triggerFileChange('change', 'index.js', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2801,10 +2819,10 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
delete filesystem.root.foo;
triggerFileChange('delete', 'foo.js', root);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2865,7 +2883,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['bar.js'] = [
'/**',
' * @providesModule bar',
@ -2877,7 +2895,7 @@ describe('DependencyGraph', function() {
filesystem.root.aPackage['main.js'] = 'require("bar")';
triggerFileChange('change', 'aPackage/main.js', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2948,7 +2966,7 @@ describe('DependencyGraph', function() {
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -2967,7 +2985,7 @@ describe('DependencyGraph', function() {
filesystem.root['foo.png'] = '';
triggerFileChange('add', 'foo.png', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps2) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
{
@ -3020,7 +3038,7 @@ describe('DependencyGraph', function() {
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{ id: 'index',
@ -3038,7 +3056,7 @@ describe('DependencyGraph', function() {
filesystem.root['foo.png'] = '';
triggerFileChange('add', 'foo.png', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps2) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
{
@ -3107,7 +3125,7 @@ describe('DependencyGraph', function() {
},
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['bar.js'] = [
'/**',
' * @providesModule bar',
@ -3119,7 +3137,7 @@ describe('DependencyGraph', function() {
filesystem.root.aPackage['main.js'] = 'require("bar")';
triggerFileChange('change', 'aPackage/main.js', root, mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3192,11 +3210,11 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
triggerFileChange('change', 'aPackage', '/root', {
isDirectory: function(){ return true; }
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3263,7 +3281,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] = filesystem.root['index.js'].replace(/aPackage/, 'bPackage');
triggerFileChange('change', 'index.js', root, mockStat);
@ -3273,7 +3291,7 @@ describe('DependencyGraph', function() {
});
triggerFileChange('change', 'package.json', '/root/aPackage', mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3330,7 +3348,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root.aPackage['package.json'] = JSON.stringify({
name: 'aPackage',
main: 'main.js',
@ -3338,7 +3356,7 @@ describe('DependencyGraph', function() {
});
triggerFileChange('change', 'package.json', '/root/aPackage', mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3395,14 +3413,14 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root.aPackage['package.json'] = JSON.stringify({
name: 'bPackage',
main: 'main.js',
});
triggerFileChange('change', 'package.json', '/root/aPackage', mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3458,7 +3476,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
@ -3499,7 +3517,7 @@ describe('DependencyGraph', function() {
filesystem.root.node_modules.foo['main.js'] = 'lol';
triggerFileChange('change', 'main.js', '/root/node_modules/foo', mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps2) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
{
@ -3558,7 +3576,7 @@ describe('DependencyGraph', function() {
assetExts: ['png', 'jpg'],
cache: cache,
});
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
filesystem.root.node_modules.foo['package.json'] = JSON.stringify({
name: 'foo',
main: 'main.js',
@ -3566,7 +3584,7 @@ describe('DependencyGraph', function() {
});
triggerFileChange('change', 'package.json', '/root/node_modules/foo', mockStat);
return dgraph.getOrderedDependencies('/root/index.js').then(function(deps2) {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
{

View File

@ -110,10 +110,6 @@ class DependencyGraph {
}
resolveDependency(fromModule, toModuleName) {
if (fromModule._ref) {
fromModule = fromModule._ref;
}
const resHash = resolutionHash(fromModule.path, toModuleName);
if (this._immediateResolutionCache[resHash]) {
@ -199,8 +195,7 @@ class DependencyGraph {
};
return collect(entry)
.then(() => Promise.all(deps.map(dep => dep.getPlainObject())))
.then();
.then(() => deps);
});
}

View File

@ -109,29 +109,24 @@ class Module {
return this._reading;
}
getPlainObject() {
return Promise.all([
this.getName(),
this.getDependencies(),
]).then(([name, dependencies]) => this.addReference({
path: this.path,
isJSON: path.extname(this.path) === '.json',
isAsset: false,
isAsset_DEPRECATED: false,
isPolyfill: false,
resolution: undefined,
id: name,
dependencies
}));
}
hash() {
return `Module : ${this.path}`;
}
addReference(obj) {
Object.defineProperty(obj, '_ref', { value: this });
return obj;
isJSON() {
return path.extname(this.path) === '.json';
}
isAsset() {
return false;
}
isPolyfill() {
return false;
}
isAsset_DEPRECATED() {
return false;
}
}

View File

@ -0,0 +1,38 @@
'use strict';
const Promise = require('promise');
const Module = require('./Module');
class Polyfill extends Module {
constructor({ path, id, dependencies }) {
super(path);
this._id = id;
this._dependencies = dependencies;
}
isHaste() {
return Promise.resolve(false);
}
getName() {
return Promise.resolve(this._id);
}
getPackage() {
return null;
}
getDependencies() {
return Promise.resolve(this._dependencies);
}
isJSON() {
return false;
}
isPolyfill() {
return true;
}
}
module.exports = Polyfill;

View File

@ -9,22 +9,24 @@
'use strict';
jest.dontMock('../')
.dontMock('q')
.dontMock('underscore')
.dontMock('../replacePatterns');
jest.mock('path');
var Promise = require('promise');
var _ = require('underscore');
describe('HasteDependencyResolver', function() {
var HasteDependencyResolver;
function createModule(o) {
o.getPlainObject = () => Promise.resolve(o);
return o;
}
var Module;
var Polyfill;
beforeEach(function() {
Module = require('../Module');
Polyfill = require('../Polyfill');
Polyfill.mockClear();
// For the polyfillDeps
require('path').join.mockImpl(function(a, b) {
return b;
@ -32,12 +34,16 @@ describe('HasteDependencyResolver', function() {
HasteDependencyResolver = require('../');
});
function createModule(id, dependencies) {
var module = new Module();
module.getName.mockImpl(() => Promise.resolve(id));
module.getDependencies.mockImpl(() => Promise.resolve(dependencies));
return module;
}
describe('getDependencies', function() {
pit('should get dependencies with polyfills', function() {
var module = createModule({
id: 'index',
path: '/root/index.js', dependencies: ['a']
});
var module = createModule('index');
var deps = [module];
var depResolver = new HasteDependencyResolver({
@ -56,7 +62,8 @@ describe('HasteDependencyResolver', function() {
return depResolver.getDependencies('/root/index.js', { dev: false })
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
expect(result.dependencies[result.dependencies.length - 1]).toBe(module);
expect(_.pluck(Polyfill.mock.calls, 0)).toEqual([
{ path: 'polyfills/prelude.js',
id: 'polyfills/prelude.js',
isPolyfill: true,
@ -114,18 +121,12 @@ describe('HasteDependencyResolver', function() {
'polyfills/String.prototype.es6.js',
],
},
module
]);
});
});
pit('should get dependencies with polyfills', function() {
var module = createModule({
id: 'index',
path: '/root/index.js',
dependencies: ['a'],
});
var module = createModule('index');
var deps = [module];
var depResolver = new HasteDependencyResolver({
@ -144,75 +145,15 @@ describe('HasteDependencyResolver', function() {
return depResolver.getDependencies('/root/index.js', { dev: true })
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
{ path: 'polyfills/prelude_dev.js',
id: 'polyfills/prelude_dev.js',
isPolyfill: true,
dependencies: []
},
{ path: 'polyfills/require.js',
id: 'polyfills/require.js',
isPolyfill: true,
dependencies: ['polyfills/prelude_dev.js']
},
{ path: 'polyfills/polyfills.js',
id: 'polyfills/polyfills.js',
isPolyfill: true,
dependencies: ['polyfills/prelude_dev.js', 'polyfills/require.js']
},
{ id: 'polyfills/console.js',
isPolyfill: true,
path: 'polyfills/console.js',
dependencies: [
'polyfills/prelude_dev.js',
'polyfills/require.js',
'polyfills/polyfills.js'
],
},
{ id: 'polyfills/error-guard.js',
isPolyfill: true,
path: 'polyfills/error-guard.js',
dependencies: [
'polyfills/prelude_dev.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'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
]);
expect(depGraph.getOrderedDependencies).toBeCalledWith('/root/index.js');
expect(result.dependencies[0]).toBe(Polyfill.mock.instances[0]);
expect(result.dependencies[result.dependencies.length - 1])
.toBe(module);
});
});
pit('should pass in more polyfills', function() {
var module = createModule({
id: 'index',
path: '/root/index.js',
dependencies: ['a']
});
var module = createModule('index');
var deps = [module];
var depResolver = new HasteDependencyResolver({
@ -230,66 +171,9 @@ describe('HasteDependencyResolver', function() {
});
return depResolver.getDependencies('/root/index.js', { dev: false })
.then(function(result) {
.then((result) => {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
{ path: 'polyfills/prelude.js',
id: 'polyfills/prelude.js',
isPolyfill: true,
dependencies: []
},
{ path: 'polyfills/require.js',
id: 'polyfills/require.js',
isPolyfill: true,
dependencies: ['polyfills/prelude.js']
},
{ path: 'polyfills/polyfills.js',
id: 'polyfills/polyfills.js',
isPolyfill: true,
dependencies: ['polyfills/prelude.js', 'polyfills/require.js']
},
{ id: 'polyfills/console.js',
isPolyfill: true,
path: 'polyfills/console.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js'
],
},
{ id: 'polyfills/error-guard.js',
isPolyfill: true,
path: 'polyfills/error-guard.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'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',
],
},
expect(Polyfill.mock.calls[result.dependencies.length - 2]).toEqual([
{ path: 'some module',
id: 'some module',
isPolyfill: true,
@ -303,7 +187,6 @@ describe('HasteDependencyResolver', function() {
'polyfills/Array.prototype.es6.js'
]
},
module
]);
});
});
@ -462,22 +345,18 @@ describe('HasteDependencyResolver', function() {
depGraph.resolveDependency.mockImpl(function(fromModule, toModuleName) {
if (toModuleName === 'x') {
return Promise.resolve(createModule({
id: 'changed'
}));
return Promise.resolve(createModule('changed'));
} else if (toModuleName === 'y') {
return Promise.resolve(createModule({ id: 'Y' }));
return Promise.resolve(createModule('Y'));
}
return Promise.resolve(null);
});
return depResolver.wrapModule({
id: 'test module',
path: '/root/test.js',
dependencies: dependencies
}, code).then(processedCode => {
return depResolver.wrapModule(
createModule('test module', ['x', 'y']),
code
).then(processedCode => {
expect(processedCode).toEqual([
'__d(\'test module\',["changed","Y"],function(global, require,' +
' module, exports) { ' +

View File

@ -11,6 +11,7 @@
var path = require('path');
var DependencyGraph = require('./DependencyGraph');
var replacePatterns = require('./replacePatterns');
var Polyfill = require('./Polyfill');
var declareOpts = require('../lib/declareOpts');
var Promise = require('promise');
@ -83,25 +84,28 @@ HasteDependencyResolver.prototype.getDependencies = function(main, options) {
var depGraph = this._depGraph;
var self = this;
return depGraph
.load()
.then(() => Promise.all([
depGraph.getOrderedDependencies(main),
depGraph.getAsyncDependencies(main),
]))
.then(([dependencies, asyncDependencies]) => {
const mainModuleId = dependencies[0].id;
.then(
([dependencies, asyncDependencies]) => dependencies[0].getName().then(
mainModuleId => {
self._prependPolyfillDependencies(
dependencies,
opts.dev,
);
return {
mainModuleId: mainModuleId,
dependencies: dependencies,
asyncDependencies: asyncDependencies,
mainModuleId,
dependencies,
asyncDependencies,
};
}
)
);
};
@ -122,7 +126,7 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
].concat(this._polyfillModuleNames);
var polyfillModules = polyfillModuleNames.map(
(polyfillModuleName, idx) => ({
(polyfillModuleName, idx) => new Polyfill({
path: polyfillModuleName,
id: polyfillModuleName,
dependencies: polyfillModuleNames.slice(0, idx),
@ -134,23 +138,26 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
};
HasteDependencyResolver.prototype.wrapModule = function(module, code) {
if (module.isPolyfill) {
if (module.isPolyfill()) {
return Promise.resolve(code);
}
const resolvedDeps = Object.create(null);
const resolvedDepsArr = [];
return Promise.all(
module.dependencies.map(depName => {
return this._depGraph.resolveDependency(module, depName)
.then((dep) => dep && dep.getPlainObject().then(mod => {
if (mod) {
resolvedDeps[depName] = mod.id;
resolvedDepsArr.push(mod.id);
return module.getDependencies().then(
dependencies => Promise.all(dependencies.map(
depName => this._depGraph.resolveDependency(module, depName)
.then(depModule => {
if (depModule) {
return depModule.getName().then(name => {
resolvedDeps[depName] = name;
resolvedDepsArr.push(name);
});
}
}));
})
)
)
).then(() => {
const relativizeCode = (codeMatch, pre, quot, depName, post) => {
const depId = resolvedDeps[depName];
@ -161,13 +168,15 @@ HasteDependencyResolver.prototype.wrapModule = function(module, code) {
}
};
return defineModuleCode({
return module.getName().then(
name => defineModuleCode({
code: code
.replace(replacePatterns.IMPORT_RE, relativizeCode)
.replace(replacePatterns.REQUIRE_RE, relativizeCode),
deps: JSON.stringify(resolvedDepsArr),
moduleName: module.id,
});
moduleName: name,
})
);
});
};