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

brunch-feature-switcher

v1.0.2

Published

__How to install:__ - `yarn install brunch-feature-switcher` - `npm i brunch-feature-switcher`

Downloads

11

Readme

Plugin that hepls to add/remove/replace code based on included features

How to install:

  • yarn install brunch-feature-switcher
  • npm i brunch-feature-switcher

Get Started:

Example:

// #brunch-config.js
module.exports = {
  plugins: {
    featureSwitcher: {
      features: {
        // set feature on
        billing: false,
        // Retrieve from env vars
        stat: process.env.FEATURE_STAT === 'true',
      },
    },
  },
}

// #some.code.js

/* @feature billing:off:remove */
import billingUtils from './billingUtils'
import someOtherStuff from './someOtherStuff'

/* @feature billing:off:remove */
const BILLING_IS_OK = 'status_billing_is_ok'

/* @feature billing:on:remove */
const billingFaker = () => { /* ... */ };

class Checker {
  constructor(...) {
    ...somecode
  }

  get billingKeys() {
    /* @feature billing:off:inl_replace:/return []/ */
    return self.keys.filter(({ billing }) => billing)
  }

  get devKeys() {
    return self.keys.filter(({ dev }) => dev)
  }

  get allKeys() {
    return [...this.devKeys, ...this.billingKeys]
  }
}

It produces the following code after brunch build if billing feature is off

Example: Result after build

import someOtherStuff from './someOtherStuff'

const billingFaker = () => { /* ... */ };

class Checker {
  constructor(...) {
    ...somecode
  }

  get billingKeys() {
    return []
  }

  get devKeys() {
    return self.keys.filter(({ dev }) => dev)
  }

  get allKeys() {
    return [...this.devKeys, ...this.billingKeys]
  }
}

It produces the following code after brunch build if billing feature is on.

import billingUtils from './billingUtils'
import someOtherStuff from './someOtherStuff'

const BILLING_IS_OK = 'status_billing_is_ok'

class Checker {
  constructor(...) {
    ...somecode
  }

  get billingKeys() {
    return self.keys.filter(({ billing }) => billing)
  }

  get devKeys() {
    return self.keys.filter(({ dev }) => dev)
  }

  get allKeys() {
    return [...this.devKeys, ...this.billingKeys]
  }
}

Short explanation:

This plugin adds some sort of pre-build directives that help you to remove code blocks/expressions/ and other code stuff based on state of features.

To mark blocks/exprs/other for handling you should use a comment with defined format:

  • /* @feature _name_:_state_:_applied_action_:_args_ */
  • // @feature _name_:_state_:_applied_action_:_args_

Example:


const dropdownItems = [
  /* @feature billing:off:remove */
  {
    title: 'Hello',
  },
  /* @feature billing:on:remove */
  {
    title: 'World',
  }
]

/* @feature billing:off:inl_replace:/throw new Error('error')/ */
const a = [1]

You can read this pre-build directive as:

  • if billing feature is off, then remove {title: 'Hello'}
  • if billing feature is on, then remove {title: 'World'}
  • if billing feature is off, then replace const a = [1] with throw new Error('error')

Multiple features in directive:

You can list more than one features in directive thru commas or spaces, if code existence depends on more than one condition:


const dropdownItems = [
  /* @feature billing:off:remove, stat:off:remove */
  {
    title: 'Hello',
  },
  /* @feature billing:on:remove, stat:on:remove */
  {
    title: 'World',
  }
]

/* @feature billing:off:inl_replace:/throw new Error('error')/ */
const a = [1]

NB!: If you set a few features in row it will work as logical and, also be careful: if one of the actions should replace something it should be listed as last:

/* @feature n1:off:remove, n2:off:inl_replace:/[1, 2, 3]/ */

NB!: If pay your attention to inl_replace action you will see that arg is surrouneded by /, it made on purpose of escaping. (cuz spaces and commas tells parser to handle new feature, but we want to avoid that in case of args)


Important !

You should move brunch-feature-switcher up in package.json (to make it stand before all other code transformation (es6 to es5 transformation for instance))

Packages order


TODO:

  • Going to add complex replacment procedure
  • Add block directives, such as:
    /* @feature:begin name:state:action:args */
    ...some code...
    /* @feature:end */
  • Add docs and more examples