diff --git a/docs/config.md b/docs/config.md index 26ef717..698d2de 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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` diff --git a/src/builder/bundle/index.ts b/src/builder/bundle/index.ts index c3f226a..8cc0d3c 100644 --- a/src/builder/bundle/index.ts +++ b/src/builder/bundle/index.ts @@ -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'); diff --git a/src/features/configPlugins/schema.ts b/src/features/configPlugins/schema.ts index 398e35f..d390f28 100644 --- a/src/features/configPlugins/schema.ts +++ b/src/features/configPlugins/schema.ts @@ -54,6 +54,7 @@ export function getSchemas(): Record 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({ diff --git a/src/types.ts b/src/types.ts index d9bae36..599496b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -169,6 +169,11 @@ export interface IFatherBundleConfig extends IFatherBaseConfig { * configure autoprefixer */ autoprefixer?: Autoprefixer.Options; + + /** + * output library name + */ + name?: string; } export interface IFatherPreBundleConfig { diff --git a/tests/fixtures/build/bundle-name/.fatherrc.ts b/tests/fixtures/build/bundle-name/.fatherrc.ts new file mode 100644 index 0000000..d3281e0 --- /dev/null +++ b/tests/fixtures/build/bundle-name/.fatherrc.ts @@ -0,0 +1,7 @@ +import { defineConfig } from '../../../../src'; + +export default defineConfig({ + umd: { + name: 'fatherDemo', + }, +}); diff --git a/tests/fixtures/build/bundle-name/expect.ts b/tests/fixtures/build/bundle-name/expect.ts new file mode 100644 index 0000000..506354e --- /dev/null +++ b/tests/fixtures/build/bundle-name/expect.ts @@ -0,0 +1,3 @@ +export default (files: Record) => { + expect(files['umd/index.min.js']).toContain('fatherDemo'); +}; diff --git a/tests/fixtures/build/bundle-name/package.json b/tests/fixtures/build/bundle-name/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/fixtures/build/bundle-name/package.json @@ -0,0 +1 @@ +{} diff --git a/tests/fixtures/build/bundle-name/src/index.ts b/tests/fixtures/build/bundle-name/src/index.ts new file mode 100644 index 0000000..eef0eae --- /dev/null +++ b/tests/fixtures/build/bundle-name/src/index.ts @@ -0,0 +1,4 @@ +let umdDemo = { + hello: 'hello father', +}; +export default umdDemo;