From 8aca272f8e67fc4c11ed601e7744555728417bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=9D=E5=BF=A0?= Date: Wed, 5 Aug 2020 00:22:33 +0800 Subject: [PATCH 1/3] feat: check the targets and compatible browserslist --- packages/plugin-app-core/src/index.ts | 48 ++++++++++++++++++- .../src/userConfig/injectBabel.js | 3 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/plugin-app-core/src/index.ts b/packages/plugin-app-core/src/index.ts index 7c21d1e015..22ef779bfe 100644 --- a/packages/plugin-app-core/src/index.ts +++ b/packages/plugin-app-core/src/index.ts @@ -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 @@ -68,3 +75,42 @@ function initGenerator(api, options) { log }); } + +function checkTargets(targets) { + 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); + }); +} diff --git a/packages/plugin-react-app/src/userConfig/injectBabel.js b/packages/plugin-react-app/src/userConfig/injectBabel.js index b54461118d..c01bfecdb0 100644 --- a/packages/plugin-react-app/src/userConfig/injectBabel.js +++ b/packages/plugin-react-app/src/userConfig/injectBabel.js @@ -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; } From 0048fd86a88e17be9b10508beca7eae7f913da1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=9D=E5=BF=A0?= Date: Wed, 5 Aug 2020 10:12:33 +0800 Subject: [PATCH 2/3] docs: update docs --- docs/guide/basic/build.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/guide/basic/build.md b/docs/guide/basic/build.md index 7d3d899ca6..2ac2b400db 100644 --- a/docs/guide/basic/build.md +++ b/docs/guide/basic/build.md @@ -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` From e26d77b451a765930e11a721cf39776b569020e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=9D=E5=BF=A0?= Date: Wed, 5 Aug 2020 10:35:06 +0800 Subject: [PATCH 3/3] fix: dependencies --- packages/create-ice/package.json | 2 +- packages/plugin-app-core/package.json | 1 + packages/plugin-ssr/package.json | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/create-ice/package.json b/packages/create-ice/package.json index cd48a60532..d4a20ce28d 100644 --- a/packages/create-ice/package.json +++ b/packages/create-ice/package.json @@ -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" }, diff --git a/packages/plugin-app-core/package.json b/packages/plugin-app-core/package.json index 307aa0a641..0de8beaf7f 100644 --- a/packages/plugin-app-core/package.json +++ b/packages/plugin-app-core/package.json @@ -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", diff --git a/packages/plugin-ssr/package.json b/packages/plugin-ssr/package.json index f012cc898a..237602a46c 100644 --- a/packages/plugin-ssr/package.json +++ b/packages/plugin-ssr/package.json @@ -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",