Skip to content

Commit 6cff2e8

Browse files
committed
Improve Inertia initializers
1 parent fcf18ca commit 6cff2e8

File tree

20 files changed

+206
-80
lines changed

20 files changed

+206
-80
lines changed

.github/workflows/generators.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ jobs:
1818
test:
1919
runs-on: ubuntu-latest
2020
strategy:
21+
fail-fast: false
2122
matrix:
22-
framework: [react, vue, svelte4, svelte]
23+
framework: [react, vue, svelte, svelte4]
2324
typescript: [true, false]
2425
tailwind: [true, false]
2526
ruby: ['3.3']
2627
node: ['22']
28+
inertia_version: ['1.2.0', 'next', 'beta']
29+
exclude:
30+
# 1.2.0 does not support typescript
31+
- typescript: true
32+
inertia_version: '1.2.0'
33+
# 1.2.0 doesn't support Svelte 5
34+
- framework: svelte
35+
inertia_version: '1.2.0'
2736

28-
name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }})
37+
name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }}, Inertia:${{ matrix.inertia_version }})
2938

3039
steps:
3140
- uses: actions/checkout@v4
@@ -48,7 +57,7 @@ jobs:
4857
tmp/bundle_cache
4958
tmp/npm_cache
5059
~/.npm
51-
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
60+
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ matrix.inertia_version }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
5261
restore-keys: |
5362
${{ runner.os }}-deps-${{ matrix.framework }}-
5463
${{ runner.os }}-deps-
@@ -60,13 +69,13 @@ jobs:
6069
run: |
6170
ts_flag=${{ matrix.typescript && '--typescript' || '--no-typescript' }}
6271
tw_flag=${{ matrix.tailwind && '--tailwind' || '--no-tailwind' }}
63-
bin/generate_scaffold_example --framework=${{ matrix.framework }} $ts_flag $tw_flag
72+
bin/generate_scaffold_example --framework=${{ matrix.framework }} --inertia-version=${{ matrix.inertia_version }} $ts_flag $tw_flag
6473
6574
- name: Upload test artifacts
6675
if: failure()
67-
uses: actions/upload-artifact@v3
76+
uses: actions/upload-artifact@v4
6877
with:
69-
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}
78+
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}-v${{ matrix.inertia_version }}
7079
path: |
7180
tmp/scaffold_example/log
7281
tmp/scaffold_example/tmp/screenshots

.rubocop_todo.yml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --exclude-limit 10000`
3-
# on 2024-11-11 08:39:28 UTC using RuboCop version 1.68.0.
3+
# on 2024-11-22 18:44:50 UTC using RuboCop version 1.68.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -29,6 +29,14 @@ Layout/BlockEndNewline:
2929
Exclude:
3030
- 'spec/inertia/rendering_spec.rb'
3131

32+
# Offense count: 2
33+
# This cop supports safe autocorrection (--autocorrect).
34+
# Configuration parameters: EnforcedStyle, IndentOneStep, IndentationWidth.
35+
# SupportedStyles: case, end
36+
Layout/CaseIndentation:
37+
Exclude:
38+
- 'lib/inertia_rails/renderer.rb'
39+
3240
# Offense count: 2
3341
# This cop supports safe autocorrection (--autocorrect).
3442
# Configuration parameters: AllowForAlignment.
@@ -57,6 +65,14 @@ Layout/EmptyLinesAroundBlockBody:
5765
Exclude:
5866
- 'spec/inertia/rails_mimic_spec.rb'
5967

68+
# Offense count: 1
69+
# This cop supports safe autocorrection (--autocorrect).
70+
# Configuration parameters: EnforcedStyleAlignWith, Severity.
71+
# SupportedStylesAlignWith: keyword, variable, start_of_line
72+
Layout/EndAlignment:
73+
Exclude:
74+
- 'lib/inertia_rails/renderer.rb'
75+
6076
# Offense count: 3
6177
# This cop supports safe autocorrection (--autocorrect).
6278
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
@@ -123,20 +139,19 @@ Layout/SpaceInsideArrayLiteralBrackets:
123139
- 'spec/dummy/app/controllers/inertia_test_controller.rb'
124140
- 'spec/dummy/config/environments/production.rb'
125141

126-
# Offense count: 22
142+
# Offense count: 20
127143
# This cop supports safe autocorrection (--autocorrect).
128144
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
129145
# SupportedStyles: space, no_space
130146
# SupportedStylesForEmptyBraces: space, no_space
131147
Layout/SpaceInsideBlockBraces:
132148
Exclude:
133-
- 'lib/inertia_rails/renderer.rb'
134149
- 'spec/inertia/configuration_spec.rb'
135150
- 'spec/inertia/error_sharing_spec.rb'
136151
- 'spec/inertia/rendering_spec.rb'
137152
- 'spec/inertia/request_spec.rb'
138153

139-
# Offense count: 130
154+
# Offense count: 128
140155
# This cop supports safe autocorrection (--autocorrect).
141156
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
142157
# SupportedStyles: space, no_space, compact
@@ -145,7 +160,6 @@ Layout/SpaceInsideHashLiteralBraces:
145160
Exclude:
146161
- 'lib/inertia_rails/controller.rb'
147162
- 'lib/inertia_rails/middleware.rb'
148-
- 'lib/patches/mapper.rb'
149163
- 'spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb'
150164
- 'spec/dummy/app/controllers/inertia_share_test_controller.rb'
151165
- 'spec/inertia/conditional_sharing_spec.rb'
@@ -282,6 +296,12 @@ Style/ConditionalAssignment:
282296
Exclude:
283297
- 'lib/inertia_rails/renderer.rb'
284298

299+
# Offense count: 1
300+
# This cop supports safe autocorrection (--autocorrect).
301+
Style/EachWithObject:
302+
Exclude:
303+
- 'lib/inertia_rails/renderer.rb'
304+
285305
# Offense count: 1
286306
# This cop supports safe autocorrection (--autocorrect).
287307
# Configuration parameters: AutoCorrect, EnforcedStyle, AllowComments.
@@ -420,12 +440,15 @@ Style/HashSyntax:
420440
Exclude:
421441
- 'lib/tasks/inertia_rails.rake'
422442

423-
# Offense count: 3
443+
# Offense count: 6
424444
# This cop supports safe autocorrection (--autocorrect).
425445
Style/IfUnlessModifier:
426446
Exclude:
427447
- 'lib/inertia_rails/configuration.rb'
428448
- 'lib/inertia_rails/controller.rb'
449+
- 'lib/patches/better_errors.rb'
450+
- 'lib/patches/debug_exceptions/patch-5-0.rb'
451+
- 'lib/patches/debug_exceptions/patch-5-1.rb'
429452
- 'spec/dummy/config/environments/test.rb'
430453

431454
# Offense count: 2
@@ -440,13 +463,14 @@ Style/KeywordArgumentsMerging:
440463
Exclude:
441464
- 'lib/inertia_rails/configuration.rb'
442465

443-
# Offense count: 2
466+
# Offense count: 3
444467
# This cop supports safe autocorrection (--autocorrect).
445468
# Configuration parameters: EnforcedStyle.
446469
# SupportedStyles: line_count_dependent, lambda, literal
447470
Style/Lambda:
448471
Exclude:
449472
- 'spec/dummy/app/controllers/inertia_lambda_shared_props_controller.rb'
473+
- 'spec/dummy/app/controllers/inertia_render_test_controller.rb'
450474
- 'spec/dummy/app/controllers/transformed_inertia_rails_mimic_controller.rb'
451475

452476
# Offense count: 1
@@ -586,7 +610,7 @@ Style/SymbolArray:
586610
EnforcedStyle: percent
587611
MinSize: 3
588612

589-
# Offense count: 11
613+
# Offense count: 16
590614
# This cop supports safe autocorrection (--autocorrect).
591615
# Configuration parameters: EnforcedStyleForMultiline.
592616
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
@@ -599,7 +623,7 @@ Style/TrailingCommaInArguments:
599623
- 'spec/inertia/configuration_spec.rb'
600624
- 'spec/inertia/rendering_spec.rb'
601625

602-
# Offense count: 17
626+
# Offense count: 21
603627
# This cop supports safe autocorrection (--autocorrect).
604628
# Configuration parameters: EnforcedStyleForMultiline.
605629
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
@@ -623,7 +647,7 @@ Style/WordArray:
623647
EnforcedStyle: percent
624648
MinSize: 4
625649

626-
# Offense count: 16
650+
# Offense count: 21
627651
# This cop supports safe autocorrection (--autocorrect).
628652
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
629653
# URISchemes: http, https

bin/generate_scaffold_example

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ OptionParser.new do |opts|
2626
opts.on('--[no-]tailwind', 'Enable/disable Tailwind') do |t|
2727
options[:tailwind] = t
2828
end
29+
30+
opts.on('--inertia-version VERSION', 'Specify Inertia version') do |v|
31+
options[:inertia_version] = v
32+
end
2933
end.parse!
3034

3135
# Build generator args string
3236
generator_args = "--framework=#{options[:framework]}"
3337
generator_args += ' --typescript' if options[:typescript]
34-
generator_args += ' --install-vite'
35-
generator_args += ' --install-tailwind' if options[:tailwind]
38+
generator_args += ' --tailwind' if options[:tailwind]
39+
generator_args += " --inertia-version=#{options[:inertia_version]}" if options[:inertia_version]
3640

3741
# Setup paths relative to project root
3842
project_root = File.expand_path('..', __dir__)
@@ -57,25 +61,28 @@ system("rails new #{app_dir} -J")
5761
# Install and configure with caching
5862
Dir.chdir(app_dir) do
5963
# Configure bundler to use cache in project root
60-
system("bundle config set --local path '#{gem_cache}'")
64+
system("bundle config set --local path '#{gem_cache}'", exception: true)
6165

6266
# Configure npm to use cache in project root
63-
system("npm config set cache '#{npm_cache}'")
67+
system("npm config set cache '#{npm_cache}'", exception: true)
6468

6569
# Install dependencies
66-
system('bundle add inertia_rails --path ../../')
67-
system('bundle add bcrypt')
68-
system('bin/rails active_storage:install')
70+
system('bundle add inertia_rails --path ../../', exception: true)
71+
system('bundle add bcrypt', exception: true)
72+
system('bin/rails active_storage:install', exception: true)
6973

7074
# Run install generator with configured options
71-
system("bin/rails g inertia:install --no-interactive --force #{generator_args}")
75+
system("bin/rails g inertia:install --no-interactive --force --vite #{generator_args} --verbose", exception: true)
7276

7377
# Generate a scaffold
74-
system('bin/rails g inertia:scaffold user name email admin:boolean password:digest avatar:attachment')
75-
system('bin/rails g inertia:scaffold post content:text published_at:date gallery:attachments')
76-
system('bin/rails db:migrate')
78+
system('bin/rails g inertia:scaffold user name email admin:boolean password:digest avatar:attachment', exception: true)
79+
system('bin/rails g inertia:scaffold post content:text published_at:date gallery:attachments', exception: true)
80+
system('bin/rails db:migrate', exception: true)
7781

7882
# Run tests
79-
system('bin/rails test')
80-
system('bin/rails test:system')
83+
system('bin/rails test', exception: true)
84+
system('bin/rails test:system', exception: true)
85+
86+
# Lint code
87+
system('npm run check', exception: true) if options[:typescript]
8188
end

docs/cookbook/integrating-shadcn-ui.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ If you're starting fresh, create a new Rails application with Inertia (or skip t
1414
rails new -JA shadcn-inertia-rails
1515
cd shadcn-inertia-rails
1616

17-
rails generate inertia:install `--framework=react --typescript --install-vite --install-tailwind --no-interactive`
17+
rails generate inertia:install `--framework=react --typescript --vite --tailwind --no-interactive`
1818
Installing Inertia's Rails adapter
1919
...
2020
```
@@ -25,7 +25,7 @@ Installing Inertia's Rails adapter
2525
rails new -JA shadcn-inertia-rails
2626
cd shadcn-inertia-rails
2727
28-
rails generate inertia:install --framework=react --install-vite --install-tailwind --no-interactive
28+
rails generate inertia:install --framework=react --vite --tailwind --no-interactive
2929
Installing Inertia's Rails adapter
3030
...
3131
```

lib/generators/inertia/install/frameworks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ vue:
3030
- "vue"
3131
- "@vitejs/plugin-vue"
3232
packages_ts:
33-
- "typescript"
33+
- "typescript@~5.6.2"
3434
- "vue-tsc"
3535
vite_plugin_import: "import vue from '@vitejs/plugin-vue'"
3636
vite_plugin_call: "vue()"

lib/generators/inertia/install/install_generator.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class InstallGenerator < Rails::Generators::Base
3434
class_option :interactive, type: :boolean, default: true,
3535
desc: 'Whether to prompt for optional installations'
3636

37-
class_option :install_tailwind, type: :boolean, default: false,
38-
desc: 'Whether to install Tailwind CSS'
39-
class_option :install_vite, type: :boolean, default: false,
40-
desc: 'Whether to install Vite Ruby'
37+
class_option :tailwind, type: :boolean, default: false,
38+
desc: 'Whether to install Tailwind CSS'
39+
class_option :vite, type: :boolean, default: false,
40+
desc: 'Whether to install Vite Ruby'
4141
class_option :example_page, type: :boolean, default: true,
4242
desc: 'Whether to add an example Inertia page'
4343

@@ -127,6 +127,11 @@ def install_typescript
127127
end
128128

129129
add_dependencies(*FRAMEWORKS[framework]['packages_ts'])
130+
131+
say 'Copying adding scripts to package.json'
132+
run 'npm pkg set scripts.check="svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"' if svelte?
133+
run 'npm pkg set scripts.check="vue-tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'vue'
134+
run 'npm pkg set scripts.check="tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'react'
130135
end
131136

132137
def install_example_page
@@ -236,13 +241,13 @@ def vite_config_path
236241
def install_vite?
237242
return @install_vite if defined?(@install_vite)
238243

239-
@install_vite = options[:install_vite] || yes?('Would you like to install Vite Ruby? (y/n)', :green)
244+
@install_vite = options[:vite] || yes?('Would you like to install Vite Ruby? (y/n)', :green)
240245
end
241246

242247
def install_tailwind?
243248
return @install_tailwind if defined?(@install_tailwind)
244249

245-
@install_tailwind = options[:install_tailwind] || yes?('Would you like to install Tailwind CSS? (y/n)', :green)
250+
@install_tailwind = options[:tailwind] || yes?('Would you like to install Tailwind CSS? (y/n)', :green)
246251
end
247252

248253
def typescript?

lib/generators/inertia/install/templates/react/inertia.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@ createInertiaApp({
1515

1616
resolve: (name) => {
1717
const pages = import.meta.glob('../pages/**/*.jsx', { eager: true })
18-
return pages[`../pages/${name}.jsx`]
18+
const page = pages[`../pages/${name}.jsx`]
19+
if (!page) {
20+
console.error(`Missing Inertia page component: '${name}.jsx'`)
21+
}
1922

2023
// To use a default layout, import the Layout component
2124
// and use the following lines.
2225
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
2326
//
24-
// const page = pages[`../pages/${name}.jsx`]
2527
// page.default.layout ||= (page) => createElement(Layout, null, page)
26-
// return page
28+
29+
return page
2730
},
2831

2932
setup({ el, App, props }) {
30-
const root = createRoot(el)
31-
32-
root.render(createElement(App, props))
33+
if (el) {
34+
createRoot(el).render(createElement(App, props))
35+
} else {
36+
console.error(
37+
'Missing root element.\n\n' +
38+
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
39+
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
40+
)
41+
}
3342
},
3443
})

lib/generators/inertia/install/templates/react/inertia.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,30 @@ createInertiaApp({
1616
// progress: false,
1717

1818
resolve: (name) => {
19-
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.tsx', { eager: true })
20-
return pages[`../pages/${name}.tsx`]
19+
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.tsx', {eager: true})
20+
const page = pages[`../pages/${name}.tsx`]
21+
if (!page) {
22+
console.error(`Missing Inertia page component: '${name}.tsx'`)
23+
}
2124

2225
// To use a default layout, import the Layout component
23-
// and use the following lines.
26+
// and use the following line.
2427
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
2528
//
26-
// const page = pages[`../pages/${name}.tsx`]
2729
// page.default.layout ||= (page) => createElement(Layout, null, page)
28-
// return page
29-
},
3030

31-
setup({ el, App, props }) {
32-
const root = createRoot(el)
31+
return page
32+
},
3333

34-
root.render(createElement(App, props))
34+
setup({el, App, props}) {
35+
if (el) {
36+
createRoot(el).render(createElement(App, props))
37+
} else {
38+
console.error(
39+
'Missing root element.\n\n' +
40+
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
41+
'Consider moving <%%= vite_typescript_tag "inertia" %> to the Inertia-specific layout instead.'
42+
)
43+
}
3544
},
3645
})

0 commit comments

Comments
 (0)