Skip to content

Commit e88c74f

Browse files
authored
Merge pull request #281 from skryukov/update-install-generator
Update inertia generator
2 parents e547270 + e77e236 commit e88c74f

File tree

75 files changed

+409
-1841
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+409
-1841
lines changed

.github/workflows/generators.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
framework: [react, vue, svelte, svelte4]
23+
framework: [react, vue, svelte]
2424
typescript: [true, false]
2525
tailwind: [true, false]
26-
ruby: ['3.3']
26+
ruby: ['3.4']
2727
node: ['22']
2828
inertia_version: ['latest']
2929

bin/generate_scaffold_example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ options = {
1414
OptionParser.new do |opts|
1515
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
1616

17-
opts.on('--framework FRAMEWORK', %w[react vue svelte svelte4],
18-
'Choose framework (react/vue/svelte4/svelte)') do |f|
17+
opts.on('--framework FRAMEWORK', %w[react vue svelte],
18+
'Choose framework (react/vue/svelte)') do |f|
1919
options[:framework] = f
2020
end
2121

docs/guide/server-side-setup.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This command will:
2727

2828
- Check for Vite Rails and install it if not present
2929
- Ask if you want to use TypeScript
30-
- Ask you to choose your preferred frontend framework (React, Vue, Svelte 4, or Svelte 5)
30+
- Ask you to choose your preferred frontend framework (React, Vue, Svelte)
3131
- Ask if you want to install Tailwind CSS
3232
- Install necessary dependencies
3333
- Set up the application to work with Inertia
@@ -49,7 +49,7 @@ Vite Rails gem successfully installed
4949
Vite Rails successfully installed
5050
Would you like to use TypeScript? (y/n) y
5151
Adding TypeScript support
52-
What framework do you want to use with Inertia? [react, vue, svelte4, svelte] (react)
52+
What framework do you want to use with Inertia? [react, vue, svelte] (react)
5353
run npm add @types/react @types/react-dom typescript --silent from "."
5454
Would you like to install Tailwind CSS? (y/n) y
5555
Installing Tailwind CSS

lib/generators/inertia/install/frameworks.yml

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ react:
1212
vite_plugin_import: "import react from '@vitejs/plugin-react'"
1313
vite_plugin_call: "react()"
1414
copy_files_ts:
15-
"InertiaExample.tsx": "%{js_destination_path}/pages/InertiaExample.tsx"
15+
"InertiaExample.tsx": "%{js_destination_path}/pages/inertia_example/index.tsx"
1616
"tsconfig.json": "tsconfig.json"
1717
"tsconfig.app.json": "tsconfig.app.json"
1818
"tsconfig.node.json": "tsconfig.node.json"
19-
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
19+
"types/vite-env.d.ts": "%{js_destination_path}/types/vite-env.d.ts"
20+
"types/globals.d.ts": "%{js_destination_path}/types/globals.d.ts"
21+
"types/index.ts": "%{js_destination_path}/types/index.ts"
2022
copy_files_js:
21-
"InertiaExample.jsx": "%{js_destination_path}/pages/InertiaExample.jsx"
23+
"InertiaExample.jsx": "%{js_destination_path}/pages/inertia_example/index.jsx"
2224
copy_files:
23-
"InertiaExample.module.css": "%{js_destination_path}/pages/InertiaExample.module.css"
25+
"InertiaExample.module.css": "%{js_destination_path}/pages/inertia_example/index.module.css"
2426
"../assets/react.svg": "%{js_destination_path}/assets/react.svg"
2527
"../assets/inertia.svg": "%{js_destination_path}/assets/inertia.svg"
2628
"../assets/vite_ruby.svg": "%{js_destination_path}/assets/vite_ruby.svg"
@@ -41,39 +43,15 @@ vue:
4143
"../assets/inertia.svg": "%{js_destination_path}/assets/inertia.svg"
4244
"../assets/vite_ruby.svg": "%{js_destination_path}/assets/vite_ruby.svg"
4345
copy_files_ts:
44-
"InertiaExample.ts.vue": "%{js_destination_path}/pages/InertiaExample.vue"
46+
"InertiaExample.ts.vue": "%{js_destination_path}/pages/inertia_example/index.vue"
4547
"tsconfig.json": "tsconfig.json"
4648
"tsconfig.app.json": "tsconfig.app.json"
4749
"tsconfig.node.json": "tsconfig.node.json"
48-
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
50+
"types/vite-env.d.ts": "%{js_destination_path}/types/vite-env.d.ts"
51+
"types/globals.d.ts": "%{js_destination_path}/types/globals.d.ts"
52+
"types/index.ts": "%{js_destination_path}/types/index.ts"
4953
copy_files_js:
50-
"InertiaExample.vue": "%{js_destination_path}/pages/InertiaExample.vue"
51-
52-
svelte4:
53-
inertia_package: "@inertiajs/svelte"
54-
packages:
55-
- "svelte@4"
56-
- "@sveltejs/vite-plugin-svelte@3"
57-
- "vite@5"
58-
packages_ts:
59-
- "@tsconfig/svelte@4"
60-
- "svelte-check"
61-
- "typescript"
62-
- "tslib"
63-
vite_plugin_import: "import { svelte } from '@sveltejs/vite-plugin-svelte'"
64-
vite_plugin_call: "svelte()"
65-
copy_files_ts:
66-
"InertiaExample.ts.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
67-
"tsconfig.json": "tsconfig.json"
68-
"tsconfig.node.json": "tsconfig.node.json"
69-
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
70-
copy_files_js:
71-
"InertiaExample.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
72-
copy_files:
73-
"svelte.config.js": "svelte.config.js"
74-
"../assets/svelte.svg": "%{js_destination_path}/assets/svelte.svg"
75-
"../assets/inertia.svg": "%{js_destination_path}/assets/inertia.svg"
76-
"../assets/vite_ruby.svg": "%{js_destination_path}/assets/vite_ruby.svg"
54+
"InertiaExample.vue": "%{js_destination_path}/pages/inertia_example/index.vue"
7755

7856
svelte:
7957
inertia_package: "@inertiajs/svelte"
@@ -89,12 +67,14 @@ svelte:
8967
vite_plugin_import: "import { svelte } from '@sveltejs/vite-plugin-svelte'"
9068
vite_plugin_call: "svelte()"
9169
copy_files_ts:
92-
"InertiaExample.ts.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
70+
"InertiaExample.ts.svelte": "%{js_destination_path}/pages/inertia_example/index.svelte"
9371
"tsconfig.json": "tsconfig.json"
9472
"tsconfig.node.json": "tsconfig.node.json"
95-
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
73+
"types/vite-env.d.ts": "%{js_destination_path}/types/vite-env.d.ts"
74+
"types/globals.d.ts": "%{js_destination_path}/types/globals.d.ts"
75+
"types/index.ts": "%{js_destination_path}/types/index.ts"
9676
copy_files_js:
97-
"InertiaExample.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
77+
"InertiaExample.svelte": "%{js_destination_path}/pages/inertia_example/index.svelte"
9878
copy_files:
9979
"svelte.config.js": "svelte.config.js"
10080
"../assets/svelte.svg": "%{js_destination_path}/assets/svelte.svg"

lib/generators/inertia/install/install_generator.rb

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,21 @@ def install
6565
install_example_page if options[:example_page]
6666

6767
say 'Copying bin/dev'
68-
copy_file "#{__dir__}/templates/dev", 'bin/dev'
68+
copy_file 'dev', 'bin/dev'
6969
chmod 'bin/dev', 0o755, verbose: verbose?
7070

71+
if install_vite?
72+
say 'Adding redirect to localhost'
73+
routing_code = <<~RUBY
74+
\n # Redirect to localhost from 127.0.0.1 to use same IP address with Vite server
75+
constraints(host: "127.0.0.1") do
76+
get "(*path)", to: redirect { |params, req| "\#{req.protocol}localhost:\#{req.port}/\#{params[:path]}" }
77+
end
78+
RUBY
79+
80+
route routing_code
81+
end
82+
7183
say "Inertia's Rails adapter successfully installed", :green
7284
end
7385

@@ -87,12 +99,15 @@ def install_inertia
8799
end
88100

89101
say "Copying #{inertia_entrypoint} entrypoint"
90-
template "#{framework}/#{inertia_entrypoint}", js_file_path("entrypoints/#{inertia_entrypoint}")
102+
copy_file "#{framework}/#{inertia_entrypoint}", js_file_path("entrypoints/#{inertia_entrypoint}")
103+
104+
say 'Copying InertiaController'
105+
copy_file 'inertia_controller.rb', file_path('app/controllers/inertia_controller.rb')
91106

92107
if application_layout.exist?
93108
say "Adding #{inertia_entrypoint} script tag to the application layout"
94109
headers = <<-ERB
95-
<%= #{vite_tag} "inertia" %>
110+
<%= #{vite_tag} %>
96111
<%= inertia_ssr_head %>
97112
ERB
98113
insert_into_file application_layout.to_s, headers, after: "<%= vite_client_tag %>\n"
@@ -103,14 +118,14 @@ def install_inertia
103118
before: '<%= vite_client_tag %>'
104119
end
105120

106-
gsub_file application_layout.to_s, /<title>/, '<title inertia>' unless svelte?
121+
gsub_file application_layout.to_s, /<title>/, '<title data-inertia>' unless svelte?
107122
else
108123
say_error 'Could not find the application layout file. Please add the following tags manually:', :red
109124
say_error '- <title>...</title>'
110-
say_error '+ <title inertia>...</title>'
125+
say_error '+ <title data-inertia>...</title>'
111126
say_error '+ <%= inertia_ssr_head %>'
112127
say_error '+ <%= vite_react_refresh_tag %>' if framework == 'react'
113-
say_error "+ <%= #{vite_tag} \"inertia\" %>"
128+
say_error "+ <%= #{vite_tag} %>"
114129
end
115130
end
116131

@@ -192,6 +207,8 @@ def install_vite
192207
say_error 'Failed to install Vite Rails', :red
193208
exit(false)
194209
end
210+
211+
add_package_manager_to_bin_setup
195212
end
196213
end
197214
end
@@ -258,11 +275,13 @@ def typescript?
258275
end
259276

260277
def inertia_entrypoint
261-
"inertia.#{typescript? ? 'ts' : 'js'}"
278+
"inertia.#{typescript? ? 'ts' : 'js'}#{'x' if react?}"
262279
end
263280

264281
def vite_tag
265-
typescript? ? 'vite_typescript_tag' : 'vite_javascript_tag'
282+
tag = typescript? ? 'vite_typescript_tag' : 'vite_javascript_tag'
283+
filename = "\"#{react? ? inertia_entrypoint : 'inertia'}\""
284+
"#{tag} #{filename}"
266285
end
267286

268287
def inertia_resolved_version
@@ -280,6 +299,10 @@ def svelte?
280299
framework.start_with? 'svelte'
281300
end
282301

302+
def react?
303+
framework.start_with? 'react'
304+
end
305+
283306
def inertia_package
284307
"#{FRAMEWORKS[framework]['inertia_package']}@#{options[:inertia_version]}"
285308
end
@@ -288,6 +311,26 @@ def framework
288311
@framework ||= options[:framework] || ask('What framework do you want to use with Inertia?', :green,
289312
limited_to: FRAMEWORKS.keys, default: 'react')
290313
end
314+
315+
def add_package_manager_to_bin_setup
316+
setup_file = file_path('bin/setup')
317+
return unless File.exist?(setup_file)
318+
319+
content = File.read(setup_file)
320+
pm_name = package_manager.name
321+
322+
# Check if package manager install already exists
323+
return if content.include?("#{pm_name} install")
324+
325+
if content.match?(/system\('bundle check'\) \|\| system!\('bundle install'\)/)
326+
say 'Adding package manager install to bin/setup'
327+
cmd = "system! '#{pm_name} install'"
328+
insert_into_file setup_file, " #{cmd}\n",
329+
after: /system\('bundle check'\) \|\| system!\('bundle install'\)\n/
330+
else
331+
say_status "Couldn't add `#{cmd}` script to bin/setup, add it manually", :red
332+
end
333+
end
291334
end
292335
end
293336
end

lib/generators/inertia/install/js_package_manager.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ def initialize(generator)
1212
end
1313

1414
def present?
15-
package_manager.present?
15+
name.present?
1616
end
1717

1818
def add_dependencies(*dependencies)
1919
options = @generator.options[:verbose] ? '' : ' --silent'
2020
@generator.in_root do
21-
@generator.run "#{package_manager} add #{dependencies.join(' ')}#{options}"
21+
@generator.run "#{name} add #{dependencies.join(' ')}#{options}"
2222
end
2323
end
2424

25-
private
26-
27-
def package_manager
28-
@package_manager ||= @generator.options[:package_manager] || detect_package_manager
25+
def name
26+
@name ||= @generator.options[:package_manager] || detect_package_manager
2927
end
3028

29+
private
30+
3131
def detect_package_manager
3232
return nil unless file?('package.json')
3333

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# frozen_string_literal: true
22

3-
class InertiaExampleController < ApplicationController
3+
class InertiaExampleController < InertiaController
44
def index
5-
render inertia: 'InertiaExample', props: {
6-
name: params.fetch(:name, 'World'),
7-
}
5+
render inertia: { name: params.fetch(:name, 'World') }
86
end
97
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class InertiaController < ApplicationController
4+
inertia_share flash: -> { flash.to_hash }
5+
end

lib/generators/inertia/install/templates/react/InertiaExample.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import inertiaSvg from '/assets/inertia.svg'
55
import reactSvg from '/assets/react.svg'
66
import viteRubySvg from '/assets/vite_ruby.svg'
77

8-
import cs from './InertiaExample.module.css'
8+
import cs from './index.module.css'
99

1010
export default function InertiaExample({ name }) {
1111
const [count, setCount] = useState(0)
@@ -47,7 +47,7 @@ export default function InertiaExample({ name }) {
4747
count is {count}
4848
</button>
4949
<p>
50-
Edit <code>app/frontend/pages/InertiaExample.jsx</code> and save to
50+
Edit <code>app/frontend/pages/inertia_example/index.jsx</code> and save to
5151
test HMR
5252
</p>
5353
</div>

lib/generators/inertia/install/templates/react/InertiaExample.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import inertiaSvg from '/assets/inertia.svg'
55
import reactSvg from '/assets/react.svg'
66
import viteRubySvg from '/assets/vite_ruby.svg'
77

8-
import cs from './InertiaExample.module.css'
8+
import cs from './index.module.css'
99

1010
export default function InertiaExample({ name }: { name: string }) {
1111
const [count, setCount] = useState(0)
@@ -47,7 +47,7 @@ export default function InertiaExample({ name }: { name: string }) {
4747
count is {count}
4848
</button>
4949
<p>
50-
Edit <code>app/frontend/pages/InertiaExample.jsx</code> and save to
50+
Edit <code>app/frontend/pages/inertia_example/index.tsx</code> and save to
5151
test HMR
5252
</p>
5353
</div>

0 commit comments

Comments
 (0)