updates to enhanced-require, optimizing amd

pull/33/head
Tobias Koppers 10 years ago
parent 3d08e8cf14
commit 638e82254a
  1. 2
      README.md
  2. 5
      buildin/__webpack_amd_define.js
  3. 33
      buildin/__webpack_amd_require.js
  4. 8
      lib/parse.js
  5. 23
      test/browsertest/lib/index.web.js
  6. 2
      test/polyfills.js

@ -238,7 +238,7 @@ var b = require("./b"); // and files
// like in node.js
// polyfill require method to use the new members in node.js too
require = require("enhanced-require")(__filename, require.valueOf());
require = require("enhanced-require")(module);
// create a lazy loaded bundle
require.ensure([], function(require) {

@ -1,6 +1,5 @@
var amdRequire = require("./__webpack_amd_require");
module.exports = function(module, req) {
req = amdRequire(req);
module.exports = function(module) {
function define(id, dependencies, factory) {
if(typeof id != "number") {
factory = dependencies;
@ -9,7 +8,7 @@ module.exports = function(module, req) {
}
if(!factory) {
factory = dependencies;
dependencies = [req, module.exports, module];
dependencies = [amdRequire, module.exports, module];
}
var result = typeof factory == "function" ? factory.apply(null, dependencies) : factory;
if(result !== undefined)

@ -1,20 +1,17 @@
function amdRequireFactory(req) {
function amdRequire(chunk, requiresFn, fn) {
if(!requiresFn) {
// commonjs
return req(chunk);
}
req.e(chunk, function() {
var modules = requiresFn();
if(fn)
return fn.apply(null, modules);
});
var req = require.valueOf();
function amdRequire(chunk, requiresFn, fn) {
if(!requiresFn) {
// commonjs
return req(chunk);
}
for(var name in req)
amdRequire[name] = req[name];
amdRequire.amd = amdRequireFactory.amd;
amdRequire.config = function() {/* config is ignored, use webpack options */};
return amdRequire;
req.e(chunk, function() {
var modules = requiresFn();
if(fn)
return fn.apply(null, modules);
});
}
amdRequireFactory.amd = {};
module.exports = amdRequireFactory;
for(var name in req)
amdRequire[name] = req[name];
amdRequire.amd = {};
amdRequire.config = function() {/* config is ignored, use webpack options */};
module.exports = amdRequire;

@ -259,7 +259,6 @@ function walkExpression(context, expression) {
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"
@ -277,7 +276,6 @@ function walkExpression(context, expression) {
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"
@ -326,7 +324,7 @@ function walkExpression(context, expression) {
context.requires = context.requires || [];
context.requires.push({
name: "__webpack_amd_define",
append: "(module,require)",
append: "(module)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
amdNameRange: amdNameRange,
@ -343,7 +341,7 @@ function walkExpression(context, expression) {
expression.callee.name === "define") {
context.requires.push({
name: "__webpack_amd_define",
append: "(module,require)",
append: "(module)",
line: expression.callee.loc.start.line,
column: expression.callee.loc.start.column,
variable: "define"
@ -363,7 +361,7 @@ function walkExpression(context, expression) {
context.requires = context.requires || [];
context.requires.push({
name: "__webpack_amd_define",
append: "(module,require)",
append: "(module)",
amdNameRange: expression.arguments[0].range,
label: expression.arguments[0].value+"",
line: expression.callee.loc.start.line,

@ -1,5 +1,6 @@
// Should not break it... should not include complete directory...
require = require("../../../require-polyfill")(require.valueOf());
require = require("enhanced-require")(module);
if(typeof define != "function") var define = require.define;
window.test(true, "index.js should be replaced with index.web.js");
require("script!../js/libary1.js");
@ -161,26 +162,26 @@ window.test(require("./singluar2") !== singlarObj, "require.cache can be deleted
// AMD
var template = "tmpl";
var amdLoaded = 0;
var amdLoaded = "";
require(["./circular", "../templates/" + template, true ? "./circular" : "./circular"], function(circular, testTemplate, circular2) {
window.test(circular === 1, "AMD-style requires should work");
window.test(circular2 === 1, "AMD-style requires should work with conditionals");
window.test(testTemplate === "test template", "AMD-style requires should work with context");
amdLoaded++;
amdLoaded+=1;
});
define("name", ["./circular"], function(circular) {
window.test(circular === 1, "AMD-style requires should work, in define");
amdLoaded++;
amdLoaded+=2;
});
define("name", [], function() {
amdLoaded++;
amdLoaded+=3;
});
define(["./circular"], function(circular) {
window.test(circular === 1, "AMD-style requires should work, in define without name");
amdLoaded++;
amdLoaded+=4;
});
define("blaa", function() {
amdLoaded++;
amdLoaded+=5;
});
var obj = {};
define("blaaa", obj);
@ -193,17 +194,17 @@ define(function(require, exports, module) {
window.test(exports == _test_exports, "AMD-style define CommonJs: exports is correct");
window.test(module == _test_module, "AMD-style define CommonJs: module is correct");
window.test(require("./circular") === 1, "AMD-style requires should work, in define without name and requires");
amdLoaded++;
amdLoaded+=6;
});
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++;
amdLoaded+=7;
});
window.test(amdLoaded == 6, "AMD-style require should work (sync)");
window.test(amdLoaded == "123456", "AMD-style require should work (sync) "+amdLoaded);
setTimeout(function() {
window.test(amdLoaded == 7, "AMD-style require should work (async)");
window.test(amdLoaded == "1234567", "AMD-style require should work (async) " + amdLoaded);
}, 1500);
// cross module system support

@ -4,7 +4,7 @@
*/
var should = require("should");
var path = require("path");
require = require("enhanced-require")(__filename);
require = require("enhanced-require")(module);
describe("polyfill", function() {
describe("loader", function() {

Loading…
Cancel
Save