Skip to content

Commit e8bc40b

Browse files
authored
setup generator for inertia installs (#64)
* Install generator for react apps * Move csrf token above the render call in the inertia template file * Add separate rake tasks for each front end type (only react actually works for now) * Verify that Webpacker is installed before running generator * Add descriptions for the (currently nonfunctioning) vue and svelte install tasks
1 parent e4ac019 commit e8bc40b

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class InertiaExampleController < ApplicationController
2+
def index
3+
render inertia: 'InertiaExample', props: {
4+
name: 'World',
5+
}
6+
end
7+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { App } from '@inertiajs/inertia-react';
2+
import React from 'react';
3+
import { render } from 'react-dom';
4+
import axios from 'axios';
5+
import { InertiaProgress } from '@inertiajs/progress';
6+
7+
document.addEventListener('DOMContentLoaded', () => {
8+
InertiaProgress.init();
9+
const el = document.getElementById('app')
10+
11+
const csrfToken = document.querySelector('meta[name=csrf-token]').content;
12+
axios.defaults.headers.common['X-CSRF-Token'] = csrfToken;
13+
14+
render(
15+
<App
16+
initialPage={JSON.parse(el.dataset.page)}
17+
resolveComponent={name => require(`../Pages/${name}`).default}
18+
/>,
19+
el
20+
)
21+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
const InertiaExample = ({name}) => (
4+
<>
5+
<h1>Hello {name}!</h1>
6+
</>
7+
);
8+
9+
export default InertiaExample;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module InertiaRails
2+
class InstallGenerator < Rails::Generators::Base
3+
source_root File.expand_path('./install', __dir__)
4+
class_option :front_end, type: :string, default: 'react'
5+
6+
FRONT_END_INSTALLERS = [
7+
'react',
8+
]
9+
10+
def install
11+
exit! unless installable?
12+
13+
install_base!
14+
15+
send "install_#{options[:front_end]}!"
16+
17+
say "You're all set! Run rails s and checkout localhost:3000/inertia-example", :green
18+
end
19+
20+
protected
21+
22+
def installable?
23+
unless run("./bin/rails webpacker:verify_install")
24+
say "Sorry, you need to have webpacker installed for inertia_rails default setup.", :red
25+
return false
26+
end
27+
28+
unless options[:front_end].in? FRONT_END_INSTALLERS
29+
say "Sorry, there is no generator for #{options[:front_end]}!\n\n", :red
30+
say "If you are a #{options[:front_end]} developer, please help us improve inertia_rails by contributing an installer.\n\n"
31+
say "https://github.com/inertiajs/inertia-rails/\n\n"
32+
33+
return false
34+
end
35+
end
36+
37+
def install_base!
38+
say "Adding inertia pack tag to application layout", :blue
39+
insert_into_file Rails.root.join("app/views/layouts/application.html.erb").to_s, after: "<%= javascript_pack_tag 'application' %>\n" do
40+
"\t\t<%= javascript_pack_tag 'inertia' %>\n"
41+
end
42+
43+
say "Installing inertia client packages", :blue
44+
run "yarn add @inertiajs/inertia @inertiajs/progress"
45+
46+
say "Copying example files", :blue
47+
template "controller.rb", Rails.root.join("app/controllers/inertia_example_controller.rb").to_s
48+
49+
say "Adding a route for the example inertia controller...", :blue
50+
route "get 'inertia-example', to: 'inertia_example#index'"
51+
end
52+
53+
def install_react!
54+
say "Creating a React page component...", :blue
55+
run 'yarn add @inertiajs/inertia-react'
56+
template "react.jsx", Rails.root.join("app/javascript/Pages/InertiaExample.js").to_s
57+
say "Copying inertia.jsx into webpacker's packs folder...", :blue
58+
template "inertia.jsx", Rails.root.join("app/javascript/packs/inertia.jsx").to_s
59+
say "done!", :green
60+
end
61+
end
62+
end

lib/tasks/inertia_rails.rake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace :inertia_rails do
2+
namespace :install do
3+
desc "Installs inertia_rails packages and configurations for a React based app"
4+
task :react => :environment do
5+
system 'rails g inertia_rails:install --front_end react'
6+
end
7+
desc "Installs inertia_rails packages and configurations for a Vue based app"
8+
task vue: :environment do
9+
system 'rails g inertia_rails:install --front_end vue'
10+
end
11+
desc "Installs inertia_rails packages and configurations for a Svelte based app"
12+
task svelte: :environment do
13+
system 'rails g inertia_rails:install --front_end svelte'
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)