diff --git a/providers/vite_provider.ts b/providers/vite_provider.ts index 82e822b..1774177 100644 --- a/providers/vite_provider.ts +++ b/providers/vite_provider.ts @@ -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 @@ -37,7 +46,7 @@ export default class ViteProvider { register() { const config = this.app.config.get('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)) } @@ -45,7 +54,7 @@ export default class ViteProvider { 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') @@ -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() diff --git a/src/vite.ts b/src/vite.ts index f4054e2..c741f5c 100644 --- a/src/vite.ts +++ b/src/vite.ts @@ -29,7 +29,7 @@ export class Vite { #devServer?: ViteDevServer constructor( - protected inDev: boolean, + protected isViteRunning: boolean, options: ViteOptions ) { this.#options = options @@ -122,7 +122,7 @@ export class Vite { */ #generateTag(asset: string, attributes?: Record): AdonisViteElement { let url = '' - if (this.inDev) { + if (this.isViteRunning) { url = `/${asset}` } else { url = `${this.#options.assetsUrl}/${asset}` @@ -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) } @@ -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 } @@ -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}` } @@ -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') } @@ -292,7 +292,7 @@ export class Vite { * Returns the script needed for the HMR working with React */ getReactHmrScript(attributes?: Record): AdonisViteElement | null { - if (!this.inDev) { + if (!this.isViteRunning) { return null } diff --git a/tests/backend/provider.spec.ts b/tests/backend/provider.spec.ts index e961b3b..4969e30 100644 --- a/tests/backend/provider.spec.ts +++ b/tests/backend/provider.spec.ts @@ -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'