checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 803f622685cadb160b0cf327865bf93587e178a4ffffead1f8605f9934cd6416
4
+ data.tar.gz: 5e41faf3bfb89d88005c3667d916ace976a4800c1b8336d1763e24b554b5dcd6
5
+ SHA512:
6
+ metadata.gz: dd37845719179bf39ec81db748348b6d761a7877d1a0e3ddc20eb74115f45f6c0992c014a3be9087b3cdc432ce91faf87f2fcacc0bcdc2bd4a3c6dacc1da64ef
7
+ data.tar.gz: 5741fd20f61a7bde1f5acd7db3aa440a9613e125edc24b088e52a9ad3e6e1dec3d01c32c754a8120aadad66f2fb1195e58748642b6e4bfae8d1c169ddd7ae128
data/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ # editorconfig
2
+ root = true
3
+
4
+ # Unix-style newlines with a newline ending every file
5
+ [*]
6
+ charset = utf-8
7
+ end_of_line = lf
8
+ indent_size = 2
9
+ indent_style = space
10
+ insert_final_newline = true
11
+ trim_trailing_whitespace = true
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle
2
+ /.config
3
+ /coverage
4
+ /doc
5
+ /pkg
6
+ /spec/examples.txt
7
+ /tmp
8
+ /vendor/bundle
9
+
10
+ *.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,211 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'bin/**'
7
+ - 'config/initializers/integration*'
8
+ - 'db/schema.rb'
9
+ - 'vendor/**'
10
+ TargetRubyVersion: 2.5
11
+ TargetRailsVersion: 5.2
12
+
13
+
14
+
15
+ # -----------------------------------------------------------------------------
16
+ # RAILS
17
+
18
+ Rails:
19
+ Enabled: true
20
+
21
+ #
22
+ # False positives:
23
+ # * On Acfs::Resource#update_attriubutes!
24
+ #
25
+ Rails/ActiveRecordAliases:
26
+ Exclude:
27
+ - spec/**/*_spec.rb
28
+
29
+ #
30
+ # False positives:
31
+ # * On embedded models in migrations.
32
+ #
33
+ Rails/ApplicationRecord:
34
+ Exclude:
35
+ - 'db/**/*'
36
+
37
+ #
38
+ # Often when triggered it just wants to pass joined paths as separate
39
+ # arguments but that can hurt readability.
40
+ #
41
+ Rails/FilePath:
42
+ Enabled: false
43
+
44
+ #
45
+ # False positives:
46
+ # * Specs and migrations often need to update things without validations.
47
+ #
48
+ Rails/SkipsModelValidations:
49
+ Exclude:
50
+ - 'db/migrations/**.rb'
51
+ - 'spec/**/*_spec.rb'
52
+
53
+
54
+
55
+ # -----------------------------------------------------------------------------
56
+ # RSPEC
57
+
58
+ RSpec:
59
+ Include:
60
+ - 'spec/**/*_spec.rb'
61
+ - 'spec/spec_helper.rb'
62
+ - 'spec/rails_helper.rb'
63
+
64
+ RSpec/ContextWording:
65
+ Enabled: false
66
+
67
+ RSpec/ExampleLength:
68
+ Enabled: false
69
+
70
+ RSpec/ExpectInHook:
71
+ Enabled: false
72
+
73
+ RSpec/FilePath:
74
+ Exclude:
75
+ - 'spec/client/**/*_spec.rb'
76
+
77
+ RSpec/MessageSpies:
78
+ Enabled: false
79
+
80
+ RSpec/MissingExampleGroupArgument:
81
+ Enabled: false
82
+
83
+ RSpec/MultipleExpectations:
84
+ Enabled: false
85
+
86
+ RSpec/NestedGroups:
87
+ Max: 5
88
+
89
+ # -----------------------------------------------------------------------------
90
+ # Layout
91
+
92
+ Layout/AlignParameters:
93
+ EnforcedStyle: with_fixed_indentation
94
+
95
+ Layout/CaseIndentation:
96
+ EnforcedStyle: end
97
+ SupportedStyles:
98
+ - case
99
+ - end
100
+ IndentOneStep: true
101
+
102
+ Layout/MultilineMethodCallIndentation:
103
+ EnforcedStyle: indented
104
+
105
+ Layout/SpaceInsideBlockBraces:
106
+ EnforcedStyle: space
107
+ EnforcedStyleForEmptyBraces: no_space
108
+ SpaceBeforeBlockParameters: false
109
+
110
+ Layout/SpaceInsideHashLiteralBraces:
111
+ EnforcedStyle: no_space
112
+
113
+
114
+
115
+ # -----------------------------------------------------------------------------
116
+ # Lint
117
+
118
+ #
119
+ # False positives:
120
+ # * expect { something }.to change { something } often triggers this
121
+ #
122
+ Lint/AmbiguousBlockAssociation:
123
+ Exclude:
124
+ - 'spec/**/*_spec.rb'
125
+
126
+
127
+
128
+ # -----------------------------------------------------------------------------
129
+ # Metrics
130
+
131
+ Metrics/BlockLength:
132
+ Exclude:
133
+ - 'db/**/*'
134
+ - 'spec/**/*'
135
+ - 'config/**/*'
136
+ - '**/*.rake'
137
+
138
+ Metrics/ClassLength:
139
+ Max: 200
140
+
141
+ Metrics/LineLength:
142
+ Exclude:
143
+ - 'db/**/*'
144
+ - 'spec/**/*'
145
+ - 'config/**/*'
146
+
147
+ Metrics/MethodLength:
148
+ Exclude:
149
+ - 'db/**/*'
150
+
151
+
152
+
153
+ # -----------------------------------------------------------------------------
154
+ # Style
155
+
156
+ Style/BracesAroundHashParameters:
157
+ EnforcedStyle: context_dependent
158
+
159
+ #
160
+ # Both styles or mixtures are reasonable
161
+ #
162
+ Style/ClassAndModuleChildren:
163
+ EnforcedStyle: compact
164
+ Enabled: false
165
+
166
+ #
167
+ # Would be better but unlikely...
168
+ #
169
+ Style/Documentation:
170
+ Enabled: false
171
+
172
+ #
173
+ # Scripts might include on top-level
174
+ #
175
+ Style/MixinUsage:
176
+ Exclude:
177
+ - 'scripts/**/*'
178
+
179
+ Style/RaiseArgs:
180
+ EnforcedStyle: compact
181
+
182
+ #
183
+ # Quick single line rescues in specs
184
+ #
185
+ Style/RescueModifier:
186
+ Exclude:
187
+ - 'spec/**/*_spec.rb'
188
+
189
+ #
190
+ # Quick single line rescues in specs
191
+ #
192
+ Style/RescueStandardError:
193
+ Exclude:
194
+ - 'spec/**/*_spec.rb'
195
+
196
+ #
197
+ # Often used pattern in chaining subjects in specs
198
+ #
199
+ Style/Semicolon:
200
+ Exclude:
201
+ - 'spec/**/*_spec.rb'
202
+
203
+ Style/SignalException:
204
+ EnforcedStyle: only_raise
205
+
206
+ Style/TrailingCommaInArrayLiteral:
207
+ EnforcedStyleForMultiline: comma
208
+
209
+ Style/TrailingCommaInHashLiteral:
210
+ EnforcedStyleForMultiline: comma
211
+
data/.travis.yml ADDED
@@ -0,0 +1,37 @@
1
+ dist: xenial
2
+ language: ruby
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.6.3
7
+ - 2.5.5
8
+
9
+ gemfile:
10
+ - gemfiles/rails-5-2.gemfile
11
+ - gemfiles/rails-master.gemfile
12
+
13
+ before_install:
14
+ - gem update --system
15
+ - gem install bundler
16
+
17
+ script:
18
+ - bundle exec rspec spec
19
+
20
+ jobs:
21
+ allow_failures:
22
+ - gemfile: gemfiles/rails-master.gemfile
23
+
24
+ include:
25
+ - stage: Release
26
+ rvm: 2.6.3
27
+ env: []
28
+ script: skip
29
+ install: skip
30
+ deploy:
31
+ provider: rubygems
32
+ api_key:
33
+ secure: m5gUzb8J40lo4EJ2xE8XwoeHq4S3ZEDE+AyikDHzGKc5NEQQcCRoc469UlDO7fYfBHGKLh2eEe6+e/T+dyRE/C0qE/tfd6pVWf8xdyzt9+rB0zM3k7pR2Aud2hZ8NFF4bAQ28OvQ2y9pWWLVS9Bn/C0Xou951yZM+vufPVZpX7+08Aw+ZdPQrmg+v9SxsvWElm/is0CMjsHxpEy1vpyyBlF/DhNpHgd+69nupyTuZP8bg3lirZexek0xNaccNpflmrUeRwE7yofVGMfrRhvm5kOniYI+sQl/Zwru9+hK2WaSzfX/ou4ENihiLDGo5gdWeYcyLbS61yQARsRn21cN6SA0iYVuVnAruHC3uoxRh7ty1H8s3aUJnPTnnBj5cnfP9pqOyZI8zFi54CujkEFbRVLlWfGjKtCScgFBwkpXS5aB6CBplulMrX3kz/jYXFgodXLsV10WD725WGJ2Jkb52PoT5i2GyA/Ao746s7+wPIEV/l8NAurcroSHOzr9PcAUG/CwdzgidSnLfwl+qplMCGaUHtdHkPaRHqJL9j4BmcqoGVMaU0a4mAXsrlJG/drT2PMMrREKChtnQPtWg3L9HHDHBIKuXZCIQ7G9n2Dz7lpWnyKZKjJTcFyZWbbnKHLXlE1KDBUiG9qqrYbpky+IEPUt4jZ1W8hx4tKGjNlNwjU=
34
+ gem: rails-assets-manifest
35
+ on:
36
+ tags: true
37
+ repo: jgraichen/rails-assets-manifest
data/CHANGELOG.md ADDED
@@ -0,0 +1,65 @@
1
+ # CHANGELOG
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Changelog](http://keepachangelog.com/).
5
+
6
+
7
+
8
+ ## Unreleased
9
+ ---
10
+
11
+ ### New
12
+
13
+ ### Changes
14
+
15
+ ### Fixes
16
+
17
+ ### Breaks
18
+
19
+
20
+ ## 2.1.0 - (2019-09-11)
21
+ ---
22
+
23
+ ### New
24
+ * Automatically add `crossorigin="anonymous"` for SRI resources
25
+ * Separate manifest caching and eager loading
26
+
27
+ ### Changes
28
+ * Remove defunct onboot manifest validation (f3fe8f57)
29
+
30
+
31
+ ## 2.0.1 - (2019-08-09)
32
+ ---
33
+
34
+ ### Fixes
35
+ * Fix check if assets option node already exists
36
+
37
+
38
+ ## 2.0.0 - (2019-08-08)
39
+ ---
40
+
41
+ ### New
42
+ * Accept glob patterns for `manifests` option
43
+
44
+
45
+ ### Breaks
46
+ * Change `manifest` configuration option into `manifests` to support multiple files
47
+
48
+
49
+ ## 1.1.0 - (2019-08-08)
50
+ ---
51
+
52
+ ### New
53
+ * Add passthrough option to load assets from other plugins (e.g. sprockets)
54
+
55
+ ### Changes
56
+ * Only default to add available integrity if asset is from the manifest
57
+
58
+
59
+ ## 1.0.0 - (2019-08-07)
60
+ ---
61
+
62
+ ### New
63
+ * Initial release
64
+
65
+
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load gem's dependencies
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jan Graichen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Rails::Assets::Manifest
2
+
3
+ Load all assets in your rails application from a `manifest.json`, e.g. generated by [webpack-assets-manifest](https://github.com/webdeveric/webpack-assets-manifest).
4
+
5
+ This gem does not make any assumption on which tool to use to make your assets not how to configure or invoke it. It only requires a manifest JSON. The Manifest must either be a simple string map or must map to objects having a `'src'` attribute. Subresource integrity is supported too:
6
+
7
+ ```json
8
+ {
9
+ "application.css": {
10
+ "src": "2b16adf6f756625a0194.css",
11
+ "integrity": "sha384-/oreyvcV6U6htGZD0fDWR8/Txezke8KhD0QNgHb660hSaW7M+ZzxxuB4Vo+PuAC9"
12
+ },
13
+ "application.js": {
14
+ "src": "2b16adf6f756625a0194.js",
15
+ "integrity": "sha384-iJ55fQQApbQGxWEWSbWStBabi+yNGxZSQy/010+1Dhxl+rymyhGF4NtjUkOsYv7B"
16
+ }
17
+ }
18
+ ```
19
+
20
+ This gem does not add new helper methods but extends the existing helpers. SRI is automatically added if available and within a secure context. A `crossorigin="anonymous"` attribute is automatically added if non is present.
21
+
22
+ ```slim
23
+ html
24
+ head
25
+ = stylesheet_link_tag 'application', media: 'all'
26
+ = javascript_include_tag 'application'
27
+ ```
28
+
29
+ ```html
30
+ <link rel="stylesheet" media="all" href="2b16adf6f756625a0194.css" integrity="sha384-/oreyvcV6U6htGZD0fDWR8/Txezke8KhD0QNgHb660hSaW7M+ZzxxuB4Vo+PuAC9" crossorigin="anonymous">
31
+ <script src="2b16adf6f756625a0194.js" integrity="sha384-iJ55fQQApbQGxWEWSbWStBabi+yNGxZSQy/010+1Dhxl+rymyhGF4NtjUkOsYv7B" crossorigin="anonymous"></script>
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'rails-assets-manifest'
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ The manifest path can be configured e.g. in an environment:
45
+
46
+ ```ruby
47
+ config.assets.manifests = %w(public/.asset-manifest.json)
48
+ ```
49
+
50
+ If `config.cache_classes` is set to `true` the manifest file be loaded once on boot.
51
+
52
+ Assets included with `integrity: true` will raise an error if the integrity option is missing in the manifest.
53
+
54
+ Multiple manifests can be loaded by specifying multiple files. Glob patterns are supported:
55
+
56
+ ```ruby
57
+ config.assets.manifests = %w(public/.manifest.*.json lib/manifest.json)
58
+ ```
59
+
60
+ ## TODO
61
+
62
+ * Override/Join with `asset_host` URL?
63
+
64
+ ## Contributing
65
+
66
+ 1. [Fork it](http://github.com/jgraichen/rails-assets-manifest/fork)
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit specs for your feature so that I do not break it later
69
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 5. Push to the branch (`git push origin my-new-feature`)
71
+ 6. Create new Pull Request
72
+
73
+ ## MIT License
74
+
75
+ Copyright (c) 2018 Jan Graichen
76
+
77
+ Permission is hereby granted, free of charge, to any person obtaining a copy
78
+ of this software and associated documentation files (the "Software"), to deal
79
+ in the Software without restriction, including without limitation the rights
80
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
81
+ copies of the Software, and to permit persons to whom the Software is
82
+ furnished to do so, subject to the following conditions:
83
+
84
+ The above copyright notice and this permission notice shall be included in all
85
+ copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
88
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
89
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
90
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
91
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
92
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
93
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rails/assets/manifest'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=#x27;\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gemfiles/rails-4-2.gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load root Gemfile
6
+ eval_gemfile '../Gemfile'
7
+
8
+ gem 'activesupport', '~> 4.2.0'
9
+ gem 'railties', '~> 4.2.0'
data/gemfiles/rails-5-0.gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load root Gemfile
6
+ eval_gemfile '../Gemfile'
7
+
8
+ gem 'activesupport', '~> 5.0.0'
9
+ gem 'railties', '~> 5.0.0'
data/gemfiles/rails-5-1.gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load root Gemfile
6
+ eval_gemfile '../Gemfile'
7
+
8
+ gem 'activesupport', '~> 5.1.0'
9
+ gem 'railties', '~> 5.1.0'
data/gemfiles/rails-5-2.gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load root Gemfile
6
+ eval_gemfile '../Gemfile'
7
+
8
+ gem 'activesupport', '~> 5.2.0'
9
+ gem 'railties', '~> 5.2.0'
data/gemfiles/rails-master.gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Load root Gemfile
6
+ eval_gemfile '../Gemfile'
7
+
8
+ gem 'activesupport', git: 'https://github.com/rails/rails'
9
+ gem 'railties', git: 'https://github.com/rails/rails'
data/lib/rails/assets/manifest.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+
6
+ require 'rails/assets/manifest/version'
7
+
8
+ module Rails
9
+ module Assets
10
+ module Manifest
11
+ require 'rails/assets/manifest/manifest'
12
+ require 'rails/assets/manifest/helper'
13
+ require 'rails/assets/manifest/railtie' if defined?(Rails::Railtie)
14
+
15
+ class ManifestMissing < StandardError; end
16
+ class ManifestInvalid < StandardError; end
17
+ class EntryMissing < StandardError; end
18
+ class IntegrityMissing < StandardError; end
19
+
20
+ class << self
21
+ delegate :lookup, :lookup!, to: :instance
22
+
23
+ attr_reader :instance
24
+
25
+ def prepare!
26
+ @instance = begin
27
+ config = Rails.application.config
28
+
29
+ Manifest.new \
30
+ files: config.assets.manifests,
31
+ cache: config.cache_classes
32
+ end
33
+ end
34
+
35
+ def eager_load!
36
+ return unless @instance
37
+ @instance.eager_load!
38
+ end
39
+
40
+ def passthrough?
41
+ Rails.application.config.assets.passthrough
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/rails/assets/manifest/helper.rb ADDED
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails::Assets::Manifest
4
+ module Helper
5
+ def compute_asset_path(name, **_kwargs)
6
+ ::Rails::Assets::Manifest.lookup!(name).src
7
+ rescue EntryMissing => e
8
+ return super if Rails::Assets::Manifest.passthrough?
9
+ raise
10
+ end
11
+
12
+ def asset_integrity(name, **kwargs)
13
+ ::Rails::Assets::Manifest.lookup!(path_with_extname(name, **kwargs)).integrity
14
+ rescue EntryMissing => e
15
+ return super if Rails::Assets::Manifest.passthrough?
16
+ raise
17
+ end
18
+
19
+ def javascript_include_tag(*sources, integrity: nil, **kwargs)
20
+ return super(*sources, **kwargs) unless manifest_use_integrity?(integrity)
21
+
22
+ with_integrity(sources, integrity, :javascript, **kwargs) do |source, options|
23
+ super(source, options)
24
+ end
25
+ end
26
+
27
+ def stylesheet_link_tag(*sources, integrity: nil, **kwargs)
28
+ return super(*sources, **kwargs) unless manifest_use_integrity?(integrity)
29
+
30
+ with_integrity(sources, integrity, :stylesheet, **kwargs) do |source, options|
31
+ super(source, options)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def manifest_use_integrity?(option)
38
+ return false unless secure_request_context?
39
+
40
+ option || option.nil?
41
+ end
42
+
43
+ # http://www.w3.org/TR/SRI/#non-secure-contexts-remain-non-secure
44
+ def secure_request_context?
45
+ respond_to?(:request) && self.request && (self.request.local? || self.request.ssl?)
46
+ end
47
+
48
+ def with_integrity(sources, required, type, **kwargs)
49
+ sources.map do |source|
50
+ path = path_with_extname(source, type: type, **kwargs)
51
+
52
+ # integrity hash passed directly
53
+ if required.is_a?(String)
54
+ next yield(source, integrity: required, crossorigin: 'anonymous', **kwargs)
55
+
56
+ # Explicit passed `true` option
57
+ elsif required
58
+ integrity = asset_integrity(source, type: type, **kwargs)
59
+
60
+ if !integrity
61
+ raise IntegrityMissing.new "SRI missing for #{path}"
62
+ end
63
+
64
+ next yield(source, integrity: integrity, crossorigin: 'anonymous', **kwargs)
65
+
66
+ # No integrity option passed or `nil` default from above
67
+ elsif required.nil?
68
+ entry = ::Rails::Assets::Manifest.lookup(path)
69
+
70
+ # Only if it is an asset from our manifest and there is an integrity
71
+ # we default to adding one
72
+ if(entry && entry.integrity)
73
+ next yield(source, integrity: entry.integrity, crossorigin: 'anonymous', **kwargs)
74
+ end
75
+ end
76
+
77
+ yield(source, **kwargs)
78
+ end.join.html_safe
79
+ end
80
+
81
+ def path_with_extname(path, options)
82
+ path = path.to_s
83
+ "#{path}#{compute_asset_extname(path, options)}"
84
+ end
85
+ end
86
+ end
data/lib/rails/assets/manifest/manifest.rb ADDED
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails::Assets::Manifest
4
+ class Manifest
5
+ attr_reader :path
6
+
7
+ def initialize(files:, cache: true)
8
+ @files = Array(files).flatten.each(&:freeze).freeze
9
+ @cache = cache
10
+ end
11
+
12
+ def cache?
13
+ @cache
14
+ end
15
+
16
+ def lookup(name)
17
+ data[name.to_s].presence
18
+ end
19
+
20
+ def lookup!(name)
21
+ lookup(name) || begin
22
+ raise EntryMissing.new <<~ERROR
23
+ Can't find #{name} in #{path}. Your manifests contain:
24
+ #{JSON.pretty_generate(data.keys)}
25
+ ERROR
26
+ end
27
+ end
28
+
29
+ def key?(name)
30
+ data.key?(name.to_s)
31
+ end
32
+
33
+ def eager_load!
34
+ data
35
+ end
36
+
37
+ private
38
+
39
+ Entry = Struct.new(:src, :integrity) do
40
+ def integrity?
41
+ integrity.present?
42
+ end
43
+ end
44
+
45
+ def data
46
+ if cache?
47
+ @data ||= load
48
+ else
49
+ load
50
+ end
51
+ end
52
+
53
+ def files
54
+ @files.map {|path| Dir.glob(path) }.flatten
55
+ end
56
+
57
+ def load
58
+ files.each_with_object({}) do |file, entries|
59
+ JSON.parse(File.read(file)).each_pair do |key, entry|
60
+ if entry.is_a?(String)
61
+ entries[key] = Entry.new(entry, nil)
62
+ elsif entry.is_a?(Hash) && entry.key?('src')
63
+ entries[key] = Entry.new(entry['src'], entry['integrity'])
64
+ else
65
+ raise "Invalid manifest entry: #{entry.inspect}"
66
+ end
67
+ end
68
+ end
69
+ rescue Errno::ENOENT => e
70
+ raise ManifestMissing.new <<~ERROR
71
+ Asset manifest does not exist: #{e}
72
+ ERROR
73
+ rescue => e
74
+ raise ManifestInvalid.new <<~ERROR
75
+ Asset manifest could not be loaded: #{e}
76
+ ERROR
77
+ end
78
+ end
79
+ end
data/lib/rails/assets/manifest/railtie.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Assets
5
+ module Manifest
6
+ class Railtie < ::Rails::Railtie
7
+ # If this plugin is used with sprockets this option
8
+ # already exists and must not be overriden. Otherwise
9
+ # all sprockets default options are removed breaking
10
+ # sprockets.
11
+ unless config.respond_to?(:assets)
12
+ config.assets = ::ActiveSupport::OrderedOptions.new
13
+ end
14
+
15
+ # Path where the manifest files are loaded from.
16
+ config.assets.manifests = ['public/assets/manifest.json']
17
+
18
+ # If set to true missing assets will not raise an
19
+ # exception but are passed through to sprockets
20
+ # or rails own asset methods.
21
+ config.assets.passthrough = false
22
+
23
+ config.after_initialize do |_|
24
+ ActiveSupport.on_load(:action_view) do
25
+ include Helper
26
+ end
27
+ end
28
+
29
+ config.to_prepare do
30
+ ::Rails::Assets::Manifest.prepare!
31
+ end
32
+
33
+ config.before_eager_load do
34
+ ::Rails::Assets::Manifest.eager_load!
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/rails/assets/manifest/version.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Assets
5
+ module Manifest
6
+ module VERSION
7
+ MAJOR = 2
8
+ MINOR = 1
9
+ PATCH = 0
10
+ STAGE = nil
11
+
12
+ STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.')
13
+
14
+ def self.to_s
15
+ STRING
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/rails-assets-manifest.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'rails/assets/manifest/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rails-assets-manifest'
9
+ spec.version = Rails::Assets::Manifest::VERSION
10
+ spec.authors = ['Jan Graichen']
11
+ spec.email = ['jgraichen@altimos.de']
12
+
13
+ spec.summary = 'Load all rails assets from an external manifest.'
14
+ spec.description = 'Load all rails assets from an external manifest.'
15
+ spec.homepage = 'https://github.com/jgraichen/rails-assets-manifest'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/jgraichen/rails-assets-manifest'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/jgraichen/rails-assets-manifest/blob/master/CHANGELOG.md'
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+ end
26
+
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'activesupport', '> 4.2'
32
+ spec.add_dependency 'railties', '> 4.2'
33
+
34
+ spec.add_development_dependency 'bundler'
35
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-assets-manifest
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Graichen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
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
+ description: Load all rails assets from an external manifest.
56
+ email:
57
+ - jgraichen@altimos.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".editorconfig"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".travis.yml"
67
+ - CHANGELOG.md
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - gemfiles/rails-4-2.gemfile
75
+ - gemfiles/rails-5-0.gemfile
76
+ - gemfiles/rails-5-1.gemfile
77
+ - gemfiles/rails-5-2.gemfile
78
+ - gemfiles/rails-master.gemfile
79
+ - lib/rails/assets/manifest.rb
80
+ - lib/rails/assets/manifest/helper.rb
81
+ - lib/rails/assets/manifest/manifest.rb
82
+ - lib/rails/assets/manifest/railtie.rb
83
+ - lib/rails/assets/manifest/version.rb
84
+ - rails-assets-manifest.gemspec
85
+ homepage: https://github.com/jgraichen/rails-assets-manifest
86
+ licenses: []
87
+ metadata:
88
+ homepage_uri: https://github.com/jgraichen/rails-assets-manifest
89
+ source_code_uri: https://github.com/jgraichen/rails-assets-manifest
90
+ changelog_uri: https://github.com/jgraichen/rails-assets-manifest/blob/master/CHANGELOG.md
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.0.6
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Load all rails assets from an external manifest.
110
+ test_files: []