From 201d65dead480701d9d401bfc8bc7065843d5939 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Mon, 20 Apr 2015 15:54:24 -0700 Subject: [PATCH] [react-packager] Implement Packager::getAssets --- react-packager/src/Packager/Package.js | 11 +++++++ .../src/Packager/__tests__/Package-test.js | 12 +++++++ .../src/Packager/__tests__/Packager-test.js | 8 +++++ react-packager/src/Packager/index.js | 33 ++++++++++--------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/react-packager/src/Packager/Package.js b/react-packager/src/Packager/Package.js index 0f55c8ed..67e31e47 100644 --- a/react-packager/src/Packager/Package.js +++ b/react-packager/src/Packager/Package.js @@ -17,6 +17,7 @@ module.exports = Package; function Package(sourceMapUrl) { this._finalized = false; this._modules = []; + this._assets = []; 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) { options = options || {}; if (options.runMainModule) { @@ -49,6 +54,8 @@ Package.prototype.finalize = function(options) { Object.freeze(this._modules); Object.seal(this._modules); + Object.freeze(this._assets); + Object.seal(this._assets); this._finalized = true; }; @@ -146,6 +153,10 @@ Package.prototype.getSourceMap = function(options) { return map; }; +Package.prototype.getAssets = function() { + return this._assets; +}; + Package.prototype._getMappings = function() { var modules = this._modules; diff --git a/react-packager/src/Packager/__tests__/Package-test.js b/react-packager/src/Packager/__tests__/Package-test.js index 5a7438d2..db596a7b 100644 --- a/react-packager/src/Packager/__tests__/Package-test.js +++ b/react-packager/src/Packager/__tests__/Package-test.js @@ -76,6 +76,18 @@ describe('Package', function() { 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) { diff --git a/react-packager/src/Packager/__tests__/Packager-test.js b/react-packager/src/Packager/__tests__/Packager-test.js index 333e0f56..c1773216 100644 --- a/react-packager/src/Packager/__tests__/Packager-test.js +++ b/react-packager/src/Packager/__tests__/Packager-test.js @@ -140,6 +140,14 @@ describe('Packager', function() { expect(p.finalize.mock.calls[0]).toEqual([ {runMainModule: true} ]); + + expect(p.addAsset.mock.calls[0]).toEqual([ + imgModule_DEPRECATED + ]); + + expect(p.addAsset.mock.calls[1]).toEqual([ + imgModule + ]); }); }); diff --git a/react-packager/src/Packager/index.js b/react-packager/src/Packager/index.js index 2b1eb6b1..aab55c08 100644 --- a/react-packager/src/Packager/index.js +++ b/react-packager/src/Packager/index.js @@ -100,9 +100,9 @@ Packager.prototype.kill = function() { }; Packager.prototype.package = function(main, runModule, sourceMapUrl, isDev) { - var transformModule = this._transformModule.bind(this); var ppackage = new Package(sourceMapUrl); + var transformModule = this._transformModule.bind(this, ppackage); var findEventId = Activity.startEvent('find dependencies'); var transformEventId; @@ -140,16 +140,13 @@ Packager.prototype.getDependencies = function(main, isDev) { return this._resolver.getDependencies(main, { dev: isDev }); }; -Packager.prototype._transformModule = function(module) { +Packager.prototype._transformModule = function(ppackage, module) { var transform; if (module.isAsset_DEPRECATED) { - transform = generateAssetModule_DEPRECATED(module); + transform = this.generateAssetModule_DEPRECATED(ppackage, module); } else if (module.isAsset) { - transform = generateAssetModule( - module, - getPathRelativeToRoot(this._projectRoots, module.path) - ); + transform = this.generateAssetModule(ppackage, module); } else { transform = this._transformer.loadFileAndTransform( path.resolve(module.path) @@ -166,17 +163,11 @@ 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() { return this._resolver.getDebugInfo(); }; -function generateAssetModule_DEPRECATED(module) { +Packager.prototype.generateAssetModule_DEPRECATED = function(ppackage, module) { return sizeOf(module.path).then(function(dimensions) { var img = { isStatic: true, @@ -187,6 +178,7 @@ function generateAssetModule_DEPRECATED(module) { deprecated: true, }; + ppackage.addAsset(img); var code = 'module.exports = ' + JSON.stringify(img) + ';'; @@ -196,9 +188,11 @@ function generateAssetModule_DEPRECATED(module) { sourcePath: module.path, }; }); -} +}; + +Packager.prototype.generateAssetModule = function(ppackage, module) { + var relPath = getPathRelativeToRoot(this._projectRoots, module.path); -function generateAssetModule(module, relPath) { return sizeOf(module.path).then(function(dimensions) { var img = { isStatic: true, @@ -208,6 +202,8 @@ function generateAssetModule(module, relPath) { height: dimensions.height / module.resolution, }; + ppackage.addAsset(img); + var code = 'module.exports = ' + JSON.stringify(img) + ';'; return { @@ -231,4 +227,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;