From 6316b2f0d9a0e19b34bcaf492eefd636c4c82f1a Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Fri, 8 Mar 2024 01:05:09 +0100 Subject: [PATCH 1/6] refactor: move stubs to `inertia` directory --- configure.ts | 12 +++--- src/define_config.ts | 6 ++- src/files_detector.ts | 14 +++--- stubs/app.css.stub | 2 +- stubs/config.stub | 2 +- stubs/react/app.tsx.stub | 8 ++-- stubs/react/home.tsx.stub | 2 +- stubs/react/root.edge.stub | 2 +- stubs/react/ssr.tsx.stub | 6 +-- stubs/react/tsconfig.json.stub | 16 ++----- stubs/solid/app.tsx.stub | 8 ++-- stubs/solid/home.tsx.stub | 5 +-- stubs/solid/root.edge.stub | 2 +- stubs/solid/ssr.tsx.stub | 6 +-- stubs/solid/tsconfig.json.stub | 20 +++------ stubs/svelte/app.ts.stub | 8 ++-- stubs/svelte/home.svelte.stub | 2 +- stubs/svelte/root.edge.stub | 2 +- stubs/svelte/ssr.ts.stub | 6 +-- stubs/svelte/tsconfig.json.stub | 18 ++------ stubs/vue/app.ts.stub | 8 ++-- stubs/vue/home.vue.stub | 2 +- stubs/vue/root.edge.stub | 2 +- stubs/vue/ssr.ts.stub | 6 +-- stubs/vue/tsconfig.json.stub | 20 +++------ tests/configure.spec.ts | 76 ++++++++++++++++----------------- tests/define_config.spec.ts | 4 +- 27 files changed, 110 insertions(+), 155 deletions(-) diff --git a/configure.ts b/configure.ts index 79b0b69..080a49a 100644 --- a/configure.ts +++ b/configure.ts @@ -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', @@ -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', @@ -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', @@ -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', }, } @@ -214,7 +214,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, [ @@ -234,7 +234,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' }, ]) diff --git a/src/define_config.ts b/src/define_config.ts index 45c885a..d938800 100644 --- a/src/define_config.ts +++ b/src/define_config.ts @@ -28,12 +28,14 @@ export function defineConfig(config: InertiaConfig): ConfigProvider ctx.session.flashMessages.get('errors'), + errors: (ctx) => ctx.session?.flashMessages.get('errors'), }, /** diff --git a/stubs/react/app.tsx.stub b/stubs/react/app.tsx.stub index 33ac58a..a00c5b0 100644 --- a/stubs/react/app.tsx.stub +++ b/stubs/react/app.tsx.stub @@ -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' @@ -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'), ) }, diff --git a/stubs/react/home.tsx.stub b/stubs/react/home.tsx.stub index 345270b..065d46e 100644 --- a/stubs/react/home.tsx.stub +++ b/stubs/react/home.tsx.stub @@ -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' diff --git a/stubs/react/root.edge.stub b/stubs/react/root.edge.stub index 69ad119..51f1f60 100644 --- a/stubs/react/root.edge.stub +++ b/stubs/react/root.edge.stub @@ -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`])" }} diff --git a/stubs/react/ssr.tsx.stub b/stubs/react/ssr.tsx.stub index d22392b..7adbe4a 100644 --- a/stubs/react/ssr.tsx.stub +++ b/stubs/react/ssr.tsx.stub @@ -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' @@ -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 }) => , }) diff --git a/stubs/react/tsconfig.json.stub b/stubs/react/tsconfig.json.stub index e6ecf13..872babd 100644 --- a/stubs/react/tsconfig.json.stub +++ b/stubs/react/tsconfig.json.stub @@ -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"], } diff --git a/stubs/solid/app.tsx.stub b/stubs/solid/app.tsx.stub index d4471ad..3854ace 100644 --- a/stubs/solid/app.tsx.stub +++ b/stubs/solid/app.tsx.stub @@ -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'; @@ -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'), ) }, diff --git a/stubs/solid/home.tsx.stub b/stubs/solid/home.tsx.stub index f118de8..3a72152 100644 --- a/stubs/solid/home.tsx.stub +++ b/stubs/solid/home.tsx.stub @@ -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 ( <> - Homepage -
AdonisJS {props.version} x Inertia x Solid.js
diff --git a/stubs/solid/root.edge.stub b/stubs/solid/root.edge.stub index 4287d3f..0929992 100644 --- a/stubs/solid/root.edge.stub +++ b/stubs/solid/root.edge.stub @@ -11,7 +11,7 @@ AdonisJS x Inertia x SolidJS @inertiaHead() - {{ "@vite(['resources/app.tsx', `resources/pages/${page.component}.tsx`])" }} + {{ "@vite(['inertia/app/app.tsx', `inertia/pages/${page.component}.tsx`])" }} diff --git a/stubs/solid/ssr.tsx.stub b/stubs/solid/ssr.tsx.stub index a4bdbed..5a6fc1b 100644 --- a/stubs/solid/ssr.tsx.stub +++ b/stubs/solid/ssr.tsx.stub @@ -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' @@ -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(() => , el) diff --git a/stubs/solid/tsconfig.json.stub b/stubs/solid/tsconfig.json.stub index 141bd8a..70b4969 100644 --- a/stubs/solid/tsconfig.json.stub +++ b/stubs/solid/tsconfig.json.stub @@ -1,26 +1,16 @@ {{{ - exports({ to: app.makePath('resources/tsconfig.json') }) + exports({ to: app.makePath('inertia/tsconfig.json') }) }}} { + "extends": "@adonisjs/tsconfig/tsconfig.client.json", "compilerOptions": { - "target": "ESNext", - "jsx": "preserve", - "jsxImportSource": "solid-js", - "lib": ["DOM", "ESNext", "DOM.Iterable", "ES2020"], - "useDefineForClassFields": true, "baseUrl": ".", + "jsx": "preserve", "module": "ESNext", - "moduleResolution": "Bundler", + "jsxImportSource": "solid-js", "paths": { - "@/*": ["./*"], - "~/*": ["../*"], + "~/*": ["./*"], }, - "resolveJsonModule": true, - "types": ["vite/client"], - "allowSyntheticDefaultImports": true, - "esModuleInterop": true, - "verbatimModuleSyntax": true, - "skipLibCheck": true, }, "include": ["./**/*.ts", "./**/*.tsx"], } diff --git a/stubs/svelte/app.ts.stub b/stubs/svelte/app.ts.stub index fa68765..4d29ee5 100644 --- a/stubs/svelte/app.ts.stub +++ b/stubs/svelte/app.ts.stub @@ -1,7 +1,7 @@ {{{ - exports({ to: app.makePath('resources/app.ts') }) + exports({ to: app.makePath('inertia/app/app.ts') }) }}} -import './css/app.css'; +import '../css/app.css'; import { createInertiaApp } from '@inertiajs/svelte' import { resolvePageComponent } from '@adonisjs/inertia/helpers' @@ -15,8 +15,8 @@ createInertiaApp({ resolve: (name) => { return resolvePageComponent( - {{ '`./pages/${name}.svelte`' }}, - import.meta.glob('./pages/**/*.svelte'), + {{ '`../pages/${name}.svelte`' }}, + import.meta.glob('../pages/**/*.svelte'), ) }, diff --git a/stubs/svelte/home.svelte.stub b/stubs/svelte/home.svelte.stub index 8891270..ba1ef6f 100644 --- a/stubs/svelte/home.svelte.stub +++ b/stubs/svelte/home.svelte.stub @@ -1,5 +1,5 @@ {{{ - exports({ to: app.makePath('resources/pages/home.svelte') }) + exports({ to: app.makePath('inertia/pages/home.svelte') }) }}} + +
+
+
Server Error
+ + {error.message} +
+
diff --git a/stubs/vue/errors/not_found.vue.stub b/stubs/vue/errors/not_found.vue.stub new file mode 100644 index 0000000..098f68c --- /dev/null +++ b/stubs/vue/errors/not_found.vue.stub @@ -0,0 +1,10 @@ +{{{ + exports({ to: app.makePath('inertia/pages/errors/not_found.vue') }) +}}} + diff --git a/stubs/vue/errors/server_error.vue.stub b/stubs/vue/errors/server_error.vue.stub new file mode 100644 index 0000000..d4e0e18 --- /dev/null +++ b/stubs/vue/errors/server_error.vue.stub @@ -0,0 +1,14 @@ +{{{ + exports({ to: app.makePath('inertia/pages/errors/server_error.vue') }) +}}} + + + diff --git a/tests/configure.spec.ts b/tests/configure.spec.ts index 79b57b4..4b1c9a9 100644 --- a/tests/configure.spec.ts +++ b/tests/configure.spec.ts @@ -113,6 +113,8 @@ test.group('Frameworks', (group) => { await assert.fileExists('resources/views/root.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.vue') + await assert.fileExists('inertia/pages/errors/not_found.vue') + await assert.fileExists('inertia/pages/errors/server_error.vue') await assert.fileContains('inertia/app/app.ts', 'createApp') const viteConfig = await fs.contents('vite.config.ts') @@ -140,6 +142,8 @@ test.group('Frameworks', (group) => { await assert.fileExists('resources/views/root.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.tsx') + await assert.fileExists('inertia/pages/errors/not_found.tsx') + await assert.fileExists('inertia/pages/errors/server_error.tsx') await assert.fileContains('inertia/app/app.tsx', 'createRoot') const viteConfig = await fs.contents('vite.config.ts') @@ -167,6 +171,8 @@ test.group('Frameworks', (group) => { await assert.fileExists('resources/views/root.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.tsx') + await assert.fileExists('inertia/pages/errors/not_found.tsx') + await assert.fileExists('inertia/pages/errors/server_error.tsx') await assert.fileNotContains('inertia/app/app.tsx', 'hydrateRoot') const viteConfig = await fs.contents('vite.config.ts') @@ -194,6 +200,8 @@ test.group('Frameworks', (group) => { await assert.fileExists('resources/views/root.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.svelte') + await assert.fileExists('inertia/pages/errors/not_found.svelte') + await assert.fileExists('inertia/pages/errors/server_error.svelte') await assert.fileNotContains('inertia/app/app.ts', 'hydrate') const viteConfig = await fs.contents('vite.config.ts') From 66b90da91e3747646c6ceef9b5ebef6d885487da Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Fri, 8 Mar 2024 01:37:21 +0100 Subject: [PATCH 4/6] fix: entrypoint vue --- stubs/vue/root.edge.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/vue/root.edge.stub b/stubs/vue/root.edge.stub index 04442f3..9780345 100644 --- a/stubs/vue/root.edge.stub +++ b/stubs/vue/root.edge.stub @@ -10,7 +10,7 @@ AdonisJS x Inertia x VueJS - {{ "@vite(['inertia/app.ts', `inertia/pages/${page.component}.vue`])" }} + {{ "@vite(['inertia/app/app.ts', `inertia/pages/${page.component}.vue`])" }} @inertiaHead() From 291081a83871620fe603566a6a289621fca9d947 Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Fri, 8 Mar 2024 12:09:12 +0100 Subject: [PATCH 5/6] refactor: move to inertia_layout.edge --- src/define_config.ts | 2 +- src/types.ts | 2 +- stubs/config.stub | 2 +- stubs/react/root.edge.stub | 2 +- stubs/solid/root.edge.stub | 2 +- stubs/svelte/root.edge.stub | 2 +- stubs/vue/root.edge.stub | 2 +- tests/configure.spec.ts | 16 ++++++++-------- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/define_config.ts b/src/define_config.ts index d938800..ef67b67 100644 --- a/src/define_config.ts +++ b/src/define_config.ts @@ -26,7 +26,7 @@ export function defineConfig(config: InertiaConfig): ConfigProvider diff --git a/stubs/solid/root.edge.stub b/stubs/solid/root.edge.stub index 0929992..a1531ba 100644 --- a/stubs/solid/root.edge.stub +++ b/stubs/solid/root.edge.stub @@ -1,5 +1,5 @@ {{{ - exports({ to: app.makePath('resources/views/root.edge') }) + exports({ to: app.makePath('resources/views/inertia_layout.edge') }) }}} diff --git a/stubs/svelte/root.edge.stub b/stubs/svelte/root.edge.stub index 2cf9580..ad381d5 100644 --- a/stubs/svelte/root.edge.stub +++ b/stubs/svelte/root.edge.stub @@ -1,5 +1,5 @@ {{{ - exports({ to: app.makePath('resources/views/root.edge') }) + exports({ to: app.makePath('resources/views/inertia_layout.edge') }) }}} diff --git a/stubs/vue/root.edge.stub b/stubs/vue/root.edge.stub index 9780345..bf8613b 100644 --- a/stubs/vue/root.edge.stub +++ b/stubs/vue/root.edge.stub @@ -1,5 +1,5 @@ {{{ - exports({ to: app.makePath('resources/views/root.edge') }) + exports({ to: app.makePath('resources/views/inertia_layout.edge') }) }}} diff --git a/tests/configure.spec.ts b/tests/configure.spec.ts index 4b1c9a9..539fb1a 100644 --- a/tests/configure.spec.ts +++ b/tests/configure.spec.ts @@ -110,7 +110,7 @@ test.group('Frameworks', (group) => { await command.exec() await assert.fileExists('inertia/app/app.ts') - await assert.fileExists('resources/views/root.edge') + await assert.fileExists('resources/views/inertia_layout.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.vue') await assert.fileExists('inertia/pages/errors/not_found.vue') @@ -139,7 +139,7 @@ test.group('Frameworks', (group) => { await command.exec() await assert.fileExists('inertia/app/app.tsx') - await assert.fileExists('resources/views/root.edge') + await assert.fileExists('resources/views/inertia_layout.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.tsx') await assert.fileExists('inertia/pages/errors/not_found.tsx') @@ -168,7 +168,7 @@ test.group('Frameworks', (group) => { await command.exec() await assert.fileExists('inertia/app/app.tsx') - await assert.fileExists('resources/views/root.edge') + await assert.fileExists('resources/views/inertia_layout.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.tsx') await assert.fileExists('inertia/pages/errors/not_found.tsx') @@ -197,7 +197,7 @@ test.group('Frameworks', (group) => { await command.exec() await assert.fileExists('inertia/app/app.ts') - await assert.fileExists('resources/views/root.edge') + await assert.fileExists('resources/views/inertia_layout.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.svelte') await assert.fileExists('inertia/pages/errors/not_found.svelte') @@ -243,7 +243,7 @@ test.group('Frameworks | SSR', (group) => { /** * 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 @@ -289,7 +289,7 @@ test.group('Frameworks | SSR', (group) => { /** * 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 @@ -335,7 +335,7 @@ test.group('Frameworks | SSR', (group) => { /** * 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 @@ -366,7 +366,7 @@ test.group('Frameworks | SSR', (group) => { await command.exec() await assert.fileExists('inertia/app/app.ts') - await assert.fileExists('resources/views/root.edge') + await assert.fileExists('resources/views/inertia_layout.edge') await assert.fileExists('inertia/tsconfig.json') await assert.fileExists('inertia/pages/home.svelte') await assert.fileContains('inertia/app/app.ts', 'hydrate') From 21c413bb621f55142aed6292cb8c9a6df1148cfd Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Fri, 8 Mar 2024 12:11:33 +0100 Subject: [PATCH 6/6] test: fix failing test --- tests/inertia.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/inertia.spec.ts b/tests/inertia.spec.ts index a4803b8..1890796 100644 --- a/tests/inertia.spec.ts +++ b/tests/inertia.spec.ts @@ -55,7 +55,7 @@ test.group('Inertia', () => { const inertia = await new InertiaFactory().create() const result: any = await inertia.render('foo', { foo: 'bar' }) - assert.deepEqual(result.view, 'root') + assert.deepEqual(result.view, 'inertia_layout') assert.deepEqual(result.props.page, { component: 'foo', version: '1',