|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import path from 'node:path' |
3 | 3 | import { fileURLToPath } from 'node:url' |
| 4 | +import { exactRegex } from 'rolldown/filter' |
4 | 5 | import { cleanUrl } from '../../shared/utils' |
5 | 6 | import type { ResolvedConfig } from '../config' |
6 | 7 | import { |
7 | 8 | BUNDLED_DEV_CLIENT_ENTRY, |
| 9 | + BUNDLED_DEV_CLIENT_FILENAME, |
8 | 10 | BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR, |
9 | 11 | CLIENT_ENTRY, |
| 12 | + CLIENT_PUBLIC_PATH, |
10 | 13 | ENV_ENTRY, |
| 14 | + ENV_PUBLIC_PATH, |
11 | 15 | } from '../constants' |
12 | 16 | import { perEnvironmentState } from '../environment' |
13 | 17 | import type { Plugin } from '../plugin' |
14 | | -import { isObject, normalizePath, resolveHostname } from '../utils' |
| 18 | +import { escapeRegex, isObject, normalizePath, resolveHostname } from '../utils' |
15 | 19 | import { replaceDefine, serializeDefine } from './define' |
16 | 20 |
|
17 | 21 | // ids in transform are normalized to unix style |
@@ -196,3 +200,46 @@ export function getRolldownDevRuntimeFiles(): Map<string, string> { |
196 | 200 | } |
197 | 201 | return files |
198 | 202 | } |
| 203 | + |
| 204 | +export function getBundledDevClientAliases( |
| 205 | + config: ResolvedConfig, |
| 206 | +): Map<string, string> { |
| 207 | + const clientUrl = path.posix.join(config.base, BUNDLED_DEV_CLIENT_FILENAME) |
| 208 | + return new Map([ |
| 209 | + [CLIENT_PUBLIC_PATH.slice(1), `export * from ${JSON.stringify(clientUrl)}`], |
| 210 | + // defines are replaced at build time in bundled dev |
| 211 | + [ENV_PUBLIC_PATH.slice(1), ''], |
| 212 | + ]) |
| 213 | +} |
| 214 | + |
| 215 | +export function bundledDevClientImportsPlugin(config: ResolvedConfig): Plugin { |
| 216 | + const clientUrl = path.posix.join(config.base, BUNDLED_DEV_CLIENT_FILENAME) |
| 217 | + return { |
| 218 | + name: 'vite:bundled-dev-client-imports', |
| 219 | + applyToEnvironment(environment) { |
| 220 | + return ( |
| 221 | + config.command === 'serve' && |
| 222 | + environment.config.isBundled && |
| 223 | + environment.config.consumer === 'client' |
| 224 | + ) |
| 225 | + }, |
| 226 | + resolveId: { |
| 227 | + filter: { id: exactRegex(clientUrl) }, |
| 228 | + handler(id) { |
| 229 | + return { id, external: true } |
| 230 | + }, |
| 231 | + }, |
| 232 | + load: { |
| 233 | + filter: { |
| 234 | + id: new RegExp( |
| 235 | + `^(?:${escapeRegex(normalizedClientEntry)}|${escapeRegex(normalizedEnvEntry)})(?:\\?.*)?$`, |
| 236 | + ), |
| 237 | + }, |
| 238 | + handler(id) { |
| 239 | + return cleanUrl(id) === normalizedClientEntry |
| 240 | + ? `export * from ${JSON.stringify(clientUrl)}` |
| 241 | + : '' |
| 242 | + }, |
| 243 | + }, |
| 244 | + } |
| 245 | +} |
0 commit comments