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 🙏

© 2026 – Pkg Stats / Ryan Hefner

my-wx-components

v2.0.0

Published

要将原生微信小程序组件库发布到npm,需要遵循以下步骤:

Readme

原生微信小程序组件库发布到npm的步骤和实现

要将原生微信小程序组件库发布到npm,需要遵循以下步骤:

1. 初始化项目结构

首先创建一个符合微信小程序规范的组件库项目结构:

my-component-library/
├── miniprogram_dist/     # 编译后的代码输出目录
├── src/                  # 源代码目录
│   ├── components/       # 组件目录
│   │   ├── button/       # 按钮组件
│   │   │   ├── button.js
│   │   │   ├── button.json
│   │   │   ├── button.wxml
│   │   │   └── button.wxss
│   │   └── ...           # 其他组件
│   └── utils/            # 工具函数
├── package.json          # npm包配置
├── project.config.json   # 微信开发者工具配置
└── .gitignore            # Git忽略配置

2. 配置package.json

确保package.json包含以下关键配置:

{
  "name": "my-wx-components",
  "version": "1.0.0",
  "description": "微信小程序组件库",
  "main": "miniprogram_dist/index.js",
  "keywords": ["微信小程序", "组件库"],
  "author": "Your Name",
  "license": "MIT",
  "scripts": {
    "build": "npm run clean && npm run compile",
    "clean": "rimraf miniprogram_dist",
    "compile": "copyfiles -u 1 src/**/* miniprogram_dist && babel src --out-dir miniprogram_dist --copy-files"
  },
  "files": ["miniprogram_dist"],
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "babel-loader": "^8.2.2",
    "copyfiles": "^2.4.1",
    "rimraf": "^3.0.2"
  }
}

3. 组件开发

src/components目录下开发组件,例如创建一个简单的按钮组件:

button.json

{
  "component": true,
  "usingComponents": {}
}

button.wxml

<button class="custom-btn {{class}}" bindtap="onTap">{{text}}</button>

button.wxss

.custom-btn {
  padding: 10rpx 20rpx;
  border-radius: 8rpx;
  text-align: center;
}

button.js

Component({
  properties: {
    text: {
      type: String,
      value: '按钮'
    },
    class: {
      type: String,
      value: ''
    }
  },
  methods: {
    onTap() {
      this.triggerEvent('click');
    }
  }
});

5. 构建组件库

执行以下命令编译组件库:

npm run build

const gulp = require('gulp'); const clean = require('gulp-clean'); const babel = require('gulp-babel'); const merge = require('merge-stream'); const through2 = require('through2');

// 清理构建目录 gulp.task('clean', () => { return gulp.src('miniprogram_dist', { allowEmpty: true }) .pipe(clean()); });

// 构建任务 gulp.task('build', gulp.series('clean', () => { // 处理 JS 文件 const jsStream = gulp.src('src//*.js') .pipe(babel({ presets: ['@babel/preset-env'] })) .pipe(gulp.dest('miniprogram_dist')); // 直接复制图片(不压缩) const imageStream = gulp.src(['src//*.{jpg,jpeg,png,gif,svg}','src/components/common.wxss','src/components/config.js']) .pipe(gulp.dest('miniprogram_dist')).pipe(through2.obj(function(file,enc,callback){ this.push(file); callback(); }));;

// 处理非 JS 文件(WXML/JSON/WXSS) const otherFilesStream = gulp.src([ 'src//*.wxml', 'src//.json', 'src/**/.wxss' ]).pipe(gulp.dest('miniprogram_dist')) .pipe(through2.obj(function(file,enc,callback){ this.push(file); callback(); }));

return merge(otherFilesStream,jsStream,imageStream); }));

gulp.task('default', gulp.series('build'));

这会将源代码编译并复制到miniprogram_dist目录。

6. 创建入口文件

miniprogram_dist目录下创建入口文件index.js,导出所有组件:

// miniprogram_dist/index.js
export { default as Button } from './components/button/button';
// 导出其他组件...

7. 注册npm账号并登录

如果你还没有npm账号,先在npm官网注册,然后在命令行登录:

npm login

8. 发布到npm

确保你的组件库目录是干净的,然后执行发布命令:

npm publish

如果是第一次发布,需要先执行npm adduser创建账号。

9. 在小程序中使用

在小程序项目中安装组件库:

npm install my-wx-components --save

在微信开发者工具中,点击"工具" > "构建npm",然后在页面或组件中引入使用:

页面json配置

{
  "usingComponents": {
    "my-button": "my-wx-components/components/button/button"
  }
}

页面wxml使用

<my-button text="点击我" bind:click="handleClick"></my-button>