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

unplugin-conditional-definition

v1.0.1

Published

A plugin that selectively compiles code based on environmental variables,for Vite & Webpack & Esbuild & Rollup

Downloads

89

Readme

unplugin-conditional-compilation

A plugin that selectively compiles code based on environmental variables,for Vite & Webpack & Esbuild & Rollup

Install

[npm|pnpm] i unplugin-conditional-definition -D

or

yarn add unplugin-conditional-definition -D

Demo

Example: playground/

// vite.config.ts
import viteConditionalDefinition from 'unplugin-conditional-definition/vite'

export default defineConfig({
  plugins: [
    viteConditionalDefinition({
      /**
       * your enviorment string
       * @type string[]
       */
      env: [],
      // type : 'strict' | 'ignore' | 'transform'
      mode: 'strict',
      // filters for transforming targets
      exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.svn[\\/]/],
    }),
  ],
})

// rollup.config.js
import rollupConditionalDefinition from 'unplugin-conditional-definition/rollup'

export default {
  plugins: [
    rollupConditionalDefinition({
      /**
       * your enviorment string
       * @type string[]
       */
      env: [],
      // type : 'strict' | 'ignore' | 'transform'
      mode: 'strict',
      // filters for transforming targets
      exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.svn[\\/]/],
    }),
  ],
}

// webpack.config.js
const webpackConditionalDefinition = require('unplugin-conditional-definition/webpack').default
const ConditionalDefinitionLoader = require('unplugin-conditional-definition/webpack').loader
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
  /* ... */
  module: {
    rules: [
      /* ... */
      // you must use the loader to transform your vue code
      {
        test: /\.vue$/,
        use: ['vue-loader', ConditionalDefinitionLoader + '.cjs'],
      },
      /* ... */
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    webpackConditionalDefinition({
      /**
       * your enviorment string
       * @type string[]
       */
      env: [],
    }),
  ],
}

// Not support.
// The esbuild will remove almost all comments in the code.

// rspack.config.js
const RspackPlugin = require('unplugin-conditional-definition/rspack').default
const ConditionalDefinitionLoader = require('unplugin-conditional-definition/webpack').loader
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
  moudle:{
    rules:{
      /* ... */
      {
        test: /\.vue$/,
        // rspack loader only support cjs files
        use: ['vue-loader', ConditionalDefinitionLoader + '.cjs'],
      },
      /* ... */
    }
  },
  plugins: [
    new VueLoaderPlugin(),
    RspackPlugin({
      /**
       * your enviorment string
       * @type string[]
       */
      env: [],
    }),
  ],
}

// rolldown.config.js
import { defineConfig } from 'rolldown'
import Rolldown from 'unplugin-conditional-definition/rolldown'

export default defineConfig({
  plugins: [
    Rolldown({
      /**
       * your enviorment string
       * @type string[]
       */
      env: [],
    }),
  ],
})

Configuration

The following shows the default values of the configuration

ConditionalDefinition({
  /**
   * your enviorment string
   * @type string[]
   */
  env: [],
  /**
   * This option controls the format of the comments.
   * The `strict` mode will throw an Error if you write comments in the wrong format.
   * The `transform` mode will try to transform your comments to the correct format.
   * The `ignore` mode will ignore the format check.
   * @type 'strict' | 'ignore' | 'transform'
   */
  mode: 'strict',
  /**
   * Whether js compilation is enabled
   * @default true
   */
  js?: boolean
  /**
   * Same as js
   * @default false
   */
  css?: boolean
  /**
   * Same as js
   * Webpack and Rspack does not support. If you want to transform .vue files, you must add loader after the vue-loader
   * @default false
   */
  vue?: boolean
  /**
   * Same as js
   * @default false
   */
  html?: boolean
  // filters for transforming targets
  exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.svn[\\/]/],
})

Usage

Here suppose the environment variable is LABTOP.

After using the plugin, some specific comments will be used to transform the code. Things to note here:

  1. Comments should be written in all uppercase letters, with - concatenation, and multiple variables separated by |. Pay attention to the value of mode, which defaults to strict.
// base usage
// input
// #ifdef MOBILE | SOMETHING-ELSE
console.log('mobile')
// #endif
// #ifndef MOBILE
console.log('not mobile')
// #endif

// output
// #ifdef MOBILE | SOMETHING-ELSE
// #endif
// #ifndef MOBILE
console.log('not mobile')
// #endif
  1. Comments must be closed and in the same scope.
// Each of these actions results in an error being thrown.
// #ifndef MOBILE
function test1() {
  // #endif
}
function test2() {
  // #ifndef MOBILE
}
// #endif
function test3() {}
// #endif
// #ifndef MOBILE
function test4() {}
  1. Comments of different types should not be shared.
//  will throw err
/* #ifndef MOBILE */
console.log('mobile')
// #endif
  1. For vue single-file components, you can't put comments at the top of the scope.
<!-- error -->
<!-- #ifndef MOBILE -->
<template>
  <div>
    <div>mobile</div>
  </div>
</template>
<!-- #endif -->

<!-- corrent -->
<template>
  <div>
    <!-- #ifndef MOBILE -->
    <div>mobile</div>
    <!-- #endif -->
  </div>
</template>
  1. Single-line comments are not allowed in css files.
/* error */
.test{
  // #ifndef MOBILE
  color: red;
  // #endif
}
/* corrent */
.test{
  /* #ifndef MOBILE */
  color: red;
  /* #endif */
}
  1. Only js and jsx(include ts and tsx) files are checked by default. This is done to improve compilation efficiency.

CHANGELOG

You can see CHANGELOG here.

License

MIT License © 2024-PRESENT lykl