JustinLove / Module
Homebrew Javascript require mechanism. Still a work in progress.
Clone this repository (size: 175.2 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/JustinLove/module/
| commit 206: | b78712a46b9e |
| parent 205: | 5733e5f2a035 |
| branch: | default |
rename Dependency attributes to id/uri
Changed (Δ326 bytes):
raw changeset »
qunit/index.html (16 lines added, 16 lines removed)
script/cgd/Module.js (14 lines added, 14 lines removed)
Up to file-list qunit/index.html:
| … | … | @@ -39,25 +39,25 @@ test("Can construct a dependency", funct |
39 |
39 |
ok(new CGD.Dependency('pass')); |
40 |
40 |
}); |
41 |
41 |
|
42 |
test("has id |
|
42 |
test("has id property", function() |
|
43 |
43 |
{ |
44 |
ok(new CGD.Dependency('pass').id |
|
44 |
ok(new CGD.Dependency('pass').id); |
|
45 |
45 |
}); |
46 |
46 |
|
47 |
test("has |
|
47 |
test("has uri property", function() |
|
48 |
48 |
{ |
49 |
ok(new CGD.Dependency('pass'). |
|
49 |
ok(new CGD.Dependency('pass').uri); |
|
50 |
50 |
}); |
51 |
51 |
|
52 |
52 |
test("Can construct a dependency with a full path", function() { |
53 |
equals(new CGD.Dependency('pass').canonically('full/pass.js'). |
|
53 |
equals(new CGD.Dependency('pass').canonically('full/pass.js').uri, 'full/pass.js'); |
|
54 |
54 |
}); |
55 |
55 |
|
56 |
56 |
test("registers itself", function() { |
57 |
57 |
var files = {}; |
58 |
58 |
var x = new CGD.Dependency('x').register(files); |
59 |
equals(files[x.identifier], x, 'identifier is registered'); |
|
60 |
equals(files[x.canonicalPath], x, 'canonicalPath is registered'); |
|
59 |
equals(files[x.id], x, 'id is registered'); |
|
60 |
equals(files[x.uri], x, 'uri is registered'); |
|
61 |
61 |
}); |
62 |
62 |
|
63 |
63 |
test("can track whether it's loaded", function() { |
| … | … | @@ -83,8 +83,8 @@ test("defaults to script type", function |
83 |
83 |
}); |
84 |
84 |
|
85 |
85 |
test("infer file extension", function() { |
86 |
equals(extension(new CGD.Dependency('type', 'text/javascript').canonicalPath), 'js'); |
|
87 |
equals(extension(new CGD.Dependency('type', 'text/css').canonicalPath), 'css'); |
|
86 |
equals(extension(new CGD.Dependency('type', 'text/javascript').uri), 'js'); |
|
87 |
equals(extension(new CGD.Dependency('type', 'text/css').uri), 'css'); |
|
88 |
88 |
}); |
89 |
89 |
|
90 |
90 |
test("makes matching element", function() { |
| … | … | @@ -98,10 +98,10 @@ test("includes a file", function() { |
98 |
98 |
setTimeout(start, CGD.test.timeout); |
99 |
99 |
}); |
100 |
100 |
|
101 |
test("improves |
|
101 |
test("improves uri", function() { |
|
102 |
102 |
file = new CGD.Dependency('subdir/../el'); |
103 |
103 |
file.element(); |
104 |
ok(file. |
|
104 |
ok(file.uri.match('qunit/el'), file.uri); |
|
105 |
105 |
}); |
106 |
106 |
|
107 |
107 |
QUnit.module("Module"); |
| … | … | @@ -130,7 +130,7 @@ test("includes a file twice", function() |
130 |
130 |
|
131 |
131 |
test("detects static script files", function() { |
132 |
132 |
for (var i in CGD.mod.files) { |
133 |
if (CGD.mod.files[i]. |
|
133 |
if (CGD.mod.files[i].uri.match(/qunit.js/)) { |
|
134 |
134 |
ok(true, 'found static file'); |
135 |
135 |
return; |
136 |
136 |
} |
| … | … | @@ -140,7 +140,7 @@ test("detects static script files", func |
140 |
140 |
|
141 |
141 |
test("has the proper type for static script files", function() { |
142 |
142 |
for (var i in CGD.mod.files) { |
143 |
if (CGD.mod.files[i]. |
|
143 |
if (CGD.mod.files[i].uri.match(/qunit.js/)) { |
|
144 |
144 |
equals(CGD.mod.files[i].type, 'text/javascript'); |
145 |
145 |
return; |
146 |
146 |
} |
| … | … | @@ -149,7 +149,7 @@ test("has the proper type for static scr |
149 |
149 |
|
150 |
150 |
test("detects static style files", function() { |
151 |
151 |
for (var i in CGD.mod.files) { |
152 |
if (CGD.mod.files[i]. |
|
152 |
if (CGD.mod.files[i].uri.match(/qunit.css/)) { |
|
153 |
153 |
ok(true, 'found static file'); |
154 |
154 |
return; |
155 |
155 |
} |
| … | … | @@ -159,7 +159,7 @@ test("detects static style files", funct |
159 |
159 |
|
160 |
160 |
test("has the proper type for static style files", function() { |
161 |
161 |
for (var i in CGD.mod.files) { |
162 |
if (CGD.mod.files[i]. |
|
162 |
if (CGD.mod.files[i].uri.match(/qunit.css/)) { |
|
163 |
163 |
equals(CGD.mod.files[i].type, 'text/css'); |
164 |
164 |
return; |
165 |
165 |
} |
| … | … | @@ -179,7 +179,7 @@ test("fileFromIdentifier gets proper fil |
179 |
179 |
}); |
180 |
180 |
|
181 |
181 |
test("fileFromIdentifier gets a file with proper extension", function() { |
182 |
equals(extension(CGD.mod.fileFromIdentifier('blarg', 'text/css').file. |
|
182 |
equals(extension(CGD.mod.fileFromIdentifier('blarg', 'text/css').file.uri), 'css'); |
|
183 |
183 |
}); |
184 |
184 |
|
185 |
185 |
test("require includes a file only once", function() { |
Up to file-list script/cgd/Module.js:
| … | … | @@ -45,9 +45,9 @@ CGD.god = window; |
45 |
45 |
}; |
46 |
46 |
|
47 |
47 |
CGD.Dependency = function(identifier, type) { |
48 |
this.id |
|
48 |
this.id = identifier; |
|
49 |
49 |
this.type = type || 'text/javascript'; |
50 |
this. |
|
50 |
this.uri = identifier + CGD.Dependency.guessFileExtension(this.type); |
|
51 |
51 |
this.load = {status: 'new'}; |
52 |
52 |
this.exports = {}; |
53 |
53 |
}; |
| … | … | @@ -55,16 +55,16 @@ CGD.god = window; |
55 |
55 |
CGD.Dependency.prototype = { |
56 |
56 |
constructor: CGD.Dependency, |
57 |
57 |
canonically: function(fullPath) { |
58 |
this. |
|
58 |
this.uri = fullPath; |
|
59 |
59 |
return this; |
60 |
60 |
}, |
61 |
61 |
under: function(root) { |
62 |
this. |
|
62 |
this.uri = root + this.uri; |
|
63 |
63 |
return this; |
64 |
64 |
}, |
65 |
65 |
register: function(files) { |
66 |
files[this.identifier] = this; |
|
67 |
files[this.canonicalPath] = this; |
|
66 |
files[this.id] = this; |
|
67 |
files[this.uri] = this; |
|
68 |
68 |
return this; |
69 |
69 |
}, |
70 |
70 |
status: function() { |
| … | … | @@ -96,13 +96,13 @@ CGD.god = window; |
96 |
96 |
switch (type) { |
97 |
97 |
case 'text/javascript': |
98 |
98 |
var element = CGD.html.makeTag('script', |
99 |
{src: this.canonicalPath, type: type, language: 'javascript'}); |
|
100 |
this.canonicalPath = element.src; |
|
99 |
{src: this.uri, type: type, language: 'javascript'}); |
|
100 |
this.uri = element.src; |
|
101 |
101 |
return element; |
102 |
102 |
case 'text/css': |
103 |
103 |
var element = CGD.html.makeTag('link', |
104 |
{href: this.canonicalPath, type: type, rel: 'stylesheet'}); |
|
105 |
this.canonicalPath = element.href; |
|
104 |
{href: this.uri, type: type, rel: 'stylesheet'}); |
|
105 |
this.uri = element.href; |
|
106 |
106 |
return element; |
107 |
107 |
default: |
108 |
108 |
throw "Don't know how to include " + type; |
| … | … | @@ -128,7 +128,7 @@ CGD.god = window; |
128 |
128 |
var module = this; |
129 |
129 |
var path = CGD.Module.pathTo(identifier); |
130 |
130 |
var file = new CGD.Dependency(identifier); |
131 |
var filename = file. |
|
131 |
var filename = file.uri; |
|
132 |
132 |
var fullPath = CGD.html.findMe('script', 'src', filename); |
133 |
133 |
this.queued = 0; |
134 |
134 |
if (fullPath) { |
| … | … | @@ -136,7 +136,7 @@ CGD.god = window; |
136 |
136 |
this.root = fullPath.slice(0, -filename.length); |
137 |
137 |
} |
138 |
138 |
this.cd(path); |
139 |
this.id = file.id |
|
139 |
this.id = file.id; |
|
140 |
140 |
this.uri = fullPath; |
141 |
141 |
window.exports = file.exports; |
142 |
142 |
window.require = function(identifier, type) {return module.require(identifier, type);}; |
| … | … | @@ -194,7 +194,7 @@ CGD.god = window; |
194 |
194 |
case 'pending': |
195 |
195 |
x.file.count = (x.file.count || 0) + 1; |
196 |
196 |
if (x.file.count > 20) { |
197 |
throw new CGD.Module.UnmetDependency(x.file. |
|
197 |
throw new CGD.Module.UnmetDependency(x.file.uri); |
|
198 |
198 |
} |
199 |
199 |
this.queued++; |
200 |
200 |
return null; |
| … | … | @@ -206,7 +206,7 @@ CGD.god = window; |
206 |
206 |
var file = this.files[identifier] || |
207 |
207 |
new CGD.Dependency(identifier, type).under(this.root); |
208 |
208 |
var element = file.element(type); |
209 |
file = this.files[file. |
|
209 |
file = this.files[file.uri] || file; |
|
210 |
210 |
return {file: file, element: element}; |
211 |
211 |
}, |
212 |
212 |
absoluteIdentifier: function(identifier) { |
