Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to configure output filename for umd #660

Merged
merged 4 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/config.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions tests/fixtures/build/bundle-output-filename/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;