Skip to content

Commit

Permalink
feat(cli): 组件库支持import npm包组件
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Aug 30, 2024
1 parent 7e18bd8 commit 00e1463
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const scripts = (defaultAppConfig: UserConfig) => {
},
};

// create vuepress app
const app = new App(appConfig);

// clean temp and cache
Expand Down
20 changes: 13 additions & 7 deletions packages/cli/src/utils/resolveAppPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
});
}
});
Expand Down Expand Up @@ -452,9 +458,9 @@ const getDependencies = (dependencies: Record<string, string>, 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);

Expand All @@ -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', '');
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};

0 comments on commit 00e1463

Please sign in to comment.