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

taro-global-component

v1.0.1

Published

## 缘由

Downloads

8

Readme

Taro React 小程序注入全局组件

缘由

众所周知,小程序中不能定义全局组件,如果我们想自定义一个 modal 弹窗,来替代 wx.showModal 的话,则需要在每个页面手动的引入组件。随着项目越来越大,手动引入组件无疑是繁琐和低效的,因而抽空开发了这个 webpack-loader 代替手动操作。

作用

为每个页面注入全局组件。支持主包和分包,支持类写法和函数写法。 支持 const 定义函数组件,底部默认导出 支持 connect 高阶组件包裹 支持小程序分包

环境

taro react

发版

npm publish

安装

npm install taro-global-component -D

配置

配置项

  • importSpecifier 导入标识符
  • componentName 导入的组件名称
  • isPage 判断当前遍历到的文件是否为页面(可选配置)

isPage 不传的情况下,默认会将 src/pages/页面名称/index.[tj]sxsrc/package-模块名称/pages/页面名称/index.[tj]sx 这两种情形下的文件识别为页面。

配置示例

  webpackChain(chain) {
    chain.merge({
      module: {
        rule: {
          injectBaseComponentLoader: {
            test: /\.tsx$/,
            use: [
              {
                loader: 'taro-global-component',
                options: {
                  // 导入标识符
                  importSpecifier: '@components/BaseComponent',

                  // 导入组件名
                  componentName: 'BaseComponent',

                  // 判断遍历到的文件是否为页面
                  isPage(filePath) {
                    // 兼容 windows 路径
                    const formatFilePath = filePath.replace(/\\/g, '/')
                    let isContainer = /(subpackage|pages)?(subpackage|pages)\/.+\/index\.tsx$/.test(formatFilePath)
                    let isNot = /^((?!components).)*index((?!components).)*$/.test(formatFilePath)
                    return isContainer && isNot
                  }
                },
              },
            ],
          },
        },
      },
    });
  },

注入后的代码示例

import { BaseComponent } from '@components/BaseComponent'

...

<BaseComponent />

...

代码示例

ts 版本

js 版本