@@ -630,6 +630,17 @@ class JavascriptModulesPlugin {
630630 }
631631 }
632632 if ( inlinedModules ) {
633+ if ( bootstrap . beforeStartup . length > 0 ) {
634+ const beforeStartup = Template . asString ( bootstrap . beforeStartup ) + "\n" ;
635+ source . add (
636+ new PrefixSource (
637+ prefix ,
638+ useSourceMap
639+ ? new OriginalSource ( beforeStartup , "webpack/before-startup" )
640+ : new RawSource ( beforeStartup )
641+ )
642+ ) ;
643+ }
633644 for ( const m of inlinedModules ) {
634645 const renderedModule = this . renderModule (
635646 m ,
@@ -658,8 +669,24 @@ class JavascriptModulesPlugin {
658669 }
659670 }
660671 }
672+ if ( bootstrap . afterStartup . length > 0 ) {
673+ const afterStartup = Template . asString ( bootstrap . afterStartup ) + "\n" ;
674+ source . add (
675+ new PrefixSource (
676+ prefix ,
677+ useSourceMap
678+ ? new OriginalSource ( afterStartup , "webpack/after-startup" )
679+ : new RawSource ( afterStartup )
680+ )
681+ ) ;
682+ }
661683 } else {
662- const startup = Template . asString ( bootstrap . startup ) + "\n" ;
684+ const startup =
685+ Template . asString ( [
686+ ...bootstrap . beforeStartup ,
687+ ...bootstrap . startup ,
688+ ...bootstrap . afterStartup
689+ ] ) + "\n" ;
663690 source . add (
664691 new PrefixSource (
665692 prefix ,
@@ -718,7 +745,7 @@ class JavascriptModulesPlugin {
718745 /**
719746 * @param {RenderBootstrapContext } renderContext options object
720747 * @param {CompilationHooks } hooks hooks
721- * @returns {{ header: string[], startup: string[], allowInlineStartup: boolean } } the generated source of the bootstrap code
748+ * @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup : string[], allowInlineStartup: boolean } } the generated source of the bootstrap code
722749 */
723750 renderBootstrap ( renderContext , hooks ) {
724751 const { chunkGraph, moduleGraph, chunk, runtimeTemplate } = renderContext ;
@@ -751,12 +778,13 @@ class JavascriptModulesPlugin {
751778
752779 const result = {
753780 header : [ ] ,
781+ beforeStartup : [ ] ,
754782 startup : [ ] ,
783+ afterStartup : [ ] ,
755784 allowInlineStartup : true
756785 } ;
757786
758- let buf = result . header ;
759- let startup = result . startup ;
787+ let { header : buf , startup, beforeStartup, afterStartup } = result ;
760788
761789 if ( result . allowInlineStartup && moduleFactories ) {
762790 startup . push (
@@ -889,40 +917,68 @@ class JavascriptModulesPlugin {
889917 buf2 . push ( `__webpack_modules__[${ moduleIdExpr } ]();` ) ;
890918 }
891919 }
892- if ( runtimeRequirements . has ( RuntimeGlobals . startup ) ) {
920+ if (
921+ runtimeRequirements . has ( RuntimeGlobals . startup ) ||
922+ ( ( returnExportsFromRuntime ||
923+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyBefore ) ) &&
924+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyAfter ) )
925+ ) {
893926 result . allowInlineStartup = false ;
927+ buf . push ( "// the startup function" ) ;
894928 buf . push (
895- Template . asString ( [
896- "// the startup function" ,
897- `${ RuntimeGlobals . startup } = ${ runtimeTemplate . basicFunction (
898- "" ,
899- buf2
900- ) } ;`
901- ] )
929+ `${ RuntimeGlobals . startup } = ${ runtimeTemplate . basicFunction (
930+ "" ,
931+ buf2
932+ ) } ;`
902933 ) ;
903934 buf . push ( "" ) ;
904935 startup . push ( "// run startup" ) ;
905936 startup . push ( `return ${ RuntimeGlobals . startup } ();` ) ;
906- } else {
907- startup . push (
908- Template . asString ( [ "// startup" , Template . asString ( buf2 ) ] )
937+ } else if ( runtimeRequirements . has ( RuntimeGlobals . startupOnlyBefore ) ) {
938+ buf . push ( "// the startup function" ) ;
939+ buf . push (
940+ `${ RuntimeGlobals . startup } = ${ runtimeTemplate . emptyFunction ( ) } ;`
941+ ) ;
942+ beforeStartup . push ( "// run runtime startup" ) ;
943+ beforeStartup . push ( `${ RuntimeGlobals . startup } ();` ) ;
944+ startup . push ( "// startup" ) ;
945+ startup . push ( Template . asString ( buf2 ) ) ;
946+ } else if ( runtimeRequirements . has ( RuntimeGlobals . startupOnlyAfter ) ) {
947+ buf . push ( "// the startup function" ) ;
948+ buf . push (
949+ `${ RuntimeGlobals . startup } = ${ runtimeTemplate . emptyFunction ( ) } ;`
909950 ) ;
951+ startup . push ( "// startup" ) ;
952+ startup . push ( Template . asString ( buf2 ) ) ;
953+ afterStartup . push ( "// run runtime startup" ) ;
954+ afterStartup . push ( `return ${ RuntimeGlobals . startup } ();` ) ;
955+ } else {
956+ startup . push ( "// startup" ) ;
957+ startup . push ( Template . asString ( buf2 ) ) ;
910958 }
911- } else if ( runtimeRequirements . has ( RuntimeGlobals . startup ) ) {
959+ } else if (
960+ runtimeRequirements . has ( RuntimeGlobals . startup ) ||
961+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyBefore ) ||
962+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyAfter )
963+ ) {
912964 buf . push (
913- Template . asString ( [
914- "// the startup function" ,
915- "// It's empty as no entry modules are in this chunk" ,
916- `${ RuntimeGlobals . startup } = ${ runtimeTemplate . basicFunction (
917- "" ,
918- ""
919- ) } `
920- ] )
965+ "// the startup function" ,
966+ "// It's empty as no entry modules are in this chunk" ,
967+ `${ RuntimeGlobals . startup } = ${ runtimeTemplate . emptyFunction ( ) } ` ,
968+ ""
921969 ) ;
922- buf . push ( "" ) ;
923970 }
924- } else if ( runtimeRequirements . has ( RuntimeGlobals . startup ) ) {
971+ } else if (
972+ runtimeRequirements . has ( RuntimeGlobals . startup ) ||
973+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyBefore ) ||
974+ runtimeRequirements . has ( RuntimeGlobals . startupOnlyAfter )
975+ ) {
925976 result . allowInlineStartup = false ;
977+ buf . push (
978+ "// the startup function" ,
979+ "// It's empty as some runtime module handles the default behavior" ,
980+ `${ RuntimeGlobals . startup } = ${ runtimeTemplate . emptyFunction ( ) } `
981+ ) ;
926982 startup . push ( "// run startup" ) ;
927983 startup . push ( `return ${ RuntimeGlobals . startup } ();` ) ;
928984 }
0 commit comments