Skip to content

Commit

Permalink
Merge pull request #32 from danwrong/scripts_should_trigger_bundle_loads
Browse files Browse the repository at this point in the history
Fix bundle loading for deferred scripts
  • Loading branch information
kennethkufluk committed Apr 7, 2016
2 parents 56cc68b + 8fb0354 commit e63094a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loadrunner",
"version": "0.1.1",
"version": "0.1.2",
"description": "Simple, flexible and sane JavaScript loader and build tool for browsers.",
"contributors": [{ "name": "Dan Webb", "email": "dan@danwebb.net" }],
"homepage": "https://github.com/danwrong/loadrunner",
Expand Down
37 changes: 21 additions & 16 deletions src/loadrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,12 @@
this.id = this.path = this.resolvePath(path);
}

this.originalPath = path;

this.force = !!force;
}
Script.autoFetch = true;

Script.prototype.start = function() {
var me = this, bundle;

if (bundle = findBundle(this.id)) {
bundle.then(function() {
me.start();
});
} else {
Dependency.prototype.start.call(this);
}
};

Script.xhrTransport = function() {
var xhr;

Expand Down Expand Up @@ -225,6 +215,22 @@
}

Script.prototype = new Dependency;

Script.prototype.start = function() {
var me = this, bundle;
if (def = Definition.provided[this.originalPath]) {
def.then(function() {
me.complete();
});
} else if (bundle = findBundle(this.originalPath)) {
bundle.then(function() {
me.start();
});
} else {
Dependency.prototype.start.call(this);
}
};

Script.prototype.resolvePath = function(filePath) {
filePath = filePath.replace(/^\$/, using.path.replace(/\/$/, '') + '/');
return filePath;
Expand All @@ -238,7 +244,7 @@
Script.prototype.fetch = Script.scriptTagTransport;
Script.prototype.loaded = function() {
this.complete();
}
};

function Module(id, force) {
this.id = id;
Expand Down Expand Up @@ -489,8 +495,8 @@
this.entries[alias].push(b);
}
}
};

}
Manifest.prototype.get = function(key) {
if (typeof this.entries[key] == 'undefined') {
return null;
Expand All @@ -503,8 +509,7 @@
}

return this.entries[key][0];
}

};

function interactiveScript() {
for (var i in scripts) {
Expand Down
26 changes: 26 additions & 0 deletions test/deferred_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ <h2 id="qunit-userAgent"></h2>
});
});
});

test('should be able load deferred scripts from a bundle', function() {
expect(2);
stop(2000);
loadrunner.reset();
window.bundleLoadedThing = false;
window.bundleLoadedAnother = false;
using.bundles.push({ 'javascripts/deferred_bundle.js': ['bundled_thing.js', 'bundled_another.js'] });
using('bundled_thing.js', function() {
equals(true, window.bundleLoadedThing);
equals(false, window.bundleLoadedAnother);
start();
});
});

test('should be able load modpath deferred scripts from a bundle', function() {
expect(1);
stop(2000);
loadrunner.reset();
window.bundleLoadedModpath = false;
using.bundles.push({ 'javascripts/deferred_bundle.js': ['$bundled_with_modpath.js'] });
using('$bundled_with_modpath.js', function() {
equals(true, window.bundleLoadedModpath);
start();
});
});
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions test/javascripts/deferred_bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
deferred('bundled_thing.js', function() {
window.bundleLoadedThing = true;
});

deferred('bundled_another.js', function() {
window.bundleLoadedAnother = true;
});

deferred('$bundled_with_modpath.js', function() {
window.bundleLoadedModpath = true;
});

0 comments on commit e63094a

Please sign in to comment.