sprockets-commonjs 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +32 -0
- data/Rakefile +20 -0
- data/examples/application.js +3 -0
- data/examples/example.js +74 -0
- data/examples/index.html +4 -0
- data/examples/modules/program.module.js +3 -0
- data/examples/modules/user.module.js +6 -0
- data/lib/sprockets/commonjs.js +60 -0
- data/lib/sprockets/commonjs.rb +40 -0
- data/sprockets-commonjs.gemspec +23 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
This library adds CommonJS support to [Sprockets](https://github.com/sstephenson/sprockets).
|
2
|
+
|
3
|
+
## What is CommonJS?
|
4
|
+
|
5
|
+
The CommonJS module format is a way of encapsulating JavaScript libraries, ensuring they have to explicitly require and export properties they use. In a nutshell:
|
6
|
+
|
7
|
+
1. You require in files using `require()`:
|
8
|
+
|
9
|
+
var Asset = require('models/asset');
|
10
|
+
|
11
|
+
2. You export properties using `module.exports`:
|
12
|
+
|
13
|
+
var Asset = function(){ /* ... */ };
|
14
|
+
module.exports = Asset;
|
15
|
+
|
16
|
+
## This library
|
17
|
+
|
18
|
+
This library adds CommonJS support to Sprockets, so it can wrap up JavaScript files as modules, and serve them appropriately. This is done by giving any JS files you want as modules, the `.module.js` extension.
|
19
|
+
|
20
|
+
Sprockets will then wrap up the JS library when it's requested, with the following:
|
21
|
+
|
22
|
+
require.define({'library/name': function(exports, require, module){ /* Your library */ }});
|
23
|
+
|
24
|
+
`require.define()` is defined inside `commonjs.js`, which you'll need to include in the page before any modules are loaded.
|
25
|
+
|
26
|
+
One caveat to the approach this library takes, is that dependencies loaded through `require()` will not be added to the dependency graph. This library will not parse the AST tree for require calls. This decision has been made for a variety of reasons, but it does mean you need to require files through both CommonJS and Sprockets.
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
1. Add `gem 'sprockets-commonjs'` to your `Gemfile`
|
31
|
+
1. Add `.module.js` to any JavaScript files you want as modules, i.e. `users.module.js`
|
32
|
+
1. Require all the modules, e.g.: `//= require_tree ./modules`
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'sprockets'
|
3
|
+
require 'sprockets/commonjs'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
task :example do
|
7
|
+
env = Sprockets::Environment.new(File.expand_path('..', __FILE__))
|
8
|
+
env.append_path 'examples/'
|
9
|
+
|
10
|
+
target = File.expand_path('../examples/example.js', __FILE__)
|
11
|
+
env['application.js'].write_to target
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
task :default => :test
|
16
|
+
|
17
|
+
Rake::TestTask.new do |t|
|
18
|
+
t.libs << "test"
|
19
|
+
t.warning = true
|
20
|
+
end
|
data/examples/example.js
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
(function() {
|
2
|
+
var modules = {}, cache = {};
|
3
|
+
|
4
|
+
if (this.require && this.require.modules) {
|
5
|
+
modules = this.require.modules;
|
6
|
+
}
|
7
|
+
|
8
|
+
var require = function(name, root) {
|
9
|
+
var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
|
10
|
+
module = cache[path] || cache[indexPath];
|
11
|
+
if (module) {
|
12
|
+
return module;
|
13
|
+
} else if (fn = modules[path] || modules[path = indexPath]) {
|
14
|
+
module = {id: path, exports: {}};
|
15
|
+
cache[path] = module.exports;
|
16
|
+
fn(module.exports, function(name) {
|
17
|
+
return require(name, dirname(path));
|
18
|
+
}, module);
|
19
|
+
return cache[path] = module.exports;
|
20
|
+
} else {
|
21
|
+
throw 'module ' + name + ' not found';
|
22
|
+
}
|
23
|
+
};
|
24
|
+
|
25
|
+
var expand = function(root, name) {
|
26
|
+
var results = [], parts, part;
|
27
|
+
// If path is relative
|
28
|
+
if (/^\.\.?(\/|$)/.test(name)) {
|
29
|
+
parts = [root, name].join('/').split('/');
|
30
|
+
} else {
|
31
|
+
parts = name.split('/');
|
32
|
+
}
|
33
|
+
for (var i = 0, length = parts.length; i < length; i++) {
|
34
|
+
part = parts[i];
|
35
|
+
if (part == '..') {
|
36
|
+
results.pop();
|
37
|
+
} else if (part != '.' && part != '') {
|
38
|
+
results.push(part);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
return results.join('/');
|
42
|
+
};
|
43
|
+
|
44
|
+
var dirname = function(path) {
|
45
|
+
return path.split('/').slice(0, -1).join('/');
|
46
|
+
};
|
47
|
+
|
48
|
+
this.require = function(name) {
|
49
|
+
return require(name, '');
|
50
|
+
};
|
51
|
+
|
52
|
+
this.require.define = function(bundle) {
|
53
|
+
for (var key in bundle) {
|
54
|
+
modules[key] = bundle[key];
|
55
|
+
}
|
56
|
+
};
|
57
|
+
|
58
|
+
this.require.modules = modules;
|
59
|
+
this.require.cache = cache;
|
60
|
+
return this.require;
|
61
|
+
}).call(this);
|
62
|
+
this.require.define({"modules/program.module":function(exports, require, module){module.exports = function(){
|
63
|
+
alert('Long live the Programs!');
|
64
|
+
};
|
65
|
+
;}});
|
66
|
+
this.require.define({"modules/user.module":function(exports, require, module){var Program = require('modules/program');
|
67
|
+
|
68
|
+
module.exports = function(){
|
69
|
+
alert('Long live the Users');
|
70
|
+
Program();
|
71
|
+
};
|
72
|
+
;}});
|
73
|
+
|
74
|
+
var self = 'application.js';
|
data/examples/index.html
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
(function() {
|
2
|
+
if (!this.require) {
|
3
|
+
var modules = {}, cache = {};
|
4
|
+
|
5
|
+
var require = function(name, root) {
|
6
|
+
var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
|
7
|
+
module = cache[path] || cache[indexPath];
|
8
|
+
if (module) {
|
9
|
+
return module;
|
10
|
+
} else if (fn = modules[path] || modules[path = indexPath]) {
|
11
|
+
module = {id: path, exports: {}};
|
12
|
+
cache[path] = module.exports;
|
13
|
+
fn(module.exports, function(name) {
|
14
|
+
return require(name, dirname(path));
|
15
|
+
}, module);
|
16
|
+
return cache[path] = module.exports;
|
17
|
+
} else {
|
18
|
+
throw 'module ' + name + ' not found';
|
19
|
+
}
|
20
|
+
};
|
21
|
+
|
22
|
+
var expand = function(root, name) {
|
23
|
+
var results = [], parts, part;
|
24
|
+
// If path is relative
|
25
|
+
if (/^\.\.?(\/|$)/.test(name)) {
|
26
|
+
parts = [root, name].join('/').split('/');
|
27
|
+
} else {
|
28
|
+
parts = name.split('/');
|
29
|
+
}
|
30
|
+
for (var i = 0, length = parts.length; i < length; i++) {
|
31
|
+
part = parts[i];
|
32
|
+
if (part == '..') {
|
33
|
+
results.pop();
|
34
|
+
} else if (part != '.' && part != '') {
|
35
|
+
results.push(part);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return results.join('/');
|
39
|
+
};
|
40
|
+
|
41
|
+
var dirname = function(path) {
|
42
|
+
return path.split('/').slice(0, -1).join('/');
|
43
|
+
};
|
44
|
+
|
45
|
+
this.require = function(name) {
|
46
|
+
return require(name, '');
|
47
|
+
};
|
48
|
+
|
49
|
+
this.require.define = function(bundle) {
|
50
|
+
for (var key in bundle) {
|
51
|
+
modules[key] = bundle[key];
|
52
|
+
}
|
53
|
+
};
|
54
|
+
|
55
|
+
this.require.modules = modules;
|
56
|
+
this.require.cache = cache;
|
57
|
+
}
|
58
|
+
|
59
|
+
return this.require;
|
60
|
+
}).call(this);
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'sprockets'
|
2
|
+
require 'tilt'
|
3
|
+
|
4
|
+
module Sprockets
|
5
|
+
class CommonJS < Tilt::Template
|
6
|
+
self.default_mime_type = 'application/javascript'
|
7
|
+
|
8
|
+
def self.default_namespace
|
9
|
+
'this.require'
|
10
|
+
end
|
11
|
+
|
12
|
+
def prepare
|
13
|
+
@namespace = self.class.default_namespace
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :namespace
|
17
|
+
|
18
|
+
def evaluate(scope, locals, &block)
|
19
|
+
if File.extname(scope.logical_path) == '.module'
|
20
|
+
path = scope.logical_path
|
21
|
+
path = File.join(File.dirname(path), File.basename(path, '.module'))
|
22
|
+
path = path.inspect
|
23
|
+
|
24
|
+
scope.require_asset 'sprockets/commonjs'
|
25
|
+
|
26
|
+
code = ''
|
27
|
+
code << "#{namespace}.define({#{path}:"
|
28
|
+
code << 'function(exports, require, module){'
|
29
|
+
code << data
|
30
|
+
code << ";}});\n"
|
31
|
+
code
|
32
|
+
else
|
33
|
+
data
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
register_postprocessor 'application/javascript', CommonJS
|
39
|
+
append_path File.expand_path('../..', __FILE__)
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sprockets-commonjs"
|
6
|
+
s.version = '0.0.2'
|
7
|
+
s.authors = ["Alex MacCaw"]
|
8
|
+
s.email = ["info@eribium.org"]
|
9
|
+
s.homepage = ""
|
10
|
+
s.summary = %q{Adds CommonJS support to Sprockets}
|
11
|
+
s.description = s.summary
|
12
|
+
|
13
|
+
s.rubyforge_project = "sprockets-commonjs"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# specify any dependencies here; for example:
|
21
|
+
# s.add_development_dependency "rspec"
|
22
|
+
s.add_runtime_dependency "sprockets", "~>2.4.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sprockets-commonjs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex MacCaw
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sprockets
|
16
|
+
requirement: &70172867486660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.4.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70172867486660
|
25
|
+
description: Adds CommonJS support to Sprockets
|
26
|
+
email:
|
27
|
+
- info@eribium.org
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- examples/application.js
|
37
|
+
- examples/example.js
|
38
|
+
- examples/index.html
|
39
|
+
- examples/modules/program.module.js
|
40
|
+
- examples/modules/user.module.js
|
41
|
+
- lib/sprockets/commonjs.js
|
42
|
+
- lib/sprockets/commonjs.rb
|
43
|
+
- sprockets-commonjs.gemspec
|
44
|
+
homepage: ''
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project: sprockets-commonjs
|
64
|
+
rubygems_version: 1.8.15
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Adds CommonJS support to Sprockets
|
68
|
+
test_files: []
|