@adonisjs/inertia 1.0.0-2 → 1.0.0-22

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/build/app.css.stub +39 -0
  2. package/build/chunk-PJEYAPJB.js +254 -0
  3. package/build/{stubs/config.stub → config.stub} +10 -2
  4. package/build/index.d.ts +3 -2
  5. package/build/index.js +214 -36
  6. package/build/providers/inertia_provider.d.ts +14 -1
  7. package/build/providers/inertia_provider.js +29 -16
  8. package/build/react/app.tsx.stub +35 -0
  9. package/build/react/errors/not_found.tsx.stub +14 -0
  10. package/build/react/errors/server_error.tsx.stub +14 -0
  11. package/build/react/home.tsx.stub +21 -0
  12. package/build/react/root.edge.stub +22 -0
  13. package/build/react/ssr.tsx.stub +17 -0
  14. package/build/react/tsconfig.json.stub +15 -0
  15. package/build/solid/app.tsx.stub +35 -0
  16. package/build/solid/errors/not_found.tsx.stub +14 -0
  17. package/build/solid/errors/server_error.tsx.stub +14 -0
  18. package/build/solid/home.tsx.stub +18 -0
  19. package/build/solid/root.edge.stub +21 -0
  20. package/build/solid/ssr.tsx.stub +19 -0
  21. package/build/solid/tsconfig.json.stub +16 -0
  22. package/build/src/helpers.d.ts +12 -0
  23. package/build/src/helpers.js +14 -0
  24. package/build/src/inertia_middleware.d.ts +13 -9
  25. package/build/src/inertia_middleware.js +1 -1
  26. package/build/src/plugins/edge/plugin.js +85 -0
  27. package/build/src/plugins/{api_client.d.ts → japa/api_client.d.ts} +2 -1
  28. package/build/src/plugins/{api_client.js → japa/api_client.js} +1 -1
  29. package/build/src/plugins/vite.d.ts +26 -0
  30. package/build/src/plugins/vite.js +36 -0
  31. package/build/src/types.d.ts +61 -2
  32. package/build/svelte/app.ts.stub +30 -0
  33. package/build/svelte/errors/not_found.svelte.stub +10 -0
  34. package/build/svelte/errors/server_error.svelte.stub +14 -0
  35. package/build/svelte/home.svelte.stub +19 -0
  36. package/build/svelte/root.edge.stub +21 -0
  37. package/build/svelte/ssr.ts.stub +15 -0
  38. package/build/svelte/tsconfig.json.stub +14 -0
  39. package/build/vue/app.ts.stub +38 -0
  40. package/build/vue/errors/not_found.vue.stub +10 -0
  41. package/build/vue/errors/server_error.vue.stub +14 -0
  42. package/build/vue/home.vue.stub +21 -0
  43. package/build/vue/root.edge.stub +21 -0
  44. package/build/vue/ssr.ts.stub +22 -0
  45. package/build/vue/tsconfig.json.stub +16 -0
  46. package/package.json +52 -43
  47. package/build/chunk-GDULL3NT.js +0 -123
  48. package/build/src/plugins/edge.js +0 -41
  49. /package/build/src/plugins/{edge.d.ts → edge/plugin.d.ts} +0 -0
@@ -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
+ }
@@ -0,0 +1,254 @@
1
+ // src/server_renderer.ts
2
+ var styleFileRE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\?)/;
3
+ var ServerRenderer = class {
4
+ constructor(config, vite) {
5
+ this.config = config;
6
+ this.vite = vite;
7
+ }
8
+ /**
9
+ * If the module is a style module
10
+ */
11
+ #isStyle(mod) {
12
+ if (styleFileRE.test(mod.url) || mod.id && /\?vue&type=style/.test(mod.id)) {
13
+ return true;
14
+ }
15
+ return false;
16
+ }
17
+ /**
18
+ * Collect CSS files from the module graph recursively
19
+ */
20
+ #collectCss(mod, styleUrls, visitedModules, importer) {
21
+ if (!mod.url)
22
+ return;
23
+ if (visitedModules.has(mod.url))
24
+ return;
25
+ visitedModules.add(mod.url);
26
+ if (this.#isStyle(mod) && (!importer || !this.#isStyle(importer))) {
27
+ if (mod.url.startsWith("/")) {
28
+ styleUrls.add(mod.url);
29
+ } else if (mod.url.startsWith("\0")) {
30
+ styleUrls.add(`/@id/__x00__${mod.url.substring(1)}`);
31
+ } else {
32
+ styleUrls.add(`/@id/${mod.url}`);
33
+ }
34
+ }
35
+ mod.importedModules.forEach((dep) => this.#collectCss(dep, styleUrls, visitedModules, mod));
36
+ }
37
+ /**
38
+ * Generate the preload tag for a CSS file
39
+ */
40
+ #getPreloadTag(href) {
41
+ return `<link rel="stylesheet" href="${href}" />`;
42
+ }
43
+ /**
44
+ * Find a page module from the entrypoint module
45
+ *
46
+ * The implementation is dumb, we are just looking for the first module
47
+ * imported by the entrypoint module that matches the regex
48
+ */
49
+ #findPageModule(entryMod, pageObject) {
50
+ const pattern = `${pageObject.component.replace(/\//g, "\\/")}.(tsx|vue|svelte|jsx|ts|js)$`;
51
+ const regex = new RegExp(pattern);
52
+ return [...entryMod?.ssrImportedModules || []].find((dep) => regex.test(dep.url));
53
+ }
54
+ /**
55
+ * Render the page on the server
56
+ *
57
+ * On development, we use the Vite Runtime API
58
+ * On production, we just import and use the SSR bundle generated by Vite
59
+ */
60
+ async render(pageObject) {
61
+ let render;
62
+ let preloadTags = [];
63
+ const devServer = this.vite?.getDevServer();
64
+ if (devServer) {
65
+ const runtime = await this.vite.createRuntime();
66
+ render = await runtime.executeEntrypoint(this.config.ssr.entrypoint);
67
+ const entryMod = devServer.moduleGraph.getModuleById(this.config.entrypoint);
68
+ const pageMod = this.#findPageModule(entryMod, pageObject);
69
+ if (pageMod)
70
+ await runtime.executeUrl(pageMod.url);
71
+ const preloadUrls = /* @__PURE__ */ new Set();
72
+ const visitedModules = /* @__PURE__ */ new Set();
73
+ if (pageMod)
74
+ this.#collectCss(pageMod, preloadUrls, visitedModules);
75
+ if (entryMod)
76
+ this.#collectCss(entryMod, preloadUrls, visitedModules);
77
+ preloadTags = Array.from(preloadUrls).map(this.#getPreloadTag);
78
+ } else {
79
+ render = await import(this.config.ssr.bundle);
80
+ }
81
+ const result = await render.default(pageObject);
82
+ const head = preloadTags.concat(result.head);
83
+ return { head, body: result.body };
84
+ }
85
+ };
86
+
87
+ // src/inertia.ts
88
+ var kLazySymbol = Symbol("lazy");
89
+ var Inertia = class {
90
+ constructor(ctx, config, vite) {
91
+ this.ctx = ctx;
92
+ this.config = config;
93
+ this.vite = vite;
94
+ this.#sharedData = config.sharedData;
95
+ this.#serverRenderer = new ServerRenderer(config, vite);
96
+ }
97
+ #sharedData = {};
98
+ #serverRenderer;
99
+ /**
100
+ * Check if a value is a lazy prop
101
+ */
102
+ #isLazyProps(value) {
103
+ return typeof value === "object" && value && kLazySymbol in value;
104
+ }
105
+ /**
106
+ * Pick props to resolve based on x-inertia-partial-data header
107
+ *
108
+ * If header is not present, resolve all props except lazy props
109
+ * If header is present, resolve only the props that are listed in the header
110
+ */
111
+ #pickPropsToResolve(component, props) {
112
+ const partialData = this.ctx.request.header("x-inertia-partial-data")?.split(",").filter(Boolean);
113
+ const partialComponent = this.ctx.request.header("x-inertia-partial-component");
114
+ let entriesToResolve = Object.entries(props);
115
+ if (partialData && partialComponent === component) {
116
+ entriesToResolve = entriesToResolve.filter(([key]) => partialData.includes(key));
117
+ } else {
118
+ entriesToResolve = entriesToResolve.filter(([key]) => !this.#isLazyProps(props[key]));
119
+ }
120
+ return entriesToResolve;
121
+ }
122
+ /**
123
+ * Resolve the props that will be sent to the client
124
+ */
125
+ async #resolvePageProps(component, props) {
126
+ const entriesToResolve = this.#pickPropsToResolve(component, props);
127
+ const entries = entriesToResolve.map(async ([key, value]) => {
128
+ if (typeof value === "function") {
129
+ return [key, await value(this.ctx)];
130
+ }
131
+ if (this.#isLazyProps(value)) {
132
+ const lazyValue = value[kLazySymbol];
133
+ return [key, await lazyValue()];
134
+ }
135
+ return [key, value];
136
+ });
137
+ return Object.fromEntries(await Promise.all(entries));
138
+ }
139
+ /**
140
+ * Build the page object that will be returned to the client
141
+ *
142
+ * See https://inertiajs.com/the-protocol#the-page-object
143
+ */
144
+ async #buildPageObject(component, pageProps) {
145
+ return {
146
+ component,
147
+ version: this.config.versionCache.getVersion(),
148
+ props: await this.#resolvePageProps(component, { ...this.#sharedData, ...pageProps }),
149
+ url: this.ctx.request.url(true)
150
+ };
151
+ }
152
+ /**
153
+ * If the page should be rendered on the server or not
154
+ *
155
+ * The ssr.pages config can be a list of pages or a function that returns a boolean
156
+ */
157
+ async #shouldRenderOnServer(component) {
158
+ const isSsrEnabled = this.config.ssr.enabled;
159
+ if (!isSsrEnabled)
160
+ return false;
161
+ let isSsrEnabledForPage = false;
162
+ if (typeof this.config.ssr.pages === "function") {
163
+ isSsrEnabledForPage = await this.config.ssr.pages(this.ctx, component);
164
+ } else if (this.config.ssr.pages) {
165
+ isSsrEnabledForPage = this.config.ssr.pages?.includes(component);
166
+ } else {
167
+ isSsrEnabledForPage = true;
168
+ }
169
+ return isSsrEnabledForPage;
170
+ }
171
+ /**
172
+ * Render the page on the server
173
+ */
174
+ async #renderOnServer(pageObject, viewProps) {
175
+ const { head, body } = await this.#serverRenderer.render(pageObject);
176
+ return this.ctx.view.render(this.config.rootView, {
177
+ ...viewProps,
178
+ page: { ssrHead: head, ssrBody: body, ...pageObject }
179
+ });
180
+ }
181
+ /**
182
+ * Share data for the current request.
183
+ * This data will override any shared data defined in the config.
184
+ */
185
+ share(data) {
186
+ this.#sharedData = { ...this.#sharedData, ...data };
187
+ }
188
+ /**
189
+ * Render a page using Inertia
190
+ */
191
+ async render(component, pageProps, viewProps) {
192
+ const pageObject = await this.#buildPageObject(component, pageProps);
193
+ const isInertiaRequest = !!this.ctx.request.header("x-inertia");
194
+ if (!isInertiaRequest) {
195
+ const shouldRenderOnServer = await this.#shouldRenderOnServer(component);
196
+ if (shouldRenderOnServer)
197
+ return this.#renderOnServer(pageObject, viewProps);
198
+ return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
199
+ }
200
+ this.ctx.response.header("x-inertia", "true");
201
+ return pageObject;
202
+ }
203
+ /**
204
+ * Create a lazy prop
205
+ *
206
+ * Lazy props are never resolved on first visit, but only when the client
207
+ * request a partial reload explicitely with this value.
208
+ *
209
+ * See https://inertiajs.com/partial-reloads#lazy-data-evaluation
210
+ */
211
+ lazy(callback) {
212
+ return { [kLazySymbol]: callback };
213
+ }
214
+ /**
215
+ * This method can be used to redirect the user to an external website
216
+ * or even a non-inertia route of your application.
217
+ *
218
+ * See https://inertiajs.com/redirects#external-redirects
219
+ */
220
+ async location(url) {
221
+ this.ctx.response.header("X-Inertia-Location", url);
222
+ this.ctx.response.status(409);
223
+ }
224
+ };
225
+
226
+ // src/inertia_middleware.ts
227
+ var InertiaMiddleware = class {
228
+ constructor(config, vite) {
229
+ this.config = config;
230
+ this.vite = vite;
231
+ }
232
+ async handle(ctx, next) {
233
+ const { response, request } = ctx;
234
+ ctx.inertia = new Inertia(ctx, this.config, this.vite);
235
+ await next();
236
+ const isInertiaRequest = !!request.header("x-inertia");
237
+ if (!isInertiaRequest)
238
+ return;
239
+ response.header("Vary", "Accept");
240
+ const method = request.method();
241
+ if (response.getStatus() === 302 && ["PUT", "PATCH", "DELETE"].includes(method)) {
242
+ response.status(303);
243
+ }
244
+ const version = this.config.versionCache.getVersion().toString();
245
+ if (method === "GET" && request.header("x-inertia-version", "") !== version) {
246
+ response.header("x-inertia-location", request.url());
247
+ response.status(409);
248
+ }
249
+ }
250
+ };
251
+
252
+ export {
253
+ InertiaMiddleware
254
+ };
@@ -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,6 +1,7 @@
1
1
  import Configure from '@adonisjs/core/commands/configure';
2
2
  import { ConfigProvider } from '@adonisjs/core/types';
3
3
  import { InertiaConfig, ResolvedConfig } from './src/types.js';
4
+ import '@tuyau/utils/types';
4
5
  import '@adonisjs/core/http';
5
6
 
6
7
  /**
@@ -8,11 +9,11 @@ import '@adonisjs/core/http';
8
9
  */
9
10
  declare function configure(command: Configure): Promise<void>;
10
11
 
11
- declare const stubsRoot: string;
12
-
13
12
  /**
14
13
  * Define the Inertia configuration
15
14
  */
16
15
  declare function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConfig>;
17
16
 
17
+ declare const stubsRoot: string;
18
+
18
19
  export { configure, defineConfig, stubsRoot };
package/build/index.js CHANGED
@@ -1,70 +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 isAlreadyDefined = routesFile.getText().includes("/inertia");
88
+ if (isAlreadyDefined) {
89
+ command.logger.warning("/inertia route is already defined. Skipping");
90
+ return;
91
+ }
92
+ const action = command.logger.action("update start/routes.ts file");
93
+ try {
94
+ routesFile?.addStatements((writer) => {
95
+ writer.writeLine(`router.on('/inertia').renderInertia('home', { version: 6 })`);
96
+ });
97
+ await tsMorph?.save();
98
+ action.succeeded();
99
+ } catch (error) {
100
+ codemods.emit("error", error);
101
+ action.failed(error.message);
102
+ }
103
+ }
29
104
  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 || []);
105
+ let adapter = command.parsedFlags.adapter;
106
+ let ssr = command.parsedFlags.ssr;
107
+ let shouldInstallPackages = command.parsedFlags.install;
108
+ if (adapter === void 0) {
109
+ adapter = await command.prompt.choice(
110
+ "Select the Inertia adapter you want to use",
111
+ ADAPTERS.map((adapterName) => string.capitalCase(adapterName)),
112
+ { name: "adapter", result: (value) => value.toLowerCase() }
113
+ );
114
+ }
115
+ if (ssr === void 0) {
116
+ ssr = await command.prompt.confirm("Do you want to use server-side rendering?", {
117
+ name: "ssr"
118
+ });
41
119
  }
120
+ if (adapter in ADAPTERS_INFO === false) {
121
+ command.logger.error(
122
+ `The selected adapter "${adapter}" is invalid. Select one from: ${string.sentence(
123
+ Object.keys(ADAPTERS_INFO)
124
+ )}`
125
+ );
126
+ command.exitCode = 1;
127
+ return;
128
+ }
129
+ const adapterInfo = ADAPTERS_INFO[adapter];
42
130
  const codemods = await command.createCodemods();
43
131
  await codemods.updateRcFile((rcFile) => {
44
132
  rcFile.addProvider("@adonisjs/inertia/inertia_provider");
45
133
  });
46
- codemods.registerMiddleware("router", [
134
+ await codemods.registerMiddleware("server", [
47
135
  { path: "@adonisjs/inertia/inertia_middleware", position: "after" }
48
136
  ]);
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" }
137
+ const appExt = adapterInfo.appExtension;
138
+ const stubFolder = adapterInfo.stubFolder;
139
+ const compExt = adapterInfo.componentsExtension;
140
+ await codemods.makeUsingStub(stubsRoot, "config.stub", {
141
+ ssr,
142
+ ssrEntrypoint: adapterInfo.ssrEntrypoint
143
+ });
144
+ await codemods.makeUsingStub(stubsRoot, `app.css.stub`, {});
145
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/root.edge.stub`, {});
146
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/tsconfig.json.stub`, {});
147
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/app.${appExt}.stub`, { ssr });
148
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/home.${compExt}.stub`, {});
149
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/not_found.${compExt}.stub`, {});
150
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/errors/server_error.${compExt}.stub`, {});
151
+ if (ssr) {
152
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/ssr.${appExt}.stub`, {});
153
+ }
154
+ const inertiaPluginCall = ssr ? `inertia({ ssr: { enabled: true, entrypoint: 'inertia/app/ssr.${appExt}' } })` : `inertia({ ssr: { enabled: false } })`;
155
+ await codemods.registerVitePlugin(inertiaPluginCall, [
156
+ { isNamed: false, module: "@adonisjs/inertia/client", identifier: "inertia" }
157
+ ]);
158
+ await codemods.registerVitePlugin(
159
+ ssr && adapterInfo.viteRegister.ssrPluginCall ? adapterInfo.viteRegister.ssrPluginCall : adapterInfo.viteRegister.pluginCall,
160
+ adapterInfo.viteRegister.importDeclarations
53
161
  );
162
+ const adonisjsPluginCall = `adonisjs({ entrypoints: ['inertia/app/app.${appExt}'], reload: ['resources/views/**/*.edge'] })`;
163
+ await codemods.registerVitePlugin(adonisjsPluginCall, [
164
+ { isNamed: false, module: "@adonisjs/vite/client", identifier: "adonisjs" }
165
+ ]);
166
+ await defineExampleRoute(command, codemods);
167
+ const pkgToInstall = adapterInfo.dependencies;
168
+ if (ssr && adapterInfo.ssrDependencies) {
169
+ pkgToInstall.push(...adapterInfo.ssrDependencies);
170
+ }
171
+ if (shouldInstallPackages === void 0) {
172
+ shouldInstallPackages = await command.prompt.confirm(
173
+ `Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(", ")}?`,
174
+ { name: "install" }
175
+ );
176
+ }
54
177
  if (shouldInstallPackages) {
55
- command.installPackages(pkgToInstall);
178
+ await codemods.installPackages(pkgToInstall);
56
179
  } else {
57
- command.listPackagesToInstall(pkgToInstall);
180
+ await codemods.listPackagesToInstall(pkgToInstall);
58
181
  }
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
182
  }
63
183
 
64
- // stubs/main.ts
65
- import { getDirname } from "@poppinss/utils";
66
- var stubsRoot = getDirname(import.meta.url);
67
-
68
184
  // src/define_config.ts
69
185
  import { configProvider } from "@adonisjs/core";
70
186
 
@@ -116,15 +232,77 @@ var VersionCache = class {
116
232
  }
117
233
  };
118
234
 
235
+ // src/files_detector.ts
236
+ import { locatePath } from "locate-path";
237
+ var FilesDetector = class {
238
+ constructor(app) {
239
+ this.app = app;
240
+ }
241
+ /**
242
+ * Try to locate the entrypoint file based
243
+ * on the conventional locations
244
+ */
245
+ async detectEntrypoint(defaultPath) {
246
+ const possiblesLocations = [
247
+ "./inertia/app/app.ts",
248
+ "./inertia/app/app.tsx",
249
+ "./resources/app.ts",
250
+ "./resources/app.tsx",
251
+ "./resources/app.jsx",
252
+ "./resources/app.js",
253
+ "./inertia/app/app.jsx"
254
+ ];
255
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
256
+ return this.app.makePath(path || defaultPath);
257
+ }
258
+ /**
259
+ * Try to locate the SSR entrypoint file based
260
+ * on the conventional locations
261
+ */
262
+ async detectSsrEntrypoint(defaultPath) {
263
+ const possiblesLocations = [
264
+ "./inertia/app/ssr.ts",
265
+ "./inertia/app/ssr.tsx",
266
+ "./resources/ssr.ts",
267
+ "./resources/ssr.tsx",
268
+ "./resources/ssr.jsx",
269
+ "./resources/ssr.js",
270
+ "./inertia/app/ssr.jsx"
271
+ ];
272
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
273
+ return this.app.makePath(path || defaultPath);
274
+ }
275
+ /**
276
+ * Try to locate the SSR bundle file based
277
+ * on the conventional locations
278
+ */
279
+ async detectSsrBundle(defaultPath) {
280
+ const possiblesLocations = ["./ssr/ssr.js", "./ssr/ssr.mjs"];
281
+ const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot });
282
+ return this.app.makePath(path || defaultPath);
283
+ }
284
+ };
285
+
119
286
  // src/define_config.ts
287
+ import { slash } from "@poppinss/utils";
120
288
  function defineConfig(config) {
121
289
  return configProvider.create(async (app) => {
290
+ const detector = new FilesDetector(app);
122
291
  const versionCache = new VersionCache(app.appRoot, config.assetsVersion);
123
292
  await versionCache.computeVersion();
124
293
  return {
125
- rootView: config.rootView ?? "root",
294
+ versionCache,
295
+ rootView: config.rootView ?? "inertia_layout",
126
296
  sharedData: config.sharedData || {},
127
- versionCache
297
+ entrypoint: slash(
298
+ config.entrypoint ?? await detector.detectEntrypoint("inertia/app/app.ts")
299
+ ),
300
+ ssr: {
301
+ enabled: config.ssr?.enabled ?? false,
302
+ pages: config.ssr?.pages,
303
+ entrypoint: config.ssr?.entrypoint ?? await detector.detectSsrEntrypoint("inertia/app/ssr.ts"),
304
+ bundle: config.ssr?.bundle ?? await detector.detectSsrBundle("ssr/ssr.js")
305
+ }
128
306
  };
129
307
  });
130
308
  }
@@ -1,5 +1,14 @@
1
1
  import { ApplicationService } from '@adonisjs/core/types';
2
2
 
3
+ declare module '@adonisjs/core/http' {
4
+ interface BriskRoute {
5
+ /**
6
+ * Render an inertia page without defining an
7
+ * explicit route handler
8
+ */
9
+ renderInertia(component: string, props?: Record<string, any>, viewProps?: Record<string, any>): void;
10
+ }
11
+ }
3
12
  /**
4
13
  * Inertia provider
5
14
  */
@@ -11,9 +20,13 @@ declare class InertiaProvider {
11
20
  */
12
21
  protected registerEdgePlugin(): Promise<void>;
13
22
  /**
14
- * Register Inertia bindings
23
+ * Register inertia middleware
15
24
  */
16
25
  register(): Promise<void>;
26
+ /**
27
+ * Register edge plugin and brisk route macro
28
+ */
29
+ boot(): Promise<void>;
17
30
  }
18
31
 
19
32
  export { InertiaProvider as default };