custom_elements-rails 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +25 -7
- data/app/assets/javascript/custom_elements-rails.js +18 -0
- data/lib/custom_elements/rails/railtie.rb +4 -1
- data/lib/custom_elements/rails/version.rb +1 -1
- data/lib/install/install.rb +31 -0
- data/lib/tasks/custom_elements/rails_tasks.rake +7 -4
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22d257cc2e8c052814180e03810ed19525ffaae2989826a5660dc0306dfc3df2
|
4
|
+
data.tar.gz: 2661b7ce42c1a797e0d52fd8deb91922e6af03b8045a31656ec4c59c2f392dc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c92ca66bdfd4c7a9bc5eb90bb383506716bf0efbe1ec977371695a45362578e15292bf22321381f04ba48ea4cb49699fa797b64ecf0d16f19f2d4c53316a5876
|
7
|
+
data.tar.gz: fa02fa675a3a96445063e0eb5a825c942c6e85658c587d6b70ca2ef6858c3e5eb892c11114715e42aebdee0d5418a46821054b01c2fa09e5ff827b76fdf9e77a
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# Custom Elements with Rails
|
2
|
+
|
3
|
+
This gem provides a small helper and installation scripts to use custom elements in conjunction with `importmap-rails`.
|
3
4
|
|
4
5
|
## Usage
|
5
|
-
How to use my plugin.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
Add this line to your application's Gemfile:
|
@@ -11,16 +11,34 @@ Add this line to your application's Gemfile:
|
|
11
11
|
gem "custom_elements-rails"
|
12
12
|
```
|
13
13
|
|
14
|
-
|
14
|
+
Install the gem:
|
15
|
+
|
15
16
|
```bash
|
16
|
-
$ bundle
|
17
|
+
$ bundle install
|
17
18
|
```
|
18
19
|
|
19
|
-
|
20
|
+
Run the initial setup:
|
21
|
+
|
20
22
|
```bash
|
21
|
-
$
|
23
|
+
$ rails custom_elements:install
|
24
|
+
```
|
25
|
+
|
26
|
+
This will setup and add the following files:
|
27
|
+
|
28
|
+
```
|
29
|
+
app/javascript
|
30
|
+
├── application.js
|
31
|
+
└── custom_elements
|
32
|
+
├── hello_element.js
|
33
|
+
└── index.js
|
22
34
|
```
|
23
35
|
|
36
|
+
You can now add the `<app-hello>` custom element in your app. No build step needed.
|
37
|
+
|
38
|
+
## Add a custom element
|
39
|
+
|
40
|
+
TODO
|
41
|
+
|
24
42
|
## Contributing
|
25
43
|
Contribution directions go here.
|
26
44
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
export function eagerDefineCustomElementsFrom(namespace, options = {}) {
|
2
|
+
const importmap = document.querySelector('script[type="importmap"]')
|
3
|
+
const { imports } = JSON.parse(importmap.textContent)
|
4
|
+
const regex = new RegExp(`${namespace}\/(.*?)_element`)
|
5
|
+
Object.entries(imports)
|
6
|
+
.filter(([name, _]) => name.match(regex) )
|
7
|
+
.map(([name, importPath]) => [name.match(regex)[1], importPath])
|
8
|
+
.forEach(([name, importPath]) => {
|
9
|
+
import(importPath)
|
10
|
+
.then((module) => {
|
11
|
+
customElements.define(`${options.prefix}-${name}`, module.default)
|
12
|
+
})
|
13
|
+
.catch((error) => {
|
14
|
+
console.error(`custom_elements-rails: Could not import custom element <${options.prefix}-${name}>`)
|
15
|
+
throw error
|
16
|
+
})
|
17
|
+
})
|
18
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
add_file "app/javascript/custom_elements/index.js", <<~JS
|
2
|
+
import { eagerDefineCustomElementsFrom } from "custom_elements-rails"
|
3
|
+
|
4
|
+
eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" })
|
5
|
+
JS
|
6
|
+
|
7
|
+
# add hello world element
|
8
|
+
add_file "app/javascript/custom_elements/hello_element.js", <<~JS
|
9
|
+
export default class extends HTMLElement {
|
10
|
+
constructor() {
|
11
|
+
super()
|
12
|
+
}
|
13
|
+
|
14
|
+
connectedCallback() {
|
15
|
+
console.log("hello world")
|
16
|
+
}
|
17
|
+
}
|
18
|
+
JS
|
19
|
+
|
20
|
+
# add import to application.js
|
21
|
+
append_to_file('app/javascript/application.js', 'import "custom_elements"')
|
22
|
+
|
23
|
+
# pin basic eager loading script
|
24
|
+
append_to_file('config/importmap.rb', <<~RUBY)
|
25
|
+
pin "custom_elements-rails"
|
26
|
+
RUBY
|
27
|
+
|
28
|
+
# pin custom elements folder to importmap
|
29
|
+
append_to_file('config/importmap.rb', <<~RUBY)
|
30
|
+
pin_all_from "app/javascript/custom_elements", under: "custom_elements"
|
31
|
+
RUBY
|
@@ -1,4 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
namespace :custom_elements do
|
2
|
+
desc "Installation"
|
3
|
+
task :install do
|
4
|
+
puts "Installing custom_elements-rails"
|
5
|
+
system "rails app:template LOCATION=#{File.expand_path('../../install/install.rb', __dir__)}"
|
6
|
+
end
|
7
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_elements-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Niklas Häusele
|
@@ -16,15 +16,15 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 7
|
19
|
+
version: '7'
|
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: 7
|
27
|
-
description: A simple way to use custom elements
|
26
|
+
version: '7'
|
27
|
+
description: 'A simple way to use custom elements to your Rails app. #nobuild'
|
28
28
|
email:
|
29
29
|
- niklas.haeusele@hey.com
|
30
30
|
executables: []
|
@@ -34,9 +34,11 @@ files:
|
|
34
34
|
- MIT-LICENSE
|
35
35
|
- README.md
|
36
36
|
- Rakefile
|
37
|
+
- app/assets/javascript/custom_elements-rails.js
|
37
38
|
- lib/custom_elements/rails.rb
|
38
39
|
- lib/custom_elements/rails/railtie.rb
|
39
40
|
- lib/custom_elements/rails/version.rb
|
41
|
+
- lib/install/install.rb
|
40
42
|
- lib/tasks/custom_elements/rails_tasks.rake
|
41
43
|
homepage: https://github.com/codergeek121/custom_elements-rails
|
42
44
|
licenses:
|
@@ -62,5 +64,5 @@ requirements: []
|
|
62
64
|
rubygems_version: 3.5.9
|
63
65
|
signing_key:
|
64
66
|
specification_version: 4
|
65
|
-
summary: "
|
67
|
+
summary: "Custom Elements + Rails + Importmaps = \U0001F389"
|
66
68
|
test_files: []
|