Skip to content

Commit

Permalink
#5 Fix bug related to de-duping and async loading
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Nov 19, 2014
1 parent 2b1197e commit 5d40008
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
12 changes: 8 additions & 4 deletions lib/dependency-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');*/
Expand All @@ -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');
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
28 changes: 20 additions & 8 deletions lib/dependency-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d40008

Please sign in to comment.