Skip to content

Commit

Permalink
feat: copy config support from-to (umijs#5440)
Browse files Browse the repository at this point in the history
* feat: copy config support from-to

* docs: config copy docs

* fix: type error
  • Loading branch information
xiaohuoni authored Sep 18, 2020
1 parent 87a16f6 commit d6b8fc0
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 9 deletions.
34 changes: 33 additions & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default {

## copy

* Type: `Array(string)`
* Type: `Array(string|{from:string,to:string})`
* Default: `[]`

设置要复制到输出目录的文件或文件夹。
Expand Down Expand Up @@ -285,6 +285,38 @@ export default {
- foo.js
```

支持配置 from-to, 需要注意的是 from 是相对于 cwd 的路径,to是相对于输出路径的路径。

比如你的目录结构如下,

```js
+ src
- index.ts
+ bar
- bar.js
```

然后设置,

```js
export default {
copy: [
{
from:'bar/bar.js',
to:'some/bar.js'
}
]
}
```

编译完成后,会额外输出以下文件,

```js
+ dist
+ some
- bar.js
```

## define

* Type: `object`
Expand Down
32 changes: 32 additions & 0 deletions docs/config/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,38 @@ export default {
- foo.js
```

支持配置 from-to, 需要注意的是 from 是相对于 cwd 的路径,to是相对于输出路径的路径。

比如你的目录结构如下,

```js
+ src
- index.ts
+ bar
- bar.js
```

然后设置,

```js
export default {
copy: [
{
from:'bar/bar.js',
to:'some/bar.js'
}
]
}
```

编译完成后,会额外输出以下文件,

```js
+ dist
+ some
- bar.js
```

## define

* Type: `object`
Expand Down
Empty file.
10 changes: 9 additions & 1 deletion packages/bundler-webpack/src/fixtures/copy/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export default {
copy: [
'b.ts'
'b.ts',
{
from: 'c.ts',
to: ''
},
{
from: 'utils/d.ts',
to: ''
}
]
}
2 changes: 2 additions & 0 deletions packages/bundler-webpack/src/fixtures/copy/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export default ({ files }: IExpectOpts) => {
expect(files).toContain(`a.ts`);
expect(files).toContain(`b.ts`);
expect(files).toContain(`index.js`);
expect(files).toContain(`c.ts`);
expect(files).toContain(`d.ts`);
}
Empty file.
23 changes: 18 additions & 5 deletions packages/bundler-webpack/src/getConfig/getConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { IConfig, IBundlerConfigType, BundlerConfigType } from '@umijs/types';
import {
IConfig,
IBundlerConfigType,
BundlerConfigType,
ICopy,
} from '@umijs/types';
import defaultWebpack from 'webpack';
import Config from 'webpack-chain';
import { join } from 'path';
Expand Down Expand Up @@ -364,10 +369,18 @@ export default async function getConfig(
to: absOutputPath,
},
...(config.copy
? config.copy.map((from) => ({
from: join(cwd, from),
to: absOutputPath,
}))
? config.copy.map((item: ICopy | string) => {
if (typeof item === 'string') {
return {
from: join(cwd, item),
to: absOutputPath,
};
}
return {
from: join(cwd, item.from),
to: join(absOutputPath, item.to),
};
})
: []),
].filter(Boolean),
]);
Expand Down
10 changes: 9 additions & 1 deletion packages/preset-built-in/src/plugins/features/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ export default (api: IApi) => {
key: 'copy',
config: {
schema(joi) {
return joi.array().items(joi.string());
return joi.array().items(
joi.alternatives(
joi.object({
from: joi.string(),
to: joi.string(),
}),
joi.string(),
),
);
},
},
});
Expand Down
7 changes: 6 additions & 1 deletion packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ interface ISSR {
staticMarkup?: boolean;
}

export interface ICopy {
from: string;
to: string;
}

export interface BaseIConfig extends IConfigCore {
alias?: {
[key: string]: string;
Expand All @@ -288,7 +293,7 @@ export interface BaseIConfig extends IConfigCore {
cssLoader?: object;
cssModulesTypescriptLoader?: { mode: 'verify' | 'emit' };
cssnano?: object;
copy?: string[];
copy?: [string, ICopy];
define?: {
[key: string]: any;
};
Expand Down

0 comments on commit d6b8fc0

Please sign in to comment.