Skip to content

[WIP] fix: config targets not working #12596

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion docs/docs/blog/legacy-browser.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group:

## 默认兼容说明

Umi 4 默认不支持 IE ,编译兼容目标 `targets` 为 `chrome: 80` ,如需调整,请指定明确的 [targets](../docs/api/config#targets) :
Umi 4 默认不支持 IE ,编译兼容目标 `targets` 为 `chrome: 40` ,如需调整,请指定明确的 [targets](../docs/api/config#targets) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编译到 40 是什么背景?


```ts
// .umirc.ts
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/blog/legacy-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group:

## 默认兼容说明

Umi 4 默认不支持 IE ,编译兼容目标 `targets` 为 `chrome: 80` ,如需调整,请指定明确的 [targets](../docs/api/config#targets) :
Umi 4 默认不支持 IE ,编译兼容目标 `targets` 为 `chrome: 40` ,如需调整,请指定明确的 [targets](../docs/api/config#targets) :

```ts
// .umirc.ts
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ import SmileUrl, { ReactComponent as SvgSmile } from './smile.svg';
## targets

- 类型:`object`
- 默认值:`{ chrome: 80 }`
- 默认值:`{ chrome: 40 }`

配置需要兼容的浏览器最低版本。Umi 会根据这个自定引入 polyfill、配置 autoprefixer 和做语法转换等。

Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var MESSAGE_TYPE = /* @__PURE__ */ ((MESSAGE_TYPE2) => {
return MESSAGE_TYPE2;
})(MESSAGE_TYPE || {});
var DEFAULT_BROWSER_TARGETS = {
chrome: 80
chrome: 40
};
var DEFAULT_ESBUILD_TARGET_KEYS = [
"chrome",
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/src/config/compressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function addCompressPlugin(opts: IOpts) {

// esbuild transform only allow `string[]` as target
const esbuildTarget = getEsBuildTarget({
targets: userConfig.targets || {},
targets: { chrome: '80' },
jsMinifier,
});
// 提升 esbuild 压缩产物的兼容性,比如不出现 ?? 这种语法
Expand Down
4 changes: 3 additions & 1 deletion packages/bundler-webpack/src/config/javaScriptRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ export async function addJavaScriptRules(opts: IOpts) {
opts.babelPreset || [
require.resolve('@umijs/babel-preset-umi'),
{
presetEnv: {},
presetEnv: {
targets: userConfig.targets,
},
presetReact: {},
presetTypeScript: {},
pluginTransformRuntime: {},
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum MESSAGE_TYPE {
}

export const DEFAULT_BROWSER_TARGETS = {
chrome: 80,
chrome: 40,
};

export const DEFAULT_ESBUILD_TARGET_KEYS = [
Expand Down
1 change: 1 addition & 0 deletions packages/mfsu/vendors/importParser/_importParser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/* parser generated by jison 0.4.18 */
/*
Returns a Parser object of the following structure:
Expand Down
10 changes: 8 additions & 2 deletions packages/preset-umi/src/commands/dev/getBabelOpts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { DEFAULT_BROWSER_TARGETS } from '@umijs/bundler-webpack';
import { semver } from '@umijs/utils';
import { IApi } from '../../types';

export async function getBabelOpts(opts: { api: IApi }) {
// TODO: 支持用户自定义
const shouldUseAutomaticRuntime = semver.gte(opts.api.appData.react.version, '17.0.0');
const shouldUseAutomaticRuntime = semver.gte(
opts.api.appData.react.version,
'17.0.0',
);
const babelPresetOpts = await opts.api.applyPlugins({
key: 'modifyBabelPresetOpts',
initialValue: {
presetEnv: {},
presetEnv: {
targets: opts.api.userConfig.targets || DEFAULT_BROWSER_TARGETS,
},
presetReact: {
runtime: shouldUseAutomaticRuntime ? 'automatic' : 'classic',
// importSource cannot be set when runtime is classic
Expand Down
Loading