Code Monkey home page Code Monkey logo

taro-ui-sample's Introduction

基于 Taro 的多端 UI 库示范用例

通过 Taro 提供的多端 UI 库打包能力,可以打包出一个多端运行的 UI 库,目前已经支持 微信/支付宝/百度小程序以及 H5,RN 端的支持还在调研中

多端 UI 库项目结构

多端 UI 库的项目目录结构与普通 Taro 项目基本一致,不同点如下

增加一个 UI 库入口文件

需要在 src 目录下添加 index.js 或者 index.ts 来作为 UI 库的入口文件,用于输出 UI 组件,如果有多个 UI 组件,可以如下书写

export { default as A } from './components/A/A'
export { default as B } from './components/B/B'

这样的话,这个组件库使用起来,会是如下的方式

import { A } from 'taro-ui-sample'

<A />

如果只有 UI 组件,也可以如下书写

import A from './components/A/A'

export default A

这样的话,这个组件库使用起来,会是如下的方式

import A from 'taro-ui-sample'

<A />

配置文件改造

为了打包出可以在 H5 端使用的组件库,需要在 config/index.js 文件中增加一些配置

if (process.env.TARO_BUILD_TYPE === 'ui') {
  Object.assign(config.h5, {
    enableSourceMap: false,
    enableExtract: false,
    enableDll: false
  })
  config.h5.webpackChain = chain => {
    chain.plugins.delete('htmlWebpackPlugin')
    chain.plugins.delete('addAssetHtmlWebpackPlugin')
    chain.merge({
      output: {
        path: path.join(process.cwd(), 'dist', 'h5'),
        filename: 'index.js',
        libraryTarget: 'umd',
        library: 'taro-ui-sample'
      },
      externals: {
        nervjs: 'commonjs2 nervjs',
        classnames: 'commonjs2 classnames',
        '@tarojs/components': 'commonjs2 @tarojs/components',
        '@tarojs/taro-h5': 'commonjs2 @tarojs/taro-h5',
        'weui': 'commonjs2 weui'
      }
    })
  }
}

以上配置可以根据需要自行修改。

package.json 依赖处理

package.json 中 dependencies 中只放必要的依赖,并且建议尽量精简,原有 Taro 相关的依赖可以放到 devDependencies 中,这样安装 UI 库的时候不需要再重复安装

打包命令

在完成以上项目结构改造后,你就可以获得一个 Taro 的多端 UI 库的项目了

这时候你可以通过如下命令来进行打包

$ TARO_BUILD_TYPE=ui taro build --ui

打包之后的文件在 dist 目录下

里面会包含一个 index.js 的入口文件,内容如下,需要注意的是,这个内容是 Taro 自动生成的,不可修改

if (process.env.TARO_ENV === 'h5') {
  module.exports = require('./h5/index')
  module.exports.default = module.exports
} else {
  module.exports = require('./weapp/index')
  module.exports.default = module.exports
}

H5 端以及小程序类(微信/支付宝/百度)的文件分别在 h5weapp 目录下,通过入口文件就能在不同的端内进行引用

项目测试

推荐采用 jest 进行测试,项目中已经包含了完整的测试配置与范例,可以直接使用,有以下值得注意的地方

使用 babel-jest

转换器使用 babel-jest,为了配合 babel 7 进行使用,需要安装

$ yarn add --dev babel-jest babel-core@^7.0.0-bridge.0 @babel/core

其中 babel-core@^7.0.0-bridge.0 一定要安装

babel.config.js

由于测试使用了 babel 7,为了避免和 Taro 本身使用的 babel 冲突,测试使用的 babel 配置位于 babel.config.js

taro-ui-sample's People

Contributors

luckyadam 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

taro-ui-sample's Issues

taro3.x 如何进行测试呢

测试步骤是

  1. npm run build:h5 生成 .temp 文件夹的中间代码
  2. test.tsx 引入 temp 内的组件进行测试

但在 taro2 下是可以成功生成 .temp 文件夹的,
taro3 却不可以,请问这个问题如何解决呢?

用这个打包的组件库,px没有转换成rem

用这个打包的组件库,px没有转换成rem.当打包成npm被其他taro使用的时候像素单位会有问题.

目前我不知道在哪里配置这种转换,目前用taro初始化的项目,正常打包是会转换单位的.
如果使用ui组件库的方式打包,px就不会转换成rem了.
当我们用这个项目初始化的ui组件库,生成npm包的时候.
使用npm包的项目,并不会把px转换成rem.

目前没有查到哪里可以配置

测试报错

Timer 组件中添加

  constructor(){
    super()
  }

后测试报错。

项目打包问题

目前本地 Taro 版本 为1.1.3

文档中说的这个方式,
我测试中该项目打包出来的dist/weapp目录并没有正确打包components目录下的文件
image
image

改回原来的导出方式后才能正确打包。

不过这两种方式打包出来的h5目录下都没有index.js文件
image

dist/index.js需要 module.exports = require('./h5/index')

也就导致运行h5端时候报错了。

taro hooks 如何测试

目前使用了 Taro 开发小程序,在 1.3.0 版本后基本使用 hooks 语法开发了。似乎相关文档尚未更新。

请问如何测试 hooks 组件?

hooks 组件如何编写测试用例?

目前使用的taro版本是2.2.18

编写的函数组件在小程序和H5的示例中均能通过编译无报错。

import { Button } from "@tarojs/components";
import Taro, { useMemo, useCallback } from "@tarojs/taro";
import { IOpenAbilityUser } from "types/open-ability";
import classNames from "classnames";

const OpenAbilityUser: IOpenAbilityUser = function(props) {
  const {
    handler,
    hoverClass = "none",
    children,
    className,
    desc = "授权用于展示您的头像,昵称",
    classic = false,
    ...otherProps
  } = props;

  const canIUseGetUserProfile = useMemo(
    () => Taro.canIUse("getUserProfile"),
    []
  );

  const handlerGetUserProfile = useCallback(
    e => {
      wx.getUserProfile({
        desc,
        lang: props.lang,
        success: detail => {
          handler({ ...e, detail });
        }
      });
    },
    [desc, props.lang, handler]
  );

  if (canIUseGetUserProfile && !classic) {
    return (
      <Button
        {...otherProps}
        onClick={handlerGetUserProfile}
        hoverClass={hoverClass}
        className={classNames("yxy-open-ability", className)}
      >
        {props.children}
      </Button>
    );
  }

  return (
    <Button
      {...otherProps}
      openType="getUserInfo"
      onGetUserInfo={handler}
      hoverClass={hoverClass}
      className={classNames("yxy-open-ability", className)}
    >
      {props.children}
    </Button>
  );
};

OpenAbilityUser.options = {
  addGlobalClass: true
};

export default OpenAbilityUser;

但是根据 taro-ui 的那套流程,在执行yarn test:ci 时,测试用例会报如下图一样的错误。
image

有排查了一下原因,是因为taro在执行 taro build --ui 时,会将H5端的函数组件直接编译成class组件。但是并没有处理 hooks
image

这就导致在jest中使用 nerv-server 去render这个组件时,会被识别为不规范的代码

Unable to resolve "./components/calendar

问题步骤:

# 编译 React Native 
npm run dev:rn 

# 在移动端 Expo Client 上扫描二维码后,报错:
Unable to resolve ./components/calendar" from ".//node_modules/taro-ui/dist/weapp/index.js`: The module `./components/calendar` could not be found"

H5编译报错

直接下载仓库代码
输入 tnpm install
输入 npm run dev:h5
报错:
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option, whose value must be a boolean. If you are migrating from Babylon/Babel 6 or want to use the old decorators proposal, you should use the 'decorators-legacy' plugin instead of 'decorators'.

怀疑是是@babel/core 7.0与transform-decorators-legacy插件冲突?

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.