Skip to content

Commit

Permalink
Improve Inertia initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Nov 21, 2024
1 parent 0401abb commit ec8aef4
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 36 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ jobs:
tailwind: [true, false]
ruby: ['3.3']
node: ['22']
inertia_version: ['latest', 'next']

name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }})
name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }}, Inertia:${{ matrix.inertia_version }})

steps:
- uses: actions/checkout@v4
Expand All @@ -48,7 +49,7 @@ jobs:
tmp/bundle_cache
tmp/npm_cache
~/.npm
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ matrix.inertia_version }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-deps-${{ matrix.framework }}-
${{ runner.os }}-deps-
Expand All @@ -60,13 +61,13 @@ jobs:
run: |
ts_flag=${{ matrix.typescript && '--typescript' || '--no-typescript' }}
tw_flag=${{ matrix.tailwind && '--tailwind' || '--no-tailwind' }}
bin/generate_scaffold_example --framework=${{ matrix.framework }} $ts_flag $tw_flag
bin/generate_scaffold_example --framework=${{ matrix.framework }} --inertia-version=${{ matrix.inertia_version }} $ts_flag $tw_flag
- name: Upload test artifacts
if: failure()
uses: actions/upload-artifact@v3
with:
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}-v${{ matrix.inertia_version }}
path: |
tmp/scaffold_example/log
tmp/scaffold_example/tmp/screenshots
Expand Down
8 changes: 8 additions & 0 deletions bin/generate_scaffold_example
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ OptionParser.new do |opts|
opts.on('--[no-]tailwind', 'Enable/disable Tailwind') do |t|
options[:tailwind] = t
end

opts.on('--inertia-version VERSION', 'Specify Inertia version') do |v|
options[:inertia_version] = v
end
end.parse!

# Build generator args string
generator_args = "--framework=#{options[:framework]}"
generator_args += ' --typescript' if options[:typescript]
generator_args += ' --install-vite'
generator_args += ' --install-tailwind' if options[:tailwind]
generator_args += " --inertia-version=#{options[:inertia_version]}" if options[:inertia_version]

# Setup paths relative to project root
project_root = File.expand_path('..', __dir__)
Expand Down Expand Up @@ -78,4 +83,7 @@ Dir.chdir(app_dir) do
# Run tests
system('bin/rails test')
system('bin/rails test:system')

# Lint code
system('npm run check') if options[:typescript]
end
5 changes: 5 additions & 0 deletions lib/generators/inertia/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def install_typescript
end

add_dependencies(*FRAMEWORKS[framework]['packages_ts'])

say 'Copying adding scripts to package.json'
run 'npm pkg set scripts.check="svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"' if svelte?
run 'npm pkg set scripts.check="vue-tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'vue'
run 'npm pkg set scripts.check="tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'react'
end

def install_example_page
Expand Down
21 changes: 15 additions & 6 deletions lib/generators/inertia/install/templates/react/inertia.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.jsx', { eager: true })
return pages[`../pages/${name}.jsx`]
const page = pages[`../pages/${name}.jsx`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.jsx'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.jsx`]
// page.default.layout ||= (page) => createElement(Layout, null, page)
// return page

return page
},

setup({ el, App, props }) {
const root = createRoot(el)

root.render(createElement(App, props))
if (el) {
createRoot(el).render(createElement(App, props))
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
27 changes: 18 additions & 9 deletions lib/generators/inertia/install/templates/react/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@ createInertiaApp({
// progress: false,

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

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

setup({ el, App, props }) {
const root = createRoot(el)
return page
},

root.render(createElement(App, props))
setup({el, App, props}) {
if (el) {
createRoot(el).render(createElement(App, props))
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_typescript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
20 changes: 16 additions & 4 deletions lib/generators/inertia/install/templates/svelte/inertia.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
const page = pages[`../pages/${name}.svelte`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.svelte'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// and use the following line.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.svelte`]
// return { default: page.default, layout: page.layout || Layout }

return page
},

setup({ el, App, props }) {
mount(App, { target: el, props })
if (el) {
mount(App, { target: el, props })
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
20 changes: 16 additions & 4 deletions lib/generators/inertia/install/templates/svelte/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
const page = pages[`../pages/${name}.svelte`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.svelte'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// and use the following line.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.svelte`]
// return { default: page.default, layout: page.layout || Layout }

return page
},

setup({ el, App, props }) {
mount(App, { target: el, props })
if (el) {
mount(App, { target: el, props })
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_typescript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
18 changes: 15 additions & 3 deletions lib/generators/inertia/install/templates/svelte4/inertia.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
const page = pages[`../pages/${name}.svelte`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.svelte'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.svelte`]
// return { default: page.default, layout: page.layout || Layout }

return page
},

setup({ el, App, props }) {
new App({ target: el, props })
if (el) {
new App({ target: el, props })
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
20 changes: 16 additions & 4 deletions lib/generators/inertia/install/templates/svelte4/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
const page = pages[`../pages/${name}.svelte`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.svelte'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// and use the following line.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.svelte`]
// return { default: page.default, layout: page.layout || Layout }

return page
},

setup({ el, App, props }) {
new App({ target: el, props })
if (el) {
new App({ target: el, props })
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MouseEvent } from 'react'
import { Link, Head } from '@inertiajs/react'
import { <%= inertia_model_type %> } from './types'
import <%= inertia_component_name %> from './<%= inertia_component_name %>'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MouseEvent } from 'react'
import { Link, Head } from '@inertiajs/react'
import { <%= inertia_model_type %> } from './types'
import <%= inertia_component_name %> from './<%= inertia_component_name %>'
Expand Down

0 comments on commit ec8aef4

Please sign in to comment.