Skip to content

Commit

Permalink
fix(umi-plugin): symlink error when using in grouped package
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Dec 23, 2019
1 parent e809975 commit 32590d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
22 changes: 8 additions & 14 deletions packages/umi-plugin-father-doc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import { exec } from 'child_process';
import { isPlainObject } from 'lodash';
import { IApi, IRoute } from 'umi-types';
import getRouteConfig from './routes/getRouteConfig';
Expand Down Expand Up @@ -118,24 +119,17 @@ export default function(api: IApi, opts: IFatherDocOpts) {
// add alias for current package(s)
getHostPkgAlias(api.paths).forEach(([pkgName, pkgPath]) => {
const srcPath = path.join(pkgPath, 'src');
// 如果 src 存在,alias设置为 src
if (fs.existsSync(srcPath)) {
config.resolve.alias.set(pkgName, srcPath);
return;

// use src path instead of main field in package.json if exists
config.resolve.alias.set(pkgName, fs.existsSync(srcPath) ? srcPath : pkgPath);

// link current pkgs into node_modules, for import module resolve when writing demo
if (!fs.existsSync(path.join(api.paths.cwd, 'node_modules', pkgName))) {
exec(`npm ln ${pkgName} ${pkgPath}`);
}
config.resolve.alias.set(pkgName, pkgPath);
});
});

// link pkg to node node_modules
// all for eslint and ts
const nodeModulesPath = path.join(api.paths.cwd, 'node_modules');
const packageNodeModulesPath = path.join(nodeModulesPath, api.pkg.name);
if (fs.existsSync(nodeModulesPath) && api.pkg.name && !fs.existsSync(packageNodeModulesPath)) {
// mk link pkg to node_modules/pkg
fs.symlinkSync(api.paths.cwd, packageNodeModulesPath, 'dir');
}

// modify help info
api._modifyHelpInfo(memo => {
memo.scriptName = 'father-doc';
Expand Down
4 changes: 1 addition & 3 deletions packages/umi-plugin-father-doc/src/utils/getHostPkgAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import glob from 'glob';
import { IApi } from 'umi-types';

function getPkgAliasForPath(absPath: string) {
const result = [undefined, absPath];
const result = [path.basename(absPath), absPath];
const pkgPath = path.join(absPath, 'package.json');

// use package.name if exists
if (fs.existsSync(pkgPath)) {
result[0] = require(pkgPath).name;
} else {
result[0] = path.basename(absPath);
}

return result;
Expand Down

0 comments on commit 32590d0

Please sign in to comment.