roli-sprockets-commonjs 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6dc87438e56ebcd2b009e5d71ffdb9afdb017783
4
+ data.tar.gz: 4fe576465cebdbdb4ba8afb421574a3747a0c04f
5
+ SHA512:
6
+ metadata.gz: 271f55c0469c463c738a79748ee8e4d9ab51f0d5b027aea7a79255b10321765fd0f5eb718ef93951e30658a0c85309ac5ddc476115253468094b9112faae7dc8
7
+ data.tar.gz: f00fbdca98911d151e1dedde17493b8d18de95d86f1a20fb980b3ccf9728c98f92c2f8e13d6d8fa7fede2242b4e2b84fdbd74b06096f3a2ed1721b4b2c3bf36c
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
@@ -0,0 +1,87 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
3
+ Include:
4
+ - "**/*.jbuilder"
5
+ - "**/*.rake"
6
+ - "**/config.ru"
7
+ - "**/Gemfile"
8
+ - "**/Rakefile"
9
+ - "**/Capfile"
10
+ - "**/Guardfile"
11
+ - "**/Vagrantfile"
12
+ Exclude:
13
+ - "bin/*"
14
+ - "old/**/*"
15
+ - "db/schema.rb"
16
+ - "node_modules/**/*"
17
+ - "vendor/**/*"
18
+
19
+ Style/ClassAndModuleChildren:
20
+ Enabled: false
21
+
22
+ Style/EmptyLines:
23
+ Enabled: false
24
+
25
+ Metrics/MethodLength:
26
+ Enabled: false
27
+
28
+ Documentation:
29
+ Enabled: false
30
+
31
+ AndOr:
32
+ Enabled: false
33
+
34
+ # Allow use of empty lines to visually group code into "paragraphs"
35
+ EmptyLines:
36
+ Enabled: false
37
+ Style/EmptyLinesAroundBlockBody:
38
+ Enabled: false
39
+ Style/EmptyLinesAroundClassBody:
40
+ Enabled: false
41
+ Style/EmptyLinesAroundMethodBody:
42
+ Enabled: false
43
+ Style/EmptyLinesAroundModuleBody:
44
+ Enabled: false
45
+ Style/ModuleFunction:
46
+ Enabled: false
47
+
48
+ Style/TrailingCommaInLiteral:
49
+ Enabled: true
50
+ EnforcedStyleForMultiline: comma
51
+ Style/TrailingCommaInArguments:
52
+ Enabled: true
53
+ EnforcedStyleForMultiline: comma
54
+
55
+ Style/SpaceAroundOperators:
56
+ Enabled: false
57
+
58
+ Style/ColonMethodCall:
59
+ Enabled: false
60
+
61
+ Metrics/AbcSize:
62
+ Exclude:
63
+ - "db/migrate/*"
64
+
65
+ Style/RescueModifier:
66
+ Enabled: false
67
+
68
+ Style/AlignHash:
69
+ Enabled: false
70
+
71
+ Style/AlignParameters:
72
+ Enabled: false
73
+
74
+ Style/SpaceBeforeFirstArg:
75
+ Enabled: false
76
+
77
+ # https://viget.com/extend/just-use-double-quoted-ruby-strings
78
+ Style/StringLiterals:
79
+ EnforcedStyle: "double_quotes"
80
+ Style/StringLiteralsInInterpolation:
81
+ EnforcedStyle: "double_quotes"
82
+
83
+ Style/GuardClause:
84
+ Enabled: false
85
+
86
+ Style/DoubleNegation:
87
+ Enabled: false
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
@@ -0,0 +1,67 @@
1
+ ROLI Sprockets CommonJS
2
+ =======================
3
+
4
+ This library adds CommonJS support to
5
+ [Sprockets](https://github.com/rails/sprockets).
6
+
7
+ A fork of [maccman's](https://github.com/maccman/sprockets-commonjs) library,
8
+ which is sadly no longer maintained.
9
+
10
+
11
+ ## What is CommonJS?
12
+
13
+ The CommonJS module format is a way of encapsulating JavaScript libraries,
14
+ ensuring they have to explicitly require and export properties they use. In a
15
+ nutshell:
16
+
17
+ 1. You require in files using `require()`:
18
+
19
+ ```javascript
20
+ var asset = require('models/asset');
21
+ ```
22
+
23
+ 2. You export properties using `module.exports`:
24
+
25
+ ```javascript
26
+ var asset = function(){ /* ... */ };
27
+ module.exports = Asset;
28
+ ```
29
+
30
+
31
+ ## This library
32
+
33
+ This library adds CommonJS support to Sprockets, so it can wrap up JavaScript
34
+ files as modules, and serve them appropriately. This is done by giving any JS
35
+ files you want as modules, the `.module.js` extension.
36
+
37
+ Sprockets will then wrap up the JS library when it's requested, with the
38
+ following:
39
+
40
+ ```javascript
41
+ require.define({'library/name': function(exports, require, module){
42
+ /* Your library */
43
+ }});
44
+ ```
45
+
46
+ `require.define()` is defined inside `commonjs.js`, which you'll need to
47
+ include in the page before any modules are loaded.
48
+
49
+ ```javascript
50
+ // Inside application.js
51
+ //
52
+ // require_tree .
53
+ ```
54
+
55
+ One caveat to the approach this library takes, is that dependencies loaded
56
+ through `require()` will not be added to the dependency graph. This library
57
+ will not parse the AST tree for require calls. This decision has been made for
58
+ a variety of reasons, but it does mean you need to require files through both
59
+ CommonJS and Sprockets.
60
+
61
+ ## Usage
62
+
63
+ 1. Add `gem "roli-sprockets-commonjs"` to your `Gemfile`
64
+ 1. Add `.module.js` to any JavaScript files you want as modules, i.e.
65
+ `users.module.js`
66
+ 1. Require all the modules, e.g.: `//= require_tree ./models`
67
+ 1. Or, require individual modules, e.g.: `//= require users.module`
@@ -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,2 @@
1
+ # rubocop:disable Style/FileName
2
+ require "sprockets/commonjs"
@@ -0,0 +1,54 @@
1
+ require "sprockets"
2
+ require "tilt"
3
+
4
+ module Sprockets
5
+ class CommonJS < Tilt::Template
6
+
7
+ EXTENSIONS = %w(.module .cjs).freeze
8
+
9
+ class << self
10
+ attr_accessor :default_namespace
11
+ end
12
+
13
+ self.default_mime_type = "application/javascript"
14
+ self.default_namespace = "this.require"
15
+
16
+ protected
17
+
18
+ def prepare
19
+ @namespace = self.class.default_namespace
20
+ end
21
+
22
+ def evaluate(scope, _locals, &_block)
23
+ if commonjs_module?(scope)
24
+ scope.require_asset "sprockets/commonjs"
25
+ wrap_code(namespace, module_name(scope), data)
26
+ else
27
+ data
28
+ end
29
+ end
30
+
31
+
32
+ private
33
+
34
+ attr_reader :namespace
35
+
36
+ def wrap_code(ns, name, code)
37
+ "#{ns}.define({#{name}:function(exports, require, module){#{code};}});\n"
38
+ end
39
+
40
+ def commonjs_module?(scope)
41
+ EXTENSIONS.include?(File.extname(scope.logical_path))
42
+ end
43
+
44
+ def module_name(scope)
45
+ scope.logical_path
46
+ .gsub(%r{\A\.?/}, "") # Remove relative paths
47
+ .chomp(".module") # Remove module ext
48
+ .inspect
49
+ end
50
+ end
51
+
52
+ register_postprocessor "application/javascript", CommonJS
53
+ append_path File.expand_path("../../../assets", __FILE__)
54
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "roli-sprockets-commonjs"
6
+ s.version = "0.0.10"
7
+ s.authors = ["ROLI", "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.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ # specify any dependencies here; for example:
19
+ # s.add_development_dependency "rspec"
20
+ s.add_runtime_dependency "sprockets", ">=2.10.1"
21
+ s.add_development_dependency "rubocop"
22
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roli-sprockets-commonjs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - ROLI
8
+ - Alex MacCaw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sprockets
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 2.10.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 2.10.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: rubocop
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Adds CommonJS support to Sprockets
43
+ email:
44
+ - info@eribium.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rubocop.yml"
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - assets/sprockets/commonjs.js
55
+ - examples/application.js
56
+ - examples/example.js
57
+ - examples/index.html
58
+ - examples/modules/program.cjs.js
59
+ - examples/modules/user.module.js
60
+ - lib/roli-sprockets-commonjs.rb
61
+ - lib/sprockets/commonjs.rb
62
+ - roli-sprockets-commonjs.gemspec
63
+ homepage: ''
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.5
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Adds CommonJS support to Sprockets
86
+ test_files: []