( server: ViteDevServer, environment: DevEnvironment, file: string, )
| 20 | } |
| 21 | |
| 22 | async function warmupFile( |
| 23 | server: ViteDevServer, |
| 24 | environment: DevEnvironment, |
| 25 | file: string, |
| 26 | ) { |
| 27 | // transform html with the `transformIndexHtml` hook as Vite internals would |
| 28 | // pre-transform the imported JS modules linked. this may cause `transformIndexHtml` |
| 29 | // plugins to be executed twice, but that's probably fine. |
| 30 | if (file.endsWith('.html')) { |
| 31 | const url = htmlFileToUrl(file, server.config.root) |
| 32 | if (url) { |
| 33 | try { |
| 34 | const html = await fs.readFile(file, 'utf-8') |
| 35 | await server.transformIndexHtml(url, html) |
| 36 | } catch (e) { |
| 37 | // Unexpected error, log the issue but avoid an unhandled exception |
| 38 | environment.logger.error( |
| 39 | `Pre-transform error (${colors.cyan(file)}): ${e.message}`, |
| 40 | { |
| 41 | error: e, |
| 42 | timestamp: true, |
| 43 | }, |
| 44 | ) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | // for other files, pass it through `transformRequest` with warmup |
| 49 | else { |
| 50 | const url = fileToUrl(file, server.config.root) |
| 51 | await environment.warmupRequest(url) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | function htmlFileToUrl(file: string, root: string) { |
| 56 | const url = path.relative(root, file) |
no test coverage detected