Skip to content

Commit

Permalink
amd require is now async in webpack too
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 7, 2012
1 parent 83b1b95 commit b26910f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
17 changes: 11 additions & 6 deletions buildin/__webpack_amd_require.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module.exports = function amdRequireFactory(req) {
function amdRequire(requires, fn) {
if(!fn) {
function amdRequireFactory(req) {
function amdRequire(chunk, requiresFn, fn) {
if(!requiresFn) {
// commonjs
return req(requires);
return req(chunk);
}
return fn.apply(null, requires);
req.e(chunk, function() {
var modules = requiresFn();
if(fn)
return fn.apply(null, modules);
});
}
for(var name in req)
amdRequire[name] = req[name];
amdRequire.amd = amdRequireFactory.amd;
return amdRequire;
}
amdRequireFactory.amd = {};
amdRequireFactory.amd = {};
module.exports = amdRequireFactory;
77 changes: 54 additions & 23 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,35 +239,65 @@ function walkExpression(context, expression) {
});
}
// AMD require
if(expression.callee && expression.arguments &&
if(context.overwrite.indexOf("require") === -1 &&
expression.callee && expression.arguments &&
expression.arguments.length >= 1 &&
expression.arguments.length <= 2 &&
expression.callee.type === "Identifier" &&
expression.callee.name === "require" &&
expression.arguments[0].type === "ArrayExpression") {
context.requires = context.requires || [];
context.requires.push({
name: "__webpack_amd_require",
append: "(require)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
variable: "require"
});
var newContext = {
requires: [],
amdRange: expression.arguments[0].range,
line: expression.loc.start.line,
column: expression.loc.start.column,
ignoreOverride: true,
overwrite: context.overwrite.slice(),
options: context.options
};
if(expression.arguments.length >= 2 &&
expression.arguments[1].type === "FunctionExpression" &&
expression.arguments[1].body &&
expression.arguments[1].body.type === "BlockStatement" &&
expression.arguments[1].body.range)
newContext.blockRange = [
expression.arguments[1].body.range[0]+1,
expression.arguments[1].body.range[1]-1
];
context.asyncs = context.asyncs || [];
context.asyncs.push(newContext);
context = newContext;
var params = parseCalculatedStringArray(expression.arguments[0]);
var elements = expression.arguments[0].elements;
processAmdArray(params, elements);
noCallee = true;
}
// AMD define
if(context.overwrite.indexOf("define") === -1 &&
expression.callee && expression.arguments &&
expression.arguments.length == 2 &&
expression.callee.type === "Identifier" &&
((context.overwrite.indexOf("require") === -1 &&
expression.callee.name === "require") ||
(context.overwrite.indexOf("define") === -1 &&
expression.callee.name === "define"))) {
expression.callee.name === "define") {
var params = parseCalculatedStringArray(expression.arguments[0]);
var elements = expression.arguments[0].type == "ArrayExpression" ?
expression.arguments[0].elements : [expression.arguments[0]];
processAmdArray(params, elements);
context.requires = context.requires || [];
if(expression.callee.name === "define") {
context.requires.push({
name: "__webpack_amd_define",
append: "(module,require)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
variable: "define"
});
} else {
context.requires.push({
name: "__webpack_amd_require",
append: "(require)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
variable: "require"
});
}
context.requires.push({
name: "__webpack_amd_define",
append: "(module,require)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
variable: "define"
});
noCallee = true;
}
// AMD define
Expand Down Expand Up @@ -312,7 +342,8 @@ function walkExpression(context, expression) {
expression.callee && expression.arguments &&
expression.arguments.length == 1 &&
expression.callee.type === "Identifier" &&
expression.callee.name === "require") {
expression.callee.name === "require" &&
expression.arguments[0].type !== "ArrayExpression") {
// "require(...)"
var param = parseCalculatedString(expression.arguments[0]);
if(param.conditional) {
Expand Down
14 changes: 14 additions & 0 deletions lib/writeSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
if(asyncItem.contexts) {
asyncItem.contexts.forEach(genContextReplaces);
}
// commonjs
if(asyncItem.namesRange) {
replaces.push({
from: asyncItem.namesRange[0],
Expand All @@ -169,6 +170,19 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
value: "e"
});
}
// amd
if(asyncItem.amdRange) {
replaces.push({
from: asyncItem.amdRange[0],
to: asyncItem.amdRange[0]-1,
value: ((asyncItem.chunkId && toRealChuckId(asyncItem.chunkId) || "0") + "") + ", function() { return "
});
replaces.push({
from: asyncItem.amdRange[1]+1,
to: asyncItem.amdRange[1],
value: "}"
});
}
if(asyncItem.blockRange) {
genReplacesFreeVars(asyncItem.blockRange, freeVars);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack",
"version": "0.5.3",
"version": "0.5.4",
"author": "Tobias Koppers @sokra",
"description": "Packs CommonJs/AMD Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loading of js, json, jade, coffee, css, ... out of the box and more with custom loaders.",
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions test/browsertest/lib/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "c";
1 change: 1 addition & 0 deletions test/browsertest/lib/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "d";
9 changes: 9 additions & 0 deletions test/browsertest/lib/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ define(function(require) {
window.test(require("./circular") === 1, "AMD-style requires should work, in define without name and requires");
amdLoaded++;
});
require(["./circular"]);
require(["./c"], function(c) {
window.test(c === "c", "AMD-style require should work, in chunk");
window.test(require("./d") === "d", "AMD-style require should work, in chunk");
amdLoaded++;
});
window.test(amdLoaded == 5, "AMD-style require should work (sync)");
setTimeout(function() {
window.test(amdLoaded == 6, "AMD-style require should work (async)");
}, 1500);

// cross module system support
window.test(typeof require === "function", "require should be a function");
Expand Down

0 comments on commit b26910f

Please sign in to comment.