|
| 1 | +import path from 'node:path' |
| 2 | +import { |
| 3 | + type Plugin, |
| 4 | + type ResolvedConfig, |
| 5 | + type UserConfig, |
| 6 | + build as viteBuild, |
| 7 | + createServer, |
| 8 | + loadConfigFromFile, |
| 9 | + mergeConfig, |
| 10 | +} from 'vite' |
| 11 | + |
| 12 | +export interface OptionItem { |
| 13 | + /** |
| 14 | + * Human friendly name of your entry point. |
| 15 | + */ |
| 16 | + name: string |
| 17 | + /** |
| 18 | + * Vite config file path. |
| 19 | + */ |
| 20 | + config: string |
| 21 | +} |
| 22 | + |
| 23 | +export default function multiple( |
| 24 | + options: OptionItem[], |
| 25 | + args: { |
| 26 | + /** |
| 27 | + * Called when all builds are complete. |
| 28 | + */ |
| 29 | + callback?: (command: ResolvedConfig['command']) => void, |
| 30 | + } = {}, |
| 31 | +): Plugin { |
| 32 | + let config: ResolvedConfig |
| 33 | + |
| 34 | + return { |
| 35 | + name: 'vite-plugin-multiple', |
| 36 | + config(config) { |
| 37 | + config.clearScreen ??= false |
| 38 | + }, |
| 39 | + async configResolved(_config) { |
| 40 | + config = _config |
| 41 | + }, |
| 42 | + configureServer(server) { |
| 43 | + if (server.httpServer) { |
| 44 | + server.httpServer.once('listening', () => serve(config, options).then(() => args.callback?.('serve'))) |
| 45 | + } else { |
| 46 | + serve(config, options).then(() => args.callback?.('serve')) |
| 47 | + } |
| 48 | + }, |
| 49 | + async closeBundle() { |
| 50 | + if (config.command === 'build') { |
| 51 | + await build(config, options) |
| 52 | + args.callback?.(config.command) |
| 53 | + } |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export async function resolveConfig(config: ResolvedConfig, option: OptionItem): Promise<UserConfig> { |
| 59 | + const { config: userConfig } = (await loadConfigFromFile({ |
| 60 | + command: config.command, |
| 61 | + mode: config.mode, |
| 62 | + ssrBuild: !!config.build?.ssr, |
| 63 | + }, option.config)) ?? { path: '', config: {}, dependencies: [] }; |
| 64 | + const defaultConfig: UserConfig = { |
| 65 | + root: config.root, |
| 66 | + mode: config.mode, |
| 67 | + build: { |
| 68 | + outDir: !userConfig.root || config.root === userConfig.root |
| 69 | + ? path.posix.join(config.build.outDir, option.name) |
| 70 | + : undefined, |
| 71 | + }, |
| 72 | + clearScreen: false, |
| 73 | + } |
| 74 | + return mergeConfig(defaultConfig, userConfig); |
| 75 | +} |
| 76 | + |
| 77 | +export async function build(config: ResolvedConfig, options: OptionItem[]) { |
| 78 | + for (const option of options) { |
| 79 | + const userConfig = await resolveConfig(config, option) |
| 80 | + await viteBuild({ |
| 81 | + // 🚧 Avoid recursive build caused by load default config file. |
| 82 | + configFile: false, |
| 83 | + ...userConfig, |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export async function serve(config: ResolvedConfig, options: OptionItem[]) { |
| 89 | + let port = 5174 // The port of main App is 5173 |
| 90 | + for (const option of options) { |
| 91 | + const userConfig = await resolveConfig(config, option) |
| 92 | + |
| 93 | + userConfig.server ??= {} |
| 94 | + userConfig.server.port ??= port++ |
| 95 | + |
| 96 | + const viteDevServer = await createServer({ |
| 97 | + configFile: false, |
| 98 | + ...userConfig, |
| 99 | + }) |
| 100 | + await viteDevServer.listen() |
| 101 | + viteDevServer.printUrls() |
| 102 | + } |
| 103 | +} |
0 commit comments