Skip to content

Commit d9b8045

Browse files
committed
Add installation script and basic loading script
1 parent 8da0ab9 commit d9b8045

File tree

8 files changed

+91
-19
lines changed

8 files changed

+91
-19
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
PATH
22
remote: .
33
specs:
4-
custom_elements-rails (0.1.0)
5-
rails (>= 7.1.3.2)
4+
custom_elements-rails (0.1.1)
5+
rails (>= 7)
66

77
GEM
88
remote: https://rubygems.org/

README.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# CustomElements::Rails
2-
Short description and motivation.
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`.
34

45
## Usage
5-
How to use my plugin.
66

77
## Installation
88
Add this line to your application's Gemfile:
@@ -11,16 +11,34 @@ Add this line to your application's Gemfile:
1111
gem "custom_elements-rails"
1212
```
1313

14-
And then execute:
14+
Install the gem:
15+
1516
```bash
16-
$ bundle
17+
$ bundle install
1718
```
1819

19-
Or install it yourself as:
20+
Run the initial setup:
21+
2022
```bash
21-
$ gem install custom_elements-rails
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
2234
```
2335

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+
2442
## Contributing
2543
Contribution directions go here.
2644

Original file line numberDiff line numberDiff line change
@@ -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+
}

custom_elements-rails.gemspec

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
66
spec.authors = ["Niklas Häusele"]
77
spec.email = ["[email protected]"]
88
spec.homepage = "https://github.com/codergeek121/custom_elements-rails"
9-
spec.summary = "custom elements + Rails = 🎉"
10-
spec.description = "A simple way to use custom elements and importmaps"
9+
spec.summary = "Custom Elements + Rails + Importmaps = 🎉"
10+
spec.description = "A simple way to use custom elements to your Rails app. #nobuild"
1111
spec.license = "MIT"
1212

1313
spec.metadata["homepage_uri"] = spec.homepage
@@ -19,6 +19,5 @@ Gem::Specification.new do |spec|
1919
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
2020
end
2121

22-
# TODO
23-
spec.add_dependency "rails", ">= 7.1.3.2"
22+
spec.add_dependency "rails", ">= 7"
2423
end

lib/custom_elements/rails/railtie.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module CustomElements
22
module Rails
3-
class Railtie < ::Rails::Railtie
3+
class Railtie < ::Rails::Engine
4+
initializer "custom_elements-rails.assets.precompile" do |app|
5+
app.config.assets.precompile += %w( custom_elements-rails.js )
6+
end
47
end
58
end
69
end

lib/custom_elements/rails/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CustomElements
22
module Rails
3-
VERSION = "0.1.0"
3+
VERSION = "0.1.1"
44
end
55
end

lib/install/install.rb

+31
Original file line numberDiff line numberDiff line change
@@ -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
+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# desc "Explaining what the task does"
2-
# task :custom_elements_rails do
3-
# # Task goes here
4-
# end
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

0 commit comments

Comments
 (0)