@adonisjs/inertia 1.0.0-3 → 1.0.0-30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +3 -3
  2. package/build/app.css.stub +39 -0
  3. package/build/{chunk-GDULL3NT.js → chunk-QKSM72AR.js} +84 -6
  4. package/build/{stubs/config.stub → config.stub} +10 -2
  5. package/build/index.d.ts +6 -4
  6. package/build/index.js +212 -36
  7. package/build/providers/inertia_provider.d.ts +15 -1
  8. package/build/providers/inertia_provider.js +29 -16
  9. package/build/react/app.tsx.stub +38 -0
  10. package/build/react/errors/not_found.tsx.stub +14 -0
  11. package/build/react/errors/server_error.tsx.stub +14 -0
  12. package/build/react/home.tsx.stub +21 -0
  13. package/build/react/root.edge.stub +22 -0
  14. package/build/react/ssr.tsx.stub +17 -0
  15. package/build/react/tsconfig.json.stub +15 -0
  16. package/build/solid/app.tsx.stub +38 -0
  17. package/build/solid/errors/not_found.tsx.stub +14 -0
  18. package/build/solid/errors/server_error.tsx.stub +14 -0
  19. package/build/solid/home.tsx.stub +18 -0
  20. package/build/solid/root.edge.stub +21 -0
  21. package/build/solid/ssr.tsx.stub +19 -0
  22. package/build/solid/tsconfig.json.stub +16 -0
  23. package/build/src/helpers.d.ts +12 -0
  24. package/build/src/helpers.js +14 -0
  25. package/build/src/inertia_middleware.d.ts +6 -43
  26. package/build/src/inertia_middleware.js +1 -1
  27. package/build/src/plugins/edge/plugin.d.ts +8 -0
  28. package/build/src/plugins/{api_client.d.ts → japa/api_client.d.ts} +3 -1
  29. package/build/src/plugins/{api_client.js → japa/api_client.js} +1 -1
  30. package/build/src/plugins/vite.d.ts +26 -0
  31. package/build/src/plugins/vite.js +36 -0
  32. package/build/src/types.d.ts +5 -69
  33. package/build/svelte/app.ts.stub +33 -0
  34. package/build/svelte/errors/not_found.svelte.stub +10 -0
  35. package/build/svelte/errors/server_error.svelte.stub +14 -0
  36. package/build/svelte/home.svelte.stub +19 -0
  37. package/build/svelte/root.edge.stub +21 -0
  38. package/build/svelte/ssr.ts.stub +15 -0
  39. package/build/svelte/tsconfig.json.stub +14 -0
  40. package/build/types-fb05P61I.d.ts +203 -0
  41. package/build/vue/app.ts.stub +41 -0
  42. package/build/vue/errors/not_found.vue.stub +10 -0
  43. package/build/vue/errors/server_error.vue.stub +14 -0
  44. package/build/vue/home.vue.stub +21 -0
  45. package/build/vue/root.edge.stub +21 -0
  46. package/build/vue/ssr.ts.stub +22 -0
  47. package/build/vue/tsconfig.json.stub +16 -0
  48. package/package.json +65 -53
  49. /package/build/{plugin-QOPSYJCV.js → src/plugins/edge/plugin.js} +0 -0
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  Official [Inertia.js](https://inertiajs.com/) adapter for AdonisJS.
9
9
 
10
10
  ## Official Documentation
11
- The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/inertia/introduction).
11
+ The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/inertia/).
12
12
 
13
13
  ## Contributing
14
14
  One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
@@ -21,8 +21,8 @@ In order to ensure that the AdonisJS community is welcoming to all, please revie
21
21
  ## License
22
22
  AdonisJS Lucid is open-sourced software licensed under the [MIT license](LICENSE.md).
23
23
 
24
- [gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/inertia/test.yml?style=for-the-badge
25
- [gh-workflow-url]: https://github.com/adonisjs/inertia/actions/workflows/test.yml "Github action"
24
+ [gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/inertia/checks.yml?style=for-the-badge
25
+ [gh-workflow-url]: https://github.com/adonisjs/inertia/actions/workflows/checks.yml "Github action"
26
26
 
27
27
  [npm-image]: https://img.shields.io/npm/v/@adonisjs/inertia/latest.svg?style=for-the-badge&logo=npm
28
28
  [npm-url]: https://www.npmjs.com/package/@adonisjs/inertia/v/latest "npm"
@@ -0,0 +1,39 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/css/app.css') })
3
+ }}}
4
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap');
5
+
6
+ * {
7
+ margin: 0;
8
+ padding: 0;
9
+ }
10
+
11
+ html,
12
+ body,
13
+ #app {
14
+ background-color: #F7F8FA;
15
+ font-family: 'Poppins', sans-serif;
16
+ color: #46444c;
17
+ height: 100%;
18
+ width: 100%;
19
+ }
20
+
21
+ .title {
22
+ font-size: 42px;
23
+ font-weight: 500;
24
+ color: #5a45ff;
25
+ }
26
+
27
+ .container {
28
+ display: flex;
29
+ justify-content: center;
30
+ align-items: center;
31
+ flex-direction: column;
32
+ height: 100%;
33
+ width: 100%;
34
+ }
35
+
36
+ a {
37
+ text-decoration: underline;
38
+ color: #5a45ff;
39
+ }
@@ -1,10 +1,42 @@
1
+ // src/server_renderer.ts
2
+ import { pathToFileURL } from "node:url";
3
+ var ServerRenderer = class {
4
+ constructor(config, vite) {
5
+ this.config = config;
6
+ this.vite = vite;
7
+ }
8
+ /**
9
+ * Render the page on the server
10
+ *
11
+ * On development, we use the Vite Runtime API
12
+ * On production, we just import and use the SSR bundle generated by Vite
13
+ */
14
+ async render(pageObject) {
15
+ let render;
16
+ const devServer = this.vite?.getDevServer();
17
+ if (devServer) {
18
+ const runtime = await this.vite.createRuntime();
19
+ render = await runtime.executeEntrypoint(this.config.ssr.entrypoint);
20
+ } else {
21
+ render = await import(pathToFileURL(this.config.ssr.bundle).href);
22
+ }
23
+ const result = await render.default(pageObject);
24
+ return { head: result.head, body: result.body };
25
+ }
26
+ };
27
+
1
28
  // src/inertia.ts
2
29
  var kLazySymbol = Symbol("lazy");
3
30
  var Inertia = class {
4
- constructor(ctx, config) {
31
+ constructor(ctx, config, vite) {
5
32
  this.ctx = ctx;
6
33
  this.config = config;
34
+ this.vite = vite;
35
+ this.#sharedData = config.sharedData;
36
+ this.#serverRenderer = new ServerRenderer(config, vite);
7
37
  }
38
+ #sharedData = {};
39
+ #serverRenderer;
8
40
  /**
9
41
  * Check if a value is a lazy prop
10
42
  */
@@ -54,10 +86,52 @@ var Inertia = class {
54
86
  return {
55
87
  component,
56
88
  version: this.config.versionCache.getVersion(),
57
- props: await this.#resolvePageProps(component, { ...this.config.sharedData, ...pageProps }),
89
+ props: await this.#resolvePageProps(component, { ...this.#sharedData, ...pageProps }),
58
90
  url: this.ctx.request.url(true)
59
91
  };
60
92
  }
93
+ /**
94
+ * If the page should be rendered on the server or not
95
+ *
96
+ * The ssr.pages config can be a list of pages or a function that returns a boolean
97
+ */
98
+ async #shouldRenderOnServer(component) {
99
+ const isSsrEnabled = this.config.ssr.enabled;
100
+ if (!isSsrEnabled)
101
+ return false;
102
+ let isSsrEnabledForPage = false;
103
+ if (typeof this.config.ssr.pages === "function") {
104
+ isSsrEnabledForPage = await this.config.ssr.pages(this.ctx, component);
105
+ } else if (this.config.ssr.pages) {
106
+ isSsrEnabledForPage = this.config.ssr.pages?.includes(component);
107
+ } else {
108
+ isSsrEnabledForPage = true;
109
+ }
110
+ return isSsrEnabledForPage;
111
+ }
112
+ /**
113
+ * Resolve the root view
114
+ */
115
+ #resolveRootView() {
116
+ return typeof this.config.rootView === "function" ? this.config.rootView(this.ctx) : this.config.rootView;
117
+ }
118
+ /**
119
+ * Render the page on the server
120
+ */
121
+ async #renderOnServer(pageObject, viewProps) {
122
+ const { head, body } = await this.#serverRenderer.render(pageObject);
123
+ return this.ctx.view.render(this.#resolveRootView(), {
124
+ ...viewProps,
125
+ page: { ssrHead: head, ssrBody: body, ...pageObject }
126
+ });
127
+ }
128
+ /**
129
+ * Share data for the current request.
130
+ * This data will override any shared data defined in the config.
131
+ */
132
+ share(data) {
133
+ this.#sharedData = { ...this.#sharedData, ...data };
134
+ }
61
135
  /**
62
136
  * Render a page using Inertia
63
137
  */
@@ -65,8 +139,12 @@ var Inertia = class {
65
139
  const pageObject = await this.#buildPageObject(component, pageProps);
66
140
  const isInertiaRequest = !!this.ctx.request.header("x-inertia");
67
141
  if (!isInertiaRequest) {
68
- return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
142
+ const shouldRenderOnServer = await this.#shouldRenderOnServer(component);
143
+ if (shouldRenderOnServer)
144
+ return this.#renderOnServer(pageObject, viewProps);
145
+ return this.ctx.view.render(this.#resolveRootView(), { ...viewProps, page: pageObject });
69
146
  }
147
+ this.ctx.response.header("x-inertia", "true");
70
148
  return pageObject;
71
149
  }
72
150
  /**
@@ -94,18 +172,18 @@ var Inertia = class {
94
172
 
95
173
  // src/inertia_middleware.ts
96
174
  var InertiaMiddleware = class {
97
- constructor(config) {
175
+ constructor(config, vite) {
98
176
  this.config = config;
177
+ this.vite = vite;
99
178
  }
100
179
  async handle(ctx, next) {
101
180
  const { response, request } = ctx;
102
- ctx.inertia = new Inertia(ctx, this.config);
181
+ ctx.inertia = new Inertia(ctx, this.config, this.vite);
103
182
  await next();
104
183
  const isInertiaRequest = !!request.header("x-inertia");
105
184
  if (!isInertiaRequest)
106
185
  return;
107
186
  response.header("Vary", "Accept");
108
- response.header("X-Inertia", "true");
109
187
  const method = request.method();
110
188
  if (response.getStatus() === 302 && ["PUT", "PATCH", "DELETE"].includes(method)) {
111
189
  response.status(303);
@@ -7,12 +7,20 @@ export default defineConfig({
7
7
  /**
8
8
  * Path to the Edge view that will be used as the root view for Inertia responses
9
9
  */
10
- rootView: 'home',
10
+ rootView: 'inertia_layout',
11
11
 
12
12
  /**
13
13
  * Data that should be shared with all rendered pages
14
14
  */
15
15
  sharedData: {
16
- errors: (ctx) => ctx.session.flashMessages.get('errors'),
16
+ errors: (ctx) => ctx.session?.flashMessages.get('errors'),
17
17
  },
18
+
19
+ /**
20
+ * Options for the server-side rendering
21
+ */
22
+ ssr: {
23
+ enabled: {{ ssr }},
24
+ entrypoint: '{{ ssrEntrypoint }}'
25
+ }
18
26
  })
package/build/index.d.ts CHANGED
@@ -1,18 +1,20 @@
1
1
  import Configure from '@adonisjs/core/commands/configure';
2
2
  import { ConfigProvider } from '@adonisjs/core/types';
3
- import { InertiaConfig, ResolvedConfig } from './src/types.js';
3
+ import { S as SharedData, I as InertiaConfig, R as ResolvedConfig } from './types-fb05P61I.js';
4
4
  import '@adonisjs/core/http';
5
+ import '@tuyau/utils/types';
6
+ import '@adonisjs/vite';
5
7
 
6
8
  /**
7
9
  * Configures the package
8
10
  */
9
11
  declare function configure(command: Configure): Promise<void>;
10
12
 
11
- declare const stubsRoot: string;
12
-
13
13
  /**
14
14
  * Define the Inertia configuration
15
15
  */
16
- declare function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConfig>;
16
+ declare function defineConfig<T extends SharedData>(config: InertiaConfig<T>): ConfigProvider<ResolvedConfig<T>>;
17
+
18
+ declare const stubsRoot: string;
17
19
 
18
20
  export { configure, defineConfig, stubsRoot };
package/build/index.js CHANGED
@@ -1,71 +1,186 @@
1
1
  // configure.ts
2
- var ADAPTERS = ["Vue 3", "React", "Svelte"];
2
+ import string from "@poppinss/utils/string";
3
+
4
+ // stubs/main.ts
5
+ import { getDirname } from "@poppinss/utils";
6
+ var stubsRoot = getDirname(import.meta.url);
7
+
8
+ // configure.ts
9
+ var ADAPTERS = ["vue", "react", "svelte", "solid"];
3
10
  var ADAPTERS_INFO = {
4
- "Vue 3": {
11
+ vue: {
12
+ stubFolder: "vue",
13
+ appExtension: "ts",
14
+ componentsExtension: "vue",
5
15
  dependencies: [
6
16
  { name: "@inertiajs/vue3", isDevDependency: false },
7
17
  { name: "vue", isDevDependency: false },
8
18
  { name: "@vitejs/plugin-vue", isDevDependency: true }
9
- ]
19
+ ],
20
+ ssrDependencies: [{ name: "@vue/server-renderer", isDevDependency: false }],
21
+ viteRegister: {
22
+ pluginCall: "vue()",
23
+ importDeclarations: [{ isNamed: false, module: "@vitejs/plugin-vue", identifier: "vue" }]
24
+ },
25
+ ssrEntrypoint: "inertia/app/ssr.ts"
10
26
  },
11
- "React": {
27
+ react: {
28
+ stubFolder: "react",
29
+ appExtension: "tsx",
30
+ componentsExtension: "tsx",
12
31
  dependencies: [
13
- { name: "@inertiajs/inertia-react", isDevDependency: false },
32
+ { name: "@inertiajs/react", isDevDependency: false },
14
33
  { name: "react", isDevDependency: false },
15
34
  { name: "react-dom", isDevDependency: false },
16
35
  { name: "@vitejs/plugin-react", isDevDependency: true },
17
36
  { name: "@types/react", isDevDependency: true },
18
37
  { name: "@types/react-dom", isDevDependency: true }
19
- ]
38
+ ],
39
+ viteRegister: {
40
+ pluginCall: "react()",
41
+ importDeclarations: [{ isNamed: false, module: "@vitejs/plugin-react", identifier: "react" }]
42
+ },
43
+ ssrEntrypoint: "inertia/app/ssr.tsx"
20
44
  },
21
- "Svelte": {
45
+ svelte: {
46
+ stubFolder: "svelte",
47
+ appExtension: "ts",
48
+ componentsExtension: "svelte",
22
49
  dependencies: [
23
- { name: "@inertiajs/inertia-svelte", isDevDependency: false },
50
+ { name: "@inertiajs/svelte", isDevDependency: false },
24
51
  { name: "svelte", isDevDependency: false },
25
52
  { name: "@sveltejs/vite-plugin-svelte", isDevDependency: true }
26
- ]
53
+ ],
54
+ viteRegister: {
55
+ pluginCall: "svelte()",
56
+ ssrPluginCall: "svelte({ compilerOptions: { hydratable: true } })",
57
+ importDeclarations: [
58
+ { isNamed: true, module: "@sveltejs/vite-plugin-svelte", identifier: "svelte" }
59
+ ]
60
+ },
61
+ ssrEntrypoint: "inertia/app/ssr.ts"
62
+ },
63
+ solid: {
64
+ stubFolder: "solid",
65
+ appExtension: "tsx",
66
+ componentsExtension: "tsx",
67
+ dependencies: [
68
+ { name: "solid-js", isDevDependency: false },
69
+ { name: "inertia-adapter-solid", isDevDependency: false },
70
+ { name: "vite-plugin-solid", isDevDependency: true },
71
+ { name: "@solidjs/meta", isDevDependency: false }
72
+ ],
73
+ viteRegister: {
74
+ pluginCall: "solid()",
75
+ ssrPluginCall: "solid({ ssr: true })",
76
+ importDeclarations: [{ isNamed: false, module: "vite-plugin-solid", identifier: "solid" }]
77
+ },
78
+ ssrEntrypoint: "inertia/app/ssr.tsx"
27
79
  }
28
80
  };
81
+ async function defineExampleRoute(command, codemods) {
82
+ const tsMorph = await codemods.getTsMorphProject();
83
+ const routesFile = tsMorph?.getSourceFile(command.app.makePath("./start/routes.ts"));
84
+ if (!routesFile) {
85
+ return command.logger.warning("Unable to find the routes file");
86
+ }
87
+ const action = command.logger.action("update start/routes.ts file");
88
+ try {
89
+ routesFile?.addStatements((writer) => {
90
+ writer.writeLine(`router.on('/').renderInertia('home', { version: 6 })`);
91
+ });
92
+ await tsMorph?.save();
93
+ action.succeeded();
94
+ } catch (error) {
95
+ codemods.emit("error", error);
96
+ action.failed(error.message);
97
+ }
98
+ }
29
99
  async function configure(command) {
30
- const adapter = await command.prompt.choice(
31
- "Select the Inertia adapter you want to use",
32
- ADAPTERS,
33
- { name: "adapter" }
34
- );
35
- const pkgToInstall = ADAPTERS_INFO[adapter].dependencies;
36
- const withSsr = await command.prompt.confirm("Do you want to enable server-side rendering?", {
37
- name: "ssr"
38
- });
39
- if (withSsr) {
40
- pkgToInstall.push(...ADAPTERS_INFO[adapter].ssrDependencies || []);
100
+ let adapter = command.parsedFlags.adapter;
101
+ let ssr = command.parsedFlags.ssr;
102
+ let shouldInstallPackages = command.parsedFlags.install;
103
+ let shouldSkipExampleRoute = command.parsedFlags["skip-example-route"];
104
+ if (adapter === void 0) {
105
+ adapter = await command.prompt.choice(
106
+ "Select the Inertia adapter you want to use",
107
+ ADAPTERS.map((adapterName) => string.capitalCase(adapterName)),
108
+ { name: "adapter", result: (value) => value.toLowerCase() }
109
+ );
110
+ }
111
+ if (ssr === void 0) {
112
+ ssr = await command.prompt.confirm("Do you want to use server-side rendering?", {
113
+ name: "ssr"
114
+ });
115
+ }
116
+ if (adapter in ADAPTERS_INFO === false) {
117
+ command.logger.error(
118
+ `The selected adapter "${adapter}" is invalid. Select one from: ${string.sentence(
119
+ Object.keys(ADAPTERS_INFO)
120
+ )}`
121
+ );
122
+ command.exitCode = 1;
123
+ return;
41
124
  }
125
+ const adapterInfo = ADAPTERS_INFO[adapter];
42
126
  const codemods = await command.createCodemods();
43
127
  await codemods.updateRcFile((rcFile) => {
44
128
  rcFile.addProvider("@adonisjs/inertia/inertia_provider");
45
129
  });
46
- codemods.registerMiddleware("router", [
130
+ await codemods.registerMiddleware("server", [
47
131
  { path: "@adonisjs/inertia/inertia_middleware", position: "after" }
48
132
  ]);
49
- await command.publishStub("config.stub");
50
- const shouldInstallPackages = await command.prompt.confirm(
51
- `Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(", ")}?`,
52
- { name: "install" }
133
+ const appExt = adapterInfo.appExtension;
134
+ const stubFolder = adapterInfo.stubFolder;
135
+ const compExt = adapterInfo.componentsExtension;
136
+ await codemods.makeUsingStub(stubsRoot, "config.stub", {
137
+ ssr,
138
+ ssrEntrypoint: adapterInfo.ssrEntrypoint
139
+ });
140
+ await codemods.makeUsingStub(stubsRoot, `app.css.stub`, {});
141
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/root.edge.stub`, {});
142
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/tsconfig.json.stub`, {});
143
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/app.${appExt}.stub`, { ssr });
144
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/home.${compExt}.stub`, {});
145
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/not_found.${compExt}.stub`, {});
146
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/server_error.${compExt}.stub`, {});
147
+ if (ssr) {
148
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/ssr.${appExt}.stub`, {});
149
+ }
150
+ const inertiaPluginCall = ssr ? `inertia({ ssr: { enabled: true, entrypoint: 'inertia/app/ssr.${appExt}' } })` : `inertia({ ssr: { enabled: false } })`;
151
+ await codemods.registerVitePlugin(inertiaPluginCall, [
152
+ { isNamed: false, module: "@adonisjs/inertia/client", identifier: "inertia" }
153
+ ]);
154
+ await codemods.registerVitePlugin(
155
+ ssr && adapterInfo.viteRegister.ssrPluginCall ? adapterInfo.viteRegister.ssrPluginCall : adapterInfo.viteRegister.pluginCall,
156
+ adapterInfo.viteRegister.importDeclarations
53
157
  );
158
+ const adonisjsPluginCall = `adonisjs({ entrypoints: ['inertia/app/app.${appExt}'], reload: ['resources/views/**/*.edge'] })`;
159
+ await codemods.registerVitePlugin(adonisjsPluginCall, [
160
+ { isNamed: false, module: "@adonisjs/vite/client", identifier: "adonisjs" }
161
+ ]);
162
+ if (shouldSkipExampleRoute !== true) {
163
+ await defineExampleRoute(command, codemods);
164
+ }
165
+ const pkgToInstall = adapterInfo.dependencies;
166
+ if (ssr && adapterInfo.ssrDependencies) {
167
+ pkgToInstall.push(...adapterInfo.ssrDependencies);
168
+ }
169
+ if (shouldInstallPackages === void 0) {
170
+ shouldInstallPackages = await command.prompt.confirm(
171
+ `Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(", ")}?`,
172
+ { name: "install" }
173
+ );
174
+ }
54
175
  if (shouldInstallPackages) {
55
- command.installPackages(pkgToInstall);
176
+ await codemods.installPackages(pkgToInstall);
56
177
  } else {
57
- command.listPackagesToInstall(pkgToInstall);
178
+ await codemods.listPackagesToInstall(pkgToInstall);
58
179
  }
59
- command.logger.success(
60
- "Inertia was configured successfully. Please note that you still need to update your vite config, setup your Edge root view and others things. Read the docs for more info."
61
- );
62
180
  }
63
181
 
64
- // stubs/main.ts
65
- import { getDirname } from "@poppinss/utils";
66
- var stubsRoot = getDirname(import.meta.url);
67
-
68
182
  // src/define_config.ts
183
+ import { slash } from "@poppinss/utils";
69
184
  import { configProvider } from "@adonisjs/core";
70
185
 
71
186
  // src/version_cache.ts
@@ -116,15 +231,76 @@ var VersionCache = class {
116
231
  }
117
232
  };
118
233
 
234
+ // src/files_detector.ts
235
+ import { locatePath } from "locate-path";
236
+ var FilesDetector = class {
237
+ constructor(app) {
238
+ this.app = app;
239
+ }
240
+ /**
241
+ * Try to locate the entrypoint file based
242
+ * on the conventional locations
243
+ */
244
+ async detectEntrypoint(defaultPath) {
245
+ const possiblesLocations = [
246
+ "./inertia/app/app.ts",
247
+ "./inertia/app/app.tsx",
248
+ "./resources/app.ts",
249
+ "./resources/app.tsx",
250
+ "./resources/app.jsx",
251
+ "./resources/app.js",
252
+ "./inertia/app/app.jsx"
253
+ ];
254
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
255
+ return this.app.makePath(path || defaultPath);
256
+ }
257
+ /**
258
+ * Try to locate the SSR entrypoint file based
259
+ * on the conventional locations
260
+ */
261
+ async detectSsrEntrypoint(defaultPath) {
262
+ const possiblesLocations = [
263
+ "./inertia/app/ssr.ts",
264
+ "./inertia/app/ssr.tsx",
265
+ "./resources/ssr.ts",
266
+ "./resources/ssr.tsx",
267
+ "./resources/ssr.jsx",
268
+ "./resources/ssr.js",
269
+ "./inertia/app/ssr.jsx"
270
+ ];
271
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
272
+ return this.app.makePath(path || defaultPath);
273
+ }
274
+ /**
275
+ * Try to locate the SSR bundle file based
276
+ * on the conventional locations
277
+ */
278
+ async detectSsrBundle(defaultPath) {
279
+ const possiblesLocations = ["./ssr/ssr.js", "./ssr/ssr.mjs"];
280
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
281
+ return this.app.makePath(path || defaultPath);
282
+ }
283
+ };
284
+
119
285
  // src/define_config.ts
120
286
  function defineConfig(config) {
121
287
  return configProvider.create(async (app) => {
288
+ const detector = new FilesDetector(app);
122
289
  const versionCache = new VersionCache(app.appRoot, config.assetsVersion);
123
290
  await versionCache.computeVersion();
124
291
  return {
125
- rootView: config.rootView ?? "root",
292
+ versionCache,
293
+ rootView: config.rootView ?? "inertia_layout",
126
294
  sharedData: config.sharedData || {},
127
- versionCache
295
+ entrypoint: slash(
296
+ config.entrypoint ?? await detector.detectEntrypoint("inertia/app/app.ts")
297
+ ),
298
+ ssr: {
299
+ enabled: config.ssr?.enabled ?? false,
300
+ pages: config.ssr?.pages,
301
+ entrypoint: config.ssr?.entrypoint ?? await detector.detectSsrEntrypoint("inertia/app/ssr.ts"),
302
+ bundle: config.ssr?.bundle ?? await detector.detectSsrBundle("ssr/ssr.js")
303
+ }
128
304
  };
129
305
  });
130
306
  }
@@ -1,5 +1,15 @@
1
+ import { Route } from '@adonisjs/core/http';
1
2
  import { ApplicationService } from '@adonisjs/core/types';
2
3
 
4
+ declare module '@adonisjs/core/http' {
5
+ interface BriskRoute {
6
+ /**
7
+ * Render an inertia page without defining an
8
+ * explicit route handler
9
+ */
10
+ renderInertia(component: string, props?: Record<string, any>, viewProps?: Record<string, any>): Route;
11
+ }
12
+ }
3
13
  /**
4
14
  * Inertia provider
5
15
  */
@@ -11,9 +21,13 @@ declare class InertiaProvider {
11
21
  */
12
22
  protected registerEdgePlugin(): Promise<void>;
13
23
  /**
14
- * Register Inertia bindings
24
+ * Register inertia middleware
15
25
  */
16
26
  register(): Promise<void>;
27
+ /**
28
+ * Register edge plugin and brisk route macro
29
+ */
30
+ boot(): Promise<void>;
17
31
  }
18
32
 
19
33
  export { InertiaProvider as default };
@@ -1,10 +1,11 @@
1
1
  import {
2
2
  InertiaMiddleware
3
- } from "../chunk-GDULL3NT.js";
3
+ } from "../chunk-QKSM72AR.js";
4
4
 
5
5
  // providers/inertia_provider.ts
6
6
  import { configProvider } from "@adonisjs/core";
7
7
  import { RuntimeException } from "@poppinss/utils";
8
+ import { BriskRoute } from "@adonisjs/core/http";
8
9
  var InertiaProvider = class {
9
10
  constructor(app) {
10
11
  this.app = app;
@@ -13,26 +14,38 @@ var InertiaProvider = class {
13
14
  * Registers edge plugin when edge is installed
14
15
  */
15
16
  async registerEdgePlugin() {
16
- try {
17
- const edgeExports = await import("edge.js");
18
- const { edgePluginInertia } = await import("../plugin-QOPSYJCV.js");
19
- edgeExports.default.use(edgePluginInertia());
20
- } catch {
21
- }
17
+ if (!this.app.usingEdgeJS)
18
+ return;
19
+ const edgeExports = await import("edge.js");
20
+ const { edgePluginInertia } = await import("../src/plugins/edge/plugin.js");
21
+ edgeExports.default.use(edgePluginInertia());
22
22
  }
23
23
  /**
24
- * Register Inertia bindings
24
+ * Register inertia middleware
25
25
  */
26
26
  async register() {
27
- const inertiaConfigProvider = this.app.config.get("inertia");
28
- const config = await configProvider.resolve(this.app, inertiaConfigProvider);
29
- if (!config) {
30
- throw new RuntimeException(
31
- 'Invalid "config/inertia.ts" file. Make sure you are using the "defineConfig" method'
32
- );
33
- }
34
- this.app.container.singleton(InertiaMiddleware, () => new InertiaMiddleware(config));
27
+ this.app.container.singleton(InertiaMiddleware, async () => {
28
+ const inertiaConfigProvider = this.app.config.get("inertia");
29
+ const config = await configProvider.resolve(this.app, inertiaConfigProvider);
30
+ const vite = await this.app.container.make("vite");
31
+ if (!config) {
32
+ throw new RuntimeException(
33
+ 'Invalid "config/inertia.ts" file. Make sure you are using the "defineConfig" method'
34
+ );
35
+ }
36
+ return new InertiaMiddleware(config, vite);
37
+ });
38
+ }
39
+ /**
40
+ * Register edge plugin and brisk route macro
41
+ */
42
+ async boot() {
35
43
  await this.registerEdgePlugin();
44
+ BriskRoute.macro("renderInertia", function(template, props, viewProps) {
45
+ return this.setHandler(({ inertia }) => {
46
+ return inertia.render(template, props, viewProps);
47
+ });
48
+ });
36
49
  }
37
50
  };
38
51
  export {
@@ -0,0 +1,38 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/app/app.tsx') })
3
+ }}}
4
+ /// <reference path="../../adonisrc.ts" />
5
+ /// <reference path="../../config/inertia.ts" />
6
+
7
+ import '../css/app.css';
8
+
9
+ {{#if ssr}}
10
+ import { hydrateRoot } from 'react-dom/client'
11
+ {{#else}}
12
+ import { createRoot } from 'react-dom/client';
13
+ {{/if}}
14
+ import { createInertiaApp } from '@inertiajs/react';
15
+ import { resolvePageComponent } from '@adonisjs/inertia/helpers'
16
+
17
+ const appName = import.meta.env.VITE_APP_NAME || 'AdonisJS'
18
+
19
+ createInertiaApp({
20
+ progress: { color: '#5468FF' },
21
+
22
+ title: (title) => {{ '`${title} - ${appName}`' }},
23
+
24
+ resolve: (name) => {
25
+ return resolvePageComponent(
26
+ {{ '`../pages/${name}.tsx`' }},
27
+ import.meta.glob('../pages/**/*.tsx'),
28
+ )
29
+ },
30
+
31
+ setup({ el, App, props }) {
32
+ {{#if ssr}}
33
+ hydrateRoot(el, <App {...props} />)
34
+ {{#else}}
35
+ createRoot(el).render(<App {...props} />);
36
+ {{/if}}
37
+ },
38
+ });