NewLife/cube-front

refactor(plugin): 优化路径解析逻辑和错误处理
笑笑 authored at 2026-05-17 09:24:56
0a0593c
Tree
1 Parent(s) a92338f
Summary: 1 changed files with 16 additions and 7 deletions.
Modified +16 -7
Modified +16 -7
diff --git a/core/plugin/index.ts b/core/plugin/index.ts
index e67aec5..f41019c 100644
--- a/core/plugin/index.ts
+++ b/core/plugin/index.ts
@@ -176,8 +176,8 @@ export default function vitePluginCubeFront() {
 
           // 判断当前是否在 cube-front 源码目录运行
           // 如果 config.root 包含 'cube-front' 但不是直接运行,说明是外部项目引用
-          const isExternalProject = !config.root.endsWith('cube-front') &&
-            config.root.includes('cube-front');
+          const isExternalProject =
+            !config.root.endsWith('cube-front') && config.root.includes('cube-front');
 
           // 构建应用配置代码
           const microAppConfigsStrList = microAppConfigs.map((app) => {
@@ -185,10 +185,14 @@ export default function vitePluginCubeFront() {
 
             if (app.packageName) {
               // 路径解析规则:
-              // 1. 以 apps 或 ./apps 开头 → 本项目路径,直接使用
+              // 1. 以 apps 或 /apps 或 ./apps 开头 → 本项目路径,直接使用
               // 2. 否则第一段视为包名,去 node_modules/{包名} 查找
               const pkgName = app.packageName;
-              if (pkgName.startsWith('apps') || pkgName.startsWith('./apps')) {
+              if (
+                pkgName.startsWith('apps') ||
+                pkgName.startsWith('/apps') ||
+                pkgName.startsWith('./apps')
+              ) {
                 // 本项目路径
                 importPath = `./${pkgName.replace(/^\.\//, '')}/src/main.ts`;
               } else {
@@ -206,12 +210,17 @@ export default function vitePluginCubeFront() {
                 }
 
                 // 验证包是否存在
-                const packageMainPath = path.resolve(config.root, 'node_modules', packageName, 'package.json');
+                const packageMainPath = path.resolve(
+                  config.root,
+                  'node_modules',
+                  packageName,
+                  'package.json',
+                );
                 if (!fs.existsSync(packageMainPath)) {
                   throw new Error(
                     `[CubeFront] 微应用 ${app.name} 的包 "${packageName}" 未找到。\n` +
-                    `请确保已安装:pnpm add ${packageName}\n` +
-                    `或检查 node_modules 中是否存在该包。`
+                      `请确保已安装:pnpm add ${packageName}\n` +
+                      `或检查 node_modules 中是否存在该包。`,
                   );
                 }