Skip to content

Commit

Permalink
feat: support to configure output filename for umd (#660)
Browse files Browse the repository at this point in the history
* feat: support to configure filename for umd

* test: add case for umd filename

* docs: describe umd filename usage

* test: update case for coverage
  • Loading branch information
PeachScript committed Jun 6, 2023
1 parent a904649 commit 8f10606
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/config.md
Expand Up @@ -201,10 +201,10 @@ export default {

#### output

- 类型:`string`
- 类型:`string` | `{ path?: string; filename?: string }`
- 默认值:`dist/umd`

指定产物的输出目录,输出文件名暂不支持配置,单 `entry` 时默认以 NPM 包名命名、多 `entry` 时默认与源码文件同名。
指定产物的输出目录及输出文件名,输出目录的默认值为 `dist/umd`,输出文件名在单 `entry` 时默认以 NPM 包名命名、多 `entry` 时默认与源码文件同名。

#### externals

Expand Down Expand Up @@ -238,7 +238,7 @@ export default {
```ts
export default {
theme: { 'primary-color': '#1890ff' },
}
};
```

### prebundle
Expand Down
20 changes: 15 additions & 5 deletions src/builder/config.ts
Expand Up @@ -40,7 +40,7 @@ export interface IBundlessConfig
type: IFatherBuildTypes.BUNDLESS;
format: IFatherBundlessTypes;
input: string;
output: NonNullable<IFatherBundleConfig['output']>;
output: NonNullable<IFatherBundlessConfig['output']>;
}

/**
Expand Down Expand Up @@ -108,6 +108,8 @@ export function normalizeUserConfig(
// normalize umd config
if (umd) {
const entryConfig = umd.entry;
const output =
typeof umd.output === 'object' ? umd.output : { path: umd.output };
const bundleConfig: Omit<IBundleConfig, 'entry'> = {
type: IFatherBuildTypes.BUNDLE,
bundler: 'webpack',
Expand All @@ -119,15 +121,22 @@ export function normalizeUserConfig(
// generate default output
output: {
// default to generate filename from package name
filename: `${getAutoBundleFilename(pkg.name)}.min.js`,
filename:
output.filename || `${getAutoBundleFilename(pkg.name)}.min.js`,
// default to output dist
path: umd.output || 'dist/umd',
path: output.path || 'dist/umd',
},
};

if (typeof entryConfig === 'object') {
// extract multiple entries to single configs
Object.keys(entryConfig).forEach((entry) => {
const outputConfig = entryConfig[entry].output;
const entryOutput =
typeof outputConfig === 'object'
? outputConfig
: { path: outputConfig };

configs.push({
...bundleConfig,

Expand All @@ -137,8 +146,9 @@ export function normalizeUserConfig(

// override output
output: {
filename: `${path.parse(entry).name}.min.js`,
path: entryConfig[entry].output || bundleConfig.output.path,
filename:
entryOutput.filename || `${path.parse(entry).name}.min.js`,
path: entryOutput.path || bundleConfig.output.path,
},
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/features/configPlugins/schema.ts
Expand Up @@ -57,7 +57,7 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
entry: Joi.alternatives()
.try(Joi.string(), Joi.object().pattern(Joi.string(), Joi.object()))
.optional(),
output: Joi.string().optional(),
output: Joi.alternatives().try(Joi.string(), Joi.object()).optional(),
externals: Joi.alternatives().try(
Joi.object(),
Joi.string(),
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Expand Up @@ -5,9 +5,9 @@ import type { IConfig as IBundlerWebpackConfig } from '@umijs/bundler-webpack/di
import type { IAdd, IModify, IServicePluginAPI, PluginAPI } from '@umijs/core';
import type { ITransformerItem } from './builder/bundless/loaders/javascript';
import type {
createConfigProviders,
IBundleConfig,
IBundlessConfig,
createConfigProviders,
} from './builder/config';
import type { IDoctorReport } from './doctor';
import type { IDoctorSourceParseResult } from './doctor/parser';
Expand Down Expand Up @@ -175,7 +175,7 @@ export interface IFatherBundleConfig extends IFatherBaseConfig {
* bundle output path
* @default dist/umd
*/
output?: string;
output?: string | { path?: string; filename?: string };

/**
* extract CSS
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/build/bundle-output-filename/.fatherrc.ts
@@ -0,0 +1,18 @@
import { defineConfig } from '../../../../src';

export default defineConfig({
umd: {
output: {
path: 'nothing',
filename: 'nothing.js',
},
entry: {
'src/index': {
output: {
path: 'dist/abc',
filename: 'index.umd.min.js',
},
},
},
},
});
3 changes: 3 additions & 0 deletions tests/fixtures/build/bundle-output-filename/expect.ts
@@ -0,0 +1,3 @@
export default (files: Record<string, string>) => {
expect(files['abc/index.umd.min.js']).not.toBeUndefined();
};
1 change: 1 addition & 0 deletions tests/fixtures/build/bundle-output-filename/package.json
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions tests/fixtures/build/bundle-output-filename/src/index.ts
@@ -0,0 +1,4 @@
let umdDemo = {
hello: 'hello father',
};
export default umdDemo;

0 comments on commit 8f10606

Please sign in to comment.