Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #72 from chaozhang/master

add AMD basic circular tests case, fix the way AMD handle dependencies issue
  • Loading branch information...
commit 4587048c52a596e0df893bcf81b6047132e1c10c 2 parents 55fc0c2 + bc891da
Jakob Heuser authored December 15, 2011
68  src/inject.coffee
@@ -63,13 +63,15 @@ functionRegex = /^[\s\(]*function[^(]*\(([^)]*)\)/
63 63
 functionNewlineRegex = /\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g
64 64
 functionSpaceRegex = /\s+/g
65 65
 requireRegex = null
  66
+defineStaticRequireRegex = null
66 67
 requireEnsureRegex = null
67 68
 commentRegex = null
68 69
 `
69 70
 // requireRegexes from Yabble - James Brantly
70 71
 requireRegex = /(?:^|[^\w\$_.])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
71 72
 // requireEnsureRegex = /(?:^|[^\w\$_.])require.ensure\s*\(\s*(\[("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'|\s*|,)*\])/g;
72  
-
  73
+// define static requirements
  74
+defineStaticRequireRegex = /^[\r\n\s]*define\(\s*("\S+",|'\S+',|\s*)\s*\[([^\]]*)\],\s*(function\s*\(|{).+/;
73 75
 // commentRegex from RequireJS
74 76
 commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
75 77
 `
@@ -128,7 +130,6 @@ db = {
128 130
           "executed": false
129 131
           "rulesApplied": false
130 132
           "requires": []
131  
-          "staticRequires": []
132 133
           "exec": null
133 134
           "pointcuts":
134 135
             "before": []
@@ -187,21 +188,6 @@ db = {
187 188
       registry = _db.moduleRegistry
188 189
       db.module.create(moduleId)
189 190
       registry[moduleId].requires = requires
190  
-    "getStaticRequires": (moduleId) ->
191  
-      ###
192  
-      ## getStaticRequires(moduleId) ##
193  
-      get the requires for a given moduleId found at declaration time (static dependencies)
194  
-      ###
195  
-      registry = _db.moduleRegistry
196  
-      if registry[moduleId]?.staticRequires then return registry[moduleId].staticRequires
197  
-    "setStaticRequires": (moduleId, staticRequires) ->
198  
-      ###
199  
-      ## setStaticRequires(moduleId, staticRequires) ##
200  
-      set the staticRequires for moduleId, found at declaration time
201  
-      ###
202  
-      registry = _db.moduleRegistry
203  
-      db.module.create(moduleId)
204  
-      registry[moduleId].staticRequires = staticRequires
205 191
     "getRulesApplied": (moduleId) ->
206 192
       ###
207 193
       ## getRulesApplied(moduleId) ##
@@ -820,7 +806,7 @@ processCallbacks = (status, moduleId, file) ->
820 806
   db.queue.file.clear(moduleId)
821 807
   cb(moduleId, file) for cb in cbs
822 808
 
823  
-extractRequires = (file, staticReqs = []) ->
  809
+extractRequires = (file) ->
824 810
   requires = []
825 811
   uniques = {}
826 812
   require = (item) ->
@@ -836,11 +822,16 @@ extractRequires = (file, staticReqs = []) ->
836 822
     catch err
837 823
       console?.log "Invalid require() syntax found in file: " + reqs.join(";")
838 824
       throw err
839  
-  
  825
+
  826
+  # get static requirements
  827
+  staticReqs = []
  828
+  if defineStaticRequireRegex.exec(file)
  829
+    staticReqs = defineStaticRequireRegex.exec(file)[2].replace(/\s|"|'|require|exports|module/g,'').split(',');
  830
+
840 831
   for staticReq in staticReqs
841  
-    requires.push(staticReq) if uniques[staticReq] isnt true
  832
+    requires.push(staticReq) if uniques[staticReq] isnt true and staticReq isnt ''
842 833
     uniques[staticReq] = true
843  
-  
  834
+
844 835
   return requires
845 836
 
846 837
 analyzeFile = (moduleId, tree) ->
@@ -848,7 +839,7 @@ analyzeFile = (moduleId, tree) ->
848 839
   ## analyzeFile(moduleId) ##
849 840
   _internal_ scan a module's file for dependencies and record them
850 841
   ###
851  
-  reqs = extractRequires(db.module.getFile(moduleId), db.module.getStaticRequires(moduleId))
  842
+  reqs = extractRequires(db.module.getFile(moduleId))
852 843
   
853 844
   # #65 remove circular depenencies before handling requires
854 845
   unsafeRequires = {}
@@ -916,8 +907,6 @@ executeFile = (moduleId) ->
916 907
   # right now, we're leaving this as recursive
917 908
   for requiredModuleId in db.module.getRequires(moduleId)
918 909
     executeFile(requiredModuleId)
919  
-  for requiredModuleId in db.module.getStaticRequires(moduleId)
920  
-    executeFile(requiredModuleId)
921 910
 
922 911
   cuts = getFormattedPointcuts(moduleId)
923 912
   path = db.module.getPath(moduleId)
@@ -1191,27 +1180,8 @@ define = (moduleId, deps, callback) ->
1191 1180
 
1192 1181
   db.module.setAmd(moduleId, true)
1193 1182
   db.module.setLoading(moduleId, true)
1194  
-  
1195  
-  # if we have a callback, scan it for dependencies
1196  
-  inFunctionDeps = if typeof(callback) is "function" then extractRequires(Function.prototype.toString.call(callback)) else []
1197  
-  
1198  
-  # get dependencies from the array provided, remove "exports", "require", and "module" as they are not
1199  
-  # true dependencies
1200  
-  strippedDeps = []
1201  
-  uniqueDeps = {}
1202  
-  for dep in deps
1203  
-    if dep isnt "exports" and dep isnt "require" and dep isnt "module" and !uniqueDeps[dep]
1204  
-      strippedDeps.push(dep)
1205  
-      uniqueDeps[dep] = true
1206  
-  db.module.setStaticRequires(moduleId, strippedDeps)
1207  
-  
1208  
-  # combine the static dependencies with runtime dependencies, as one giant request
1209  
-  allDeps = strippedDeps
1210  
-  for dep in inFunctionDeps
1211  
-    if dep isnt "exports" and dep isnt "require" and dep isnt "module" and !uniqueDeps[dep]
1212  
-      allDeps.push(dep)
1213  
-      uniqueDeps[dep] = true
1214  
-  
  1183
+  allDeps = db.module.getRequires(moduleId);
  1184
+
1215 1185
   # request all dependencies via loadModules with a callback. We do not care about order here
1216 1186
   afterLoadModules = () ->
1217 1187
     # run the callback if it is a function
@@ -1244,19 +1214,13 @@ define = (moduleId, deps, callback) ->
1244 1214
     db.queue.amd.clear(moduleId)
1245 1215
     
1246 1216
   outstandingAMDModules = 0
1247  
-  noCircularDeps = []
1248 1217
   for depId in allDeps
1249 1218
     if db.module.getAmd(depId) and db.module.getLoading(depId)
1250 1219
       outstandingAMDModules++
1251 1220
       db.queue.amd.add depId, () -> 
1252 1221
         if --outstandingAMDModules is 0
1253 1222
           afterLoadModules()
1254  
-    if db.module.getCircular(depId) is false
1255  
-      noCircularDeps.push depId
1256  
-  if db.module.getCircular(moduleId)
1257  
-    loadModules noCircularDeps, afterLoadModules
1258  
-  else
1259  
-    loadModules allDeps, afterLoadModules
  1223
+  loadModules allDeps, afterLoadModules
1260 1224
 
1261 1225
 # To allow a clear indicator that a global define function conforms to the AMD API
1262 1226
 define['amd'] =
22  tests/amd/amd.js
@@ -12,7 +12,7 @@ module("AMD Specification", {
12 12
   }
13 13
 });
14 14
 
15  
-asyncTest("Basic", 3, function() {
  15
+asyncTest("Basic - simple tests", 3, function() {
16 16
   require.setModuleRoot("http://localhost:4000/tests/amd/includes/spec/basic");
17 17
   require.ensure(["a","b"], function(require) {
18 18
     var a = require("a"),
@@ -25,6 +25,26 @@ asyncTest("Basic", 3, function() {
25 25
   });
26 26
 });
27 27
 
  28
+asyncTest("Basic - circular tests", 6, function() {
  29
+  require.setModuleRoot("http://localhost:4000/tests/amd/includes/spec/basic");
  30
+  require.ensure(["two", "funcTwo", "funcThree"], function(require) {
  31
+    var two = require("two"),
  32
+    funcTwo = require("funcTwo"),
  33
+    funcThree = require("funcThree"),
  34
+    args = two.doSomething(),
  35
+    twoInst = new funcTwo("TWO"),
  36
+    oneMod = two.getOneModule();
  37
+
  38
+    equal("small", args.size);
  39
+    equal("redtwo", args.color);
  40
+    equal("one", oneMod.id);
  41
+    equal("TWO", twoInst.name);
  42
+    equal("ONE-NESTED", twoInst.oneName());
  43
+    equal("THREE-THREE_SUFFIX", funcThree("THREE"));
  44
+    start();
  45
+  });
  46
+});
  47
+
28 48
 asyncTest("Function String", 8, function() {
29 49
   require.setModuleRoot("http://localhost:4000/tests/amd/includes/spec/funcstring");
30 50
   require.ensure(["one", "two", "three"], function(require) {
11  tests/amd/includes/spec/basic/funcFour.js
... ...
@@ -0,0 +1,11 @@
  1
+define("funcFour", ["require", "funcThree"], function (require) {
  2
+  var four = function (arg) {
  3
+    return "FOUR called with " + arg;
  4
+  };
  5
+
  6
+  four.suffix = function () {
  7
+    return require("funcThree").suffix();
  8
+  };
  9
+
  10
+  return four;
  11
+});
12  tests/amd/includes/spec/basic/funcOne.js
... ...
@@ -0,0 +1,12 @@
  1
+define("funcOne", ["require", "funcTwo"], function (require) {
  2
+  var one = function (name) {
  3
+    this.name = name;
  4
+  };
  5
+  one.prototype.getName = function () {
  6
+    var funcTwo = require("funcTwo");
  7
+    var inst = new funcTwo("-NESTED");
  8
+    return this.name + inst.name;
  9
+  };
  10
+
  11
+  return one;
  12
+});
11  tests/amd/includes/spec/basic/funcThree.js
... ...
@@ -0,0 +1,11 @@
  1
+define("funcThree", ["require", "funcFour"], function (require, four) {
  2
+  var three = function (arg) {
  3
+    return arg + "-" + require("funcFour").suffix();
  4
+  };
  5
+
  6
+  three.suffix = function () {
  7
+    return "THREE_SUFFIX";
  8
+  };
  9
+
  10
+  return three;
  11
+});
13  tests/amd/includes/spec/basic/funcTwo.js
... ...
@@ -0,0 +1,13 @@
  1
+define("funcTwo", ["require", "funcOne"], function (require) {
  2
+  var funcOne = require('funcOne');
  3
+  var two = function (name) {
  4
+    this.name = name;
  5
+    this.one = new funcOne("ONE");
  6
+  };
  7
+
  8
+  two.prototype.oneName = function () {
  9
+    return this.one.getName();
  10
+  };
  11
+
  12
+  return two;
  13
+});
7  tests/amd/includes/spec/basic/one.js
... ...
@@ -0,0 +1,7 @@
  1
+define("one", ["require", "exports", "module", "two"], function(require, exports, module) {
  2
+  exports.size = "large";
  3
+  exports.module = module;
  4
+  exports.doSomething = function() {
  5
+    return require("two");
  6
+  };
  7
+});
12  tests/amd/includes/spec/basic/two.js
... ...
@@ -0,0 +1,12 @@
  1
+define("two", ["require", "one"], function(require, one) {
  2
+  return {
  3
+    size: "small",
  4
+    color: "redtwo",
  5
+    doSomething: function() {
  6
+      return one.doSomething();
  7
+    },
  8
+    getOneModule: function() {
  9
+      return one.module;
  10
+    }
  11
+  };
  12
+});

0 notes on commit 4587048

Please sign in to comment.
Something went wrong with that request. Please try again.