sprockets-commonjs-mindreframer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a8d7b206d2851f6a79a4498970a19b1e1a5f8c45
4
+ data.tar.gz: 7e859a407497ac4072c229092cc5ee4a72e20de4
5
+ SHA512:
6
+ metadata.gz: 8d2de6c65f7adea8deb2afa622a85d72ace4967523c2eecfc7b61e79ba4ff712703096733c960550b125d8354e828a6c011a08256ca3c829dfc9abd51686dc62
7
+ data.tar.gz: 7d97d103f2db075cd613cd59a3c8c85696cb6213096083db8bd41a536cb2f0da319a05c5008cb29a2b999302c74bfa5cef738c3304c3eb318f0a71c4b3bdaf5b
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ gemfiles/*.lock
5
+ pkg/*
6
+ .DS_Store
data/Appraisals ADDED
@@ -0,0 +1,31 @@
1
+ appraise "sprockets-2-1" do
2
+ gem 'sprockets', '~> 2.1.3'
3
+ end
4
+
5
+ appraise "sprockets-2-2" do
6
+ gem 'sprockets', '~> 2.2.2'
7
+ end
8
+
9
+ appraise "sprockets-2-3" do
10
+ gem 'sprockets', '~> 2.3.2'
11
+ end
12
+
13
+ appraise "sprockets-2-4" do
14
+ gem 'sprockets', '~> 2.4.5'
15
+ end
16
+
17
+ appraise "sprockets-2-5" do
18
+ gem 'sprockets', '~> 2.5.0'
19
+ end
20
+
21
+ appraise "sprockets-2-6" do
22
+ gem 'sprockets', '~> 2.6.0'
23
+ end
24
+
25
+ appraise "sprockets-2-7" do
26
+ gem 'sprockets', '~> 2.7.0'
27
+ end
28
+
29
+ appraise "sprockets-2-8" do
30
+ gem 'sprockets', '~> 2.8.2'
31
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sprockets-commonjs.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ This library adds CommonJS support to [Sprockets](https://github.com/sstephenson/sprockets).
2
+
3
+ ## This is a fork, since the original was not published for ages.
4
+
5
+ Also this includes the changes from wazeHQ/sprockets-commonjs to allow you transparent requires without the `.module`-part in filename.
6
+
7
+ # in Gemfile
8
+ gem 'sprockets-commonjs-mindreframer'
9
+
10
+ # install
11
+ $ bundle install
12
+
13
+
14
+ ## What is CommonJS?
15
+
16
+ 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:
17
+
18
+ 1. You require in files using `require()`:
19
+
20
+ var Asset = require('models/asset');
21
+
22
+ 2. You export properties using `module.exports`:
23
+
24
+ var Asset = function(){ /* ... */ };
25
+ module.exports = Asset;
26
+
27
+ ## This library
28
+
29
+ This library adds CommonJS support to Sprockets, so it can wrap up JavaScript files as modules, and serve them appropriately.
30
+
31
+ This is done transparently by checking for `require('...')` or `module.exports = ...`-statements in the source-code.
32
+
33
+
34
+ Sprockets will then wrap up the JS library when it's requested, with the following:
35
+
36
+ require.define({'library/name': function(exports, require, module){ /* Your library */ }});
37
+
38
+ `require.define()` is defined inside `commonjs.js`, which will be included, if any CommonJS modules were found.
39
+
40
+
41
+ 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.
42
+
43
+ ## Usage
44
+
45
+ 1. Add `gem 'sprockets-commonjs-mindreframer'` to your `Gemfile`
46
+ 2. Configure Sprockets::CommonJS.module_paths, e.g.:
47
+ `Sprockets::CommonJS.module_paths += [Rails.root.to_s]`
48
+
49
+ 3. Require all the modules, e.g.: `//= require_tree ./models`
50
+ 4. Or, require individual modules, e.g.: `//= require users`
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'sprockets'
3
+ require 'sprockets/commonjs'
4
+ require 'rake/testtask'
5
+ require 'appraisal'
6
+
7
+ task :example do
8
+ example_dir = File.expand_path('..', __FILE__)
9
+ env = Sprockets::Environment.new(example_dir)
10
+ env.append_path 'examples/'
11
+ Sprockets::CommonJS.module_paths += [File.join(example_dir, 'examples') ]
12
+
13
+ target = File.expand_path('../examples/example.js', __FILE__)
14
+ env['application.js'].write_to target
15
+ end
16
+
17
+
18
+ task :default => :test
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.test_files = FileList['test/**/*_test.rb']
22
+ t.libs << 'lib' << 'test'
23
+ t.warning = true
24
+ end
@@ -0,0 +1,64 @@
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
+ if (module.__define_in_progress) {
10
+ throw new Error("Circular dependency: " + name + " already required.");
11
+ }
12
+ return module;
13
+ } else if (fn = modules[path] || modules[path = indexPath]) {
14
+ module = {id: path, exports: { __define_in_progress: true } };
15
+ cache[path] = module.exports;
16
+ fn(module.exports, function(name) {
17
+ return require(name, dirname(path));
18
+ }, module);
19
+ delete module.exports.__define_in_progress;
20
+ return cache[path] = module.exports;
21
+ } else {
22
+ throw 'module ' + name + ' not found';
23
+ }
24
+ };
25
+
26
+ var expand = function(root, name) {
27
+ var results = [], parts, part;
28
+ // If path is relative
29
+ if (/^\.\.?(\/|$)/.test(name)) {
30
+ parts = [root, name].join('/').split('/');
31
+ } else {
32
+ parts = name.split('/');
33
+ }
34
+ for (var i = 0, length = parts.length; i < length; i++) {
35
+ part = parts[i];
36
+ if (part == '..') {
37
+ results.pop();
38
+ } else if (part != '.' && part != '') {
39
+ results.push(part);
40
+ }
41
+ }
42
+ return results.join('/');
43
+ };
44
+
45
+ var dirname = function(path) {
46
+ return path.split('/').slice(0, -1).join('/');
47
+ };
48
+
49
+ this.require = function(name) {
50
+ return require(name, '');
51
+ };
52
+
53
+ this.require.define = function(bundle) {
54
+ for (var key in bundle) {
55
+ modules[key] = bundle[key];
56
+ }
57
+ };
58
+
59
+ this.require.modules = modules;
60
+ this.require.cache = cache;
61
+ }
62
+
63
+ return this.require;
64
+ }).call(this);
@@ -0,0 +1,3 @@
1
+ //= require_tree ./modules
2
+
3
+ var self = 'application.js';
@@ -0,0 +1,77 @@
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
+ if (module.__define_in_progress) {
10
+ throw new Error("Circular dependency: " + name + " already required.");
11
+ }
12
+ return module;
13
+ } else if (fn = modules[path] || modules[path = indexPath]) {
14
+ module = {id: path, exports: { __define_in_progress: true } };
15
+ cache[path] = module.exports;
16
+ fn(module.exports, function(name) {
17
+ return require(name, dirname(path));
18
+ }, module);
19
+ delete module.exports.__define_in_progress;
20
+ return cache[path] = module.exports;
21
+ } else {
22
+ throw 'module ' + name + ' not found';
23
+ }
24
+ };
25
+
26
+ var expand = function(root, name) {
27
+ var results = [], parts, part;
28
+ // If path is relative
29
+ if (/^\.\.?(\/|$)/.test(name)) {
30
+ parts = [root, name].join('/').split('/');
31
+ } else {
32
+ parts = name.split('/');
33
+ }
34
+ for (var i = 0, length = parts.length; i < length; i++) {
35
+ part = parts[i];
36
+ if (part == '..') {
37
+ results.pop();
38
+ } else if (part != '.' && part != '') {
39
+ results.push(part);
40
+ }
41
+ }
42
+ return results.join('/');
43
+ };
44
+
45
+ var dirname = function(path) {
46
+ return path.split('/').slice(0, -1).join('/');
47
+ };
48
+
49
+ this.require = function(name) {
50
+ return require(name, '');
51
+ };
52
+
53
+ this.require.define = function(bundle) {
54
+ for (var key in bundle) {
55
+ modules[key] = bundle[key];
56
+ }
57
+ };
58
+
59
+ this.require.modules = modules;
60
+ this.require.cache = cache;
61
+ }
62
+
63
+ return this.require;
64
+ }).call(this);
65
+ this.require.define({"modules/program.cjs":function(exports, require, module){module.exports = function(){
66
+ alert('Long live the Programs!');
67
+ };
68
+ ;}});
69
+ this.require.define({"modules/user.module":function(exports, require, module){var Program = require('modules/program');
70
+
71
+ module.exports = function(){
72
+ alert('Long live the Users');
73
+ Program();
74
+ };
75
+ ;}});
76
+
77
+ var self = 'application.js';
@@ -0,0 +1,4 @@
1
+ <script src="example.js" type="text/javascript" charset="utf-8"></script>
2
+ <script type="text/javascript" charset="utf-8">
3
+ require('modules/user')();
4
+ </script>
@@ -0,0 +1,3 @@
1
+ module.exports = function(){
2
+ alert('Long live the Programs!');
3
+ };
@@ -0,0 +1,6 @@
1
+ var Program = require('modules/program');
2
+
3
+ module.exports = function(){
4
+ alert('Long live the Users');
5
+ Program();
6
+ };
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.1.3"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.2.2"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.3.2"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.4.5"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.5.0"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.6.0"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.7.0"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "sprockets", "~> 2.8.2"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,61 @@
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
+ cache[path] = module.exports;
15
+ return require(name, dirname(path));
16
+ }, module);
17
+ return cache[path] = module.exports;
18
+ } else {
19
+ throw 'module ' + name + ' not found';
20
+ }
21
+ };
22
+
23
+ var expand = function(root, name) {
24
+ var results = [], parts, part;
25
+ // If path is relative
26
+ if (/^\.\.?(\/|$)/.test(name)) {
27
+ parts = [root, name].join('/').split('/');
28
+ } else {
29
+ parts = name.split('/');
30
+ }
31
+ for (var i = 0, length = parts.length; i < length; i++) {
32
+ part = parts[i];
33
+ if (part == '..') {
34
+ results.pop();
35
+ } else if (part != '.' && part != '') {
36
+ results.push(part);
37
+ }
38
+ }
39
+ return results.join('/');
40
+ };
41
+
42
+ var dirname = function(path) {
43
+ return path.split('/').slice(0, -1).join('/');
44
+ };
45
+
46
+ this.require = function(name) {
47
+ return require(name, '');
48
+ };
49
+
50
+ this.require.define = function(bundle) {
51
+ for (var key in bundle) {
52
+ modules[key] = bundle[key];
53
+ }
54
+ };
55
+
56
+ this.require.modules = modules;
57
+ this.require.cache = cache;
58
+ }
59
+
60
+ return this.require;
61
+ }).call(this);
@@ -0,0 +1 @@
1
+ require 'sprockets/commonjs'
@@ -0,0 +1,70 @@
1
+ require 'sprockets'
2
+ require 'tilt'
3
+
4
+ module Sprockets
5
+ class CommonJS < Tilt::Template
6
+
7
+ WRAPPER = '%s.define({"%s":' +
8
+ "function(exports, require, module){" +
9
+ '%s' +
10
+ ";}});\n"
11
+
12
+ class << self
13
+ attr_accessor :default_namespace
14
+ attr_accessor :module_paths
15
+ end
16
+
17
+ self.default_mime_type = 'application/javascript'
18
+ self.default_namespace = 'this.require'
19
+ self.module_paths = []
20
+
21
+ protected
22
+
23
+ def prepare
24
+ @namespace = self.class.default_namespace
25
+ end
26
+
27
+ def evaluate(scope, locals, &block)
28
+ if commonjs_module?(scope)
29
+ scope.require_asset 'sprockets/commonjs'
30
+ WRAPPER % [ namespace, module_name(scope), data ]
31
+ else
32
+ data
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :namespace
39
+
40
+ def commonjs_module?(scope)
41
+ in_path?(scope) && (exports_module? || has_dependencies?)
42
+ end
43
+
44
+ def in_path?(scope)
45
+ self.class.module_paths.any? do |path|
46
+ scope.pathname.to_s.start_with? path
47
+ end
48
+ end
49
+
50
+ def exports_module?
51
+ data.include?('module.exports')
52
+ end
53
+
54
+ def dependencies
55
+ @dependencies ||= data.scan(/require\(['"](.*)['"]\)/).map(&:last)
56
+ end
57
+
58
+ def has_dependencies?
59
+ dependencies.length > 0
60
+ end
61
+
62
+ def module_name(scope)
63
+ scope.logical_path.gsub(/^\.?\//, '') # Remove relative paths
64
+ end
65
+
66
+ end
67
+
68
+ register_postprocessor 'application/javascript', CommonJS
69
+ append_path File.expand_path('../../../assets', __FILE__)
70
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "sprockets-commonjs-mindreframer"
6
+ s.version = '0.1.0'
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.10.1"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "appraisal"
25
+ end
data/test/bar.js ADDED
@@ -0,0 +1 @@
1
+ window.bar = "Bar!"
@@ -0,0 +1 @@
1
+ module.exports = "Foo!";
data/test/source.js ADDED
@@ -0,0 +1,2 @@
1
+ //= require foo.module.js
2
+ //= require bar.js
@@ -0,0 +1,35 @@
1
+ require 'test/unit'
2
+ require 'tempfile'
3
+ require 'sprockets-commonjs'
4
+
5
+ class SprocketsCommonjsTest < Test::Unit::TestCase
6
+
7
+ TEST_DIR = File.expand_path('..', __FILE__)
8
+ LIB_DIR = File.expand_path('../lib/assets/javascripts', TEST_DIR)
9
+
10
+ attr_reader :output
11
+
12
+ def setup
13
+ env = Sprockets::Environment.new
14
+ env.register_postprocessor 'application/javascript', Sprockets::CommonJS
15
+ env.append_path TEST_DIR
16
+ env.append_path LIB_DIR
17
+ outfile = Tempfile.new('sprockets-output')
18
+ env['source.js'].write_to outfile.path
19
+ @output = File.read outfile.path
20
+ end
21
+
22
+ def test_adds_commonjs_require
23
+ assert_match %r[var require = function\(name, root\) \{], @output
24
+ end
25
+
26
+ def test_modularizes_modules
27
+ assert_match %r[require.define\(\{\"foo\":function], @output
28
+ assert_match %r["Foo!"], @output
29
+ end
30
+
31
+ def test_does_not_modularize_non_modules
32
+ assert_no_match %r[require.define\(\{\"bar\":function], @output
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-commonjs-mindreframer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex MacCaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.10.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.10.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adds CommonJS support to Sprockets
56
+ email:
57
+ - info@eribium.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Appraisals
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - assets/sprockets/commonjs.js
68
+ - examples/application.js
69
+ - examples/example.js
70
+ - examples/index.html
71
+ - examples/modules/program.cjs.js
72
+ - examples/modules/user.module.js
73
+ - gemfiles/sprockets_2_1.gemfile
74
+ - gemfiles/sprockets_2_2.gemfile
75
+ - gemfiles/sprockets_2_3.gemfile
76
+ - gemfiles/sprockets_2_4.gemfile
77
+ - gemfiles/sprockets_2_5.gemfile
78
+ - gemfiles/sprockets_2_6.gemfile
79
+ - gemfiles/sprockets_2_7.gemfile
80
+ - gemfiles/sprockets_2_8.gemfile
81
+ - lib/assets/javascripts/sprockets/commonjs.js
82
+ - lib/sprockets-commonjs.rb
83
+ - lib/sprockets/commonjs.rb
84
+ - sprockets-commonjs.gemspec
85
+ - test/bar.js
86
+ - test/foo.module.js
87
+ - test/source.js
88
+ - test/sprockets_commonjs_test.rb
89
+ homepage: ''
90
+ licenses: []
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project: sprockets-commonjs
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Adds CommonJS support to Sprockets
112
+ test_files:
113
+ - test/bar.js
114
+ - test/foo.module.js
115
+ - test/source.js
116
+ - test/sprockets_commonjs_test.rb