sprockets-components 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cee1e5b40b161d3f3f2e4ddf5fed87b3ce029ab83039a284ba411348a6b71ff
4
- data.tar.gz: ee33b95d141677dad065b0b97b94dd89718bbdd45e06c3cc9c19b7c63c00ffb4
3
+ metadata.gz: af056fec5418fa33a855c1e7f06a08b6196f7098521b673dffb380a6f3d1b408
4
+ data.tar.gz: 6823791a163c74ba986f2dd0b380e2fbdcd10da93f52b1700392e5de6e53358d
5
5
  SHA512:
6
- metadata.gz: a1f94a48dc66db774c06290628a082db5dc928935a6c9aa615f55ef7c631b941f81b0677de54685c5dcad3f72d654966edb60c6252f98da7f6cb1928a3430400
7
- data.tar.gz: 5d037d94a643e019e982c1607afcda7cd08ffe0828eb00251e99ac944b94d67f0d9e0b0edafbf23ccd839f74d8f45f0f5223d5d8a5cc5b0cb2379373b0c52a1e
6
+ metadata.gz: a4fd2ab88a2d98cef302916403faf5e3f0acbe343082ff306d1dd7ad8ad7dfe7df2d20d78732e4584dbdbd61fd6cf55d7479cc874f774fa31a0b06f467d97d7e
7
+ data.tar.gz: a7ee43c59f72171187df32db4ae468208669660d1897607b43d9d91c21229a8588fbdfa58783a08e1938e3aeb01286ca3f52d3b10091da2ef84353fabc19543f
data/README.md CHANGED
@@ -14,21 +14,21 @@ Use one directory per component, under `app/assets/components`:
14
14
  app/assets/
15
15
  └── components
16
16
     └── my-counter
17
-    ├── index.js
18
17
     ├── my-counter.js
19
18
     ├── my-counter.scss
20
19
     └── my-counter.slim
21
20
  ```
22
21
 
23
- Each component's index.js file wires everything up in a single line:
22
+ At the top of the main my-counter.js component file, add the `component` directive:
24
23
  ```js
25
- //= component my-counter
26
- ```
24
+ //= component
27
25
 
28
- Now, from the perspective of the main my-counter.js file, there are two things done:
29
- 1. There is an `HTML` variable available for use in the component. It is a string of the compiled template, with the compiled CSS inlined into an interior <style> tag.
30
- 2. `window.customElements.define("my-counter", MyCounter)` is appended to the compiled file, so the JavaScript component class should be named appropriately.
26
+ customElements.define("my-counter", class extends HTMLElement {
27
+ // ...
28
+ })
29
+ ```
31
30
 
31
+ This directive exposes an `HTML` variable for use in the component. It is a string of the compiled template, with the compiled CSS inlined into an interior <style> tag.
32
32
 
33
33
  Use the component in your views, as you'd expect. Sprockets compiles it all down to a single compiled js file. This file can be included directly, or `//= require`d into other files, or exposed to the importmap, or whatever you want!
34
34
  ```html.erb
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sprockets
4
4
  module Components
5
- VERSION = "0.1.1"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -10,16 +10,45 @@ module Sprockets
10
10
  app.config.assets.configure do |env|
11
11
  env.register_pipeline :component, Pipeline.new
12
12
  end
13
+
14
+ # there isn't a supported way to register path resolvers so we have to hack it in
15
+ Sprockets::Resolve.prepend Module.new {
16
+ def resolve_under_paths(*)
17
+ @resolvers ||= [
18
+ method(:resolve_main_under_path),
19
+ method(:resolve_alts_under_path),
20
+ method(:resolve_index_under_path),
21
+ method(:resolve_component),
22
+ ]
23
+ super
24
+ end
25
+
26
+ def resolve_component load_path, logical_name, mime_exts
27
+ dirname = File.join(load_path, logical_name)
28
+
29
+ if load_path.split("/").last == "components" && Dir.exist?(dirname)
30
+ candidates = [{
31
+ filename: "#{dirname}/#{logical_name}.js",
32
+ type: "application/javascript",
33
+ index_alias: "app/assets/components/#{logical_name}.js",
34
+ }]
35
+ else
36
+ candidates = []
37
+ end
38
+
39
+ return candidates, [ URIUtils.build_file_digest_uri(dirname) ]
40
+ end
41
+ }
13
42
  end
14
43
  end
15
44
 
16
45
  module Directives
17
46
  protected
18
47
 
19
- def process_component_directive name
20
- process_component_css_directive "#{name}/#{name}.css"
21
- process_component_html_directive "#{name}/#{name}.html"
22
- process_component_js_directive "#{name}/#{name}.js"
48
+ def process_component_directive
49
+ name = @dirname.split("/").last
50
+ process_component_css_directive "./#{name}.css"
51
+ process_component_html_directive "./#{name}.html"
23
52
  end
24
53
 
25
54
  def process_component_css_directive path
@@ -29,10 +58,6 @@ module Sprockets
29
58
  def process_component_html_directive path
30
59
  @required << resolve(path, accept: "text/html", pipeline: :component)
31
60
  end
32
-
33
- def process_component_js_directive path
34
- @required << resolve(path, accept: "application/javascript", pipeline: :component)
35
- end
36
61
  end
37
62
 
38
63
  class Pipeline
@@ -40,7 +65,6 @@ module Sprockets
40
65
  {
41
66
  "text/css" => [CSSProcessor.new],
42
67
  "text/html" => [HTMLProcessor.new],
43
- "application/javascript" => [JSProcessor.new],
44
68
  }.fetch(type) +
45
69
  env.send(:default_processors_for, type, file_type)
46
70
  end
@@ -48,20 +72,13 @@ module Sprockets
48
72
 
49
73
  class CSSProcessor
50
74
  def call input
51
- "const CSS = `#{input[:data]}`;\n"
75
+ "const __SprocketsComponent__CSS = `#{input[:data]}`;\n"
52
76
  end
53
77
  end
54
78
 
55
79
  class HTMLProcessor
56
80
  def call input
57
- "const HTML = `<style>${CSS}</style>\n#{input[:data]}`;\n"
58
- end
59
- end
60
-
61
- class JSProcessor
62
- def call input
63
- name = input[:name].split("/").first
64
- "#{input[:data]};window.customElements.define('#{name}', #{name.underscore.classify})"
81
+ "const HTML = `<style>${__SprocketsComponent__CSS}</style>\n#{input[:data]}`;\n"
65
82
  end
66
83
  end
67
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-01 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  requirements: []
62
- rubygems_version: 3.5.3
62
+ rubygems_version: 3.5.6
63
63
  signing_key:
64
64
  specification_version: 4
65
65
  summary: Make web components easier to develop using Sprockets.