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: add babel plugin preval #3867

Merged
merged 3 commits into from
Jul 26, 2019
Merged

Conversation

cyancity
Copy link
Contributor

@cyancity cyancity commented Jul 17, 2019

这个 PR 做了什么? (简要描述所做更改)

在 ast 解析阶段添加了 preval plugin,用户如果在代码中添加了 preval,则 plugin 会解析,如果用户未添加,此改动用户无感知,变动量较小

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue id #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 满足以下需求:

  • 提交到 master 分支
  • Commit 信息遵循 Angular Style Commit Message Conventions
  • 所有测试用例已经通过
  • 代码遵循相关包中的 .eslintrc, .tslintrc, .stylelintrc 所规定的规范
  • 在本地测试可用,不会影响到其它功能

这个 PR 涉及以下平台:

  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 头条小程序
  • QQ 轻应用
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)

其它需要 Reviewer 或社区知晓的内容:

@cyancity
Copy link
Contributor Author

case 是,可以满足动态变更 app.tsx 中 AppConfig的 pages, 可以根据不同环境返回不同的 pages,当然前提是用户代码和 taro cli 都要安装 babel-plugin-preval

config: Config = {
  "pages": preval`
    module.exports=(function() {
      if (process.env.TARO_ENV === 'weapp') {
        return [
          '/pages/index/index'
        ]
      }
      if (process.env.TARO_ENV === 'swan') {
        return [
          '/pages/test/test'
        ]
      }
    })()
  `
}

@yuche
Copy link
Contributor

yuche commented Jul 19, 2019

这里有个问题就是 babel-plugin-preval 的依赖babel-plugin-macros@2.2.2 是一个 babel@7 的插件,这样很可能会存在不兼容的情况。

@yuche
Copy link
Contributor

yuche commented Jul 25, 2019

加上这个插件是给用户用的,不是 taro-cli 用的

@yuche yuche merged commit 4c6a668 into NervJS:master Jul 26, 2019
@broven broven deleted the feat/add-babel-plugin-preval branch August 20, 2019 11:28
@Garfield550
Copy link
Collaborator

@luckyadam

@zpzxgcr
Copy link

zpzxgcr commented Dec 11, 2019

不好意思排查出来 是自己路径有问题

@Garfield550
Copy link
Collaborator

不好意思排查出来 是自己路径有问题

那也请不要删评论...

@zzzlucas
Copy link

zzzlucas commented Sep 17, 2020

image
image
image
问题描述
TARO Cli 版本 1.3.25
使用preval未达成预期编译效果,请问还需其他地方对babel plugin preval进行配置吗
已npm i babel plugin preval -g

package,json
{
"name": "app",
"version": "1.0.0",
"private": true,
"description": "项目描述",
"templateInfo": {
"name": "default",
"typescript": false,
"css": "sass"
},
"scripts": {
"dev:current": "concurrently 'npm run dev:weapp' 'npm run dev:swan'",
"dev:all": "concurrently 'npm run dev:weapp' 'npm run dev:swan' 'npm run dev:alipay' 'npm run dev:tt' 'npm run dev:qq'",
"build:all": "concurrently 'npm run build:weapp' 'npm run build:swan' 'npm run build:alipay' 'npm run build:tt' 'npm run build:qq'",
"tep": "node scripts/template",
"com": "node scripts/component",
"build:weapp": "taro build --type weapp",
"build:swan": "taro build --type swan",
"build:alipay": "taro build --type alipay",
"build:tt": "taro build --type tt",
"build:h5": "taro build --type h5",
"build:rn": "taro build --type rn",
"build:qq": "taro build --type qq",
"build:quickapp": "taro build --type quickapp",
"dev:weapp": "npm run build:weapp -- --watch",
"dev:swan": "npm run build:swan -- --watch",
"dev:alipay": "npm run build:alipay -- --watch",
"dev:tt": "npm run build:tt -- --watch",
"dev:h5": "npm run build:h5 -- --watch",
"dev:rn": "npm run build:rn -- --watch",
"dev:qq": "npm run build:qq -- --watch",
"dev:quickapp": "npm run build:quickapp -- --watch",
"lint": "eslint '/*.{js,jsx}'",
"lint:fix": "eslint '
/.{js,jsx}' --fix",
"format": "prettier -l --write "**/
.+(js|jsx|json|scss|md)" --no-semi --single-quote && exit 0",
"precommit": "lint-staged"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"**/*.{js,jsx}": [
"eslint --fix",
"prettier -l --write --no-semi --single-quote",
"git add"
]
},
"author": "",
"license": "MIT",
"dependencies": {
"@antv/f2": "~3.5.0",
"@antv/wx-f2": "^2.1.0",
"@jiahuix/mini-html-parser2": "^0.1.6",
"@tarojs/async-await": "2.0.2",
"@tarojs/components": "2.0.2",
"@tarojs/components-qa": "2.0.2",
"@tarojs/components-rn": "2.0.2",
"@tarojs/redux": "2.0.2",
"@tarojs/redux-h5": "2.0.2",
"@tarojs/router": "2.0.2",
"@tarojs/taro": "2.0.2",
"@tarojs/taro-alipay": "2.0.2",
"@tarojs/taro-h5": "2.0.2",
"@tarojs/taro-qq": "2.0.2",
"@tarojs/taro-quickapp": "2.0.2",
"@tarojs/taro-redux-rn": "2.0.2",
"@tarojs/taro-rn": "2.0.2",
"@tarojs/taro-router-rn": "2.0.2",
"@tarojs/taro-swan": "2.0.2",
"@tarojs/taro-tt": "2.0.2",
"@tarojs/taro-weapp": "2.0.2",
"concurrently": "^5.1.0",
"global": "^4.4.0",
"loda": "^0.1.3",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"nerv-devtools": "^1.5.6",
"nervjs": "^1.5.6",
"qs": "^6.9.3",
"react": "16.3.1",
"react-native": "0.55.4",
"redux": "^4.0.4",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"taro-chart-f2": "^0.1.1",
"taro-f2": "^2.2.0",
"taro-ui": "^2.2.4",
"tslib": "^1.8.0"
},
"devDependencies": {
"@tarojs/plugin-babel": "2.0.2",
"@tarojs/plugin-csso": "2.0.2",
"@tarojs/plugin-sass": "2.0.2",
"@tarojs/plugin-uglifyjs": "2.0.2",
"@tarojs/webpack-runner": "2.0.2",
"@types/react": "^16.4.6",
"@types/webpack-env": "^1.13.6",
"babel-eslint": "^8.2.3",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-preval": "^5.0.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-jsx-stylesheet": "^0.6.5",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "5",
"eslint-config-prettier": "^6.7.0",
"eslint-config-taro": "2.0.2",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-react-hooks": "^2.3.0",
"eslint-plugin-taro": "2.0.2",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"pre-commit": "^1.2.2",
"prettier": "^1.19.1",
"stylelint": "^12.0.0",
"stylelint-config-taro-rn": "2.0.2",
"stylelint-taro-rn": "2.0.2"
}
}

@cyancity
Copy link
Contributor Author

cyancity commented Sep 18, 2020

image
image
image
问题描述
TARO Cli 版本 1.3.25
使用preval未达成预期编译效果,请问还需其他地方对babel plugin preval进行配置吗
已npm i babel plugin preval -g

package,json
{
"name": "app",
"version": "1.0.0",
"private": true,
"description": "项目描述",
"templateInfo": {
"name": "default",
"typescript": false,
"css": "sass"
},
"scripts": {
"dev:current": "concurrently 'npm run dev:weapp' 'npm run dev:swan'",
"dev:all": "concurrently 'npm run dev:weapp' 'npm run dev:swan' 'npm run dev:alipay' 'npm run dev:tt' 'npm run dev:qq'",
"build:all": "concurrently 'npm run build:weapp' 'npm run build:swan' 'npm run build:alipay' 'npm run build:tt' 'npm run build:qq'",
"tep": "node scripts/template",
"com": "node scripts/component",
"build:weapp": "taro build --type weapp",
"build:swan": "taro build --type swan",
"build:alipay": "taro build --type alipay",
"build:tt": "taro build --type tt",
"build:h5": "taro build --type h5",
"build:rn": "taro build --type rn",
"build:qq": "taro build --type qq",
"build:quickapp": "taro build --type quickapp",
"dev:weapp": "npm run build:weapp -- --watch",
"dev:swan": "npm run build:swan -- --watch",
"dev:alipay": "npm run build:alipay -- --watch",
"dev:tt": "npm run build:tt -- --watch",
"dev:h5": "npm run build:h5 -- --watch",
"dev:rn": "npm run build:rn -- --watch",
"dev:qq": "npm run build:qq -- --watch",
"dev:quickapp": "npm run build:quickapp -- --watch",
"lint": "eslint '/*.{js,jsx}'", "lint:fix": "eslint '/.{js,jsx}' --fix", "format": "prettier -l --write "**/.+(js|jsx|json|scss|md)" --no-semi --single-quote && exit 0",
"precommit": "lint-staged"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"**/*.{js,jsx}": [
"eslint --fix",
"prettier -l --write --no-semi --single-quote",
"git add"
]
},
"author": "",
"license": "MIT",
"dependencies": {
"@antv/f2": "~3.5.0",
"@antv/wx-f2": "^2.1.0",
"@jiahuix/mini-html-parser2": "^0.1.6",
"@tarojs/async-await": "2.0.2",
"@tarojs/components": "2.0.2",
"@tarojs/components-qa": "2.0.2",
"@tarojs/components-rn": "2.0.2",
"@tarojs/redux": "2.0.2",
"@tarojs/redux-h5": "2.0.2",
"@tarojs/router": "2.0.2",
"@tarojs/taro": "2.0.2",
"@tarojs/taro-alipay": "2.0.2",
"@tarojs/taro-h5": "2.0.2",
"@tarojs/taro-qq": "2.0.2",
"@tarojs/taro-quickapp": "2.0.2",
"@tarojs/taro-redux-rn": "2.0.2",
"@tarojs/taro-rn": "2.0.2",
"@tarojs/taro-router-rn": "2.0.2",
"@tarojs/taro-swan": "2.0.2",
"@tarojs/taro-tt": "2.0.2",
"@tarojs/taro-weapp": "2.0.2",
"concurrently": "^5.1.0",
"global": "^4.4.0",
"loda": "^0.1.3",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"nerv-devtools": "^1.5.6",
"nervjs": "^1.5.6",
"qs": "^6.9.3",
"react": "16.3.1",
"react-native": "0.55.4",
"redux": "^4.0.4",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"taro-chart-f2": "^0.1.1",
"taro-f2": "^2.2.0",
"taro-ui": "^2.2.4",
"tslib": "^1.8.0"
},
"devDependencies": {
"@tarojs/plugin-babel": "2.0.2",
"@tarojs/plugin-csso": "2.0.2",
"@tarojs/plugin-sass": "2.0.2",
"@tarojs/plugin-uglifyjs": "2.0.2",
"@tarojs/webpack-runner": "2.0.2",
"@types/react": "^16.4.6",
"@types/webpack-env": "^1.13.6",
"babel-eslint": "^8.2.3",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-preval": "^5.0.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-jsx-stylesheet": "^0.6.5",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "5",
"eslint-config-prettier": "^6.7.0",
"eslint-config-taro": "2.0.2",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-react-hooks": "^2.3.0",
"eslint-plugin-taro": "2.0.2",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"pre-commit": "^1.2.2",
"prettier": "^1.19.1",
"stylelint": "^12.0.0",
"stylelint-config-taro-rn": "2.0.2",
"stylelint-taro-rn": "2.0.2"
}
}

不要使用自执行函数,preval 会自动执行 见文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants