redjs-sprockets 0.0.2

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: 25b78f55679dd13fd1322548704040a1a5b26b56
4
+ data.tar.gz: abe6a1b8cdf72b94af2fd1474f853b88eebc9c37
5
+ SHA512:
6
+ metadata.gz: e3aeb49fb330efbc99e9e78f317d2ca8f4fa39f47ea91911f9d3a24f8ffa177085b4710b2ad207c22dfa0a8778aed204711dfc9ce3d765712626bb8605a59711
7
+ data.tar.gz: 687240293795a24312436592e8141b7332bad5b91a51bad71029b61c84a966d9e771df712c0e1880745b2c46838e3a6ccbdc3ef34186aafbdc4f175849e9cf00
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dmitry Maganov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # redjs-sprockets
2
+
3
+ ## Installation
4
+
5
+ ### Common
6
+ Add 'redjs-sprockets' to a gemfile
7
+ ~~~ruby
8
+ gem 'redjs-sprockets'
9
+ ~~~
10
+
11
+ Require 'redjs-sprockets' in a javascript manifest before any .red file
12
+ ~~~js
13
+ //= require redjs-sprockets
14
+ ~~~
15
+
16
+ ### Specific
17
+
18
+ #### Rails
19
+ For Rails that is all.
20
+
21
+ #### Middleman
22
+ Activate the extension
23
+ ~~~ruby
24
+ # config.rb
25
+ activate :redjs_sprockets
26
+ ~~~
27
+
28
+ #### Bare Sprockets
29
+ On any other platform, which include Sprockets, you can activate redjs like this
30
+ ~~~ruby
31
+ # pass a Sprockets::Base instance as an argument to the Redjs::Sprockets.register method
32
+ Redjs::Sprockets.register my_sprockets_instance
33
+ ~~~
34
+
35
+ ## Usage
36
+ Add the '.red' extension after '.js' for files that will be using the Redjs dependecy manager.
37
+ In those files four functions will be available.
38
+
39
+ #### $require( path_to_file )
40
+ Return a value associated with the file.
41
+ Will add the specified file to required ones, identical to the sprockets '//= require' directive.
42
+ Path to file can't be a variable, it must be a string.
43
+ Can be relative.
44
+
45
+ #### $requires( path_to_file )
46
+ Return a value associated with the file.
47
+ Without issuing a sprockets directive.
48
+ You must ensure inclusion of required file yourself.
49
+ Path can be varialbe.
50
+
51
+ #### $define( value )
52
+ Assotiate the value with a file.
53
+ If the value is a function, its execution result will be assotiated.
54
+
55
+ #### $defines( key, value )
56
+ Assotiate the value with the key.
57
+
58
+ ### Example
59
+
60
+ ~~~js
61
+ // app/assets/javascripts/some/folder/a.js.red
62
+
63
+ $define( 34 );
64
+ ~~~
65
+
66
+ ~~~js
67
+ // app/assets/javascripts/other/folder/b.js.red
68
+
69
+ expect( $require( 'some/folder/a' ) ).to.equal( 34 );
70
+ ~~~
71
+
72
+ ## How It Works
73
+
74
+ ### Preprocessor
75
+ It searches for '$require' and '$define' directives.
76
+ For '$require' - add the specified pathname to required assets.
77
+ For '$define' - insert a file pathname as the first argument.
78
+ If there is no '$define' in a file, then it appends '$define({ pathname }, void 0)' to the end.
79
+
80
+ ### JS side
81
+ Nothing fancy to see there.
82
+ Pretty dumb.
83
+ Because required assets are included first, there is no need for asynchronous loading or fancy things like that.
84
+ If you need them for some reason, you can just write your implementation and replace 'redjs-sprockets' in your js manifest.
@@ -0,0 +1,28 @@
1
+ ( function ( context ) {
2
+
3
+ var reds = {};
4
+
5
+ var $require = function ( key ) {
6
+
7
+ if ( reds.hasOwnProperty( key ) ) return reds[ key ];
8
+
9
+ throw new Error( '$require "' + key + '" failed' );
10
+
11
+ }
12
+
13
+ var $define = function ( key, val, force ) {
14
+
15
+ if ( reds.hasOwnProperty( key ) && ! force ) throw new Error( 'module with key "' + key + '" already exists' );
16
+
17
+ if ( typeof val == 'function' ) val = val();
18
+
19
+ reds[ key ] = val;
20
+
21
+ }
22
+
23
+ context.$require = $require;
24
+ context.$requires = $require;
25
+ context.$define = $define;
26
+ context.$defines = $define;
27
+
28
+ } )( this );
@@ -0,0 +1,32 @@
1
+ require 'middleman-core'
2
+ require 'pry'
3
+
4
+
5
+ module Redjs
6
+
7
+ module Platforms
8
+
9
+ class Middleman < ::Middleman::Extension
10
+
11
+ def initialize ( app, options_hash = {}, &block )
12
+
13
+ super
14
+
15
+ ::Tilt.register 'red', Redjs::Sprockets
16
+
17
+ app.after_configuration do
18
+
19
+ Redjs::Sprockets.register sprockets
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+
32
+ ::Middleman::Extensions.register :redjs_sprockets, Redjs::Platforms::Middleman
@@ -0,0 +1,20 @@
1
+ require 'rails/engine'
2
+
3
+
4
+ module Redjs
5
+
6
+ module Platforms
7
+
8
+ class Rails < ::Rails::Engine
9
+
10
+ initializer 'redjs_rails.setup_engine', group: :all do | app |
11
+
12
+ Redjs::Sprockets.register app.assets
13
+
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,44 @@
1
+ require 'pathname'
2
+
3
+
4
+ module Redjs
5
+
6
+ class Processor
7
+
8
+ def self.process ( path, data )
9
+
10
+ required = []
11
+
12
+ unless data.gsub! /\$define\s*\(/, "\\0'#{ path }',"
13
+
14
+ unless data =~ /\$defines\s*\(\s*['"]#{ path }['"]/
15
+
16
+ data += "\n" + "$define('#{ path }',void 0);"
17
+
18
+ end
19
+
20
+ end
21
+
22
+ data.gsub! /(\$require\s*\(\s*['"])([^'"]+)(['"]\s*\))/ do
23
+
24
+ asset_path = $2
25
+
26
+ if asset_path[ 0 ] == '.'
27
+
28
+ asset_path = Pathname.new( path ).dirname + asset_path
29
+
30
+ end
31
+
32
+ required << asset_path unless asset_path == path
33
+
34
+ "#{ $1 }#{ asset_path }#{ $3 }"
35
+
36
+ end
37
+
38
+ { data: data, required: required }
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ require 'tilt'
2
+
3
+
4
+ module Redjs
5
+
6
+ class Sprockets < Tilt::Template
7
+
8
+ self.default_mime_type = 'application/javascript'
9
+
10
+ def evaluate ( context, locals )
11
+
12
+ result = Redjs::Processor.process context.logical_path, data
13
+
14
+ result[ :required ].each{ | required_path | context.require_asset required_path }
15
+
16
+ result[ :data ]
17
+
18
+ end
19
+
20
+ def prepare
21
+ end
22
+
23
+ def self.register ( sprockets )
24
+
25
+ sprockets.register_engine 'red', self
26
+
27
+ sprockets.append_path Redjs::JAVASCRIPTS_PATH
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,31 @@
1
+ require 'set'
2
+
3
+
4
+ module Redjs
5
+
6
+ class Sprockets
7
+
8
+ def self.call ( input )
9
+
10
+ result = Redjs::Processor.process input[ :filename ], input[ :data ]
11
+
12
+ required = result[ :required ].map{ | path | input[ :environment ].resolve path }
13
+
14
+ {
15
+ data: result[ :data ],
16
+ required: Set.new( input[ :metadata ][ :required ] ) + required
17
+ }
18
+
19
+ end
20
+
21
+ def self.register ( sprockets )
22
+
23
+ sprockets.register_engine '.red', self, mime_type: 'application/javascript'
24
+
25
+ sprockets.append_path Redjs::JAVASCRIPTS_PATH
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,5 @@
1
+ module Redjs
2
+
3
+ VERSION = '0.0.2'
4
+
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'sprockets'
2
+ require 'pathname'
3
+
4
+ module Redjs
5
+
6
+ JAVASCRIPTS_PATH = Pathname.new( __FILE__ ).dirname.join( 'javascripts' ).to_s
7
+
8
+ end
9
+
10
+ require 'redjs/version'
11
+ require 'redjs/processor'
12
+ require 'redjs/sprockets/v' + Sprockets::VERSION[ 0 ]
13
+
14
+ require 'redjs/platforms/rails' if defined? Rails
15
+ require 'redjs/platforms/middleman' if defined? Middleman
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redjs-sprockets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Maganov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
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: execjs
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sprockets
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.2.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.2.2
83
+ description: Require by path, Define without key. Permissive towards Sprockets directives
84
+ and global scope pollution.
85
+ email:
86
+ - vonagam@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - LICENSE.txt
92
+ - README.md
93
+ - lib/javascripts/redjs-sprockets.js
94
+ - lib/redjs-sprockets.rb
95
+ - lib/redjs/platforms/middleman.rb
96
+ - lib/redjs/platforms/rails.rb
97
+ - lib/redjs/processor.rb
98
+ - lib/redjs/sprockets/v2.rb
99
+ - lib/redjs/sprockets/v3.rb
100
+ - lib/redjs/version.rb
101
+ homepage: https://github.com/vonagam/redjs-sprockets
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: JS dependency sugar for Sprockets.
125
+ test_files: []