@@ -21,6 +21,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
2121const port = portsMap [ "api-plugin" ] ;
2222const [ portA , portB ] = portsMap [ "api-plugin-multi" ] ;
2323
24+ // TODO remove this check in the next major, once the minimum supported webpack
25+ // applies the top-level `plugins` of a multi-configuration to the MultiCompiler.
26+ const supportsMultiCompilerPlugins =
27+ new webpack . MultiCompiler ( [ ] , { plugins : [ ] } ) . options . plugins !== undefined ;
28+
2429const cssFilePath = path . resolve (
2530 __dirname ,
2631 "../fixtures/reload-config/main.css" ,
@@ -657,6 +662,68 @@ describe("API (plugin)", () => {
657662 stopSpy . mockRestore ( ) ;
658663 } ) ;
659664
665+ it (
666+ "should work from the top-level `plugins` of a multi-configuration" ,
667+ { skip : ! supportsMultiCompilerPlugins } ,
668+ async ( ) => {
669+ const server = new Server ( { port } ) ;
670+ const setupSpy = spyOn ( server , "setup" ) ;
671+ const listenSpy = spyOn ( server , "listen" ) ;
672+ const stopSpy = spyOn ( server , "stop" ) ;
673+ const compiler = webpack (
674+ Object . assign ( [ ...multiCompilerConfig ] , { plugins : [ server ] } ) ,
675+ ) ;
676+
677+ await compile ( compiler , port ) ;
678+
679+ try {
680+ const responseOne = await fetch (
681+ `http://127.0.0.1:${ port } /one-main.js` ,
682+ ) ;
683+ const responseTwo = await fetch (
684+ `http://127.0.0.1:${ port } /two-main.js` ,
685+ ) ;
686+
687+ expect ( responseOne . status ) . toBe ( 200 ) ;
688+ expect ( responseTwo . status ) . toBe ( 200 ) ;
689+ expect ( setupSpy ) . toHaveBeenCalledTimes ( 1 ) ;
690+ expect ( listenSpy ) . toHaveBeenCalledTimes ( 1 ) ;
691+ } finally {
692+ await new Promise ( ( resolve ) => {
693+ compiler . close ( resolve ) ;
694+ } ) ;
695+ }
696+
697+ expect ( stopSpy ) . toHaveBeenCalledTimes ( 1 ) ;
698+ setupSpy . mockRestore ( ) ;
699+ listenSpy . mockRestore ( ) ;
700+ stopSpy . mockRestore ( ) ;
701+ } ,
702+ ) ;
703+
704+ it ( "should stop the server once on webpack without `MultiCompiler.hooks.shutdown`" , async ( ) => {
705+ const compiler = webpack ( multiCompilerConfig ) ;
706+ // A webpack older than 5.110.0: the same compiler, minus the hook.
707+ const { shutdown, ...hooks } = compiler . hooks ;
708+ const olderCompiler = Object . create ( compiler , {
709+ hooks : { value : hooks } ,
710+ } ) ;
711+ const server = new Server ( { port } ) ;
712+ const stopSpy = spyOn ( server , "stop" ) ;
713+
714+ server . apply ( olderCompiler ) ;
715+
716+ await compile ( compiler , port ) ;
717+
718+ await new Promise ( ( resolve ) => {
719+ compiler . close ( resolve ) ;
720+ } ) ;
721+
722+ expect ( shutdown ) . toBeDefined ( ) ;
723+ expect ( stopSpy ) . toHaveBeenCalledTimes ( 1 ) ;
724+ stopSpy . mockRestore ( ) ;
725+ } ) ;
726+
660727 it ( "should run two independent plugin servers on different child compilers" , async ( ) => {
661728 const serverA = new Server ( { port : portA } ) ;
662729 const serverB = new Server ( { port : portB } ) ;
0 commit comments