Skip to content

Commit

Permalink
refactor: resolve project dep func
Browse files Browse the repository at this point in the history
  • Loading branch information
fz6m committed Nov 23, 2023
1 parent 3e57e6d commit 4f6933e
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 52 deletions.
8 changes: 6 additions & 2 deletions packages/plugins/src/antd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import AntdMomentWebpackPlugin from '@ant-design/moment-webpack-plugin';
import assert from 'assert';
import { dirname, join } from 'path';
import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi';
import { deepmerge, semver, winPath } from 'umi/plugin-utils';
import {
deepmerge,
resolveProjectDep,
semver,
winPath,
} from 'umi/plugin-utils';
import { TEMPLATES_DIR } from './constants';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { withTmpPath } from './utils/withTmpPath';

const ANTD_TEMPLATES_DIR = join(TEMPLATES_DIR, 'antd');
Expand Down
9 changes: 7 additions & 2 deletions packages/plugins/src/layout.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { existsSync, readFileSync } from 'fs';
import { dirname, join } from 'path';
import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi';
import { lodash, Mustache, NpmClientEnum, winPath } from 'umi/plugin-utils';
import { resolveProjectDep } from './utils/resolveProjectDep';
import {
lodash,
Mustache,
NpmClientEnum,
resolveProjectDep,
winPath,
} from 'umi/plugin-utils';
import { withTmpPath } from './utils/withTmpPath';

// 获取所有 icons
Expand Down
3 changes: 1 addition & 2 deletions packages/plugins/src/react-query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { winPath } from '@umijs/utils';
import { dirname } from 'path';
import { IApi } from 'umi';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { resolveProjectDep, winPath } from 'umi/plugin-utils';
import { withTmpPath } from './utils/withTmpPath';

export default (api: IApi) => {
Expand Down
15 changes: 1 addition & 14 deletions packages/preset-umi/src/features/configPlugins/configPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { getSchemas as getViteSchemas } from '@umijs/bundler-vite/dist/schema';
import { getSchemas as getWebpackSchemas } from '@umijs/bundler-webpack/dist/schema';
import { resolve } from '@umijs/utils';
import { resolveProjectDep } from '@umijs/utils';
import { dirname, join } from 'path';
import type { IApi } from '../../types';
import { getSchemas as getExtraSchemas } from './schema';

function resolveProjectDep(opts: { pkg: any; cwd: string; dep: string }) {
if (
opts.pkg.dependencies?.[opts.dep] ||
opts.pkg.devDependencies?.[opts.dep]
) {
return dirname(
resolve.sync(`${opts.dep}/package.json`, {
basedir: opts.cwd,
}),
);
}
}

export default (api: IApi) => {
const { userConfig } = api;
const reactDOMPath =
Expand Down
3 changes: 2 additions & 1 deletion packages/preset-vue/src/features/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolveProjectDep } from '@umijs/utils';
import { dirname } from 'path';
import type { IApi } from 'umi';
import { resolveProjectDep, resolveVuePath } from '../utils/resolveProjectDep';
import { resolveVuePath } from '../utils/resolveVuePath';

export default (api: IApi) => {
api.describe({
Expand Down
30 changes: 0 additions & 30 deletions packages/preset-vue/src/utils/resolveProjectDep.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/preset-vue/src/utils/resolveVuePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { resolveProjectDep } from '@umijs/utils';
import { dirname, join } from 'path';

export function resolveVuePath(opts: { pkg: any; cwd: string; path: string }) {
const vuePkgPath =
resolveProjectDep({
pkg: opts.pkg,
cwd: opts.cwd,
dep: 'vue',
}) || dirname(require.resolve('vue/package.json'));

return join(vuePkgPath, opts.path);
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export * from './npmClient';
export * from './randomColor/randomColor';
export * from './readDirFiles';
export * as register from './register';
export * from './resolveProjectDep';
export * from './setNoDeprecation';
export * from './tryPaths';
export * from './winPath';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'path';
import { resolve } from 'umi/plugin-utils';
import resolve from '../compiled/resolve';

export function resolveProjectDep(opts: {
pkg: any;
Expand Down

0 comments on commit 4f6933e

Please sign in to comment.