condenser-rails 1.0 → 1.2

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
  SHA256:
3
- metadata.gz: fcaba08537cdb45c90f7d1e2c899af0d4a405538ba9277c01b945b9592f169d2
4
- data.tar.gz: afe90aa86f685ed3f7e96a4273aa453d74d20231370a4b056fa2cd974be49413
3
+ metadata.gz: 85ea54efa8084fec3370c9109968566c25fc42e2c1dda0117af935509cdd3d04
4
+ data.tar.gz: 85d88db655be386be3fb03bcd0fdaf176b032a41d6ef0ee5025de6bfb3215bea
5
5
  SHA512:
6
- metadata.gz: 3efa07e03c561a54c347f4a111c982556febe4f172fca436e6b3f0d85a01b1a1f1e3063fc1a2057de0a32e25a1d9514117ce58891b0e8db7cb3e4ee39d4141a8
7
- data.tar.gz: 37a44b6922f19ff17b9acac7a7340c273e80ce9985d3bb905044a0a6f53db3a309138a219362d5a3dddbcf782c447742d8d1a030e50cb86dc259f3326dfa989c
6
+ metadata.gz: 32f2939a3411dd310790767e50da0dc331421752082a4f548d7e1c5dc2dffa82e3b5ea559afa9d570fc5398f0eaaa933b61184fad727b11c59162441aa390f69
7
+ data.tar.gz: bdcb847ece9af49935ec5497d053912e7114871463f1af9573e73441c7500609d7a5a80cd675d7971ae16219e84c76c7e15a3b7fe669f3bc7dcc1c58d2028719
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'action_view/helpers'
2
4
  require 'condenser'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'action_view'
2
4
  require 'condenser'
3
5
  require 'active_support/core_ext/class/attribute'
@@ -61,75 +63,115 @@ module Condenser::Rails
61
63
  def asset_integrity(path, options = {})
62
64
  asset_resolver.integrity(path)
63
65
  end
64
-
65
- # Override javascript tag helper to provide debugging support.
66
+
67
+ # Get type for asset path. (module or nil for javascript)
68
+ #
69
+ # path - String path
70
+ # options - Hash options
66
71
  #
67
- # Eventually will be deprecated and replaced by source maps.
72
+ # Returns String integrity attribute or nil if no asset was found.
73
+ def asset_type(path, options = {})
74
+ asset_resolver.type(path)
75
+ end
76
+
77
+ # TODO: perhaps prepend this function and add integrity if set to true?
68
78
  def javascript_include_tag(*sources)
69
79
  options = sources.extract_options!.stringify_keys
70
80
  path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
71
- early_hints_links = []
81
+ preload_links = []
82
+ use_preload_links_header = options["preload_links_header"].nil? ? preload_links_header : options.delete("preload_links_header")
83
+ nopush = options["nopush"].nil? ? true : options.delete("nopush")
84
+ crossorigin = options.delete("crossorigin")
85
+ crossorigin = "anonymous" if crossorigin == true
86
+ integrity = options["integrity"]
72
87
 
73
88
  sources_tags = sources.uniq.map { |source|
74
89
  href = path_to_javascript(source, path_options)
75
- early_hints_links << "<#{href}>; rel=preload; as=script"
76
- tag_options = {
77
- "src" => href
78
- }.merge!(options)
90
+ integrity = if options["integrity"] == true
91
+ asset_integrity(source.to_s.delete_suffix('.js')+'.js')
92
+ elsif options["integrity"] != false
93
+ options["integrity"]
94
+ end
95
+ type = if !options.has_key?('type')
96
+ asset_type(source.to_s.delete_suffix('.js')+'.js')
97
+ else
98
+ options["type"]
99
+ end
100
+ rel = options["type"] == "module" ? "modulepreload" : "preload"
79
101
 
102
+ if use_preload_links_header && !options["defer"] && href.present? && !href.start_with?("data:")
103
+ preload_link = "<#{href}>; rel=#{rel}; as=script"
104
+ preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil?
105
+ preload_link += "; integrity=#{integrity}" unless integrity.nil?
106
+ preload_link += "; nonce=#{content_security_policy_nonce}" if options["nonce"] == true
107
+ preload_link += "; nopush" if nopush
108
+ preload_links << preload_link
109
+ end
110
+ tag_options = {
111
+ "src" => href,
112
+ "crossorigin" => crossorigin
113
+ }.merge!(options.except('integrity', 'type'))
80
114
  if tag_options["nonce"] == true
81
115
  tag_options["nonce"] = content_security_policy_nonce
82
116
  end
83
-
84
- if secure_subresource_integrity_context?
85
- if tag_options["integrity"] == true
86
- tag_options["integrity"] = asset_integrity(source.to_s.delete_suffix('.js')+'.js')
87
- elsif tag_options["integrity"] == false
88
- tag_options.delete('integrity')
89
- end
90
- else
91
- tag_options.delete('integrity')
92
- end
117
+ tag_options['type'] = type if type
118
+ tag_options['integrity'] = integrity if integrity
93
119
 
94
120
  content_tag("script", "", tag_options)
95
121
  }.join("\n").html_safe
96
122
 
97
- request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request
123
+ if use_preload_links_header
124
+ send_preload_links_header(preload_links)
125
+ end
98
126
 
99
127
  sources_tags
100
128
  end
101
129
 
102
- # Override stylesheet tag helper to provide debugging support.
103
- #
104
- # Eventually will be deprecated and replaced by source maps.
130
+ # TODO: perhaps prepend this function and add integrity if set to true?
105
131
  def stylesheet_link_tag(*sources)
106
132
  options = sources.extract_options!.stringify_keys
107
- path_options = options.extract!("protocol", "host", "skip_pipeline").symbolize_keys
108
- early_hints_links = []
109
-
133
+ path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
134
+ use_preload_links_header = options["preload_links_header"].nil? ? preload_links_header : options.delete("preload_links_header")
135
+ preload_links = []
136
+ crossorigin = options.delete("crossorigin")
137
+ crossorigin = "anonymous" if crossorigin == true
138
+ nopush = options["nopush"].nil? ? true : options.delete("nopush")
139
+
110
140
  sources_tags = sources.uniq.map { |source|
111
141
  href = path_to_stylesheet(source, path_options)
112
- early_hints_links << "<#{href}>; rel=preload; as=style"
142
+ integrity = if options["integrity"] == true
143
+ asset_integrity(source.to_s.delete_suffix('.css')+'.css')
144
+ elsif options["integrity"] != false
145
+ options["integrity"]
146
+ end
147
+
148
+ if use_preload_links_header && href.present? && !href.start_with?("data:")
149
+ preload_link = "<#{href}>; rel=preload; as=style"
150
+ preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil?
151
+ preload_link += "; integrity=#{integrity}" unless integrity.nil?
152
+ preload_link += "; nopush" if nopush
153
+ preload_links << preload_link
154
+ end
113
155
  tag_options = {
114
156
  "rel" => "stylesheet",
115
- "media" => "screen",
157
+ "crossorigin" => crossorigin,
116
158
  "href" => href
117
- }.merge!(options)
118
-
119
- if secure_subresource_integrity_context?
120
- if tag_options["integrity"] == true
121
- tag_options["integrity"] = asset_integrity(source.to_s.delete_suffix('.css')+'.css')
122
- elsif tag_options["integrity"] == false
123
- tag_options.delete('integrity')
124
- end
125
- else
126
- tag_options.delete('integrity')
159
+ }.merge!(options.except('integrity'))
160
+ if tag_options["nonce"] == true
161
+ tag_options["nonce"] = content_security_policy_nonce
127
162
  end
163
+ tag_options['integrity'] = integrity if integrity
128
164
 
165
+ if apply_stylesheet_media_default && tag_options["media"].blank?
166
+ tag_options["media"] = "screen"
167
+ end
168
+
129
169
  tag(:link, tag_options)
130
170
  }.join("\n").html_safe
131
171
 
132
- request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request
172
+ if use_preload_links_header
173
+ send_preload_links_header(preload_links)
174
+ end
133
175
 
134
176
  sources_tags
135
177
  end
@@ -175,13 +217,16 @@ module Condenser::Rails
175
217
  end
176
218
 
177
219
  def asset_path(path)
178
- @manifest[path]['path']
220
+ @manifest[path][:path]
179
221
  end
180
222
 
181
223
  def integrity(path)
182
- @manifest[path]['integrity']
224
+ @manifest[path]&.[](:integrity)
183
225
  end
184
226
 
227
+ def type(path)
228
+ @manifest[path]&.[](:type)
229
+ end
185
230
  end
186
231
 
187
232
  class Environment #:nodoc:
@@ -201,7 +246,11 @@ module Condenser::Rails
201
246
  end
202
247
 
203
248
  def integrity(path)
204
- @env.find(path)&.integrity
249
+ @env.find(path)&.export&.integrity
250
+ end
251
+
252
+ def type(path)
253
+ @env.find(path)&.export&.type
205
254
  end
206
255
 
207
256
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake'
2
4
  require 'rake/condensertask'
3
5
  require 'condenser'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # based on http://cpan.uwinnipeg.ca/htdocs/Text-Glob/Text/Glob.pm.html#glob_to_regex_string from https://github.com/alexch/rerun/blob/master/lib/rerun/glob.rb
2
4
 
3
5
  module Condenser::Rails
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Condenser
2
4
  module Rails
3
- VERSION = "1.0"
5
+ VERSION = "1.2"
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'condenser/rails/version'
2
4
  if defined? Rails::Railtie
3
5
  require 'condenser/railtie'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
4
  require 'rails/railtie'
3
5
  require 'action_controller/railtie'
@@ -58,14 +60,9 @@ class Condenser::Railtie < ::Rails::Railtie
58
60
  end
59
61
  end
60
62
 
61
- module SassFunctions
62
- def asset_path(path, options = {})
63
- SassC::Script::Value::String.new(condenser_context.asset_path(path.value, options), :string)
64
- end
65
- end
66
-
67
63
  config.assets = OrderedOptions.new
68
64
  config.assets._blocks = []
65
+ config.assets._pipeline = nil
69
66
  config.assets.path = []
70
67
  config.assets.precompile = %w(application.css application.js **/*.jpg **/*.png **/*.gif)
71
68
  config.assets.prefix = "/assets"
@@ -73,7 +70,7 @@ class Condenser::Railtie < ::Rails::Railtie
73
70
 
74
71
  config.assets.compile = true
75
72
  config.assets.digest = true
76
- config.assets.cache_limit = 100.megabytes
73
+ config.assets.cache_limit = 1_000.megabytes
77
74
  config.assets.compressors = [:zlib]
78
75
 
79
76
  config.assets.configure do |app, env|
@@ -128,18 +125,10 @@ class Condenser::Railtie < ::Rails::Railtie
128
125
  config = app.config
129
126
 
130
127
  if config.assets._pipeline
131
-
128
+ config.assets._pipeline.call(env)
132
129
  else
133
- env.register_transformer 'text/scss', 'text/css', Condenser::ScssTransformer.new({
134
- functions: Condenser::Railtie::SassFunctions
135
- })
136
-
137
- if ::Rails.env == 'development'
138
- env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer
139
- else
140
- env.register_preprocessor 'application/javascript', Condenser::BabelProcessor
141
- end
142
-
130
+ env.register_transformer 'text/scss', 'text/css', Condenser::ScssTransformer.new
131
+ env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer
143
132
  env.register_exporter 'application/javascript', Condenser::RollupProcessor
144
133
 
145
134
  if ::Rails.env != 'development'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: condenser-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-04 00:00:00.000000000 Z
11
+ date: 2025-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: condenser
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.rc1
19
+ version: 1.5.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.rc1
26
+ version: 1.5.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
158
  requirements: []
159
- rubygems_version: 3.1.4
159
+ rubygems_version: 3.5.21
160
160
  signing_key:
161
161
  specification_version: 4
162
162
  summary: Condenser integration for Rails