forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset-umi): support monorepo redirect (umijs#427)
* wip(monorepo): add redirect feature [no ci] * dep: add workspaces utils dep * docs: add monorepo redirect desc * chore: update lock file * chore(preset-umi): replace all `joi` to `Joi` * chore: not handle `false` value * chore: replace `source` to `srcDir` * chore: auto exclude `umi` in local dev * chore: display throw error when not monorepo scene * chore: rename collect method name * dep: precompiled `@manypkg/get-packages` * fix: add type for getPackages method * chore: exclude pkg name * chore: update lock file * chore: update lock file * docs: `source` to `srcDir`
- Loading branch information
Showing
9 changed files
with
371 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/preset-umi/compiled/@manypkg/get-packages/index.js
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/preset-umi/compiled/@manypkg/get-packages/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"@manypkg/get-packages","license":"MIT"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// @ts-ignore | ||
import { getPackages } from '../../../compiled/@manypkg/get-packages'; | ||
import { logger } from '@umijs/utils'; | ||
import { pkgUp } from '@umijs/utils/compiled/pkg-up'; | ||
import assert from 'assert'; | ||
import { existsSync, statSync } from 'fs'; | ||
import { dirname, join } from 'path'; | ||
import type { IApi } from '../../types'; | ||
|
||
interface IConfigs { | ||
srcDir?: string[]; | ||
exclude?: RegExp[]; | ||
} | ||
|
||
export default (api: IApi) => { | ||
api.describe({ | ||
key: 'monorepoRedirect', | ||
config: { | ||
schema(Joi) { | ||
return Joi.alternatives( | ||
Joi.boolean(), | ||
Joi.object({ | ||
srcDir: Joi.array().items(Joi.string()), | ||
exclude: Joi.array().items(Joi.object().instance(RegExp)), | ||
}), | ||
); | ||
}, | ||
}, | ||
enableBy: api.EnableBy.config, | ||
}); | ||
|
||
api.modifyConfig(async (memo) => { | ||
const rootPkg = await pkgUp({ cwd: dirname(api.cwd) }); | ||
if (!rootPkg) return memo; | ||
const root = dirname(rootPkg); | ||
assert( | ||
isMonorepo({ root }), | ||
`The 'monorepoRedirect' option can only be used in monorepo, you don't need configure.`, | ||
); | ||
|
||
const config: IConfigs = memo.monorepoRedirect || {}; | ||
const { exclude = [], srcDir = ['src'] } = config; | ||
// Note: not match `umi` package in local dev | ||
if (__filename.includes(`packages/preset-umi`)) { | ||
logger.info( | ||
`[monorepoRedirect]: Auto excluded 'umi' package in local dev scene`, | ||
); | ||
exclude.push(/^umi$/); | ||
} | ||
// collect use workspace deps | ||
const usingDeps = collectPkgDeps(api.pkg).filter((name) => { | ||
return !exclude.some((reg) => reg.test(name)); | ||
}); | ||
if (!usingDeps.length) return memo; | ||
// collect all project | ||
const projects = await collectAllProjects({ root }); | ||
const alias = usingDeps.reduce<Record<string, string>>((obj, name) => { | ||
const root = projects[name]; | ||
if (!root) { | ||
return obj; | ||
} | ||
srcDir.some((dirName) => { | ||
const dirPath = join(root, dirName); | ||
if (existsSync(dirPath) && statSync(dirPath).isDirectory()) { | ||
// redirect to source dir | ||
obj[name] = dirPath; | ||
return true; | ||
} | ||
}); | ||
return obj; | ||
}, {}); | ||
memo.alias = { | ||
...memo.alias, | ||
...alias, | ||
}; | ||
|
||
return memo; | ||
}); | ||
}; | ||
|
||
interface IOpts { | ||
root: string; | ||
} | ||
|
||
interface IProject { | ||
packageJson: Record<string, any>; | ||
dir: string; | ||
} | ||
|
||
const DEP_KEYS = ['devDependencies', 'dependencies']; | ||
function collectPkgDeps(pkg: Record<string, any>) { | ||
const deps: string[] = []; | ||
DEP_KEYS.forEach((type) => { | ||
deps.push(...Object.keys(pkg?.[type] || {})); | ||
}); | ||
return deps; | ||
} | ||
|
||
async function collectAllProjects(opts: IOpts) { | ||
const workspaces = await getPackages(opts.root); | ||
return workspaces.packages.reduce<Record<string, string>>( | ||
(obj: Record<string, string>, pkg: IProject) => { | ||
const name = pkg.packageJson?.name; | ||
if (name) { | ||
obj[name] = pkg.dir; | ||
} | ||
return obj; | ||
}, | ||
{}, | ||
); | ||
} | ||
|
||
const MONOREPO_FILE = ['pnpm-workspace.yaml', 'lerna.json']; | ||
function isMonorepo(opts: IOpts) { | ||
const pkgExist = existsSync(join(opts.root, 'package.json')); | ||
return ( | ||
pkgExist && | ||
MONOREPO_FILE.some((file) => { | ||
return existsSync(join(opts.root, file)); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.