modulr 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/assets/modulr.js CHANGED
@@ -6,9 +6,11 @@
6
6
  var modulr = (function(global) {
7
7
  var _modules = {},
8
8
  _moduleObjects = {},
9
- _references = {},
10
9
  _exports = {},
11
- PREFIX = '__module__'; // Prefix identifiers to avoid issues in IE.
10
+ _oldDir = '',
11
+ _currentDir = '',
12
+ PREFIX = '__module__', // Prefix identifiers to avoid issues in IE.
13
+ RELATIVE_IDENTIFIER_PATTERN = /^\.\.?\//;
12
14
 
13
15
  function log(str) {
14
16
  if (global.console && console.log) { console.log(str); }
@@ -16,28 +18,63 @@ var modulr = (function(global) {
16
18
 
17
19
  function require(identifier) {
18
20
  var fn, modObj,
19
- key = PREFIX + identifier,
20
- id = _references[key] || key,
21
- expts = _exports[id];
21
+ id = resolveIdentifier(identifier),
22
+ key = PREFIX + id,
23
+ expts = _exports[key];
22
24
 
23
25
  log('Required module "' + identifier + '".');
24
26
 
25
27
  if (!expts) {
26
- _exports[id] = expts = {};
27
- _moduleObjects[id] = modObj = { id: id.replace(PREFIX, '') };
28
+ _exports[key] = expts = {};
29
+ _moduleObjects[key] = modObj = { id: id };
28
30
 
29
31
  if (!require.main) { require.main = modObj; }
30
32
 
31
- fn = _modules[id];
32
- if (!fn) { throw 'Can\'t find module "' + identifier + '".'; }
33
- if (typeof fn === 'string') {
34
- fn = new Function('require', 'exports', 'module', fn);
33
+ fn = _modules[key];
34
+ _oldDir = _currentDir;
35
+ _currentDir = id.slice(0, id.lastIndexOf('/'));
36
+
37
+ try {
38
+ if (!fn) { throw 'Can\'t find module "' + identifier + '".'; }
39
+ if (typeof fn === 'string') {
40
+ fn = new Function('require', 'exports', 'module', fn);
41
+ }
42
+ fn(require, expts, modObj);
43
+ _currentDir = _oldDir;
44
+ } catch(e) {
45
+ _currentDir = _oldDir;
46
+ // We'd use a finally statement here if it wasn't for IE.
47
+ throw e;
35
48
  }
36
- fn(require, expts, modObj);
37
49
  }
38
50
  return expts;
39
51
  }
40
52
 
53
+ function resolveIdentifier(identifier) {
54
+ var parts, part, path;
55
+
56
+ if (!RELATIVE_IDENTIFIER_PATTERN.test(identifier)) {
57
+ return identifier;
58
+ }
59
+
60
+ parts = (_currentDir + '/' + identifier).split('/');
61
+ path = [];
62
+ for (var i = 0, length = parts.length; i < length; i++) {
63
+ part = parts[i];
64
+ switch (part) {
65
+ case '':
66
+ case '.':
67
+ continue;
68
+ case '..':
69
+ path.pop();
70
+ break;
71
+ default:
72
+ path.push(part);
73
+ }
74
+ }
75
+ return path.join('/');
76
+ }
77
+
41
78
  function cache(id, fn) {
42
79
  var key = PREFIX + id;
43
80
 
@@ -48,14 +85,8 @@ var modulr = (function(global) {
48
85
  _modules[key] = fn;
49
86
  }
50
87
 
51
- function alias(identifier, id) {
52
- log('Linked "' + identifier + '" to module "' + id + '".');
53
- _references[PREFIX + identifier] = PREFIX + id;
54
- }
55
-
56
88
  return {
57
89
  require: require,
58
- cache: cache,
59
- alias: alias
90
+ cache: cache
60
91
  };
61
92
  })(this);
data/example/foo/foo.js CHANGED
@@ -1,3 +1,4 @@
1
1
  exports.toplevel = require('math').add;
2
2
  exports.relative = require('./bar').bar;
3
3
  exports.relative2 = require('../foo/bar').bar;
4
+ exports.relative3 = require('../foo/../foo/./bar').bar;
data/example/increment.js CHANGED
@@ -1,5 +1,4 @@
1
1
  var add = require('math').add;
2
- add = require('../example/math').add;
3
2
 
4
3
  exports.increment = function(val) {
5
4
  return add(val, 1);
data/example/program.js CHANGED
@@ -6,4 +6,5 @@ inspect(require('inspect') === require('inspect'));
6
6
  var foo = require('foo/foo');
7
7
  inspect(foo.relative);
8
8
  inspect(foo.relative === foo.relative2);
9
+ inspect(foo.relative === foo.relative3);
9
10
  inspect(foo.toplevel === require('math').add);
@@ -88,13 +88,11 @@ module Modulr
88
88
  end
89
89
 
90
90
  def to_js(buffer = '')
91
- call_alias_js_function(buffer)
92
91
  fn = "function(require, exports, module) {\n#{src}\n}"
93
92
  buffer << "\nmodulr.cache('#{id}', #{fn});\n"
94
93
  end
95
94
 
96
95
  def to_js_string(buffer = '')
97
- call_alias_js_function(buffer)
98
96
  buffer << "\nmodulr.cache('#{id}', '#{escaped_src}');\n"
99
97
  end
100
98
 
@@ -106,12 +104,6 @@ module Modulr
106
104
  def directory
107
105
  relative? ? File.dirname(file) : root
108
106
  end
109
-
110
- def call_alias_js_function(buffer)
111
- if relative?
112
- buffer << "\nmodulr.alias('#{identifier}', '#{id}');"
113
- end
114
- end
115
107
  end
116
108
 
117
109
  class ModuleIdentifierError < ModulrError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modulr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobie Langel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-10 00:00:00 +01:00
12
+ date: 2010-03-11 00:00:00 +01:00
13
13
  default_executable: modulrize
14
14
  dependencies: []
15
15