Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix up, remove AST parsing

  • Loading branch information...
commit 61f0341af5f54226c7ac7924c9e5f89d1181f143 1 parent 500fd60
Alex MacCaw authored
5 Rakefile
View
@@ -5,5 +5,8 @@ require 'sprockets/commonjs'
task :example do
env = Sprockets::Environment.new
env.append_path File.expand_path('../examples', __FILE__)
- puts env['application.js'].to_s
+ env.append_path File.expand_path('../assets/javascripts', __FILE__)
+ File.open(File.expand_path('../examples/example.js', __FILE__), 'w+') do |f|
+ f.write env['application.js'].to_s
+ end
end
3  examples/application.js
View
@@ -1,3 +1,4 @@
-//= require user
+//= require commonjs
+//= require_tree ./modules
var self = 'application.js';
82 examples/example.js
View
@@ -0,0 +1,82 @@
+(function() {
+ var modules = {}, cache = {};
+
+ if (this.require && this.require.modules) {
+ modules = this.require.modules;
+ }
+
+ var require = function(name, root) {
+ var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
+ module = cache[path] || cache[indexPath];
+ if (module) {
+ return module;
+ } else if (fn = modules[path] || modules[path = indexPath]) {
+ module = {id: path, exports: {}};
+ cache[path] = module.exports;
+ fn(module.exports, function(name) {
+ return require(name, dirname(path));
+ }, module);
+ return cache[path] = module.exports;
+ } else {
+ throw 'module ' + name + ' not found';
+ }
+ };
+
+ var expand = function(root, name) {
+ var results = [], parts, part;
+ // If path is relative
+ if (/^\.\.?(\/|$)/.test(name)) {
+ parts = [root, name].join('/').split('/');
+ } else {
+ parts = name.split('/');
+ }
+ for (var i = 0, length = parts.length; i < length; i++) {
+ part = parts[i];
+ if (part == '..') {
+ results.pop();
+ } else if (part != '.' && part != '') {
+ results.push(part);
+ }
+ }
+ return results.join('/');
+ };
+
+ var dirname = function(path) {
+ return path.split('/').slice(0, -1).join('/');
+ };
+
+ this.require = function(name) {
+ return require(name, '');
+ };
+
+ this.require.define = function(bundle) {
+ for (var key in bundle) {
+ modules[key] = bundle[key];
+ }
+ };
+
+ this.require.modules = modules;
+ this.require.cache = cache;
+ return this.require;
+}).call(this);
+(function() {
+ this.require.define({"modules/program": function(exports, require, module){
+ module.exports = function(){
+ alert('Long live the Programs!');
+ };
+ }});
+}).call(this);
+(function() {
+ this.require.define({"modules/user": function(exports, require, module){
+ var Program = require('modules/program');
+
+ module.exports = function(){
+ alert('Long live the Users');
+ Program();
+ };
+ }});
+}).call(this);
+
+
+
+var self = 'application.js';
4 examples/index.html
View
@@ -0,0 +1,4 @@
+<script src="example.js" type="text/javascript" charset="utf-8"></script>
+<script type="text/javascript" charset="utf-8">
+ require('modules/user')();
+</script>
3  examples/modules/program.js.module
View
@@ -0,0 +1,3 @@
+module.exports = function(){
+ alert('Long live the Programs!');
+};
6 examples/modules/user.js.module
View
@@ -0,0 +1,6 @@
+var Program = require('modules/program');
+
+module.exports = function(){
+ alert('Long live the Users');
+ Program();
+};
20 lib/sprockets/commonjs.rb
View
@@ -1,12 +1,9 @@
require 'sprockets'
require 'tilt'
-require 'holmes'
module Sprockets
class CommonJS < Tilt::Template
- def self.default_mime_type
- 'application/javascript'
- end
+ self.default_mime_type = 'application/javascript'
def self.default_namespace
'this.require'
@@ -19,22 +16,11 @@ def prepare
attr_reader :namespace
def evaluate(scope, locals, &block)
- scope.require_asset('commonjs.js')
-
- requires = Holmes.parse(data)
- warn 'Dynamic require calls' if requires['expressions'].any?
-
- requires['strings'].each do |dependency|
- scope.require_asset(dependency)
- end
-
<<-JS
(function() {
- #{namespace} || (#{namespace} = {});
- #{namespace}.modules || (#{namespace}.modules = {});
- #{namespace}.modules[#{scope.logical_path.inspect}] = function(exports, require, modules){
+ #{namespace}.define({#{scope.logical_path.inspect}: function(exports, require, module){
#{indent(data)}
- };
+ }});
}).call(this);
JS
end
1  sprockets-commonjs.gemspec
View
@@ -20,5 +20,4 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
s.add_runtime_dependency "sprockets", "~>2.1.2"
- s.add_runtime_dependency "holmes", "~>0.0.1"
end
Please sign in to comment.
Something went wrong with that request. Please try again.