Skip to content

Commit

Permalink
fix: createRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 4, 2024
1 parent 6d9d685 commit c8a5629
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@

import { readFileSync } from 'node:fs'
import type { ViteRuntime } from 'vite/runtime'
import {
createViteRuntime,
MainThreadRuntimeOptions,
type Manifest,
type ViteDevServer,
} from 'vite'
import type { MainThreadRuntimeOptions, Manifest, ViteDevServer } from 'vite'

import { makeAttributes, uniqBy } from './utils.js'
import type { AdonisViteElement, SetAttributes, ViteOptions } from './types.js'
Expand All @@ -30,7 +25,6 @@ export class Vite {
*/
#manifestCache?: Manifest
#options: ViteOptions
#runtime?: ViteRuntime
#devServer?: ViteDevServer

constructor(
Expand Down Expand Up @@ -325,14 +319,23 @@ export class Vite {
* since we don't need it
*/
async createDevServer() {
const { createViteRuntime, createServer } = await import('vite')
const { createServer } = await import('vite')

this.#devServer = await createServer({
server: { middlewareMode: true, hmr: { port: 3001 } },
appType: 'custom',
})
}

/**
* Create a runtime instance
* Will not be available when running in production since
* it needs the Vite Dev server
*/
async createRuntime(options: MainThreadRuntimeOptions = {}): Promise<ViteRuntime> {
const { createViteRuntime } = await import('vite')

this.#runtime = await createViteRuntime(this.#devServer)
return createViteRuntime(this.#devServer!, options)
}

/**
Expand All @@ -350,15 +353,6 @@ export class Vite {
return this.#devServer
}

/**
* Create a runtime instance
* Will not be available when running in production since
* it needs the Vite Dev server
*/
createRuntime(options: MainThreadRuntimeOptions = {}) {
return createViteRuntime(this.#devServer!, options)
}

/**
* Returns the script needed for the HMR working with React
*/
Expand Down

0 comments on commit c8a5629

Please sign in to comment.