npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@m-fe/taro-common

v0.0.1

Published

Taro 第三方 UI 库示范用例

Downloads

3

Readme

基于 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