11import { afterEach , beforeEach , describe , it , expect , vi } from 'vitest' ;
22import { mkdtempSync , rmSync , writeFileSync } from 'node:fs' ;
33import * as realFs from 'node:fs' ;
4- import { SourceMap } from 'node:module' ;
4+ import { createRequire , SourceMap } from 'node:module' ;
55import { tmpdir } from 'node:os' ;
66import path , { join } from 'node:path' ;
77import { normalizePath , preprocessCSS , resolveConfig , type Plugin } from 'vite' ;
@@ -1453,7 +1453,7 @@ export class AppComponent {}
14531453 60_000 ,
14541454 ) ;
14551455
1456- it ( 'keeps AOT output for environments still building after another ends' , async ( ) => {
1456+ it ( 'keeps AOT output for environments still building after another ends and closes ' , async ( ) => {
14571457 const plugin = createAppBuildPlugin ( { jit : false } ) ;
14581458 await plugin . config ( { root : fixtureDir , build : { } } , { command : 'build' } ) ;
14591459 const config = await resolveConfig (
@@ -1471,6 +1471,7 @@ export class AppComponent {}
14711471 await plugin . buildStart . call ( ctx ) ;
14721472 await plugin . buildStart . call ( ctx ) ;
14731473 await plugin . buildEnd . call ( ctx ) ;
1474+ await plugin . closeBundle ( ) ;
14741475
14751476 const code = realFs . readFileSync ( componentPath , 'utf-8' ) ;
14761477 const result = await plugin . transform . handler . call (
@@ -1481,6 +1482,10 @@ export class AppComponent {}
14811482 expect ( result ?. code ) . toContain ( 'ɵcmp' ) ;
14821483
14831484 await plugin . buildEnd . call ( ctx ) ;
1485+ await plugin . closeBundle ( ) ;
1486+ expect (
1487+ await plugin . transform . handler . call ( ctx , code , componentPath ) ,
1488+ ) . toBeUndefined ( ) ;
14841489 } , 60_000 ) ;
14851490
14861491 it . each ( [
@@ -1809,7 +1814,7 @@ export class AppComponent {}
18091814 expect ( ctx . error ) . not . toHaveBeenCalled ( ) ;
18101815 } finally {
18111816 await mainPlugin . buildEnd . call ( ctx ) ;
1812- mainPlugin . closeBundle ( ) ;
1817+ await mainPlugin . closeBundle ( ) ;
18131818 }
18141819 } , 60_000 ) ;
18151820
@@ -1976,6 +1981,91 @@ export class AppComponent {}
19761981 expect ( ctx . error ) . not . toHaveBeenCalled ( ) ;
19771982 } , 60_000 ) ;
19781983
1984+ it . each ( [
1985+ { jit : false , closeBeforeEnd : false } ,
1986+ { jit : true , closeBeforeEnd : false } ,
1987+ { jit : false , closeBeforeEnd : true } ,
1988+ { jit : true , closeBeforeEnd : true } ,
1989+ ] ) (
1990+ 'reuses the watched builder and releases it on close (jit=$jit, closeBeforeEnd=$closeBeforeEnd)' ,
1991+ async ( { jit, closeBeforeEnd } ) => {
1992+ const require = createRequire ( import . meta. url ) ;
1993+ const ts = require ( 'typescript' ) ;
1994+ const tsModule = require . cache [ require . resolve ( 'typescript' ) ] ! ;
1995+ const createBuilder = vi . fn (
1996+ ts . createEmitAndSemanticDiagnosticsBuilderProgram ,
1997+ ) ;
1998+ tsModule . exports = new Proxy ( ts , {
1999+ get ( target , property , receiver ) {
2000+ return property === 'createEmitAndSemanticDiagnosticsBuilderProgram'
2001+ ? createBuilder
2002+ : Reflect . get ( target , property , receiver ) ;
2003+ } ,
2004+ } ) ;
2005+ let plugin ;
2006+ try {
2007+ plugin = createAppBuildPlugin ( { jit } ) ;
2008+ } finally {
2009+ tsModule . exports = ts ;
2010+ }
2011+ await plugin . config ( { root : fixtureDir } , { command : 'serve' } ) ;
2012+ const config = await resolveConfig (
2013+ { configFile : false , root : fixtureDir } ,
2014+ 'serve' ,
2015+ ) ;
2016+ plugin . configResolved ( config ) ;
2017+ const ctx = {
2018+ environment : { config } ,
2019+ warn : vi . fn ( ) ,
2020+ error : vi . fn ( ) ,
2021+ addWatchFile : vi . fn ( ) ,
2022+ } ;
2023+ const code = realFs . readFileSync ( componentPath , 'utf-8' ) ;
2024+
2025+ await plugin . buildStart . call ( ctx ) ;
2026+ await plugin . buildEnd . call ( ctx ) ;
2027+ const firstBuilder = createBuilder . mock . results . at ( - 1 ) ?. value ;
2028+ expect ( firstBuilder ) . toBeDefined ( ) ;
2029+ await plugin . buildStart . call ( ctx ) ;
2030+ await plugin . buildEnd . call ( ctx ) ;
2031+ expect ( createBuilder . mock . calls . at ( - 1 ) ?. [ jit ? 3 : 2 ] ) . toBe ( firstBuilder ) ;
2032+
2033+ const retained = await plugin . transform . handler . call (
2034+ ctx ,
2035+ code ,
2036+ componentPath ,
2037+ ) ;
2038+ expect ( retained ?. code ) . toContain ( 'AppComponent' ) ;
2039+
2040+ if ( closeBeforeEnd ) {
2041+ await plugin . buildStart . call ( ctx ) ;
2042+ await plugin . closeBundle ( ) ;
2043+ const active = await plugin . transform . handler . call (
2044+ ctx ,
2045+ code ,
2046+ componentPath ,
2047+ ) ;
2048+ expect ( active ?. code ) . toContain ( 'AppComponent' ) ;
2049+ await plugin . buildEnd . call ( ctx ) ;
2050+ } else {
2051+ await plugin . closeBundle ( ) ;
2052+ }
2053+ const released = await plugin . transform . handler . call (
2054+ ctx ,
2055+ code ,
2056+ componentPath ,
2057+ ) ;
2058+ expect ( released ) . toBeUndefined ( ) ;
2059+ await plugin . closeBundle ( ) ;
2060+ await plugin . buildStart . call ( ctx ) ;
2061+ expect ( createBuilder . mock . calls . at ( - 1 ) ?. [ jit ? 3 : 2 ] ) . toBeUndefined ( ) ;
2062+ await plugin . buildEnd . call ( ctx ) ;
2063+ await plugin . closeBundle ( ) ;
2064+ expect ( ctx . error ) . not . toHaveBeenCalled ( ) ;
2065+ } ,
2066+ 60_000 ,
2067+ ) ;
2068+
19792069 it ( 'releases production compilation output at buildEnd' , async ( ) => {
19802070 const mainPlugin = createAppBuildPlugin ( ) ;
19812071
0 commit comments