Taro an open cross-end cross-frame solution

Taro an open cross-end cross-frame solution

2022-09-02 0 915
Resource Number 38018 Last Updated 2025-02-24
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

Taro recommended in this issue is an open cross-end cross-framework solution that supports the use of frameworks such as React/Vue/Nerv to develop applications such as wechat/Jingdong/Baidu/Alipay/Bytedance/QQ/Feishu applet/H5 / RN.

project context

Nowadays, the market has a variety of forms, and various terminals such as Web, React Native, and wechat mini programs are popular. When business requirements are required to perform at different ends at the same time, the cost of writing multiple sets of code for different ends is obviously very high, and the ability to write only one set of code can be adapted to multiple ends is extremely needed.

Installation and use

The Taro project is based on node. Please make sure that you have a newer node environment (>=12.0.0). It is recommended to use the node version management tool nvm to manage node, so that you can easily switch node versions and do not need to add sudo during global installation.

CLI Tool Installation

First, you need to install @tarojs/cli using npm or yarn globally, or use npx directly:

# use npm install CLI
$ npm install -g @tarojs/cli

# OR use yarn install CLI
$ yarn global add @tarojs/cli

# OR install cnpm,use cnpm install CLI
$ cnpm install -g @tarojs/cli

Taro version information can be found using npm info, where you can see the current version

npm info @tarojs/cli

Taro an open cross-end cross-frame solution插图

Project initialization

Use commands to create template projects:

$ taro init myApp

npm 5.2+ can also use npx to create template projects without global installation:

$ npx @tarojs/cli init myApp

Taro an open cross-end cross-frame solution插图1

After the project is created, Taro will install the dependencies required by the project by default. The installation tools are used to detect the dependencies in the yarn > cnpm > npm order. In general, the dependency installation will be relatively smooth, but in some cases the installation may fail, in which case you can use the installation command in the project directory to install yourself:

# Go to the project root directory
$ cd myApp

# use yarn Installation dependency
$ yarn

# OR use cnpm Installation dependency
$ cnpm install

# OR use npm Installation dependency
$ npm install

compilation run

Use Taro’s build command to compile Taro code into different side code, and then view the effect in the corresponding development tool.

Taro compilation is divided into dev and build modes:

  • dev mode (added –watch parameter) will listen for file modifications.
  • build mode (without the –watch parameter) will not listen for file modifications and will compress and package the code.
  • The files generated in dev mode are large. Setting the environment variable NODE_ENV to production can enable compression for easy preview, but the compilation speed will be reduced.

Example byte applet:

compile command

# yarn
$ yarn dev:tt
$ yarn build:tt

# npm script
$ npm run dev:tt
$ npm run build:tt

# Global installation only
$ taro build --type tt --watch
$ taro build --type tt

# npx It is also available to users
$ npx taro build --type tt --watch
$ npx taro build --type tt

# watch Simultaneous compression
$ set NODE_ENV=production && taro build --type tt --watch # Windows
$ NODE_ENV=production taro build --type tt --watch # Mac

Small program developer tools

Download and open the Bytedance applet Developer tool, and make sure the applet project configuration file project.tt.json is set. Then select the dist directory in the root of the project (the directory of the outputRoot setting in root config) to preview.

Note the project Settings of the developer tools:

  • The ES6 to ES5 function needs to be disabled. If this function is enabled, an error may be reported
  • You need to disable automatic style completion when uploading code. If enabled, an error may be reported
  • You need to disable code compression upload. If this function is enabled, an error may be reported

Project progression and optimization

CSS instrument

In Taro, we can use CSS preprocessor and post-processor freely, and the method of using CSS is very simple, as long as the relevant plug-ins are added in the compilation configuration:

const config = {
  projectName: 'v2ex',
  date: '2018-8-3',
  designWidth: 750,
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [
    '@tarojs/plugin-sass', // use Sass
    // '@tarojs/plugin-less', // use Less
    // '@tarojs/plugin-stylus', // use Stylus
  ],
  defineConstants: {
  },
  mini: {

  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    module: {
      postcss: {
        autoprefixer: {
          enable: true
        }
      }
    }
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}

Multiterminal development

In some cases, the performance or business logic of different platforms is qualitatively different. In this case, we can not do “one set of code to go around the world”.

For example, we are implementing the V2EX forum application, and the current API cannot be called cross-domain in the browser, so we need to use another API on the H5 side. We can solve this with built-in environment variables:

- import api from '../../utils/api'
// We can introduce different apis depending on the API
+ let api
+ if (process.env.TARO_ENV === 'weapp') {
+  api = require('../../utils/api-weapp')
+ } else if (process.env.TARO_ENV === 'h5') {
+  api = require('../../utils/api-h5')
+ }

Taro also provides a unified interface of multi-terminal files, through different naming ways to find dependencies, in such cases, we can keep:

import api from '../../utils/api'

Modify our file structure by adding the name of the platform between the file name and the suffix:

.
└── utils
    ├── api.h5.js
    ├── api.weapp.js
    └── index.js
资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code Taro an open cross-end cross-frame solution https://ictcoder.com/taro-an-open-cross-end-cross-frame-solution/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services