sprockets-components 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +8 -0
- data/lib/sprockets/components/version.rb +7 -0
- data/lib/sprockets/components.rb +69 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 289b2d31182c7ceb8cf37b3ef9d452f5684efb15ebb3a43d16540fc278773306
|
4
|
+
data.tar.gz: 63ff1537b1dbc46a2933d17509a7400569c6650337f85ff6cc7218da38df3cad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef741329802e424ea353733b2723f342559dddcdb9c483a6c725d4b27f0d171c5936dd5bc4a3956250b4a73ae1160a959f92ee59964a18709f3213d33f491cbf
|
7
|
+
data.tar.gz: 50a33291ae8f4e150ac82dee4cb25b4dcdd1efbd2638aedb05aa4cbb4826ffd17a73ce85445ae5505006759c4963ebf6fba8c8118ad94ec4cf664102446b4975
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.2.2@sprockets-components
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Micah Geisel
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Sprockets::Components
|
2
|
+
|
3
|
+
Make web components easier to develop in Rails using Sprockets.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Just add `gem "sprockets-components` to your Gemfile.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Use one directory per component, under `app/assets/components`:
|
12
|
+
|
13
|
+
```
|
14
|
+
app/assets/
|
15
|
+
└── components
|
16
|
+
└── my-counter
|
17
|
+
├── index.js
|
18
|
+
├── my-counter.js
|
19
|
+
├── my-counter.scss
|
20
|
+
└── my-counter.slim
|
21
|
+
```
|
22
|
+
|
23
|
+
Each component's index.js file wires everything up in a single line:
|
24
|
+
```js
|
25
|
+
//= component my-counter
|
26
|
+
```
|
27
|
+
|
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.
|
31
|
+
|
32
|
+
|
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
|
+
```html.erb
|
35
|
+
<!-- app/views/test/test.html.erb -->
|
36
|
+
<%= javascript_include_tag "my-counter" %>
|
37
|
+
<my-counter value="0"></my-counter>
|
38
|
+
```
|
39
|
+
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
44
|
+
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/sprockets-components.
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "components/version"
|
4
|
+
|
5
|
+
module Sprockets
|
6
|
+
module Components
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
initializer "sprockets-components" do |app|
|
9
|
+
Sprockets::DirectiveProcessor.include Directives
|
10
|
+
app.config.after_initialize do
|
11
|
+
app.assets.register_pipeline :component, Pipeline.new
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Directives
|
17
|
+
protected
|
18
|
+
|
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"
|
23
|
+
end
|
24
|
+
|
25
|
+
def process_component_css_directive path
|
26
|
+
@required << resolve(path, accept: "text/css", pipeline: :component)
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_component_html_directive path
|
30
|
+
@required << resolve(path, accept: "text/html", pipeline: :component)
|
31
|
+
end
|
32
|
+
|
33
|
+
def process_component_js_directive path
|
34
|
+
@required << resolve(path, accept: "application/javascript", pipeline: :component)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Pipeline
|
39
|
+
def call env, type, file_type
|
40
|
+
{
|
41
|
+
"text/css" => [CSSProcessor.new],
|
42
|
+
"text/html" => [HTMLProcessor.new],
|
43
|
+
"application/javascript" => [JSProcessor.new],
|
44
|
+
}.fetch(type) +
|
45
|
+
env.send(:default_processors_for, type, file_type)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class CSSProcessor
|
50
|
+
def call input
|
51
|
+
"const CSS = `#{input[:data]}`;\n"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class HTMLProcessor
|
56
|
+
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})"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sprockets-components
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Micah Geisel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sprockets
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
description: Make web components easier to develop using Sprockets.
|
28
|
+
email:
|
29
|
+
- micah@botandrose.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".ruby-version"
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/sprockets/components.rb
|
40
|
+
- lib/sprockets/components/version.rb
|
41
|
+
homepage: https://github.com/botandrose/sprockets-components
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata:
|
45
|
+
homepage_uri: https://github.com/botandrose/sprockets-components
|
46
|
+
source_code_uri: https://github.com/botandrose/sprockets-components
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.0.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.5.3
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Make web components easier to develop using Sprockets.
|
66
|
+
test_files: []
|