Skip to content

Commit

Permalink
perf: improve boot time of the provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jun 2, 2024
1 parent 55447d4 commit 116d615
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions providers/vite_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ export default class ViteProvider {

if (!this.#shouldRunVite) return

const vite = await this.app.container.make('vite')
const server = await this.app.container.make('server')
const [vite, server] = await Promise.all([
this.app.container.make('vite'),
this.app.container.make('server'),
])

await vite.createDevServer()
server.use([() => import('../src/middleware/vite_middleware.js')])
Expand Down
10 changes: 9 additions & 1 deletion src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Vite {
#manifestCache?: Manifest
#options: ViteOptions
#devServer?: ViteDevServer
#createServerPromise?: Promise<ViteDevServer>

constructor(
protected isViteRunning: boolean,
Expand Down Expand Up @@ -321,11 +322,17 @@ export class Vite {
async createDevServer(options?: InlineConfig) {
const { createServer } = await import('vite')

this.#devServer = await createServer({
/**
* We do not await the server creation since it will
* slow down the boot process of AdonisJS
*/
this.#createServerPromise = createServer({
server: { middlewareMode: true, hmr: { port: 3001 } },
appType: 'custom',
...options,
})

this.#devServer = await this.#createServerPromise
}

/**
Expand All @@ -343,6 +350,7 @@ export class Vite {
* Stop the Vite Dev server
*/
async stopDevServer() {
await this.#createServerPromise
await this.#devServer?.close()
}

Expand Down
9 changes: 7 additions & 2 deletions tests/backend/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
* file that was distributed with this source code.
*/

import { IgnitorFactory } from '@adonisjs/core/factories'
import { test } from '@japa/runner'
import { setTimeout } from 'node:timers/promises'
import { IgnitorFactory } from '@adonisjs/core/factories'

import { defineConfig } from '../../index.js'
import ViteMiddleware from '../../src/middleware/vite_middleware.js'

Expand All @@ -20,7 +22,7 @@ const IMPORTER = (filePath: string) => {
return import(filePath)
}

test.group('Inertia Provider', () => {
test.group('Vite Provider', () => {
test('register vite middleware singleton', async ({ assert }) => {
process.env.NODE_ENV = 'development'

Expand Down Expand Up @@ -55,6 +57,8 @@ test.group('Inertia Provider', () => {
await app.boot()

const vite = await app.container.make('vite')

await setTimeout(200)
assert.isDefined(vite.getDevServer()?.restart)

await app.terminate()
Expand Down Expand Up @@ -95,6 +99,7 @@ test.group('Inertia Provider', () => {
await app.boot()

const vite = await app.container.make('vite')
await setTimeout(200)
assert.isDefined(vite.getDevServer()?.restart)

await app.terminate()
Expand Down

0 comments on commit 116d615

Please sign in to comment.