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

gulp-miniprogram-css-modules

v0.0.4

Published

css-modules for miniprogram in gulp

Readme

gulp-miniprogram-css-modules

小程序的简化版 css-modules,比标准 css-modules 代码量更少的优化方案

介绍

css-modules 是一种 css 模块化方案,它在构建过程中生成一个原类名与新类名的 map,根据 map引用样式,通过设定 hash 规则,实现了对 CSS 类名作用域的限定,它通常用来解决页面类名冲突的问题。 gulp-miniprogram-css-modules 做了哪些事?

  • 新类名单字母编排,减少代码量
  • 移除类名映射 map,替换 js 和 wxml 中变量为编译后类名

标准 css-modules 方案:

import style from './index.scss'           
<view class="{{style.banner}}"></view>     
.index_banner_xkpkl { xx }                             
module.exports ={'banner' : 'index_banner_xkpkl'} // 额外生成的 map 文件

gulp-miniprogram-css-modules 编译后效果:

let style = {}                             
<view class="banner-xjsh236"></view>                     
.a { xx }                                   

安装

npm i gulp-miniprogram-css-modules gulp-sort
// gulpfile.js
const { weappCssModule, wcmSortFn } = require('gulp-miniprogram-css-modules')
const sort = require('gulp-sort');

gulp.task('css-module', () => {
    return gulp.src('./src/**/*')
        .pipe(sort(wcmSortFn))      // 由于处理文件有顺序依赖,需要先对文件排序
        .pipe(weappCssModule())
        .pipe(gulp.dest('./dist'))
})

使用

  1. css 文件改名字: gulp-miniprogram-css-modules 通过 css 文件是否带 module 来识别需要替换的内容

    index.wxss -> index.module.wxss

    // 或者使用scss/其他

    index.scss -> index.module.scss index.acss -> index.module.acss

  2. js 内新增样式文件的引入,目的是建立 css-modules 的样式与 js 关系

    import styles from './index.module.scss
    
    data:{
        ...,
        styles:styles
    }
    
  3. 修改 js 内类名的地方替换为 styles 的间接引入

    query.select('.banner')
    .boundingClientRect()
    .exec(function (res) {...})
    
    // 改为
    query.select('.' + styles['banner'])
    .boundingClientRect()
    .exec(function (res) {...})
    
  4. 修改 wxml 内类名的使用

    4.1. 普通类名

    <view class="banner"></view>
    // 改为
    <view class="{{styles.banner}}"></view>
    // 或者
    <view class="{{styles['banner']}}"></view>

    4.2. 三目运算符

    <view class="banner__dot {{ 'banner__dot--' + (index == swiperCurrent ? 'cur' : '')}"></view>
    
    // 改为
    <view class="{{styles['banner__dot'] + ' ' + (index == swiperCurrent ? styles['banner__dot--cur'] : '')}}"></view>
    // 或者
    <view class="{{`${style['banner__dot']} ${index == swiperCurrent ? style['banner__dot--cur'] : ''}`}}"></view>

    这里需要注意几种有问题的写法:

    4.2.1. 类名间未加空格

    <view class="{{styles['banner__dot'] + (index == swiperCurrent ? styles['banner__dot--cur'] : '')}}"></view>

    4.2.2. 三目表达式未加括号,运算优先级不明

    <view class="{{styles['banner__dot'] + ' ' + index == swiperCurrent ? styles['banner__dot--cur'] : ''}}"></view>

    4.2.3. styles 的属性需要是具体的字符串,不能使用变量表达式(这是 gulp-miniprogram-css-modules 需要单独关注的地方,因为编译阶段会对 styles.xx 进行求值,所以不能把表达式写在属性位置)

    <view class="{{styles['banner__dot'] + ' ' + styles[index == swiperCurrent ? 'banner__dot--cur': '']}}"></view>
  5. 构建过程中关注脚本的红色提示,类似于这种: image-20201026142241488

    这是由于在 js/wxml 内使用了一个banner__swiper_2,而 css 内并没有定义banner__swiper_2,css-module 编译的 map 文件是根据 css 内的样式定义来生成 key 名的,因此styles['banner__swiper_2']undefined, 针对这种情况有两种处理方式:

    5.1. 如果 js 内需要通过这个类名选择到某个元素,但是 css 内不需要编写样式,那么可以将它视为不需要编译的类名,即:

    query.selector('.banner__swiper_2') // 不改成 styles.xx 的写法
    <view class="banner__swiper_2"></view> // 相应的元素也不索引到 styles 
    // 这样实现了一个组件内不会被编译的样式

    5.2. 如果 js 内无引用,那么删掉 wxml 内该类名的定义吧~

  6. 构建完进行检查,关注样式和交互是否正常

参考示例

  • gulp项目:路径 /demo/gulp-project-demo