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: check the targets and compatible browserslist #3391

Merged
merged 3 commits into from
Aug 6, 2020
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
8 changes: 5 additions & 3 deletions docs/guide/basic/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,24 @@ icejs 中一般不允许修改该配置。

注意,devServer 不支持 port 属性配置,如需改变端口,请通过命令行参数传入。

### targets
### browserslist

- 类型: `string` | `object` 
- 默认值:`last 2 versions, Firefox ESR, > 1%, ie >= 9, iOS >= 8, Android >= 4`

配置 @babel/preset-env 的 [targets](https://babeljs.io/docs/en/babel-preset-env#targets),配置浏览器最低版本,新配置的 `targets` 会覆盖默认值。
配置 @babel/preset-env 的浏览器最低版本(https://babeljs.io/docs/en/babel-preset-env#targets),新配置的 `browserslist` 会覆盖默认值。

```json
{
"targets": {
"browserslist": {
"chrome": 49,
"ie": 11,
}
}
```

> 注: 因 targets 字段被使用,这里使用 browserslist 字段替代 @babel/preset-env 的 targets 字段。

### vendor

- 类型:`boolean`
Expand Down
2 changes: 1 addition & 1 deletion packages/create-ice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@iceworks/generate-project": "^1.0.0",
"chalk": "^3.0.0",
"chalk": "^4.0.0",
"fs-extra": "^8.1.0",
"inquirer": "^7.0.4"
},
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-app-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"chokidar": "^3.4.1",
"chalk": "^4.0.0",
"create-app-shared": "^0.1.1",
"ejs": "^3.0.1",
"fs-extra": "^8.1.0",
Expand Down
48 changes: 47 additions & 1 deletion packages/plugin-app-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import { ICE_TEMP } from './constant';
import dev from './dev';
import { setAlias, setProjectType, setEntry, setTempDir, setRegisterMethod, setRegisterUserConfig } from './config';

// eslint-disable-next-line
const chalk = require('chalk');

export default (api, options) => {
const { onHook, context } = api;
const { command } = context;
const { command, userConfig } = context;
const { targets } = userConfig;

// Check target
checkTargets(targets);

// Set temporary directory
// eg: .ice or .rax
Expand Down Expand Up @@ -68,3 +75,42 @@ function initGenerator(api, options) {
log
});
}

function checkTargets(targets) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

校验可以统一收敛在注册配置中

let hasError = false;

if (Object.prototype.toString.call(targets) === '[object Object]') {
hasError = true;
}

if (typeof targets === 'string') {
hasError = true;
}

if (Array.isArray(targets) && !matchTargets(targets)) {
hasError = true;
}

if (hasError) {
const msg = `
targets must be the array type in build.json.

e.g. { "targets": ["miniapp", "wechat-miniprogram"] }

if you want to describes the browserslist environments for your project.
you should set browserslist in build.json.

e.g. { "browserlist": { "chrome": "58", "ie": 11 } }
`;
console.log();
console.log(chalk.red(msg));
console.log();
process.exit(1);
}
}

function matchTargets(targets) {
return targets.every(target => {
return ['web', 'miniapp', 'wechat-miniprogram'].includes(target);
});
}
3 changes: 2 additions & 1 deletion packages/plugin-react-app/src/userConfig/injectBabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const addBablePlugins = require('./babelPlugins');

module.exports = (config, injectBabel, context) => {
const { userConfig: { targets = [] } } = context;
const isMiniapp = targets.includes('miniapp') || targets.includes('wechat-miniprogram');
const isMiniapp = Array.isArray(targets)
&& (targets.includes('miniapp') || targets.includes('wechat-miniprogram'));
if (isMiniapp) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"build-scripts-config": "^0.1.6",
"chalk": "^3.0.0",
"chalk": "^4.0.0",
"cheerio": "^1.0.0-rc.3",
"ejs": "^3.0.1",
"fs-extra": "^8.1.0",
Expand Down