From 5d4000886cac3ae6871b119d4d967ea05a5ac9ef Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Wed, 19 Nov 2014 09:11:18 -0700 Subject: [PATCH] #5 Fix bug related to de-duping and async loading --- lib/dependency-define.js | 12 ++++++++---- lib/dependency-require.js | 28 ++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/dependency-define.js b/lib/dependency-define.js index f0879ba..00f48f2 100644 --- a/lib/dependency-define.js +++ b/lib/dependency-define.js @@ -6,7 +6,7 @@ var streamToString = require('./util/streamToString'); var defineCode = require('raptor-modules/transport').defineCode.sync; var StringTransformer = require('./util/StringTransformer'); -function transformRequires(code, inspected, optimizerContext, callback) { +function transformRequires(code, inspected, asyncBlocks, optimizerContext, callback) { // We have two goals with this function: // 1) Comment out all non-JavaScript module requires // require('./test.css'); --> /*require('./test.css');*/ @@ -15,7 +15,6 @@ function transformRequires(code, inspected, optimizerContext, callback) { // In addition, we want to *maintain line numbers* for the transformed code to be nice! var stringTransformer = new StringTransformer(); - var asyncBlocks = inspected.asyncBlocks; function transformRequire(require) { ok(require.resolved, '"require.resolved" expected'); @@ -50,7 +49,10 @@ function transformRequires(code, inspected, optimizerContext, callback) { } } - asyncBlocks.forEach(transformAsyncCall); + if (asyncBlocks && asyncBlocks.length) { + asyncBlocks.forEach(transformAsyncCall); + } + inspected.allRequires.forEach(transformRequire); callback(null, stringTransformer.transform(code)); @@ -71,6 +73,8 @@ module.exports = { read: function(optimizerContext, callback) { var requireReader = this._requireReader; var requireInspected = this._requireInspected; + var requireAsyncBlocks = this._requireAsyncBlocks; + var isObject = this.object; var globals = this.globals; var path = this.path; @@ -85,7 +89,7 @@ module.exports = { if (isObject) { return callback(null, defineCode(path, code, { object: true })); } else { - transformRequires(code, requireInspected, optimizerContext, function(err, code) { + transformRequires(code, requireInspected, requireAsyncBlocks, optimizerContext, function(err, code) { if (err) { return callback(err); } diff --git a/lib/dependency-require.js b/lib/dependency-require.js index c0f7195..fb1c69b 100644 --- a/lib/dependency-require.js +++ b/lib/dependency-require.js @@ -13,26 +13,32 @@ var Deduper = require('./util/Deduper'); var CLIENT_OPTIMIZER_JSON_PATH = require.resolve('raptor-modules/client/optimizer.json'); -function buildAsyncMeta(path, asyncBlocks, optimizerContext) { +function buildAsyncInfo(path, asyncBlocks, optimizerContext) { if (asyncBlocks.length === 0) { return null; } var key = 'require-async|' + path; + + var asyncInfo = optimizerContext.data[key]; + if (!optimizerContext.data[key]) { - optimizerContext.data[key] = true; - var async = {}; + + var asyncMeta = {}; asyncBlocks.forEach(function(asyncBlock) { var uniqueId = optimizerContext.uniqueId(); var name = asyncBlock.name = '_' + uniqueId; - async[name] = asyncBlock.dependencies; + asyncMeta[name] = asyncBlock.dependencies; }); - return async; + asyncInfo = optimizerContext.data[key] = { + asyncMeta: asyncMeta, + asyncBlocks: asyncBlocks + }; } - return; + return asyncInfo; } function create(config, optimizer) { @@ -201,10 +207,14 @@ function create(config, optimizer) { var mainRequire; var requires; // the requires that were read from inspection (may remain undefined if no inspection result) var asyncMeta; - + var asyncBlocks; if (inspected && inspected.asyncBlocks.length) { - asyncMeta = buildAsyncMeta(resolved.filePath, inspected.asyncBlocks, optimizerContext); + var asyncInfo = buildAsyncInfo(resolved.filePath, inspected.asyncBlocks, optimizerContext); + if (asyncInfo) { + asyncBlocks = asyncInfo.asyncBlocks; + asyncMeta = asyncInfo.asyncMeta; + } } // Include client module system if needed and we haven't included it yet @@ -302,6 +312,8 @@ function create(config, optimizer) { // Pass along the reader and the lastModified to the def dependency defDependency._requireReader = inspected.reader; defDependency._requireInspected = inspected; + defDependency._requireAsyncBlocks = asyncBlocks; + defDependency._requireLastModified = inspected.lastModified; // Also check if the directory has an optimizer.json and if so we should include that as well