From 5ed534e36491e196a94e0bb91fa32c810a525242 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 8 Aug 2024 20:45:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E7=BB=84=E4=BB=B6=E5=BA=93?= =?UTF-8?q?=E6=94=AF=E6=8C=81import=20npm=E5=8C=85=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/commands/index.ts | 1 - packages/cli/src/utils/resolveAppPackages.ts | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index 4445e326..0b7dd97f 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -34,7 +34,6 @@ export const scripts = (defaultAppConfig: UserConfig) => { }, }; - // create vuepress app const app = new App(appConfig); // clean temp and cache diff --git a/packages/cli/src/utils/resolveAppPackages.ts b/packages/cli/src/utils/resolveAppPackages.ts index dee9d367..b64aa8e1 100644 --- a/packages/cli/src/utils/resolveAppPackages.ts +++ b/packages/cli/src/utils/resolveAppPackages.ts @@ -2,7 +2,7 @@ import { execSync } from 'child_process'; import path from 'path'; import { exit } from 'process'; -import fs from 'fs-extra'; +import fs, { existsSync } from 'fs-extra'; import * as recast from 'recast'; import type App from '../Core'; @@ -271,10 +271,16 @@ const getComponentPackageImports = function ({ }); if (propertyMatch) { + let file = getIndexPath(path.resolve(path.dirname(indexPath), propertyMatch.source.value)); + + if (!existsSync(file)) { + file = propertyMatch.source.value; + } + result.imports.push({ type: property.key.name ?? property.key.value, name: propertyMatch.specifiers[0].local.name, - indexPath: getIndexPath(path.resolve(path.dirname(indexPath), propertyMatch.source.value)), + indexPath: file, }); } }); @@ -452,9 +458,9 @@ const getDependencies = (dependencies: Record, packagePath: stri dependencies[moduleName] = version; }; -const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string, key?: string) => { +const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string, cwd: string, key?: string) => { const { options } = app; - const { temp, source, componentFileAffix, datasoucreSuperClass } = options; + const { temp, componentFileAffix, datasoucreSuperClass } = options; let { name: moduleName } = splitNameVersion(packagePath); @@ -474,7 +480,7 @@ const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string // 获取完整路径 const indexPath = execSync(`node -e "console.log(require.resolve('${moduleName.replace(/\\/g, '/')}'))"`, { - cwd: source, + cwd, }) .toString() .replace('\n', ''); @@ -486,7 +492,7 @@ const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string // 组件&插件&数据源包 if (result.type === PackageType.COMPONENT_PACKAGE) { result.imports.forEach((i) => { - setPackages(packages, app, i.indexPath, i.type); + setPackages(packages, app, i.indexPath, moduleName!, i.type); }); return; @@ -570,7 +576,7 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => { dsValueMap: {}, }; - packagePaths.forEach(([packagePath, key]) => setPackages(packagesMap, app, packagePath, key)); + packagePaths.forEach(([packagePath, key]) => setPackages(packagesMap, app, packagePath, source, key)); return packagesMap; };