diff --git a/packages/plugins/src/antd.ts b/packages/plugins/src/antd.ts index 0e29a20cbc86..e7cf1ceece29 100644 --- a/packages/plugins/src/antd.ts +++ b/packages/plugins/src/antd.ts @@ -3,13 +3,9 @@ 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, - resolveProjectDep, - semver, - winPath, -} from 'umi/plugin-utils'; +import { deepmerge, 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'); diff --git a/packages/plugins/src/layout.ts b/packages/plugins/src/layout.ts index 674d4ee07351..796dd20b8dd0 100644 --- a/packages/plugins/src/layout.ts +++ b/packages/plugins/src/layout.ts @@ -1,13 +1,8 @@ import { existsSync, readFileSync } from 'fs'; import { dirname, join } from 'path'; import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi'; -import { - lodash, - Mustache, - NpmClientEnum, - resolveProjectDep, - winPath, -} from 'umi/plugin-utils'; +import { lodash, Mustache, NpmClientEnum, winPath } from 'umi/plugin-utils'; +import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; // 获取所有 icons diff --git a/packages/plugins/src/react-query.ts b/packages/plugins/src/react-query.ts index 3e624d370927..ae07f5cb3151 100644 --- a/packages/plugins/src/react-query.ts +++ b/packages/plugins/src/react-query.ts @@ -1,6 +1,7 @@ +import { winPath } from '@umijs/utils'; import { dirname } from 'path'; import { IApi } from 'umi'; -import { resolveProjectDep, winPath } from 'umi/plugin-utils'; +import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; export default (api: IApi) => { diff --git a/packages/utils/src/resolveProjectDep.ts b/packages/plugins/src/utils/resolveProjectDep.ts similarity index 88% rename from packages/utils/src/resolveProjectDep.ts rename to packages/plugins/src/utils/resolveProjectDep.ts index 7b1406712e9b..ab487064faa9 100644 --- a/packages/utils/src/resolveProjectDep.ts +++ b/packages/plugins/src/utils/resolveProjectDep.ts @@ -1,5 +1,5 @@ import { dirname } from 'path'; -import resolve from '../compiled/resolve'; +import { resolve } from 'umi/plugin-utils'; export function resolveProjectDep(opts: { pkg: any; diff --git a/packages/preset-umi/src/features/configPlugins/configPlugins.ts b/packages/preset-umi/src/features/configPlugins/configPlugins.ts index 98835113ba43..2034004d5bef 100644 --- a/packages/preset-umi/src/features/configPlugins/configPlugins.ts +++ b/packages/preset-umi/src/features/configPlugins/configPlugins.ts @@ -1,10 +1,23 @@ import { getSchemas as getViteSchemas } from '@umijs/bundler-vite/dist/schema'; import { getSchemas as getWebpackSchemas } from '@umijs/bundler-webpack/dist/schema'; -import { resolveProjectDep } from '@umijs/utils'; +import { resolve } 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 = diff --git a/packages/preset-vue/src/features/default.ts b/packages/preset-vue/src/features/default.ts index 62e9a0f683f3..c858afe537d2 100644 --- a/packages/preset-vue/src/features/default.ts +++ b/packages/preset-vue/src/features/default.ts @@ -1,7 +1,6 @@ -import { resolveProjectDep } from '@umijs/utils'; import { dirname } from 'path'; import type { IApi } from 'umi'; -import { resolveVuePath } from '../utils/resolveVuePath'; +import { resolveProjectDep, resolveVuePath } from '../utils/resolveProjectDep'; export default (api: IApi) => { api.describe({ diff --git a/packages/preset-vue/src/utils/resolveProjectDep.ts b/packages/preset-vue/src/utils/resolveProjectDep.ts new file mode 100644 index 000000000000..0ab48775ba1b --- /dev/null +++ b/packages/preset-vue/src/utils/resolveProjectDep.ts @@ -0,0 +1,30 @@ +import { dirname, join } from 'path'; +import { resolve } from 'umi/plugin-utils'; + +export 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 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); +} diff --git a/packages/preset-vue/src/utils/resolveVuePath.ts b/packages/preset-vue/src/utils/resolveVuePath.ts deleted file mode 100644 index 2765e56723e4..000000000000 --- a/packages/preset-vue/src/utils/resolveVuePath.ts +++ /dev/null @@ -1,13 +0,0 @@ -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); -} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 8b7b4e24ca09..574ac11cebe9 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -50,7 +50,6 @@ 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';