klenod-build 0.0.1 → 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: 0a05a70d821a5b94964b4bade32066b9fd1bdade13d2b3899becdeb46d3d33ef
4
- data.tar.gz: 68ac76064521f6ca7678d51256672d5ea9161814215bf4c9e3b5a3f743350bc8
3
+ metadata.gz: a3c7b7667bd38de08f8f31ebb2aecf97bcbde81d8e189dae356ac64651dd340b
4
+ data.tar.gz: 55d2493b7d0047d97f9fb26766f7f6a1ed07867e1b1024c04d92dac3e04088e7
5
5
  SHA512:
6
- metadata.gz: 440df52a915ecb0b8312622268b94d413c3447d80912ef42a797a959aaadca42ec3c108a08439d79eb7cf9fff0d7baf4a5425e565d087b4f6b18efee84d0f2df
7
- data.tar.gz: 1ddb4e48b0156a21160fa3d93f855ab657bc4ed40d323e5f3d2f6bbd9dc3d27b36c093ec9731b1eeb791a4b601499721ce89665c23c84be4ad57c02c84491d9f
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
@@ -28,9 +28,9 @@ module Klenod
28
28
  end
29
29
  end
30
30
 
31
- def initialize(profiler: nil, global_variables: nil)
31
+ def initialize(profiler: nil, variables: nil)
32
32
  @profiler = profiler
33
- @global_variables = global_variables
33
+ @variables = variables || {}
34
34
  @expression_cache = {}
35
35
  @statements_cache = {}
36
36
  @program_cache = {}
@@ -411,6 +411,12 @@ module Klenod
411
411
  source_factory_call(factory: factory, tag: tag, children: children, props: props, mark: mark)
412
412
  end
413
413
 
414
+ def component_factory_call(factory:, tag:, children:, props:, mark: nil)
415
+ call = source_factory_call(factory: factory, tag: tag, children: [], props: props, mark: mark)
416
+ body = Fragment.new("[#{children.map { |child| argument_source(child) }.join(", ")}]", nil)
417
+ script_block("#{call.source} do", body)
418
+ end
419
+
414
420
  def slot_call(name:, fallback:)
415
421
  arguments = ["self", name ? argument_source(name) : "nil"]
416
422
  arguments << argument_source(fallback) if fallback
@@ -505,7 +511,7 @@ module Klenod
505
511
 
506
512
  def rewrite_ruby_source(source, line_no)
507
513
  source = rewrite_line_constant(source, line_no)
508
- rewrite_global_variables(source)
514
+ rewrite_variables(source)
509
515
  end
510
516
 
511
517
  def rewrite_line_constant(source, line_no)
@@ -527,25 +533,33 @@ module Klenod
527
533
  end
528
534
  end
529
535
 
530
- def rewrite_global_variables(source)
531
- return source unless @global_variables
536
+ VARIABLE_TOKEN_KINDS = {
537
+ on_gvar: [:global, "$", /\A\$[a-z]\w*\z/],
538
+ on_cvar: [:class, "@@", /\A@@[a-z]\w*\z/],
539
+ on_ivar: [:instance, "@", /\A@[a-z]\w*\z/]
540
+ }.freeze
541
+
542
+ def rewrite_variables(source)
543
+ return source if @variables.empty?
532
544
 
533
545
  line_offsets = [0]
534
546
  source.each_line(chomp: false) { |line| line_offsets << line_offsets.last + line.length }
535
547
 
536
548
  Ripper
537
549
  .lex(source)
538
- .select { |(_line, _column), type, token, _state| type == :on_gvar && prop_global_variable?(token) }
539
- .reverse_each
540
- .each_with_object(source.dup) do |((line, column), _type, token, _state), rewritten|
541
- name = token.delete_prefix("$")
550
+ .filter_map do |(line, column), type, token, _state|
551
+ kind, prefix, pattern = VARIABLE_TOKEN_KINDS[type]
552
+ receiver = @variables[kind]
553
+ next unless receiver && token.match?(pattern)
554
+
555
+ name = token.delete_prefix(prefix)
542
556
  offset = line_offsets.fetch(line - 1) + column
543
- rewritten[offset, token.length] = "#{@global_variables}[#{symbol_source(name)}]"
557
+ [offset, token.length, "(#{receiver})[#{symbol_source(name)}]"]
558
+ end
559
+ .reverse_each
560
+ .each_with_object(source.dup) do |(offset, length, replacement), rewritten|
561
+ rewritten[offset, length] = replacement
544
562
  end
545
- end
546
-
547
- def prop_global_variable?(token)
548
- token.match?(/\A\$[a-z]\w*\z/)
549
563
  end
550
564
 
551
565
  def source_expressions(expressions)
@@ -36,27 +36,31 @@ module Klenod
36
36
  factory:,
37
37
  styles_source:,
38
38
  translations_source:,
39
+ component_children: :eager,
39
40
  i18n_source: nil,
40
41
  styleable: false,
41
42
  profiler: nil,
42
43
  import_rewriter: nil,
43
44
  markdown_components_source: "{}",
44
- global_variables: nil,
45
+ variables: nil,
45
46
  haml_helper_source: nil,
46
47
  cache_static_subtrees: false
47
48
  )
48
49
  component_base_class = ConstPath.parse(component_base_class, name: "component_base_class")
49
50
  factory = ConstPath.parse(factory, name: "factory")
50
- builder = RubyBuilder.new(profiler: profiler, global_variables: global_variables)
51
+ validate_component_children(component_children)
52
+ builder = RubyBuilder.new(profiler: profiler, variables: variables)
51
53
  haml_helper_source ||= builder.constant_assignment("HamlHelper", "Object") if styleable || cache_static_subtrees
52
54
  previous_profiler = @profiler
53
55
  previous_module_id = @module_id
54
56
  previous_static_constants = @static_constants
55
57
  previous_cache_static_subtrees = @cache_static_subtrees
58
+ previous_component_children = @component_children
56
59
  @profiler = profiler
57
60
  @module_id = module_id
58
61
  @static_constants = []
59
62
  @cache_static_subtrees = cache_static_subtrees
63
+ @component_children = component_children
60
64
  template =
61
65
  if profiler
62
66
  profiler.measure(:haml_compile_template, module_id: module_id.to_s) do
@@ -131,12 +135,19 @@ module Klenod
131
135
  @module_id = previous_module_id
132
136
  @static_constants = previous_static_constants
133
137
  @cache_static_subtrees = previous_cache_static_subtrees
138
+ @component_children = previous_component_children
134
139
  end
135
140
 
136
141
  private
137
142
 
138
143
  Template = Data.define(:ruby, :render, :static_constants)
139
144
 
145
+ def validate_component_children(value)
146
+ return if HamlPlugin::COMPONENT_CHILDREN_MODES.include?(value)
147
+
148
+ raise ArgumentError, "component_children must be one of: #{HamlPlugin::COMPONENT_CHILDREN_MODES.join(", ")}"
149
+ end
150
+
140
151
  def measure_compile(name)
141
152
  return yield unless @profiler
142
153
 
@@ -456,13 +467,17 @@ module Klenod
456
467
  tag = measure_compile_detail(:haml_compile_tag_name) { compile_tag_name(node, builder: builder) }
457
468
  props = measure_compile_detail(:haml_compile_tag_attributes) { attributes(node, styleable: styleable, builder: builder) }
458
469
 
459
- builder.factory_call(
460
- factory: factory,
461
- tag: tag,
462
- children: children,
463
- props: props,
464
- mark: mark
465
- )
470
+ if @component_children == :lazy && constant_tag_name?(node.value.fetch(:name)) && !children.empty?
471
+ builder.component_factory_call(factory: factory, tag: tag, children: children, props: props, mark: mark)
472
+ else
473
+ builder.factory_call(
474
+ factory: factory,
475
+ tag: tag,
476
+ children: children,
477
+ props: props,
478
+ mark: mark
479
+ )
480
+ end
466
481
  end
467
482
 
468
483
  def compile_tag_name(node, builder:)
@@ -28,6 +28,8 @@ module Klenod
28
28
 
29
29
  DEFAULT_COMPONENT_BASE_CLASS = ComponentDefaults::DEFAULT_COMPONENT_BASE_CLASS
30
30
  DEFAULT_FACTORY = ComponentDefaults::DEFAULT_FACTORY
31
+ DEFAULT_COMPONENT_CHILDREN = :eager
32
+ COMPONENT_CHILDREN_MODES = %i[eager lazy].freeze
31
33
  HAML_HELPER_SPECIFIER = "virtual:klenod/haml_helper"
32
34
  HAML_HELPER_MODULE_ID = ModuleId.new("#{HAML_HELPER_SPECIFIER}.rb", nil)
33
35
  STATIC_CLASS_SOURCE_PATTERN = /^[ \t]*(?:%[-:\w]+)?(?:#[\w-]+)?\.[\w-]|[({][^)}\n]*\bclass\s*=/m
@@ -43,6 +45,8 @@ module Klenod
43
45
 
44
46
  DEFAULT_COMPONENT_BASE_CLASS = HamlPlugin::DEFAULT_COMPONENT_BASE_CLASS
45
47
  DEFAULT_FACTORY = HamlPlugin::DEFAULT_FACTORY
48
+ DEFAULT_COMPONENT_CHILDREN = HamlPlugin::DEFAULT_COMPONENT_CHILDREN
49
+ COMPONENT_CHILDREN_MODES = HamlPlugin::COMPONENT_CHILDREN_MODES
46
50
  HAML_HELPER_SPECIFIER = HamlPlugin::HAML_HELPER_SPECIFIER
47
51
  HAML_HELPER_MODULE_ID = HamlPlugin::HAML_HELPER_MODULE_ID
48
52
  STATIC_CLASS_SOURCE_PATTERN = HamlPlugin::STATIC_CLASS_SOURCE_PATTERN
@@ -58,14 +62,16 @@ module Klenod
58
62
  def initialize(
59
63
  component_base_class: DEFAULT_COMPONENT_BASE_CLASS,
60
64
  factory: DEFAULT_FACTORY,
61
- global_variables: nil,
65
+ component_children: DEFAULT_COMPONENT_CHILDREN,
66
+ variables: nil,
62
67
  i18n_class: nil,
63
68
  i18n_constant: nil,
64
69
  cache_static_subtrees: false
65
70
  )
66
71
  @component_base_class = component_base_class
67
72
  @factory = factory
68
- @global_variables = validate_global_variables(global_variables)
73
+ @component_children = validate_component_children(component_children)
74
+ @variables = validate_variables(variables)
69
75
  @i18n_class, @i18n_constant = validate_i18n_options(i18n_class, i18n_constant)
70
76
  @cache_static_subtrees = cache_static_subtrees
71
77
  @transformer = Transformer.new
@@ -191,6 +197,7 @@ module Klenod
191
197
  component_class_name: component_class_name,
192
198
  component_base_class: @component_base_class,
193
199
  factory: @factory,
200
+ component_children: @component_children,
194
201
  styles_source: styles_source,
195
202
  translations_source: translations_source,
196
203
  i18n_source: i18n_source_for(builder),
@@ -199,7 +206,7 @@ module Klenod
199
206
  profiler: context.profiler,
200
207
  import_rewriter: import_rewriter,
201
208
  markdown_components_source: markdown_components_dependency ? "__klenod_import__(#{markdown_components_dependency.id.inspect})::Default" : "{}",
202
- global_variables: @global_variables,
209
+ variables: @variables,
203
210
  cache_static_subtrees: @cache_static_subtrees
204
211
  )
205
212
  import_rewrite =
@@ -257,16 +264,36 @@ module Klenod
257
264
 
258
265
  InlineCssSource = Data.define(:text, :line_offset, :column_offset)
259
266
 
260
- def validate_global_variables(global_variables)
261
- return nil if global_variables.nil?
267
+ VARIABLE_KINDS = %i[global class instance].freeze
268
+
269
+ def validate_component_children(value)
270
+ return value if COMPONENT_CHILDREN_MODES.include?(value)
271
+
272
+ raise ArgumentError, "component_children must be one of: #{COMPONENT_CHILDREN_MODES.join(", ")}"
273
+ end
274
+
275
+ def validate_variables(variables)
276
+ return {}.freeze if variables.nil?
277
+ raise ArgumentError, "variables must be a Hash" unless variables.is_a?(Hash)
278
+
279
+ unknown_kinds = variables.keys - VARIABLE_KINDS
280
+ unless unknown_kinds.empty?
281
+ raise ArgumentError, "variables contains unknown kinds: #{unknown_kinds.map(&:inspect).join(", ")}"
282
+ end
283
+
284
+ variables.to_h { |kind, source| [kind, validate_variable_receiver(kind, source)] }.freeze
285
+ end
286
+
287
+ def validate_variable_receiver(kind, source)
288
+ raise ArgumentError, "variables[#{kind.inspect}] must be a Ruby expression String" unless source.is_a?(String)
262
289
 
263
- source = global_variables.to_s
264
290
  parsed = SyntaxTree.parse(source)&.statements&.body
265
- raise ArgumentError, "global_variables must be a Ruby expression" unless parsed&.length == 1
291
+ raise ArgumentError, "variables[#{kind.inspect}] must be a Ruby expression" unless parsed&.length == 1
292
+ SyntaxTree.parse("(#{source})[:__klenod_variable__]")
266
293
 
267
294
  source
268
295
  rescue SyntaxTree::Parser::ParseError
269
- raise ArgumentError, "global_variables must be a Ruby expression"
296
+ raise ArgumentError, "variables[#{kind.inspect}] must be a Ruby expression"
270
297
  end
271
298
 
272
299
  def validate_i18n_options(i18n_class, i18n_constant)
@@ -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.1"
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.1
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.1
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.1
25
+ version: 0.0.3
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: async
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -219,8 +219,7 @@ dependencies:
219
219
  - - "~>"
220
220
  - !ruby/object:Gem::Version
221
221
  version: 0.2.0
222
- description: Klenod build constructs Ruby module graphs, runs plugins, emits assets,
223
- and serializes runtime bundles.
222
+ description: Build system and devtools for Klenod.
224
223
  email:
225
224
  - andreas.alin@gmail.com
226
225
  executables: []
@@ -250,6 +249,7 @@ files:
250
249
  - lib/klenod/build/module_record.rb
251
250
  - lib/klenod/build/plugin.rb
252
251
  - lib/klenod/build/plugins.rb
252
+ - lib/klenod/build/plugins/asset_javascript_metadata.rb
253
253
  - lib/klenod/build/plugins/class_names_runtime.rb
254
254
  - lib/klenod/build/plugins/component_defaults.rb
255
255
  - lib/klenod/build/plugins/data_plugin.rb
@@ -306,5 +306,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
306
306
  requirements: []
307
307
  rubygems_version: 4.0.16
308
308
  specification_version: 4
309
- summary: Build graph and plugins for Klenod.
309
+ summary: Build system for Klenod.
310
310
  test_files: []