klenod-build 0.0.2 → 0.0.3

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: 3f6f0bd5e9b747b26e394d5c041f30ce22aa9436f8483689172c48ae2219af1b
4
- data.tar.gz: 8a11818a46097229cab6c5c2f94515306f9f989c7b2c49eefcb045b46e68a3a8
3
+ metadata.gz: a3c7b7667bd38de08f8f31ebb2aecf97bcbde81d8e189dae356ac64651dd340b
4
+ data.tar.gz: 55d2493b7d0047d97f9fb26766f7f6a1ed07867e1b1024c04d92dac3e04088e7
5
5
  SHA512:
6
- metadata.gz: f10d5c296da38e42bfd54e37366dd28e834bbb21b2ed455ea0ab1b325eb4e43ca2e8dc41b0e0d30b6709db8563441573ed5132a9d85ef1f81d8287459a9c40a0
7
- data.tar.gz: ed6f70303f40f60b71147980de86b84482106407d927ac7aa248efccb92c2ee83cff94fdf0baa382e2e18229460e526cad265679383d27b8d80aa2ce8106edde
6
+ metadata.gz: ce9d72bdc5127d334c72baa154e6d16997081c3e3a7f016074ec41088a16007b4c35c93d4bfb96a220fd950539b0e3496d7b3c9b33439c6777d5760cad2e2715
7
+ data.tar.gz: 156d7564ff2e42c7831585790e906598510f5b27c74de16378f06870e792baa91f15101abe932212b6669a7a368d5b852c9f1be2cdeae5cc75ffa2f951eff46f
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ require_relative "../asset"
6
+ require_relative "../hashing"
7
+
8
+ module Klenod
9
+ module Build
10
+ module Plugins
11
+ module AssetJavaScriptMetadata
12
+ IDENTIFIER_PATTERN = /\A[$A-Z_a-z][$0-9A-Z_a-z]*\z/
13
+ LOGICAL_NAME = "virtual:klenod/asset-metadata.js"
14
+ SOURCE = <<~JAVASCRIPT
15
+ export class ImageBase {
16
+ constructor({ src, width, height, contentType, aspectRatio }) {
17
+ this.src = src;
18
+ this.width = width;
19
+ this.height = height;
20
+ this.contentType = contentType;
21
+ this.aspectRatio = aspectRatio;
22
+ }
23
+ }
24
+
25
+ export class ImageVariant extends ImageBase {
26
+ constructor({ src, width, height, contentType, aspectRatio, format, descriptor, quality }) {
27
+ super({ src, width, height, contentType, aspectRatio });
28
+ this.format = format;
29
+ this.descriptor = descriptor;
30
+ this.quality = quality;
31
+ Object.freeze(this);
32
+ }
33
+ }
34
+
35
+ export default class ImageMetadata extends ImageBase {
36
+ constructor({ src, width, height, contentType, aspectRatio, variants = [] }) {
37
+ super({ src, width, height, contentType, aspectRatio });
38
+ this.variants = Object.freeze([...variants]);
39
+ Object.freeze(this);
40
+ }
41
+
42
+ get srcset() {
43
+ if (this.variants.length === 0) return null;
44
+ return this.variants.map((variant) => `${variant.src} ${variant.descriptor}`).join(", ");
45
+ }
46
+
47
+ get sizes() {
48
+ const variantWidths = this.variants.map((variant) => variant.width).filter(Boolean);
49
+ const displayWidth = variantWidths.length > 0 ? Math.max(...variantWidths) : this.width;
50
+ return displayWidth ? `(max-width: ${displayWidth}px) 100vw, ${displayWidth}px` : null;
51
+ }
52
+ }
53
+
54
+ export class SvgMetadata extends ImageBase {
55
+ constructor({ src, width, height, contentType, aspectRatio }) {
56
+ super({ src, width, height, contentType, aspectRatio });
57
+ Object.freeze(this);
58
+ }
59
+ }
60
+ JAVASCRIPT
61
+ CONTENT_HASH = Hashing.short(SOURCE)
62
+ OUTPUT_PATH = "/assets/klenod_asset_metadata.#{CONTENT_HASH}.js"
63
+
64
+ module_function
65
+
66
+ def object_literal(properties)
67
+ "{#{properties.map { |key, value| property(key, value) }.join(",")}}"
68
+ end
69
+
70
+ def aspect_ratio(width, height)
71
+ return nil unless width && height && !height.zero?
72
+
73
+ width.to_f / height
74
+ end
75
+
76
+ def property(key, value)
77
+ name = key.to_s
78
+ name = JSON.generate(name) unless name.match?(IDENTIFIER_PATTERN)
79
+ "#{name}:#{JSON.generate(value)}"
80
+ end
81
+
82
+ def asset
83
+ Asset.new(
84
+ LOGICAL_NAME,
85
+ CONTENT_HASH,
86
+ OUTPUT_PATH,
87
+ nil,
88
+ SOURCE,
89
+ "application/javascript",
90
+ {type: :javascript, javascript_runtime: :asset_metadata}
91
+ )
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "image_size"
4
- require "json"
5
4
  require "rmagick"
6
5
  require "uri"
7
6
 
@@ -11,6 +10,7 @@ require_relative "../hashing"
11
10
  require_relative "../load_result"
12
11
  require_relative "../plugin"
13
12
  require_relative "../transform_result"
13
+ require_relative "asset_javascript_metadata"
14
14
 
15
15
  module Klenod
16
16
  module Build
@@ -52,7 +52,7 @@ module Klenod
52
52
  asset = default_image_asset(module_id, source_path, source_hash, dimensions, image_options, context.asset_generation_queue)
53
53
  variant_assets = generate_variant_assets(module_id, source_path, source_hash, dimensions, image_options, context.asset_generation_queue)
54
54
  javascript_asset = javascript_image_asset(module_id, asset, variant_assets)
55
- assets = [asset, *variant_assets, javascript_asset]
55
+ assets = [asset, *variant_assets]
56
56
  image_runtime_dependency =
57
57
  Dependency
58
58
  .create(
@@ -69,7 +69,7 @@ module Klenod
69
69
  nil,
70
70
  assets,
71
71
  [],
72
- {image_javascript_asset_path: javascript_asset.output_path}
72
+ {image_javascript_asset: javascript_asset}
73
73
  )
74
74
  LoadResult.new(
75
75
  image_source(module_id),
@@ -185,8 +185,12 @@ module Klenod
185
185
 
186
186
  def image_runtime_source
187
187
  <<~RUBY
188
- Image =
189
- Data.define(:src, :width, :height, :content_type, :variants) do
188
+ ImageMetadata =
189
+ Data.define(:src, :width, :height, :content_type, :aspect_ratio, :variants) do
190
+ def initialize(src:, width:, height:, content_type:, aspect_ratio:, variants:)
191
+ super(src:, width:, height:, content_type:, aspect_ratio:, variants: variants.dup.freeze)
192
+ end
193
+
190
194
  def srcset
191
195
  return nil if variants.empty?
192
196
 
@@ -201,7 +205,7 @@ module Klenod
201
205
  end
202
206
  end
203
207
 
204
- ImageVariant = Data.define(:src, :width, :height, :content_type, :format, :descriptor, :metadata)
208
+ ImageVariant = Data.define(:src, :width, :height, :content_type, :aspect_ratio, :format, :descriptor, :quality)
205
209
  RUBY
206
210
  end
207
211
 
@@ -216,11 +220,12 @@ module Klenod
216
220
  <<~RUBY
217
221
  ImageRuntime = __klenod_import__(#{image_runtime_dependency.id.inspect})
218
222
  Default =
219
- ImageRuntime::Image.new(
223
+ ImageRuntime::ImageMetadata.new(
220
224
  src: #{asset.output_path.inspect},
221
225
  width: #{asset.metadata[:width].inspect},
222
226
  height: #{asset.metadata[:height].inspect},
223
227
  content_type: #{asset.content_type.inspect},
228
+ aspect_ratio: #{AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height]).inspect},
224
229
  variants: [
225
230
  #{variants.join(",\n ")}
226
231
  ]
@@ -236,9 +241,10 @@ module Klenod
236
241
  width: #{asset.metadata[:width].inspect},
237
242
  height: #{asset.metadata[:height].inspect},
238
243
  content_type: #{asset.content_type.inspect},
244
+ aspect_ratio: #{AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height]).inspect},
239
245
  format: #{asset.metadata[:format].inspect},
240
246
  descriptor: #{asset.metadata[:descriptor].inspect},
241
- metadata: #{asset.metadata.inspect}
247
+ quality: #{asset.metadata[:quality].inspect}
242
248
  )
243
249
  RUBY
244
250
  end
@@ -249,7 +255,7 @@ module Klenod
249
255
  Asset.new(
250
256
  module_id.path,
251
257
  hash,
252
- "#{asset.output_path}.js",
258
+ "/assets/#{asset_name(module_id)}.#{hash}#{module_id.extname}.js",
253
259
  nil,
254
260
  code,
255
261
  "application/javascript",
@@ -258,21 +264,20 @@ module Klenod
258
264
  end
259
265
 
260
266
  def javascript_image_module_source(asset, variant_assets)
261
- variants = variant_assets.map { |variant_asset| javascript_image_variant(variant_asset) }
262
- srcset = variant_assets.empty? ? nil : variant_assets.map { "#{it.output_path} #{it.metadata[:descriptor]}" }.join(", ")
263
- display_width = variant_assets.filter_map { it.metadata[:width] }.max || asset.metadata[:width]
264
- sizes = display_width ? "(max-width: #{display_width}px) 100vw, #{display_width}px" : nil
265
- metadata = {
267
+ variants = variant_assets.map { "new ImageVariant(#{AssetJavaScriptMetadata.object_literal(javascript_image_variant(it))})" }
268
+ properties = {
266
269
  src: asset.output_path,
267
270
  width: asset.metadata[:width],
268
271
  height: asset.metadata[:height],
269
272
  contentType: asset.content_type,
270
- variants: variants,
271
- srcset: srcset,
272
- sizes: sizes
273
- }
274
-
275
- "export default #{JSON.generate(metadata)};\n"
273
+ aspectRatio: AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height])
274
+ }.map { |key, value| AssetJavaScriptMetadata.property(key, value) }
275
+ properties << %(variants:[#{variants.join(",")}])
276
+
277
+ <<~JAVASCRIPT
278
+ import ImageMetadata, { ImageVariant } from #{AssetJavaScriptMetadata::OUTPUT_PATH.inspect};
279
+ export default new ImageMetadata({#{properties.join(",")}});
280
+ JAVASCRIPT
276
281
  end
277
282
 
278
283
  def javascript_image_variant(asset)
@@ -281,10 +286,11 @@ module Klenod
281
286
  width: asset.metadata[:width],
282
287
  height: asset.metadata[:height],
283
288
  contentType: asset.content_type,
289
+ aspectRatio: AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height]),
284
290
  format: asset.metadata[:format],
285
291
  descriptor: asset.metadata[:descriptor],
286
- metadata: asset.metadata
287
- }
292
+ quality: asset.metadata[:quality]
293
+ }.compact
288
294
  end
289
295
 
290
296
  def image_asset(assets)
@@ -334,8 +340,7 @@ module Klenod
334
340
  width: width,
335
341
  height: scaled_height(dimensions, width),
336
342
  format: format.to_sym,
337
- descriptor: descriptor,
338
- source_width: width
343
+ descriptor: descriptor
339
344
  }
340
345
  metadata[:quality] = quality if quality
341
346
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
-
5
3
  require_relative "../asset"
6
4
  require_relative "../dependency"
7
5
  require_relative "../errors"
@@ -9,6 +7,7 @@ require_relative "../hashing"
9
7
  require_relative "../module_id"
10
8
  require_relative "../plugin"
11
9
  require_relative "../transform_result"
10
+ require_relative "asset_javascript_metadata"
12
11
 
13
12
  module Klenod
14
13
  module Build
@@ -71,9 +70,9 @@ module Klenod
71
70
  svg_module_source(asset, svg_runtime_dependency),
72
71
  [svg_runtime_dependency],
73
72
  nil,
74
- [asset, javascript_asset],
73
+ [asset],
75
74
  [],
76
- {asset_bytes: code, svg_javascript_asset_path: javascript_asset.output_path}
75
+ {asset_bytes: code, svg_javascript_asset: javascript_asset}
77
76
  )
78
77
  end
79
78
 
@@ -95,7 +94,7 @@ module Klenod
95
94
 
96
95
  def svg_runtime_source
97
96
  <<~RUBY
98
- Svg = Data.define(:src, :width, :height, :content_type)
97
+ SvgMetadata = Data.define(:src, :width, :height, :content_type, :aspect_ratio)
99
98
  RUBY
100
99
  end
101
100
 
@@ -103,11 +102,12 @@ module Klenod
103
102
  <<~RUBY
104
103
  SvgRuntime = __klenod_import__(#{svg_runtime_dependency.id.inspect})
105
104
  Default =
106
- SvgRuntime::Svg.new(
105
+ SvgRuntime::SvgMetadata.new(
107
106
  src: #{asset.output_path.inspect},
108
107
  width: #{asset.metadata[:width].inspect},
109
108
  height: #{asset.metadata[:height].inspect},
110
- content_type: #{asset.content_type.inspect}
109
+ content_type: #{asset.content_type.inspect},
110
+ aspect_ratio: #{AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height]).inspect}
111
111
  )
112
112
 
113
113
  RUBY
@@ -119,7 +119,7 @@ module Klenod
119
119
  Asset.new(
120
120
  module_id.path,
121
121
  hash,
122
- "#{asset.output_path}.js",
122
+ "/assets/#{asset_name(module_id)}.#{hash}#{module_id.extname}.js",
123
123
  nil,
124
124
  code,
125
125
  "application/javascript",
@@ -132,10 +132,14 @@ module Klenod
132
132
  src: asset.output_path,
133
133
  width: asset.metadata[:width],
134
134
  height: asset.metadata[:height],
135
- contentType: asset.content_type
135
+ contentType: asset.content_type,
136
+ aspectRatio: AssetJavaScriptMetadata.aspect_ratio(asset.metadata[:width], asset.metadata[:height])
136
137
  }
137
138
 
138
- "export default #{JSON.generate(metadata)};\n"
139
+ <<~JAVASCRIPT
140
+ import { SvgMetadata } from #{AssetJavaScriptMetadata::OUTPUT_PATH.inspect};
141
+ export default new SvgMetadata(#{AssetJavaScriptMetadata.object_literal(metadata)});
142
+ JAVASCRIPT
139
143
  end
140
144
 
141
145
  def svg_dimensions(code)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Klenod
4
4
  module Build
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-build
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.0.2
18
+ version: 0.0.3
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.0.2
25
+ version: 0.0.3
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: async
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -249,6 +249,7 @@ files:
249
249
  - lib/klenod/build/module_record.rb
250
250
  - lib/klenod/build/plugin.rb
251
251
  - lib/klenod/build/plugins.rb
252
+ - lib/klenod/build/plugins/asset_javascript_metadata.rb
252
253
  - lib/klenod/build/plugins/class_names_runtime.rb
253
254
  - lib/klenod/build/plugins/component_defaults.rb
254
255
  - lib/klenod/build/plugins/data_plugin.rb