sprockets4-commonjs 0.0.7

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: a9c5c458d8dbdcb619fce6b6bcf42abb99db3058
4
+ data.tar.gz: 75f143576300d196c4cbd2346c25cabb6243dd77
5
+ SHA512:
6
+ metadata.gz: a27f190bb3cd4c576e66f09e39cba81055ab9ac88fb97cd6a42e749e196f182c220cddda28ff48846c98cd6aae06c4a6dfe6626edfa0274c02f1661f00cacdf9
7
+ data.tar.gz: 4e7b892906ec19b2fa951e6decf5279e3b68832170a662f8520b724bda4ea42d5a6218b77e5eaea78cd21d9de58e23cc469a63ad31af82aed9a8bffd388e2f62
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
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,33 @@
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 ./models`
33
+ 1. Or, require individual modules, e.g.: `//= require users.module`
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
@@ -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,3 @@
1
+ //= require_tree ./modules
2
+
3
+ var self = 'application.js';
@@ -0,0 +1,73 @@
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);
61
+ this.require.define({"modules/program.cjs":function(exports, require, module){module.exports = function(){
62
+ alert('Long live the Programs!');
63
+ };
64
+ ;}});
65
+ this.require.define({"modules/user":function(exports, require, module){var Program = require('modules/program');
66
+
67
+ module.exports = function(){
68
+ alert('Long live the Users');
69
+ Program();
70
+ };
71
+ ;}});
72
+
73
+ 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,39 @@
1
+ require 'sprockets'
2
+
3
+ module Sprockets
4
+ class CommonJS
5
+ WRAPPER = 'this.require.define({"%s":' +
6
+ 'function(exports, require, module){' +
7
+ '%s' +
8
+ ";}});\n"
9
+
10
+ EXTENSIONS = %w{.module .cjs}
11
+
12
+ def self.instance
13
+ @instance ||= new
14
+ end
15
+
16
+ def self.call(input)
17
+ instance.call(input)
18
+ end
19
+
20
+ def call(input)
21
+ if commonjs_module?(input)
22
+ required = Set.new(input[:metadata][:required])
23
+ required << input[:environment].resolve("sprockets/commonjs.js")[0]
24
+ { data: WRAPPER % [ input[:name], input[:data] ], required: required }
25
+ else
26
+ input[:data]
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def commonjs_module?(input)
33
+ EXTENSIONS.include?(File.extname(input[:name]))
34
+ end
35
+ end
36
+
37
+ register_postprocessor 'application/javascript', CommonJS
38
+ append_path File.expand_path('../../../assets', __FILE__)
39
+ end
@@ -0,0 +1 @@
1
+ require 'sprockets/commonjs'
@@ -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 = "sprockets4-commonjs"
6
+ s.version = '0.0.7'
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",">= 4.0.0.beta4"
23
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets4-commonjs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Alex MacCaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-25 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: 4.0.0.beta4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0.beta4
27
+ description: Adds CommonJS support to Sprockets
28
+ email:
29
+ - info@eribium.org
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - assets/sprockets/commonjs.js
39
+ - examples/application.js
40
+ - examples/example.js
41
+ - examples/index.html
42
+ - examples/modules/program.cjs.js
43
+ - examples/modules/user.module.js
44
+ - lib/sprockets-commonjs.rb
45
+ - lib/sprockets/commonjs.rb
46
+ - sprockets-commonjs.gemspec
47
+ homepage: ''
48
+ licenses: []
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: sprockets-commonjs
66
+ rubygems_version: 2.5.1
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Adds CommonJS support to Sprockets
70
+ test_files: []