Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
feat: support to configure export name for umd (#61)
Browse files Browse the repository at this point in the history
* feat: umd name

* docs: umd name

* chore: typo

* docs: name default

* docs: tip

* docs: typo

* Update src/builder/bundle/index.ts

Co-authored-by: Peach <[email protected]>

* feat: name optional

Co-authored-by: xiaohuoni <[email protected]>
Co-authored-by: Peach <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2022
1 parent 3171ea9 commit e1bf838
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ export default {

配置将源码打包为 UMD 产物,支持以下子配置项,也支持覆盖外部的公共配置项。

#### name

- 类型:`string`
- 默认值:无

指定 umd 包的导出 library 名称,例如:

```ts
export default {
umd: {
name: 'fatherDemo',
},
};
```

默认是全量导出 member exports,需要拆解 `default` 的话,可以通过 `chainWebpack` 配置修改 `libraryExport`,例如:

```ts
export default {
umd: {
name: 'fatherDemo',
chainWebpack: (memo: any) => {
memo.output.libraryExport('default');
return memo;
},
},
};
```

#### entry

- 类型:`string` | `Record<string, Config>`
Expand Down
4 changes: 4 additions & 0 deletions src/builder/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export default async (opts: {
chainWebpack(memo: any) {
memo.output.libraryTarget('umd');

if (config?.name) {
memo.output.library(config.name);
}

// modify webpack target
if (config.platform === 'node') {
memo.target('node');
Expand Down
1 change: 1 addition & 0 deletions src/features/configPlugins/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
output: Joi.string().optional(),
externals: Joi.object().pattern(Joi.string(), Joi.string()),
chainWebpack: Joi.function().optional(),
name: Joi.string().optional(),
}),
prebundle: (Joi) =>
Joi.object({
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export interface IFatherBundleConfig extends IFatherBaseConfig {
* configure autoprefixer
*/
autoprefixer?: Autoprefixer.Options;

/**
* output library name
*/
name?: string;
}

export interface IFatherPreBundleConfig {
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/build/bundle-name/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from '../../../../src';

export default defineConfig({
umd: {
name: 'fatherDemo',
},
});
3 changes: 3 additions & 0 deletions tests/fixtures/build/bundle-name/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (files: Record<string, string>) => {
expect(files['umd/index.min.js']).toContain('fatherDemo');
};
1 change: 1 addition & 0 deletions tests/fixtures/build/bundle-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions tests/fixtures/build/bundle-name/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let umdDemo = {
hello: 'hello father',
};
export default umdDemo;

0 comments on commit e1bf838

Please sign in to comment.