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: ✨ 新增targets配置,用于修改构建产物的兼容性 #549

Merged
merged 11 commits into from Dec 7, 2022
27 changes: 25 additions & 2 deletions docs/config.md
Expand Up @@ -48,9 +48,9 @@ father 支持以下配置项。
- 类型:`browser` | `node`
- 默认值:`<auto>`

指定构建产物的目标平台,其中 `esm` 与 `umd` 产物的默认 `platform` 为 `browser`,`cjs` 产物的默认 `platform` 为 `node`;指定为 `browser` 时产物默认兼容至 IE11,指定为 `node` 时产物默认兼容至 Node.js v14,兼容性不支持配置
指定构建产物的目标平台,其中 `esm` 与 `umd` 产物的默认 `platform` 为 `browser`,`cjs` 产物的默认 `platform` 为 `node`;指定为 `browser` 时产物默认兼容至 IE11,指定为 `node` 时产物默认默认兼容至 Node.js v14。
huarse marked this conversation as resolved.
Show resolved Hide resolved

> 注:Bundless 模式下,如果手动将 `transformer` 指定为 `esbuild`,那么 `browser` 产物兼容性为 ES6 而不是 IE11。
> 注:Bundless 模式下,如果手动将 `transformer` 指定为 `esbuild`,那么 `browser` 产物默认兼容性为 ES6 而不是 IE11。

### sourcemap

Expand Down Expand Up @@ -93,6 +93,22 @@ father 以构建产物类型划分构建配置,其中 `esm`、`cjs` 产物为

指定源码的编译工具,当 `platform` 为 `node` 时,默认值为 `esbuild`,当 `platform` 为 `browser` 时,默认值为 `babel`。

#### targets

- 类型: `string` | `object`
- 默认值:`undefined`

指定源码编译产物的兼容性,目前仅支持 `babel` 和 `esbuild` 编译模式。以下是 `platform` 和 `transformer` 设置为不同值时传递的属性值示例
huarse marked this conversation as resolved.
Show resolved Hide resolved

| `platform` | `transformer` | `targets` example |
| ---------- | ------------- | ----------------- |
| `browser` | `babel` | `{ chrome: 80 }` |
| `browser` | `esbuild` | `es2017` |
| `browser` | `swc` | -- |
huarse marked this conversation as resolved.
Show resolved Hide resolved
| `node` | `babel` | `{ node: 14 }` |
| `node` | `esbuild` | `node14` |
| `node` | `swc` | -- |

#### overrides

- 类型:`object`
Expand Down Expand Up @@ -190,6 +206,13 @@ export default {

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

#### targets
huarse marked this conversation as resolved.
Show resolved Hide resolved

- 类型: `object`
- 默认值: `undefined`

指定产物的兼容性,覆盖默认的 `{ ie: 11 }`

#### externals

- 类型:`Record<string, string>`
Expand Down
6 changes: 4 additions & 2 deletions src/builder/bundle/index.ts
Expand Up @@ -52,7 +52,7 @@ export default async (opts: {
theme: config.theme,

// compatible with IE11 by default
targets: { ie: 11 },
targets: config.targets || { ie: 11 },
jsMinifier: JSMinifier.terser,
cssMinifier: CSSMinifier.cssnano,
extraBabelIncludes: [/node_modules/],
Expand All @@ -66,7 +66,9 @@ export default async (opts: {
babelPreset: [
require.resolve('@umijs/babel-preset-umi'),
{
presetEnv: {},
presetEnv: {
targets: config.targets || { ie: 11 },
},
presetReact: getBabelPresetReactOpts(opts.configProvider.pkg),
presetTypeScript: {},
pluginTransformRuntime: {},
Expand Down
4 changes: 3 additions & 1 deletion src/builder/bundless/loaders/javascript/babel.ts
Expand Up @@ -36,7 +36,9 @@ const babelTransformer: IJSTransformer = function (content) {
const presetOpts: any = {
presetEnv: {
targets:
this.config.platform === IFatherPlatformTypes.BROWSER
typeof this.config.targets === 'object'
huarse marked this conversation as resolved.
Show resolved Hide resolved
? this.config.targets
: this.config.platform === IFatherPlatformTypes.BROWSER
? { ie: 11 }
: { node: 14 },
modules: this.config.format === IFatherBundlessTypes.ESM ? false : 'auto',
Expand Down
6 changes: 5 additions & 1 deletion src/builder/bundless/loaders/javascript/esbuild.ts
Expand Up @@ -60,7 +60,11 @@ const esbuildTransformer: IJSTransformer = async function () {
define: this.config.define,
platform: this.config.platform,
target:
this.config.platform === IFatherPlatformTypes.NODE ? 'node14' : 'es6',
typeof this.config.targets === 'string'
? this.config.targets
: this.config.platform === IFatherPlatformTypes.NODE
? 'node14'
: 'es6',
// esbuild need relative entry path
entryPoints: [path.relative(this.paths.cwd, this.paths.fileAbsPath)],
absWorkingDir: this.paths.cwd,
Expand Down
2 changes: 2 additions & 0 deletions src/features/configPlugins/schema.ts
Expand Up @@ -37,6 +37,7 @@ function getBundlessSchemas(Joi: Root) {
transformer: Joi.string(),
overrides: Joi.object(),
ignores: Joi.array().items(Joi.string()),
targets: Joi.alternatives().try(Joi.object(), Joi.string()).optional(),
});
}

Expand All @@ -58,6 +59,7 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
extractCSS: Joi.boolean().optional(),
name: Joi.string().optional(),
theme: Joi.object().pattern(Joi.string(), Joi.string()),
targets: Joi.object().optional(),
}),
prebundle: (Joi) =>
Joi.object({
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Expand Up @@ -149,6 +149,15 @@ export interface IFatherBundlessConfig extends IFatherBaseConfig {
* ignore specific directories & files via ignore syntax
*/
ignores?: string[];

/**
* bundless compile targets
* - browser:
* - esbuild: es6, es2017, etc.
* - babel: { chrome: 80, ... }
* - node: node14, node16, etc.
*/
targets?: Record<string, string | number> | string;
}

export interface IFatherBundleConfig extends IFatherBaseConfig {
Expand Down Expand Up @@ -206,6 +215,11 @@ export interface IFatherBundleConfig extends IFatherBaseConfig {
* configure less variables
*/
theme?: IBundlerWebpackConfig['theme'];

/**
* bundle compile targets
*/
targets?: Record<string, string | number>;
}

export interface IFatherPreBundleConfig {
Expand Down