roda-opal_assets 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c9dcd57976ad4f43162650970c924cc97c7c4c1
4
- data.tar.gz: b96333d25cd36f1677bc6abb66aeab6c71a559ad
3
+ metadata.gz: 4268ec439af85776a734938139a570992e58f4f9
4
+ data.tar.gz: ce2ad9a1027e4c30ac9f006b3a359af5712f780b
5
5
  SHA512:
6
- metadata.gz: 677bdf9ba26804fe8064f265299b159223d658e2be766826196831a4afc28dcb41f0509f06421e195cab84751a4dc1226e34b9c6e53ccfa764a90ecbd54afe7c
7
- data.tar.gz: d9346711abd39247dd19ace123d9da3150b9f462a299831a43f2b6a9c6fc6202315ea879d9abf1b097bf0ca8bbc231ad2ec04d86e4d95e97245525aabee0fb4c
6
+ metadata.gz: c1c3bae8afee858877712c60e741582ef5f4db030067968173830b7188cd0f764d96c23441ad96312f7a56636f40c97eec9a4a676fa723aa1fba92e7db9b84e6
7
+ data.tar.gz: ff8ef11217a7e6869c67062f7bb1a1ce900e039976a6274e60823618e5c1385d4b63bd7468feffadb5e7f261c44052859e650dd5250e71a6ff5a1641e8236a78
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Roda::OpalAssets
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/roda/opal_assets`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Simple compilation for Opal apps on the Roda web framework for Ruby.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,15 +20,37 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ In your Roda app:
26
24
 
27
- ## Development
25
+ ```ruby
26
+ class App < Roda
27
+ assets = Roda::OpalAssets.new
28
28
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+ route do |r|
30
+ assets.route r
30
31
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+ # Other routes here
33
+ end
32
34
 
33
- ## Contributing
35
+ define_method(:js) { |file| assets.js file }
36
+ define_method(:stylesheet) { |file| assets.stylesheet file }
37
+ end
38
+ ```
39
+
40
+ Then you can put your Ruby and JS assets in `assets/js` and your stylesheets in `assets/css`.
34
41
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/roda-opal_assets. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+ Inside your views, you just need to call the `js` and `stylesheet` methods above. Here is an example `layout.slim` template:
43
+
44
+ ```slim
45
+ doctype 5
46
+ html
47
+ head
48
+ title My App
49
+ == js 'app'
50
+
51
+ body= yield
52
+ ```
53
+
54
+ ## Contributing
36
55
 
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/clearwater-rb/roda-opal_assets. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -1,3 +1,4 @@
1
+ require "yaml"
1
2
  require "roda/opal_assets/version"
2
3
  require "roda"
3
4
  require "opal"
@@ -5,45 +6,96 @@ require "uglifier" if ENV['RACK_ENV'] == 'production'
5
6
 
6
7
  class Roda
7
8
  class OpalAssets
8
- def initialize env: ENV['RACK_ENV']
9
- @env = env
9
+ def initialize env: ENV.fetch('RACK_ENV') { 'development' }
10
+ @env = env.to_sym
11
+ @assets = []
12
+ @file_cache = {}
10
13
 
11
- Opal::Config.source_map_enabled = !production?
12
14
  sprockets
13
- source_maps unless production?
15
+
16
+ source_maps
14
17
  end
15
18
 
16
19
  def route r
17
- r.on source_map_prefix[1..-1] do
18
- r.run source_maps
19
- end
20
- r.on 'assets/js' do
21
- r.run sprockets
22
- end
23
- r.on 'assets/css' do
24
- r.run sprockets
20
+ unless production?
21
+ r.on source_map_prefix[1..-1] do
22
+ r.run source_maps
23
+ end
24
+ r.on 'assets/js' do
25
+ r.run sprockets
26
+ end
27
+ r.on 'assets/css' do
28
+ r.run sprockets
29
+ end
25
30
  end
26
31
  end
27
32
 
28
33
  def js file
29
- ::Opal::Sprockets.javascript_include_tag(
30
- file,
31
- sprockets: sprockets,
32
- prefix: '/assets/js',
33
- debug: !production?,
34
- )
34
+ file << '.js' unless file.end_with? '.js'
35
+ scripts = ''
36
+
37
+ if production?
38
+ scripts << %{<script src="/assets/#{manifest[file]}"></script>\n}
39
+ else
40
+ asset = sprockets[file]
41
+
42
+ asset.to_a.each { |dependency|
43
+ scripts << %{<script src="/assets/js/#{dependency.digest_path}?body=1"></script>\n}
44
+ }
45
+
46
+ scripts << %{<script>#{opal_boot_code file}</script>}
47
+ end
48
+
49
+ scripts
35
50
  end
36
51
 
37
52
  def stylesheet file, media: :all
38
- asset = sprockets["#{file}.css"]
39
- if asset.nil?
40
- raise "File not found: #{file}.css"
41
- end
53
+ file << '.css' unless file.end_with? '.css'
54
+
55
+ path = if production?
56
+ "/assets/#{manifest[file]}"
57
+ else
58
+ asset = sprockets[file]
59
+
60
+ if asset.nil?
61
+ raise "File not found: #{file}.css"
62
+ end
63
+
64
+ asset.filename.to_s.sub(Dir.pwd, '')
65
+ end
42
66
 
43
- path = asset.filename.to_s.sub(Dir.pwd, '')
44
67
  %{<link href="#{path}" media="#{media}" rel="stylesheet" />}
45
68
  end
46
69
 
70
+ def << asset
71
+ @assets << asset
72
+ end
73
+
74
+ def build
75
+ FileUtils.mkdir_p 'public/assets'
76
+
77
+ @manifest = @assets.each_with_object({}) do |file, hash|
78
+ print "Compiling #{file}..."
79
+ asset = sprockets[file]
80
+ hash[file] = asset.digest_path
81
+ compile_file file, "public/assets/#{asset.digest_path}"
82
+ puts ' done'
83
+ end
84
+
85
+ File.write 'assets.yml', YAML.dump(manifest)
86
+ end
87
+
88
+ def compile_file file, output_filename
89
+ compiled = sprockets[file].to_s + opal_boot_code(file)
90
+
91
+ File.write output_filename, compiled
92
+ nil
93
+ end
94
+
95
+ def compile file
96
+ sprockets[file].to_s
97
+ end
98
+
47
99
  def sprockets
48
100
  return @sprockets if defined? @sprockets
49
101
 
@@ -66,8 +118,16 @@ class Roda
66
118
  def source_maps
67
119
  return @source_maps if defined? @source_maps
68
120
 
121
+ source_map_handler = if supports_opal_config?
122
+ Opal::Config
123
+ else
124
+ Opal::Processor
125
+ end
126
+
127
+ source_map_handler.source_map_enabled = !production? && min_opal_version?('0.8.0')
128
+
69
129
  source_maps = Opal::SourceMapServer.new(sprockets, source_map_prefix)
70
- unless production?
130
+ if !production? && min_opal_version?('0.8.0')
71
131
  ::Opal::Sprockets::SourceMapHeaderPatch.inject!(source_map_prefix)
72
132
  end
73
133
 
@@ -79,8 +139,39 @@ class Roda
79
139
  end
80
140
  end
81
141
 
142
+ def manifest
143
+ return @manifest if defined? @manifest
144
+
145
+ @manifest = YAML.load_file 'assets.yml'
146
+ unless @manifest
147
+ warn 'Assets manifest is broken'
148
+ end
149
+
150
+ @manifest
151
+ end
152
+
82
153
  def production?
83
- @env == 'production'
154
+ @env == :production
155
+ end
156
+
157
+ def opal_boot_code file
158
+ if min_opal_version? '0.9.0'
159
+ Opal::Sprockets.load_asset(file, sprockets)
160
+ elsif min_opal_version? '0.8.0'
161
+ Opal::Processor.load_asset_code(sprockets, file)
162
+ else
163
+ ''
164
+ end
165
+ end
166
+
167
+ def supports_opal_config?
168
+ min_opal_version? '0.8.0'
169
+ end
170
+
171
+ # Check for minimum Opal version, but do it in a weird way because Array
172
+ # implements <=> but doesn't include Comparable
173
+ def min_opal_version? version
174
+ (Opal::VERSION.split('.').map(&:to_i) <=> version.split('.').map(&:to_i)) >= 0
84
175
  end
85
176
  end
86
177
  end
@@ -1,5 +1,5 @@
1
1
  class Roda
2
2
  class OpalAssets
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -17,9 +17,11 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_runtime_dependency "opal", "~> 0.9", "< 0.12.0"
20
+ spec.add_runtime_dependency "opal", "~> 0.7", "< 0.11.0"
21
+ spec.add_runtime_dependency "sprockets", "~> 3.6.0"
21
22
  spec.add_runtime_dependency "roda"
22
23
  spec.add_runtime_dependency "uglifier", "~> 3.0"
24
+ spec.add_runtime_dependency "rack", "~> 1.0"
23
25
 
24
26
  spec.add_development_dependency "bundler", "~> 1.12"
25
27
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-opal_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Gaskins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-09 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -16,20 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.9'
19
+ version: '0.7'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 0.12.0
22
+ version: 0.11.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.9'
29
+ version: '0.7'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.12.0
32
+ version: 0.11.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: sprockets
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.6.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 3.6.0
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: roda
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -58,6 +72,20 @@ dependencies:
58
72
  - - "~>"
59
73
  - !ruby/object:Gem::Version
60
74
  version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rack
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.0'
61
89
  - !ruby/object:Gem::Dependency
62
90
  name: bundler
63
91
  requirement: !ruby/object:Gem::Requirement