Skip to content

Commit

Permalink
refactor: run dev server only in test & web environments
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Feb 27, 2024
1 parent 8e65616 commit 51ef82e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
17 changes: 13 additions & 4 deletions providers/vite_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ declare module '@adonisjs/core/types' {
}

export default class ViteProvider {
constructor(protected app: ApplicationService) {}
#shouldRunVite: boolean

constructor(protected app: ApplicationService) {
const env = this.app.getEnvironment()

/**
* We should only run Vite in development and test environments
*/
this.#shouldRunVite = (this.app.inDev || this.app.inTest) && (env === 'web' || env === 'test')
}

/**
* Registers edge plugin when edge is installed
Expand All @@ -37,15 +46,15 @@ export default class ViteProvider {
register() {
const config = this.app.config.get<ViteOptions>('vite')

const vite = new Vite(this.app.inDev, config)
const vite = new Vite(this.#shouldRunVite, config)
this.app.container.bind('vite', () => vite)
this.app.container.bind(ViteMiddleware, () => new ViteMiddleware(vite))
}

async boot() {
await this.registerEdgePlugin()

if (!this.app.inDev) return
if (!this.#shouldRunVite) return

const vite = await this.app.container.make('vite')
const server = await this.app.container.make('server')
Expand All @@ -55,7 +64,7 @@ export default class ViteProvider {
}

async shutdown() {
if (!this.app.inDev) return
if (!this.#shouldRunVite) return

const vite = await this.app.container.make('vite')
await vite.stopDevServer()
Expand Down
14 changes: 7 additions & 7 deletions src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Vite {
#devServer?: ViteDevServer

constructor(
protected inDev: boolean,
protected isViteRunning: boolean,
options: ViteOptions
) {
this.#options = options
Expand Down Expand Up @@ -122,7 +122,7 @@ export class Vite {
*/
#generateTag(asset: string, attributes?: Record<string, any>): AdonisViteElement {
let url = ''
if (this.inDev) {
if (this.isViteRunning) {
url = `/${asset}`
} else {
url = `${this.#options.assetsUrl}/${asset}`
Expand Down Expand Up @@ -201,7 +201,7 @@ export class Vite {
): AdonisViteElement[] {
entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints]

if (this.inDev) {
if (this.isViteRunning) {
return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes)
}

Expand All @@ -214,7 +214,7 @@ export class Vite {
* "assets" URL
*/
assetsUrl() {
if (this.inDev) return this.#devServer!.config.server.host
if (this.isViteRunning) return this.#devServer!.config.server.host

return this.#options.assetsUrl
}
Expand All @@ -223,7 +223,7 @@ export class Vite {
* Returns path to a given asset file
*/
assetPath(asset: string): string {
if (this.inDev) {
if (this.isViteRunning) {
return `/${asset}`
}

Expand All @@ -237,7 +237,7 @@ export class Vite {
* @throws Will throw an exception when running in dev
*/
manifest(): Manifest {
if (this.inDev) {
if (this.isViteRunning) {
throw new Error('Cannot read the manifest file when running in dev mode')
}

Expand Down Expand Up @@ -292,7 +292,7 @@ export class Vite {
* Returns the script needed for the HMR working with React
*/
getReactHmrScript(attributes?: Record<string, any>): AdonisViteElement | null {
if (!this.inDev) {
if (!this.isViteRunning) {
return null
}

Expand Down
38 changes: 38 additions & 0 deletions tests/backend/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@ test.group('Inertia Provider', () => {
await app.terminate()
})

test('run dev server in test', async ({ assert }) => {
process.env.NODE_ENV = 'test'

const ignitor = new IgnitorFactory()
.merge({ rcFileContents: { providers: [() => import('../../providers/vite_provider.js')] } })
.withCoreConfig()
.withCoreProviders()
.merge({ config: { vite: defineConfig({}) } })
.create(BASE_URL, { importer: IMPORTER })

const app = ignitor.createApp('test')
await app.init()
await app.boot()

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

await app.terminate()
})

test('doesnt launch dev server in console environment', async ({ assert }) => {
const ignitor = new IgnitorFactory()
.merge({ rcFileContents: { providers: [() => import('../../providers/vite_provider.js')] } })
.withCoreConfig()
.withCoreProviders()
.merge({ config: { vite: defineConfig({}) } })
.create(BASE_URL, { importer: IMPORTER })

const app = ignitor.createApp('console')
await app.init()
await app.boot()

const vite = await app.container.make('vite')
assert.isUndefined(vite.getDevServer())

await app.terminate()
})

test('register edge plugin', async ({ assert }) => {
process.env.NODE_ENV = 'development'

Expand Down

0 comments on commit 51ef82e

Please sign in to comment.