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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@canvases/ifdef-loader

v1.0.1

Published

"A loader used to distinguish and manage the configurations of multiple projects. It utilizes the ifdef conditional compilation directive and clearly marks the configurations of different projects through comments, facilitating development and compilation

Readme

README: UniApp 条件编译 Loader for Webpack + Vue2

本项目提供了一个 Webpack Loader,用于在 Vue2 项目中模拟 UniApp 的条件编译功能。它允许你在 Web 和不同平台(如 APP-PLUSH5MP-WEIXIN 等)之间切换不同的业务代码。


📌 功能特点

支持 #ifdef / #ifndef 条件编译,根据 platform 变量选择代码块。
支持 #elseif / #else,确保只有一个分支生效。
支持 || 逻辑运算,如 APP-PLUS || H5
适用于 Webpack + Vue2 项目,可在运行打包时自动处理条件编译。


📂 使用示例

在 Vue2 组件或 JavaScript 代码中,你可以使用 UniApp 风格的条件编译语法:

// #ifdef APP-PLUS
console.log('This is for APP-PLUS');
// #elseif H5
console.log('This is for H5');
// #else
console.log('This is for other platforms');
// #endif
.cColor {
  color: #ffc400;

  /* #ifdef  APP-PLUS */
  background: #42b983;
  /* #elseif H5  */
  background: #0077ff;
  /* #else */
  background: red;
  /* #endif */
}
<!-- #ifdef APP-PLUS -->
<div class="XAM-only">if APP-PLUS平台显示</div>
<!--  #elseif  H5 || MP-WEIXIN  -->
<div class="app-only">elseif MP-WEIXIN || H5 平台显示</div>
<!--  #else -->
<div class="app-only">else 平台显示</div>
<!-- #endif -->

⚙ 安装与配置

1️⃣ 安装 Loader

npm install @canvases/ifdef-loader

2️⃣ 配置 Webpack

webpack.config.js 中添加 Loader 配置:

module.exports = {
  module: {
    rules: [
        // 处理 Vue 文件
        {
            test: /\.vue$/,
            use: [

                {
                    loader: '@canvases/ifdef-loader',
                    options: { platform: process.env.PLATFORM } // 注入环境变量
                }
            ]
        },
        // 处理 JS 文件
        {
            test: /\.js$/,
            use: [

                {
                    loader: '@canvases/ifdef-loader',
                    options: { platform: process.env.PLATFORM } // 注入环境变量
                }
            ]
        },
        // 处理 CSS/SCSS 文件
        {
            test: /\.(css|scss)$/,
            use: [

                {
                    loader: '@canvases/ifdef-loader',
                    options: { platform: process.env.PLATFORM } // 注入环境变量
                }
        }
    ]
  }
};

package.json 中添加不同平台的构建命令:

"scripts": {
  "serve:h5": "cross-env PLATFORM=H5 vue-cli-service serve",
  "build:h5": "cross-env PLATFORM=H5 webpack --mode production",
  "build:app": "cross-env PLATFORM=APP-PLUS webpack --mode production",
  "build:wx": "cross-env PLATFORM=MP-WEIXIN webpack --mode production"
}

然后,你可以运行:

npm run serve:h5    # 运行 H5 代码
npm run build:h5    # 生成 H5 代码
npm run build:app   # 生成 App 代码
npm run build:wx    # 生成小程序代码