@@ -86,6 +86,7 @@ import {
8686import type { MinimalPluginContext , Plugin , PluginContext } from './plugin'
8787import type { RollupPluginHooks } from './typeUtils'
8888import { buildOxcPlugin } from './plugins/oxc'
89+ import type { ViteDevServer } from './server'
8990
9091export interface BuildEnvironmentOptions {
9192 /**
@@ -539,20 +540,21 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
539540export async function build (
540541 inlineConfig : InlineConfig = { } ,
541542) : Promise < RolldownOutput | RolldownOutput [ ] /* | RollupWatcher */ > {
542- const builder = await createBuilder ( inlineConfig , true )
543+ const builder = await createBuilder ( inlineConfig , true , 'build' )
543544 const environment = Object . values ( builder . environments ) [ 0 ]
544545 if ( ! environment ) throw new Error ( 'No environment found' )
545546 return builder . build ( environment )
546547}
547548
548549function resolveConfigToBuild (
550+ command : 'build' | 'serve' ,
549551 inlineConfig : InlineConfig = { } ,
550552 patchConfig ?: ( config : ResolvedConfig ) => void ,
551553 patchPlugins ?: ( resolvedPlugins : Plugin [ ] ) => void ,
552554) : Promise < ResolvedConfig > {
553555 return resolveConfig (
554556 inlineConfig ,
555- 'build' ,
557+ command ,
556558 'production' ,
557559 'production' ,
558560 false ,
@@ -566,6 +568,7 @@ function resolveConfigToBuild(
566568 **/
567569async function buildEnvironment (
568570 environment : BuildEnvironment ,
571+ server ?: ViteDevServer
569572) : Promise < RolldownOutput | RolldownOutput [ ] /* | RollupWatcher */ > {
570573 const { root, packageCache } = environment . config
571574 const options = environment . config . build
@@ -651,6 +654,12 @@ async function buildEnvironment(
651654 ...options . rollupOptions . moduleTypes ,
652655 '.css' : 'js' ,
653656 } ,
657+ experimental : {
658+ hmr : server ? {
659+ host : server . _currentServerHost ! ,
660+ port : server . _currentServerPort ! ,
661+ } : false ,
662+ }
654663 }
655664
656665 /**
@@ -885,6 +894,29 @@ async function buildEnvironment(
885894 logger . info (
886895 `${ colors . green ( `✓ built in ${ displayTime ( Date . now ( ) - startTime ) } ` ) } ` ,
887896 )
897+
898+ if ( server ) {
899+ for ( const output of res ) {
900+ for ( const outputFile of output . output ) {
901+ server . memoryFiles [ outputFile . fileName ] = outputFile . type === 'chunk' ? outputFile . code : outputFile . source ;
902+ }
903+ }
904+ server . watcher . on ( 'change' , async ( file ) => {
905+ const patch = await bundle ! . generateHmrPatch ( [ file ] ) ;
906+ if ( patch ) {
907+ const url = `${ Date . now ( ) } .js` ;
908+ server . memoryFiles [ url ] = patch ;
909+ // TODO(underfin): fix ws msg typing
910+ // @ts -expect-error
911+ server . ws . send ( {
912+ type : 'update' ,
913+ url
914+ } )
915+ }
916+ } )
917+ // server.watcher = watcher
918+ }
919+
888920 return Array . isArray ( outputs ) ? res : res [ 0 ]
889921 } catch ( e ) {
890922 enhanceRollupError ( e )
@@ -1611,9 +1643,10 @@ export class BuildEnvironment extends BaseEnvironment {
16111643export interface ViteBuilder {
16121644 environments : Record < string , BuildEnvironment >
16131645 config : ResolvedConfig
1614- buildApp ( ) : Promise < void >
1646+ buildApp ( server ?: ViteDevServer ) : Promise < void >
16151647 build (
16161648 environment : BuildEnvironment ,
1649+ server ?: ViteDevServer
16171650 ) : Promise < RolldownOutput | RolldownOutput [ ] /* | RollupWatcher */ >
16181651}
16191652
@@ -1632,12 +1665,12 @@ export interface BuilderOptions {
16321665 * @experimental
16331666 */
16341667 sharedPlugins ?: boolean
1635- buildApp ?: ( builder : ViteBuilder ) => Promise < void >
1668+ buildApp ?: ( builder : ViteBuilder , server ?: ViteDevServer ) => Promise < void >
16361669}
16371670
1638- async function defaultBuildApp ( builder : ViteBuilder ) : Promise < void > {
1671+ async function defaultBuildApp ( builder : ViteBuilder , server ?: ViteDevServer ) : Promise < void > {
16391672 for ( const environment of Object . values ( builder . environments ) ) {
1640- await builder . build ( environment )
1673+ await builder . build ( environment , server )
16411674 }
16421675}
16431676
@@ -1666,6 +1699,7 @@ export type ResolvedBuilderOptions = Required<BuilderOptions>
16661699export async function createBuilder (
16671700 inlineConfig : InlineConfig = { } ,
16681701 useLegacyBuilder : null | boolean = false ,
1702+ command : 'build' | 'serve' ,
16691703) : Promise < ViteBuilder > {
16701704 const patchConfig = ( resolved : ResolvedConfig ) => {
16711705 if ( ! ( useLegacyBuilder ?? ! resolved . builder ) ) return
@@ -1679,7 +1713,7 @@ export async function createBuilder(
16791713 ...resolved . environments [ environmentName ] . build ,
16801714 }
16811715 }
1682- const config = await resolveConfigToBuild ( inlineConfig , patchConfig )
1716+ const config = await resolveConfigToBuild ( command , inlineConfig , patchConfig )
16831717 useLegacyBuilder ??= ! config . builder
16841718 const configBuilder = config . builder ?? resolveBuilderOptions ( { } ) !
16851719
@@ -1688,11 +1722,11 @@ export async function createBuilder(
16881722 const builder : ViteBuilder = {
16891723 environments,
16901724 config,
1691- async buildApp ( ) {
1692- return configBuilder . buildApp ( builder )
1725+ async buildApp ( server ?: ViteDevServer ) {
1726+ return configBuilder . buildApp ( builder , server )
16931727 } ,
1694- async build ( environment : BuildEnvironment ) {
1695- return buildEnvironment ( environment )
1728+ async build ( environment : BuildEnvironment , server ?: ViteDevServer ) {
1729+ return buildEnvironment ( environment , server )
16961730 } ,
16971731 }
16981732
@@ -1742,6 +1776,7 @@ export async function createBuilder(
17421776 }
17431777 }
17441778 environmentConfig = await resolveConfigToBuild (
1779+ command ,
17451780 inlineConfig ,
17461781 patchConfig ,
17471782 patchPlugins ,
0 commit comments