Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix relative identifiers by expanding them on client-side.

`modulr.alias` removed as useless with these changes.
  • Loading branch information...
commit 0bf84b892fc7fc0959a24ea3e213df39d510e5ce 1 parent 30714e4
@samleb samleb authored committed
View
64 assets/modulr.js
@@ -6,9 +6,11 @@
var modulr = (function(global) {
var _modules = {},
_moduleObjects = {},
- _references = {},
_exports = {},
- PREFIX = '__module__'; // Prefix identifiers to avoid issues in IE.
+ oldDir = '',
+ currentDir = '',
+ PREFIX = '__module__', // Prefix identifiers to avoid issues in IE.
+ RELATIVE_IDENTIFIER_PATTERN = /^\.\.?\//;
function log(str) {
if (global.console && console.log) { console.log(str); }
@@ -16,28 +18,58 @@ var modulr = (function(global) {
function require(identifier) {
var fn, modObj,
- key = PREFIX + identifier,
- id = _references[key] || key,
- expts = _exports[id];
+ id = expandIdentifier(identifier),
+ key = PREFIX + id,
+ expts = _exports[key];
log('Required module "' + identifier + '".');
if (!expts) {
- _exports[id] = expts = {};
- _moduleObjects[id] = modObj = { id: id.replace(PREFIX, '') };
+ _exports[key] = expts = {};
+ _moduleObjects[key] = modObj = { id: id };
if (!require.main) { require.main = modObj; }
- fn = _modules[id];
- if (!fn) { throw 'Can\'t find module "' + identifier + '".'; }
- if (typeof fn === 'string') {
- fn = new Function('require', 'exports', 'module', fn);
+ fn = _modules[key];
+ oldDir = currentDir;
+ currentDir = id.slice(0, id.lastIndexOf('/'));
+
+ try {
+ if (!fn) { throw 'Can\'t find module "' + identifier + '".'; }
+ if (typeof fn === 'string') {
+ fn = new Function('require', 'exports', 'module', fn);
+ }
+ fn(require, expts, modObj);
+ } catch(e) { // IE doesn't support `finally` without `catch`.
+ throw e;
+ } finally {
+ currentDir = oldDir;
}
- fn(require, expts, modObj);
}
return expts;
}
+ function expandIdentifier(identifier) {
+ if (!RELATIVE_IDENTIFIER_PATTERN.test(identifier)) {
+ return identifier;
+ }
+ var parts = (currentDir + '/' + identifier).split('/'),
+ path = [];
+ for (var i = 0; i < parts.length; i++) {
+ switch (parts[i]) {
+ case '':
+ case '.':
+ continue;
+ case '..':
+ path.pop();
+ break;
+ default:
+ path.push(parts[i]);
+ }
+ }
+ return path.join('/');
+ }
+
function cache(id, fn) {
var key = PREFIX + id;
@@ -48,14 +80,8 @@ var modulr = (function(global) {
_modules[key] = fn;
}
- function alias(identifier, id) {
- log('Linked "' + identifier + '" to module "' + id + '".');
- _references[PREFIX + identifier] = PREFIX + id;
- }
-
return {
require: require,
- cache: cache,
- alias: alias
+ cache: cache
};
})(this);
View
1  example/foo/foo.js
@@ -1,3 +1,4 @@
exports.toplevel = require('math').add;
exports.relative = require('./bar').bar;
exports.relative2 = require('../foo/bar').bar;
+exports.relative3 = require('../foo/../foo/./bar').bar;
View
1  example/increment.js
@@ -1,5 +1,4 @@
var add = require('math').add;
-add = require('../example/math').add;
exports.increment = function(val) {
return add(val, 1);
View
1  example/program.js
@@ -6,4 +6,5 @@ inspect(require('inspect') === require('inspect'));
var foo = require('foo/foo');
inspect(foo.relative);
inspect(foo.relative === foo.relative2);
+inspect(foo.relative === foo.relative3);
inspect(foo.toplevel === require('math').add);
View
8 lib/modulr/js_module.rb
@@ -88,13 +88,11 @@ def dependencies
end
def to_js(buffer = '')
- call_alias_js_function(buffer)
fn = "function(require, exports, module) {\n#{src}\n}"
buffer << "\nmodulr.cache('#{id}', #{fn});\n"
end
def to_js_string(buffer = '')
- call_alias_js_function(buffer)
buffer << "\nmodulr.cache('#{id}', '#{escaped_src}');\n"
end
@@ -106,12 +104,6 @@ def partial_path
def directory
relative? ? File.dirname(file) : root
end
-
- def call_alias_js_function(buffer)
- if relative?
- buffer << "\nmodulr.alias('#{identifier}', '#{id}');"
- end
- end
end
class ModuleIdentifierError < ModulrError
Please sign in to comment.
Something went wrong with that request. Please try again.