From db19eb3d25cd5e98f51096953279cce5946fe396 Mon Sep 17 00:00:00 2001
From: xuhongbin <xuhongbin@yy.com>
Date: Tue, 30 Jul 2024 14:35:33 +0800
Subject: [PATCH] fix: add library to fix module federation hmr

---
 src/index.ts | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/index.ts b/src/index.ts
index 890dc1cb3..325327c84 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -52,6 +52,11 @@ export interface VueLoaderOptions {
   customElement?: boolean | RegExp
 
   hotReload?: boolean
+  /**
+   * To address the issue of identical component names in different projects
+   * module federation
+   */
+  library?: string
   exposeFilename?: boolean
   appendExtension?: boolean
   enableTsInTemplate?: boolean
@@ -137,7 +142,7 @@ export default function loader(
     })
     return ``
   }
-
+  const hmrPrifix = options.library ? options.library + '_' : ''
   // module id for scoped CSS & hot-reload
   const rawShortFilePath = path
     .relative(rootContext || process.cwd(), filename)
@@ -146,7 +151,7 @@ export default function loader(
   const id = hash(
     isProduction
       ? shortFilePath + '\n' + source.replace(/\r\n/g, '\n')
-      : shortFilePath
+      : hmrPrifix + shortFilePath
   )
 
   // if the query has a type field, this is a language block request
@@ -316,7 +321,7 @@ export default function loader(
     // from the devtools.
     propsToAttach.push([
       `__file`,
-      JSON.stringify(rawShortFilePath.replace(/\\/g, '/')),
+      JSON.stringify(hmrPrifix + rawShortFilePath.replace(/\\/g, '/')),
     ])
   } else if (options.exposeFilename) {
     // Libraries can opt-in to expose their components' filenames in production builds.