Skip to content

Commit

Permalink
feat: build support auto-sorting & filter for lerna project (umijs#393)
Browse files Browse the repository at this point in the history
* chore: add yarn workspaces

* feat: add getLernaPackages

* feat: getLernaPackages replace getPackages

* feat: 支持配置getLernaPackages参数

* chore: update lerna.json

* feat: schema add filterPkgs

* test: 修复测试报错

* feat: filterPkgs rename pkgFilter

* feat: getLernaPackages remove stream option

* test: 修复测试报错

* feat: showPrivate => buildPrivate

* docs: add pkgFilter doc

* docs: 统一文档注释格式

* style: update comment style

* feat: buildPrivate >> skipPrivate

Co-authored-by: Peach <scdzwyxst@gmail.com>
  • Loading branch information
wangxingkang and PeachScript authored Aug 19, 2021
1 parent a23b823 commit 206d042
Show file tree
Hide file tree
Showing 40 changed files with 467 additions and 22 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,61 @@ export default {
};
```

### pkgFilter

在 lerna 构建中,有需要对包进行过滤的需求

可配置项如下:

```ts
{
/**
* 指定包含的包
*/
include?: string[];
/**
* 指定排除的包
*/
exclude?: string[];
/**
* 是否跳过私有的包 package.json private
* @default false
*/
skipPrivate?: boolean;
}
```

`pkgFilter` 允许自定义排除/包含部分包,只对过滤后的包进行构建

例如: 存在 `@umi/util-1``@umi/util-2``@umi/core``@umi/test`(private) 多个包

```ts
// 只构建 `@umi/util-1`、`@umi/util-2`
export default {
pkgFilter: {
include: ['@umi/util-*']
}
}

// 只构建 `@umi/util-1`、`@umi/core`、`@umi/test`
export default {
pkgFilter: {
exclude: ['@umi/util-2']
}
}

// 只构建 `@umi/util-1`、`@umi/util-2`、`@umi/core`
export default {
pkgFilter: {
skipPrivate: true
}
}
```

### pkgs

**已提供根据依赖自动排序的功能,除非有特殊需求定制构建顺序的,此配置项可忽略**

在 lerna 构建中,有可能出现组件间有构建先后的需求,比如在有两个包 `packages/father-a``packages/father-util`,在 `father-a` 中对 `father-util` 有依赖,此时需要先构建 `father-util` 才能保证构建的正确性

`pkgs` 允许你自定义 packages 目录下的构建顺序,如以上场景对应的配置为
Expand Down
4 changes: 3 additions & 1 deletion packages/father-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"@babel/preset-react": "7.12.1",
"@babel/preset-typescript": "7.12.1",
"@babel/register": "7.12.1",
"@lerna/project": "^3.21.0",
"@lerna/filter-packages": "4.0.0",
"@lerna/project": "4.0.0",
"@lerna/query-graph": "4.0.0",
"@rollup/plugin-babel": "5.2.1",
"@rollup/plugin-commonjs": "16.0.0",
"@rollup/plugin-inject": "4.0.2",
Expand Down
5 changes: 2 additions & 3 deletions packages/father-build/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import * as assert from 'assert';
import { merge } from 'lodash';
import signale from 'signale';
import chalk from 'chalk';
import { getPackages } from '@lerna/project';
import { IOpts, IBundleOptions, IBundleTypeOutput, ICjs, IEsm, Dispose } from './types';
import babel from './babel';
import rollup from './rollup';
import registerBabel from './registerBabel';
import { getExistFile } from './utils';
import { getExistFile, getLernaPackages } from './utils';
import getUserConfig, { CONFIG_FILES } from './getUserConfig';
import randomColor from "./randomColor";

Expand Down Expand Up @@ -194,7 +193,7 @@ export async function buildForLerna(opts: IOpts) {

const userConfig = merge(getUserConfig({ cwd }), rootConfig, buildArgs);

let pkgs = await getPackages(cwd);
let pkgs = await getLernaPackages(cwd, userConfig.pkgFilter);

// support define pkgs in lerna
// TODO: 使用lerna包解决依赖编译问题
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "@hoo/bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "foo"
}
1 change: 1 addition & 0 deletions packages/father-build/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const successValidates = {
doc: [{}],
typescriptOpts: [{}],
pkgs: [[]],
pkgFilter: [{}],
};

Object.keys(successValidates).forEach(key => {
Expand Down
3 changes: 3 additions & 0 deletions packages/father-build/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,8 @@ export default {
pkgs: {
type: 'array',
},
pkgFilter: {
type: 'object',
},
},
};
12 changes: 12 additions & 0 deletions packages/father-build/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ export interface IBundleOptions {
[opt: string]: any
};
pkgs?: string[];
/** 处理 lerna 包 */
pkgFilter?: {
/** 指定包含的包 */
include?: string[];
/** 指定排除的包 */
exclude?: string[];
/**
* 跳过私有的包 package.json private
* @default false
* */
skipPrivate?: boolean;
}
}

export interface IOpts {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "core1",
"version": "1.0.0",
"dependencies": {
"foo": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('bar');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "core2",
"private": true,
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packages": [
"core/*",
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "default-packages",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "bar",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('bar');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo",
"version": "1.0.0",
"dependencies": {
"bar": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "default-packages",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "bar",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('bar');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo",
"version": "1.0.0",
"dependencies": {
"bar": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('foo');
83 changes: 83 additions & 0 deletions packages/father-build/src/utils/getLernaPackages/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import path from 'path';
import fs from 'fs-extra';
import { getLernaPackages } from './';

export const FIXTURES_DIR = path.resolve(__dirname, 'fixtures');

export function getDirs() {
return fs
.readdirSync(FIXTURES_DIR)
.filter(fixturePath =>
fs.statSync(path.resolve(FIXTURES_DIR, fixturePath)).isDirectory(),
);
}

export function fixture(...args: string[]) {
return path.join(FIXTURES_DIR, ...args)
}

describe('default', () => {
const fixturePath = fixture('default');

it('获取所有的包', async () => {
expect.assertions(1);

const pkgs = await getLernaPackages(fixturePath);

const pkgNames = ['bar', 'foo'];

expect(pkgNames).toEqual(pkgs.map(item => item.name));
})
});

describe('customize', () => {
const fixturePath = fixture('customize');

it('获取所有的包', async () => {
expect.assertions(1);

const pkgs = await getLernaPackages(fixturePath, {});

const pkgNames = ['core2', 'bar', 'foo', 'core1'];

expect(pkgNames).toEqual(pkgs.map(item => item.name));
});

it('过滤私有的包', async () => {
expect.assertions(1);

const pkgs = await getLernaPackages(fixturePath, {
skipPrivate: true,
});

const pkgNames = ['bar', 'foo', 'core1'];

expect(pkgNames).toEqual(pkgs.map(item => item.name));
});

it('设置包含部分包', async () => {
expect.assertions(1);

const pkgs = await getLernaPackages(fixturePath, {
include: [
'core*'
]
});
const pkgNames = ['core1', 'core2'];

expect(pkgNames).toEqual(pkgs.map(item => item.name));
})

it('设置包含部分包', async () => {
expect.assertions(1);

const pkgs = await getLernaPackages(fixturePath, {
exclude: [
'core1'
]
});
const pkgNames = ['core2', 'bar', 'foo'];

expect(pkgNames).toEqual(pkgs.map(item => item.name));
})
});
Loading

0 comments on commit 206d042

Please sign in to comment.