Skip to content

Commit

Permalink
Add basic require.define.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie committed Mar 29, 2010
1 parent bbdbda1 commit 806d213
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
34 changes: 33 additions & 1 deletion assets/modulr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ var modulr = (function(global) {
_exports = {},
_oldDir = '',
_currentDir = '',
hasOwnProp = Object.prototype.hasOwnProperty,
PREFIX = '__module__', // Prefix identifiers to avoid issues in IE.
RELATIVE_IDENTIFIER_PATTERN = /^\.\.?\//;
RELATIVE_IDENTIFIER_PATTERN = /^\.\.?\//,
DONT_ENUM_PROPERTIES = [
'constructor',
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable'
];

function log(str) {
if (global.console && console.log) { console.log(str); }
Expand Down Expand Up @@ -85,6 +95,27 @@ var modulr = (function(global) {
_modules[key] = fn;
}

function _each(obj, callback) {
var prop;
for(prop in obj) {
if (hasOwnProp.call(obj, prop)) {
callback(prop, obj[prop]);
}
}
for (var i = 0, length = DONT_ENUM_PROPERTIES.length; i < length; i++) {
prop = DONT_ENUM_PROPERTIES[i];
if (hasOwnProp.call(obj, prop)) {
callback(prop, obj[prop]);
}
}
}

function define(moduleDescriptors) {
_each(moduleDescriptors, function(k, v) {
_modules[PREFIX + k] = v;
});
}

function ensure(identifiers, onAvailable, onMissing) {
for (var i = 0, length = identifiers.length; i < length; i++) {
var identifier = identifiers[i];
Expand All @@ -100,6 +131,7 @@ var modulr = (function(global) {
onAvailable();
}

require.define = define;
require.ensure = ensure;

return {
Expand Down
21 changes: 14 additions & 7 deletions lib/modulr/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ def parse_file(path)

def to_js(buffer = '')
buffer << File.read(PATH_TO_MODULR_JS)
modules.each do |js_module|
if lazy_eval_module?(js_module)
js_module.to_js_string(buffer)
buffer << "\n(function(require, module) {"
buffer << transport
buffer << main.ensure
buffer << "})(modulr.require, {});\n"
end

def transport
pairs = modules.map do |m|
if lazy_eval_module?(m)
value = m.escaped_src
else
js_module.to_js(buffer)
value = m.factory
end
end
ens = main.to_js_ensure
buffer << "\n(function(require, module) { #{ens} })(modulr.require, {});\n"
"\n'#{m.id}': #{value}"
end.join(', ')
"require.define({#{pairs}\n});"
end

private
Expand Down
22 changes: 10 additions & 12 deletions lib/modulr/js_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,23 @@ def escaped_src
}
end

def factory
"function(require, exports, module) {\n#{src}\n}"
end

def dependencies
@dependencies ||= self.class.find_dependencies(self)
end

def to_js_ensure(buffer = '')
fn = "function() {\n#{src}\n}"
dep = dependencies.map { |d| d.id.inspect }
buffer << "\nrequire.ensure([#{dep.join(', ')}], #{fn});\n"
end

def to_js(buffer = '')
fn = "function(require, exports, module) {\n#{src}\n}"
buffer << "\nmodulr.cache('#{id}', #{fn});\n"
def dependency_array
'[' << dependencies.map { |d| "'#{d.id}'" }.join(', ') << ']'
end

def to_js_string(buffer = '')
buffer << "\nmodulr.cache('#{id}', '#{escaped_src}');\n"
def ensure(buffer = '')
fn = "function() {\n#{src}\n}"
buffer << "\nrequire.ensure(#{dependency_array}, #{fn});\n"
end

protected
def partial_path
File.join(*terms)
Expand Down

0 comments on commit 806d213

Please sign in to comment.