Skip to content

Commit

Permalink
refactor: move stubs to inertia/ directory (#6)
Browse files Browse the repository at this point in the history
* refactor: move stubs to `inertia` directory

* chore: move inertia middleware to server stack

* feat: add not_found and server_error components

* fix: entrypoint vue

* refactor: move to inertia_layout.edge

* test: fix failing test
  • Loading branch information
Julien-R44 authored Mar 8, 2024
1 parent 7638372 commit 250958b
Show file tree
Hide file tree
Showing 37 changed files with 243 additions and 175 deletions.
16 changes: 9 additions & 7 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const ADAPTERS_INFO: {
pluginCall: 'vue()',
importDeclarations: [{ isNamed: false, module: '@vitejs/plugin-vue', identifier: 'vue' }],
},
ssrEntrypoint: 'resources/ssr.ts',
ssrEntrypoint: 'inertia/app/ssr.ts',
},
react: {
stubFolder: 'react',
Expand All @@ -61,7 +61,7 @@ const ADAPTERS_INFO: {
pluginCall: 'react()',
importDeclarations: [{ isNamed: false, module: '@vitejs/plugin-react', identifier: 'react' }],
},
ssrEntrypoint: 'resources/ssr.tsx',
ssrEntrypoint: 'inertia/app/ssr.tsx',
},
svelte: {
stubFolder: 'svelte',
Expand All @@ -79,7 +79,7 @@ const ADAPTERS_INFO: {
{ isNamed: true, module: '@sveltejs/vite-plugin-svelte', identifier: 'svelte' },
],
},
ssrEntrypoint: 'resources/ssr.ts',
ssrEntrypoint: 'inertia/app/ssr.ts',
},
solid: {
stubFolder: 'solid',
Expand All @@ -96,7 +96,7 @@ const ADAPTERS_INFO: {
ssrPluginCall: 'solid({ ssr: true })',
importDeclarations: [{ isNamed: false, module: 'vite-plugin-solid', identifier: 'solid' }],
},
ssrEntrypoint: 'resources/ssr.tsx',
ssrEntrypoint: 'inertia/app/ssr.tsx',
},
}

Expand Down Expand Up @@ -185,7 +185,7 @@ export async function configure(command: Configure) {
/**
* Add Inertia middleware
*/
await codemods.registerMiddleware('router', [
await codemods.registerMiddleware('server', [
{ path: '@adonisjs/inertia/inertia_middleware', position: 'after' },
])

Expand All @@ -205,6 +205,8 @@ export async function configure(command: Configure) {
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/tsconfig.json.stub`, {})
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/app.${appExt}.stub`, { ssr })
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/home.${compExt}.stub`, {})
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/not_found.${compExt}.stub`, {})
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/server_error.${compExt}.stub`, {})

if (ssr) {
await codemods.makeUsingStub(stubsRoot, `${stubFolder}/ssr.${appExt}.stub`, {})
Expand All @@ -214,7 +216,7 @@ export async function configure(command: Configure) {
* Register the inertia plugin in vite config
*/
const inertiaPluginCall = ssr
? `inertia({ ssr: { enabled: true, entrypoint: 'resources/ssr.${appExt}' } })`
? `inertia({ ssr: { enabled: true, entrypoint: 'inertia/app/ssr.${appExt}' } })`
: `inertia({ ssr: { enabled: false } })`

await codemods.registerVitePlugin(inertiaPluginCall, [
Expand All @@ -234,7 +236,7 @@ export async function configure(command: Configure) {
/**
* Register vite with adonisjs plugin
*/
const adonisjsPluginCall = `adonisjs({ entrypoints: ['resources/app.${appExt}'], reload: ['resources/views/**/*.edge'] })`
const adonisjsPluginCall = `adonisjs({ entrypoints: ['inertia/app/app.${appExt}'], reload: ['resources/views/**/*.edge'] })`
await codemods.registerVitePlugin(adonisjsPluginCall, [
{ isNamed: false, module: '@adonisjs/vite/client', identifier: 'adonisjs' },
])
Expand Down
8 changes: 5 additions & 3 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConf

return {
versionCache,
rootView: config.rootView ?? 'root',
rootView: config.rootView ?? 'inertia_layout',
sharedData: config.sharedData || {},
entrypoint: slash(config.entrypoint ?? (await detector.detectEntrypoint('resources/app.ts'))),
entrypoint: slash(
config.entrypoint ?? (await detector.detectEntrypoint('inertia/app/app.ts'))
),
ssr: {
enabled: config.ssr?.enabled ?? false,
pages: config.ssr?.pages,
entrypoint:
config.ssr?.entrypoint ?? (await detector.detectSsrEntrypoint('resources/ssr.ts')),
config.ssr?.entrypoint ?? (await detector.detectSsrEntrypoint('inertia/app/ssr.ts')),

bundle: config.ssr?.bundle ?? (await detector.detectSsrBundle('ssr/ssr.js')),
},
Expand Down
14 changes: 6 additions & 8 deletions src/files_detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export class FilesDetector {
*/
async detectEntrypoint(defaultPath: string) {
const possiblesLocations = [
'./inertia/app/app.ts',
'./inertia/app/app.tsx',
'./resources/app.ts',
'./resources/app.tsx',
'./resources/application/app.ts',
'./resources/application/app.tsx',
'./resources/app.jsx',
'./resources/app.js',
'./resources/application/app.jsx',
'./resources/application/app.js',
'./inertia/app/app.jsx',
]

const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot })
Expand All @@ -39,14 +38,13 @@ export class FilesDetector {
*/
async detectSsrEntrypoint(defaultPath: string) {
const possiblesLocations = [
'./inertia/app/ssr.ts',
'./inertia/app/ssr.tsx',
'./resources/ssr.ts',
'./resources/ssr.tsx',
'./resources/application/ssr.ts',
'./resources/application/ssr.tsx',
'./resources/ssr.jsx',
'./resources/ssr.js',
'./resources/application/ssr.jsx',
'./resources/application/ssr.js',
'./inertia/app/ssr.jsx',
]

const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot })
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type AssetsVersion = string | number | undefined
export interface InertiaConfig {
/**
* Path to the Edge view that will be used as the root view for Inertia responses.
* @default root (resources/views/root.edge)
* @default root (resources/views/inertia_layout.edge)
*/
rootView?: string

Expand Down
2 changes: 1 addition & 1 deletion stubs/app.css.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/css/app.css') })
exports({ to: app.makePath('inertia/css/app.css') })
}}}
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap');

Expand Down
4 changes: 2 additions & 2 deletions stubs/config.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export default defineConfig({
/**
* Path to the Edge view that will be used as the root view for Inertia responses
*/
rootView: 'root',
rootView: 'inertia_layout',

/**
* Data that should be shared with all rendered pages
*/
sharedData: {
errors: (ctx) => ctx.session.flashMessages.get('errors'),
errors: (ctx) => ctx.session?.flashMessages.get('errors'),
},

/**
Expand Down
8 changes: 4 additions & 4 deletions stubs/react/app.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{{
exports({ to: app.makePath('resources/app.tsx') })
exports({ to: app.makePath('inertia/app/app.tsx') })
}}}
import './css/app.css';
import '../css/app.css';

{{#if ssr}}
import { hydrateRoot } from 'react-dom/client'
Expand All @@ -20,8 +20,8 @@ createInertiaApp({

resolve: (name) => {
return resolvePageComponent(
{{ '`./pages/${name}.tsx`' }},
import.meta.glob('./pages/**/*.tsx'),
{{ '`../pages/${name}.tsx`' }},
import.meta.glob('../pages/**/*.tsx'),
)
},

Expand Down
14 changes: 14 additions & 0 deletions stubs/react/errors/not_found.tsx.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{{
exports({ to: app.makePath('inertia/pages/errors/not_found.tsx') })
}}}
export default function NotFound() {
return (
<>
<div className="container">
<div className="title">Page not found</div>

<span>This page does not exist.</span>
</div>
</>
)
}
14 changes: 14 additions & 0 deletions stubs/react/errors/server_error.tsx.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{{
exports({ to: app.makePath('inertia/pages/errors/server_error.tsx') })
}}}
export default function ServerError(props: { error: any }) {
return (
<>
<div className="container">
<div className="title">Server Error</div>

<span>{props.error.message}</span>
</div>
</>
)
}
2 changes: 1 addition & 1 deletion stubs/react/home.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/pages/home.tsx') })
exports({ to: app.makePath('inertia/pages/home.tsx') })
}}}
import { Head } from '@inertiajs/react'

Expand Down
4 changes: 2 additions & 2 deletions stubs/react/root.edge.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/views/root.edge') })
exports({ to: app.makePath('resources/views/inertia_layout.edge') })
}}}
<!DOCTYPE html>
<html>
Expand All @@ -12,7 +12,7 @@

@viteReactRefresh()
@inertiaHead()
{{ "@vite(['resources/app.tsx', `resources/pages/${page.component}.tsx`])" }}
{{ "@vite(['inertia/app/app.tsx', `inertia/pages/${page.component}.tsx`])" }}
</head>

<body>
Expand Down
6 changes: 3 additions & 3 deletions stubs/react/ssr.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/ssr.tsx') })
exports({ to: app.makePath('inertia/app/ssr.tsx') })
}}}
import ReactDOMServer from 'react-dom/server'
import { createInertiaApp } from '@inertiajs/react'
Expand All @@ -9,8 +9,8 @@ export default function render(page: any) {
page,
render: ReactDOMServer.renderToString,
resolve: (name) => {
const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
{{ 'return pages[`./pages/${name}.tsx`]' }}
const pages = import.meta.glob('../pages/**/*.tsx', { eager: true })
{{ 'return pages[`../pages/${name}.tsx`]' }}
},
setup: ({ App, props }) => <App {...props} />,
})
Expand Down
16 changes: 3 additions & 13 deletions stubs/react/tsconfig.json.stub
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
{{{
exports({ to: app.makePath('resources/tsconfig.json') })
exports({ to: app.makePath('inertia/tsconfig.json') })
}}}
{
"extends": "@adonisjs/tsconfig/tsconfig.client.json",
"compilerOptions": {
"target": "ESNext",
"jsx": "react-jsx",
"lib": ["DOM", "ESNext", "DOM.Iterable", "ES2020"],
"useDefineForClassFields": true,
"baseUrl": ".",
"module": "ESNext",
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"paths": {
"@/*": ["./*"],
"~/*": ["../*"],
},
"resolveJsonModule": true,
"types": ["vite/client"],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true,
},
"include": ["./**/*.ts", "./**/*.tsx"],
}
8 changes: 4 additions & 4 deletions stubs/solid/app.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{{
exports({ to: app.makePath('resources/app.tsx') })
exports({ to: app.makePath('inertia/app/app.tsx') })
}}}
import './css/app.css'
import '../css/app.css'

{{#if ssr}}
import { hydrate } from 'solid-js/web';
Expand All @@ -20,8 +20,8 @@ createInertiaApp({

resolve: (name) => {
return resolvePageComponent(
{{ '`./pages/${name}.tsx`' }},
import.meta.glob('./pages/**/*.tsx'),
{{ '`../pages/${name}.tsx`' }},
import.meta.glob('../pages/**/*.tsx'),
)
},

Expand Down
14 changes: 14 additions & 0 deletions stubs/solid/errors/not_found.tsx.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{{
exports({ to: app.makePath('inertia/pages/errors/not_found.tsx') })
}}}
export default function NotFound() {
return (
<>
<div class="container">
<div class="title">Page not found</div>

<span>This page does not exist.</span>
</div>
</>
)
}
14 changes: 14 additions & 0 deletions stubs/solid/errors/server_error.tsx.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{{
exports({ to: app.makePath('inertia/pages/errors/server_error.tsx') })
}}}
export default function ServerError(props: { error: any }) {
return (
<>
<div class="container">
<div class="title">Server Error</div>

<span>{props.error.message}</span>
</div>
</>
)
}
5 changes: 1 addition & 4 deletions stubs/solid/home.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{{{
exports({ to: app.makePath('resources/pages/home.tsx') })
exports({ to: app.makePath('inertia/pages/home.tsx') })
}}}
import { Title } from '@solidjs/meta'

export default function Home(props: { version: number }) {
return (
<>
<Title>Homepage</Title>

<div class="container">
<div class="title">AdonisJS {props.version} x Inertia x Solid.js</div>

Expand Down
4 changes: 2 additions & 2 deletions stubs/solid/root.edge.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/views/root.edge') })
exports({ to: app.makePath('resources/views/inertia_layout.edge') })
}}}
<!DOCTYPE html>
<html>
Expand All @@ -11,7 +11,7 @@
<title inertia>AdonisJS x Inertia x SolidJS</title>

@inertiaHead()
{{ "@vite(['resources/app.tsx', `resources/pages/${page.component}.tsx`])" }}
{{ "@vite(['inertia/app/app.tsx', `inertia/pages/${page.component}.tsx`])" }}
</head>

<body>
Expand Down
6 changes: 3 additions & 3 deletions stubs/solid/ssr.tsx.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{
exports({ to: app.makePath('resources/ssr.tsx') })
exports({ to: app.makePath('inertia/app/ssr.tsx') })
}}}

import { hydrate } from 'solid-js/web'
Expand All @@ -9,8 +9,8 @@ export default function render(page: any) {
return createInertiaApp({
page,
resolve: (name) => {
const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
{{ 'return pages[`./pages/${name}.tsx`]' }}
const pages = import.meta.glob('../pages/**/*.tsx', { eager: true })
{{ 'return pages[`../pages/${name}.tsx`]' }}
},
setup({ el, App, props }) {
hydrate(() => <App {...props} />, el)
Expand Down
Loading

0 comments on commit 250958b

Please sign in to comment.