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

vite-plugin-conditional-compile

v1.4.5

Published

<h2 align='center'>vite-plugin-conditional-compile</h2>

Downloads

388

Readme

English | 简体中文 | 1.3.x旧版本

功能特性

  • 支持根据自定义的条件配置选择性地包含或排除代码块
  • 可以在开发环境和生产环境中使用不同的代码逻辑
  • 灵活的条件表达式,支持逻辑运算和环境变量
  • 可以在 Vite 构建过程中动态生成不同的输出文件

安装

yarn add vite-plugin-conditional-compile -D

使用

务必将该插件的加载顺序放在最前位,防止 jsx 之类的代码被其他插件编译,导致本插件无法解析

// vite.config.ts
import { defineConfig } from "vite";
import vitePluginConditionalCompile from "vite-plugin-conditional-compile";

export default defineConfig({
    plugins: [vitePluginConditionalCompile({
      // 插件配置项...
    }),
    // 其他插件...
  ],
});

配置项

该插件支持以下配置项:

  1. include: string | RegExp | readonly (string | RegExp)[] | null 需要进行条件编译的文件路径或文件匹配模式。

    默认值: ['**/*']

    示例:

       {
         include: [/^.+\/packages\/.+\/.+.(ts|tsx)$/],
       }
  2. exclude: string | RegExp | readonly (string | RegExp)[] | null 需要排除条件编译的文件路径或文件匹配模式。

    默认值: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/]

    示例:

       {
          exclude: ['/\.eslintrc.js/']
       }
  3. env: Record<string, any> 自定义环境变量选项,用于在条件编译语句中使用自定义的环境变量。示例:

    {
      'MY_VARIABLE': 'my value',
      // 更多自定义环境变量...
    }

    同时,本插件会调用 vite 提供的 loadEnv 来获取项目的环境变量。

示例

// vite.config.ts
import { defineConfig } from "vite";
import vitePluginConditionalCompile from "vite-plugin-conditional-compile";

export default defineConfig({
    plugins: [vitePluginConditionalCompile({
      env:{
        //自定义环境变量, 提供给条件语句使用
        feature: 'code',
        prod_version: 'vite'
      }
    }),
  ],
});


// main.jsx
// #if [DEV]
import { featureA } from './featureA.js';

// 这里的 PROD 取至 项目的环境变量, 若当前为生产环境时将加载此条 import 语句
// #elif [PROD]
import { featureB } from './featureB.js';

//这里的 feature 和 prod_version 取至用户的 env 配置项, 判断 feature 的值是否与 ”code“ 相同、prod_version的值是否不等于 ”vite“. 当满足其中之一时, 该条件成立
// #elif [feature=code || !(prod_version=vite)]
import { featureC } from './featureC.js';

// #else
import { featureD } from './featureD.js';

// #endif

// #if [DEV]
console.log('Development environment');
// #endif

// #if [PROD]
console.log('Production environment');
// #endif

// #if [!!API_URL]
fetch(import.meta.env.API_URL)
  .then(response => response.json())
  .then(data => console.log(data));
// #endif

const Com = () => {
return (
    <div>
      {/* #if [PROD] */}
      <span>prod mode</span>
      {/* #elif [DEV] */}
      <span>dev mode</span>
      {/*#else */}
      <span>unknown mode</span>
      {/* #endif */}
    </div>
  );
}
<script setup lang="ts">
// #if [DEV]
import { featureA } from './featureA.js';
// #endif

</script>

<template>

  <!-- #if [DEV] -->
  <router-link to="/debug">
    <span class="m-2 text-green-500">demo</span>
  </router-link>

  <!-- #elif [PROD] -->
 <router-link to="/prod">
    <span class="m-2 text-green-500">demo</span>
  </router-link>

  <!-- #else -->
  <router-link to="/helloWorld">
    <span class="text-green-500">helloWorld</span>
  </router-link>
  <!-- #endif -->

  <router-view />
</template>

支持的条件语句

注: 条件需要使用 [] 进行包裹

  1. 等式: feature=codeprod_version=4.5.0 等式右侧支持字符以及数字类型。
  2. 不等式: prod_version != vite
  3. 逻辑语句: feature=code || !(prod_version=vite) 支持嵌套逻辑语句。
  4. 变量: DEV 判断 env['DEV'] 的值是否为 true。
  5. 取反符号: !PROD 判断 env['PROD'] 的值是否为 false,可以使用双重取反来模拟 #ifdef。

VS Code 插件

后续将会支持 VS Code插件来高亮条件以及语法校验等功能。