sinatra-asset-snack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .*.swp
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # AssetSnack for Sinatra
2
+
3
+ A lean asset compiler for Sinatra developed specifically for Coffeescript and SASS.
4
+ It stitches all assets for a route into a single file, no uglification, no minification (configure your server to use Gzip),
5
+ just lean, fast compilation.
6
+
7
+ ## Installation
8
+ ### Bundler users
9
+
10
+ If you use Bundler, add it to your *Gemfile.*
11
+
12
+ ``` ruby
13
+ gem install sinatra-asset-snack
14
+ ```
15
+
16
+
17
+ ## Setup
18
+
19
+ Install the plugin configure your routes.
20
+
21
+ ``` ruby
22
+ require 'sinatra/asset_snack'
23
+
24
+ class App < Sinatra::Base
25
+ register Sinatra::AssetSnack
26
+ asset_map '/javascript/application.js', ['assets/js/**/*.js', 'assets/js/**/*.coffee']
27
+ asset_map '/stylesheets/application.css', ['assets/stylesheets/**/*.css', 'assets/stylesheets/**/*.scss']
28
+ end
29
+ ```
30
+
31
+ In your view / layout reference your assets using the relevant tag
32
+ ```
33
+ <link rel="stylesheet" href="/stylesheets/application.css" type="text/css" />
34
+ <script src="/javascript/application.js" type="text/javascript"></script>
35
+ ```
36
+
37
+ If you want - use the helpers to add cache busting
38
+ ```
39
+ <link rel="stylesheet" href="<%= nocache '/stylesheets/application.css' %>" type="text/css" />
40
+ <script src="<%= nocache '/javascript/application.js' %>" type="text/javascript"></script>
41
+ ```
42
+
43
+ [sinatra]: http://sinatrarb.com
44
+ [coffee-script]: http://github.com/josh/ruby-coffee-script
45
+ [sass]: http://sass-lang.com/
@@ -0,0 +1,28 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ class AssetCompiler
4
+
5
+ class << self
6
+ attr_reader :compiled_mime_type, :handled_extensions
7
+
8
+ def compile_file(file_path)
9
+ ext = File.extname(file_path).downcase[1..-1]
10
+ return unless ext && handled_extensions.include?(ext.to_sym)
11
+
12
+ file_content = File.read file_path
13
+ new.compile(file_content)
14
+ end
15
+
16
+ def handle_extensions(*extensions)
17
+ @handled_extensions = extensions
18
+ AssetSnack.register_compiler self, extensions
19
+ end
20
+
21
+ def mime_type(type)
22
+ @compiled_mime_type = type
23
+ end
24
+
25
+ end
26
+ end
27
+ end #Asset
28
+ end #GreyNomad
@@ -0,0 +1,59 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ class << self
4
+ attr_reader :compilers
5
+
6
+ def registered(app)
7
+ app.extend ClassMethods
8
+ app.send(:include, InstanceMethods)
9
+ app.send(:helpers, Helpers)
10
+ end
11
+
12
+ def register_compiler(compiler, handled_extensions)
13
+ @compilers ||= {}
14
+ handled_extensions.each do |ext|
15
+ @compilers[ext.downcase.to_sym] = compiler
16
+ end
17
+ end
18
+
19
+ def compiler_for(file_path)
20
+ ext = (File.extname(file_path) || '.').downcase[1..-1]
21
+ @compilers[ext.to_sym] unless ext.nil?
22
+ end
23
+ end # self
24
+
25
+ module ClassMethods
26
+ def asset_map(route, paths)
27
+ self.get route do
28
+ content = compile paths
29
+ [200, {'Content-Type' => content[:mime_type]}, content[:body]]
30
+ end
31
+ end
32
+ end # ClassMethods
33
+
34
+ module InstanceMethods
35
+ def compile(paths)
36
+ expand(paths).reduce({body: ''}) do |content, path|
37
+ next unless File.exists?(path)
38
+
39
+ if compiler = AssetSnack.compiler_for(path)
40
+ compiled = compiler.compile_file(path)
41
+ content_type = compiler.compiled_mime_type
42
+ else
43
+ compiled = File.read(path)
44
+ end
45
+
46
+ {
47
+ mime_type: content_type || content[:mime_type] || 'text/plain',
48
+ body: content[:body] + "\n\n/** #{File.basename path} **/\n\n" + compiled
49
+ }
50
+ end
51
+ end
52
+
53
+ def expand(paths)
54
+ paths.reduce([]) { |file_list, path| file_list + Dir.glob(path) }.uniq
55
+ end
56
+ end #InstanceMethods
57
+
58
+ end # AssetSnack
59
+ end #Sinatra
@@ -0,0 +1,12 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ class CoffeeCompiler < AssetCompiler
4
+ handle_extensions :coffee
5
+ mime_type 'text/js'
6
+
7
+ def compile(coffee_script)
8
+ CoffeeScript.compile(coffee_script, {bare: true})
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ module Helpers
4
+
5
+ def nocache(url)
6
+ "#{url}?cb=#{Time.now.to_i}"
7
+ end
8
+
9
+ end # Helpers
10
+ end # AssetSnack
11
+ end # Sinatra
@@ -0,0 +1,12 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ class SassCompiler < AssetCompiler
4
+ handle_extensions :scss
5
+ mime_type 'text/css'
6
+
7
+ def compile(sass_script)
8
+ Sass.compile(sass_script)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Sinatra
2
+ module AssetSnack
3
+ def self.version
4
+ '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'sass'
2
+ require 'coffee-script'
3
+
4
+ here = File.dirname(__FILE__)
5
+ Dir[File.join(here, 'asset_snack', '*.rb')].each { |f| require f }
@@ -0,0 +1,16 @@
1
+ require './lib/sinatra/asset_snack'
2
+ Gem::Specification.new do |s|
3
+ s.name = 'sinatra-asset-snack'
4
+ s.version = Sinatra::AssetSnack.version
5
+ s.summary = 'Real quick asset compilation of Coffeescript to Javascript and SASS to CSS for Sinatra'
6
+ s.description = 'Simple and effective asset compilation, supporting Coffeescript and SASS. A lean alternative to larger, slower asset management gems.'
7
+ s.authors = ['Ben Kitzelman']
8
+ s.email = ['benkitzelman@hotmail.com']
9
+ s.homepage = 'http://github.com/benkitzelman/sinatra-asset-snack'
10
+ s.files = `git ls-files`.strip.split("\n")
11
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
12
+
13
+ s.add_dependency 'sinatra'
14
+ s.add_dependency 'coffee-script'
15
+ s.add_dependency 'sass'
16
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-asset-snack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Kitzelman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: coffee-script
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sass
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Simple and effective asset compilation, supporting Coffeescript and SASS.
63
+ A lean alternative to larger, slower asset management gems.
64
+ email:
65
+ - benkitzelman@hotmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - README.md
72
+ - lib/sinatra/asset_snack.rb
73
+ - lib/sinatra/asset_snack/asset_compiler.rb
74
+ - lib/sinatra/asset_snack/asset_snack.rb
75
+ - lib/sinatra/asset_snack/coffee_compiler.rb
76
+ - lib/sinatra/asset_snack/helpers.rb
77
+ - lib/sinatra/asset_snack/sass_compiler.rb
78
+ - lib/sinatra/asset_snack/version.rb
79
+ - sinatra-asset-snack.gemspec
80
+ homepage: http://github.com/benkitzelman/sinatra-asset-snack
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Real quick asset compilation of Coffeescript to Javascript and SASS to CSS
104
+ for Sinatra
105
+ test_files: []