Giter VIP home page Giter VIP logo

taro2-to-3's Introduction

taro2-to-3

帮助你将 Taro 2.x 项目升级到 Taro 3.x 项目,基于 jscodeshift 构建。

该工具的处理规则参考 Taro 官方 迁移指南

动机

愿将 Taro 2.x 项目升级到 Taro 3.x 项目的过程变得轻松愉悦。

使用

在运行前,请先提交你的本地代码修改。

# 全局安装
npm i -g taro2-to-3
# or for yarn user
#  yarn global add taro2-to-3
taro2-to-3

已知问题

  1. Taro 内部 __taroRouterChange 事件参数变更,h5 中参数发生变化,小程序中不触发 NervJS/taro#7903
  2. Taro 3.2.x 以上版本中 preloadData api 发生变更 https://github.com/NervJS/taro/commit/d2c4aaf5d9d755bc3ca40f9b449340b360c673d5
  3. 缺失对 this.$scope api 进行转换 NervJS/taro#7795

处理流程

  1. 读取 Taro 项目中的编译配置文件 config/index.js.
    1. 读取其中的配置项 sourceRoot ,获取项目入口文件所在目录。
    2. 更新编译配置文件。
  2. 读取当前项目中的入口文件,如 {sourceRoot}/app.js
    1. 读取其中的配置项 pages ,获取项目中所有的页面路径。
    2. 更新入口文件。
  3. 执行以下脚本。
  4. 读取项目的 package.json 文件,检查并更新依赖。

脚本包括

taro-imports

在 Taro 2.x 中,所有面向应用开发者的 API 都在 @tarojs/taro 包中。

在 Taro 3.x 中,将 React 的 API 从 react 包中引入,其它的 API 仍然从 @tarojs/taro 引入。

- import Taro, { Component } from '@tarojs/taro';
+ import React, { Component } from '@tarojs/taro';
+ import Taro from '@tarojs/taro';

class Wallace extends Component {
    componentDidMount() {
        Taro.request().then(() => {});
    }
    render() {
        return <div />;
    }
}

page-config

在 Taro 2.x 中,页面的配置挂载在类组件的类属性或函数式的属性上,通过 AST 分析取出来,然后生成 JSON 文件。

在 Taro 3.x 中,使用 *.config.js ,该文件和页面文件在同一文件夹。

// foo.jsx
export default class Index extends Component {
-   config = {
-       navigationBarTitleText: '首页'
-   }
    render() {
        return null;
    }
}

+// foo.config.js
+export default config = {
+   navigationBarTitleText: '首页'
+};

router

在 Taro 2.x 中,通过 this.$router 访问当前组件/页面路由的详情。

在 Taro 3.x 等价的 API 是 @tarojs/taro 包中的 getCurrentInstance().router。

class Foo extends Component {
+   $instance = getCurrentInstance();
    componentWillMount() {
-       console.log(this.$router);
+       console.log(this.$instance.router)
    }
}

License

MIT

taro2-to-3's People

Contributors

symind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

taro2-to-3's Issues

转换时出现错误Cannot find module 'semver/functions/satisfies'

使用步骤:

在项目下执行

npm i -g taro2-to-3

接着执行

taro2-to-3

出现以下错误:

internal/modules/cjs/loader.js:796
    throw err;
    ^

Error: Cannot find module 'semver/functions/satisfies'
Require stack:
- C:\Users\Administrator\AppData\Roaming\npm\node_modules\taro2-to-3\bin\cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.Module._load (internal/modules/cjs/loader.js:686:27)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\taro2-to-3\bin\cli.js:12:25)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\taro2-to-3\\bin\\cli.js'
  ]
}

环境信息:

npm -v
7.6.2

taro -v
� Taro v2.2.16

Unexpected token (10:9)

Taro项目,目前使用Taro 2,使用本工具升级3.

yarn global add taro2-to-3

taro2-to-3

提示

Unexpected token (10:9)

使用环境macOS和ubuntu都不行

taro cli

👽 Taro v3.3.0


  Taro CLI 3.3.0 environment info:
    System:
      OS: macOS 11.4
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 12.18.4 - ~/.nvm/versions/node/v12.18.4/bin/node
      Yarn: 1.22.10 - ~/.nvm/versions/node/v12.18.4/bin/yarn
      npm: 6.14.8 - ~/.nvm/versions/node/v12.18.4/bin/npm
    npmPackages:
      @tarojs/cli: 3.3.0 => 3.3.0
      @tarojs/components: 3.3.0 => 3.3.0
      @tarojs/mini-runner: 3.3.0 => 3.3.0
      @tarojs/react: ^3.3.0 => 3.3.0
      @tarojs/router: 3.3.0 => 3.3.0
      @tarojs/runtime: ^3.3.0 => 3.3.0
      @tarojs/taro: 3.3.0 => 3.3.0
      @tarojs/taro-h5: 3.3.0 => 3.3.0
      @tarojs/webpack-runner: 3.3.0 => 3.3.0
      eslint-config-taro: 3.3.0 => 3.3.0
      eslint-plugin-taro: 3.3.0 => 3.3.0
      nerv-devtools: ^1.5.7 => 1.5.7
      nervjs: ^1.5.7 => 1.5.7
      react: ^17.0.2 => 17.0.2
      taro-ui: 1.5.4 => 1.5.4
    npmGlobalPackages:
      typescript: 4.0.5

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.