@@ -113,7 +113,7 @@ const getCssModulesPlugin = memoize(() => require("../css/CssModulesPlugin"));
113113 * @property {boolean= } remove set true to delete the whole element
114114 */
115115/** @typedef {{ outputName: string, html: string } } HtmlTransformTagsContext */
116- /** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined } } HtmlEntrySpec */
116+ /** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined, page: boolean } } HtmlEntrySpec */
117117
118118const createCompilationHooks = ( ) => ( {
119119 /**
@@ -686,7 +686,8 @@ class HtmlModulesPlugin {
686686 request : entry . request ,
687687 name : entry . entryName ,
688688 index : entry . index ,
689- dependOn
689+ dependOn,
690+ page : groupKind === "html"
690691 } ) ;
691692 }
692693 }
@@ -770,8 +771,8 @@ class HtmlModulesPlugin {
770771 holder = compilation . moduleGraph . getIssuer ( holder ) ;
771772 }
772773 for ( const entry of ordered ) {
773- // A name the page or `output. html.chunkName` asked for is what the
774- // file is called too; otherwise the tag's url lends its basename.
774+ // A name the page or `module.parser. html.chunkName` asked for is what
775+ // the file is called too; otherwise the tag's url lends its basename.
775776 const base =
776777 wrapsEntry && namedEntry
777778 ? namedEntry . name
@@ -789,7 +790,9 @@ class HtmlModulesPlugin {
789790 for ( const link of links ) {
790791 // A linked page is named after its own path; a name the page asked
791792 // for replaces that, and the link to it is rewritten either way.
792- if ( link . chunkName ) pageNames . set ( link . entryName , link . chunkName ) ;
793+ if ( link . chunkName && ! contestedPageNames . has ( link . entryName ) ) {
794+ pageNames . set ( link . entryName , link . chunkName ) ;
795+ }
793796 const page = linkedPages . get ( link . entryName ) ;
794797 if ( page ) recordNames ( page , seen ) ;
795798 }
@@ -815,6 +818,28 @@ class HtmlModulesPlugin {
815818 ) ;
816819 } ) ;
817820
821+ // What each page name was claimed for, so a second page asking for a
822+ // taken one is refused rather than appended to the entry holding it.
823+ /** @type {Map<string, string> } */
824+ const pageRequests = new Map ( ) ;
825+ // Names a second page asked for and did not get, so it keeps the one its
826+ // own path lends and emits beside the page holding the name.
827+ /** @type {Set<string> } */
828+ const contestedPageNames = new Set ( ) ;
829+ /**
830+ * @param {string } name the name two things ask for
831+ * @param {string } taken what holds it
832+ * @param {string } request what asked for it
833+ */
834+ const reportNameTaken = ( name , taken , request ) => {
835+ contestedPageNames . add ( name ) ;
836+ compilation . errors . push (
837+ new WebpackError (
838+ `The name "${ name } " is asked for by ${ request } and is already taken by ${ taken } . A page is emitted as its name, so give one of them another.`
839+ )
840+ ) ;
841+ } ;
842+
818843 // Create one HTML module's entries. A `type: "html"` link is itself an
819844 // HTML entry, so after it builds we recurse into it — the same handling
820845 // every HTML entry already gets. `processed` guards diamonds and cycles.
@@ -826,7 +851,32 @@ class HtmlModulesPlugin {
826851 processed . add ( module ) ;
827852 await Promise . all (
828853 collectEntrySpecs ( module ) . map ( async ( spec ) => {
854+ if ( spec . page ) {
855+ const claimed = pageRequests . get ( spec . name ) ;
856+ // The page holding the name is already an entry of its own:
857+ // a second link to it adds nothing, a second page cannot have it.
858+ if ( claimed !== undefined ) {
859+ if ( claimed !== spec . request ) {
860+ reportNameTaken ( spec . name , claimed , spec . request ) ;
861+ }
862+ return ;
863+ }
864+ pageRequests . set ( spec . name , spec . request ) ;
865+ }
829866 const entryModule = await addEntry ( spec ) ;
867+ // A page entry webpack made for us holds that page alone; anything
868+ // else in it named the same and would be emitted over.
869+ if ( spec . page ) {
870+ const entry = compilation . entries . get ( spec . name ) ;
871+ if ( entry && entry . dependencies . length !== 1 ) {
872+ reportNameTaken (
873+ spec . name ,
874+ `entry "${ spec . name } "` ,
875+ spec . request
876+ ) ;
877+ return ;
878+ }
879+ }
830880 if ( entryModule && entryModule . type === HTML_MODULE_TYPE ) {
831881 linkedPages . set ( spec . name , entryModule ) ;
832882 await processEntry ( entryModule ) ;
@@ -1262,9 +1312,8 @@ class HtmlModulesPlugin {
12621312 resolvedSentinelHashCache . set ( placeholderSource , cached ) ;
12631313 }
12641314 const resource = module . getResource ( ) || module . resource ;
1265- // A page is named after its own path, unless it asked for a name;
1266- // a synthetic `output.html` entry is a `data:` module with no path,
1267- // so it falls back to the entry that named it.
1315+ // A page is named after its own path, unless it asked for a name; a
1316+ // synthetic `output.html` entry has none, so its entry names it.
12681317 const requestedPageName = chunk . name
12691318 ? getState ( compilation ) . pageNames . get ( chunk . name )
12701319 : undefined ;
0 commit comments