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

config-reducer

v2.0.5

Published

One config file for development, test and production

Downloads

4

Readme

config-reducer

One config file for development, test and production. :sparkles:

Useful when you want to have simplicity your configuration pipeline. Work with a webpack-merge.

A picture is worth a thousand words:

Example 1 (trivial)

{
  development: {    // <--
    stats: {
      colors: true,
    },
  },
  module: {},
  plugins: [],
}

Output for process.env.NODE_ENV === 'development':

{
  stats: {
    colors: true,
  },
  module: {},
  plugins: [],
}

Output for process.env.NODE_ENV === 'production':

{
  module: {},
  plugins: [],
}

Example 2 (webpack.config.js)

{
  development: {    // <--
    stats: {
      colors: true,
      warnings: true,
    },
  },
  module: {
    rules: [
      {
        include: [paths.dir.app],
        use: [
          {
            loader: 'css-loader',
            options: {
              modules: true,
              development: {    // <--
                minimize: false,
              },
            }
          },
        ],
      },
    ],
  },
  plugins: [
    new Dotenv(config.dotenv),
    {development: () => {    // <--
      const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
      return [
        new HardSourceWebpackPlugin(),
      ]
    }},
    {production: [    // <--
      new webpack.optimize.OccurrenceOrderPlugin(),
      new webpack.HashedModuleIdsPlugin(),
    ]},
  ],
}

Output for process.env.NODE_ENV === 'development':

{
  stats: {
    colors: true,
    warnings: true,
  },
  module: {
    rules: [
      {
        include: [paths.dir.app],
        use: [
          {
            loader: 'css-loader',
            options: {
              modules: true,
              minimize: false,
            }
          },
        ],
      },
    ],
  },
  plugins: [
    new Dotenv(config.dotenv),
    new HardSourceWebpackPlugin(),
  ],
}

Example 3 (babel.config.js)

{
  presets: ['@babel/preset-env'],
  plugins: [
    [
      'emotion',
      {
        hoist: true,
        development: {    // <--
          sourceMap: true,
          hoist: false,
        },
      },
    ],

    'react-hot-loader/babel',
    {development: ['@babel/plugin-transform-react-jsx-source']},    // <--
    {production: [    // <--
      'transform-react-remove-prop-types',
    ]},
  ],
}

Output for process.env.NODE_ENV === 'production':

{
  presets: ['@babel/preset-env'],
  plugins: [
    [
      'emotion',
      {
        hoist: true,
      },
    ],

    'react-hot-loader/babel',
    'transform-react-remove-prop-types',
  ],
}

Installation

$ npm install config-reducer

or

$ yarn add config-reducer

Usage

Copy and paste ;)

const { devProdReducer } = require('config-reducer')

module.exports = devProdReducer({
  development: {
    devtool: 'inline-cheap-module-source-map',
    mode: 'development',
  },
  devtool: 'source-map',
  name: 'client',
  target: 'web',
  // ...
})

API

Import variants:

default

const configReducer = require('config-reducer').default

const commonConfig = {
  development: {
    stats: {
      colors: true,
      warnings: true,
    },
  },
  // ... config -> go to example 2
}

const result = configReducer(commonConfig, process.env.NODE_ENV, [
  'development',
  'production',
])
const configReducer = require('config-reducer').default

module.exports = api => {
  const babelConfig = {
    presets: ['@babel/preset-env', '@babel/preset-react'],
    // ... config -> go to example 3
  }

  return configReducer(babelConfig, api.env(), [
    'development',
    'production',
    'test',
  ])
}

devReducer

const { devReducer } = require('config-reducer')

module.exports = devReducer({
  development: {
    devtool: 'inline-cheap-module-source-map',
    mode: 'development',
  },
  devtool: 'source-map',
  name: 'client',
  target: 'web',
  // ... config -> go to example 2
})

prodReducer

const { prodReducer } = require('config-reducer')

testReducer

const { testReducer } = require('config-reducer')

devProdReducer

const { devProdReducer } = require('config-reducer')

module.exports = devReducer({
  development: {
    devtool: 'inline-cheap-module-source-map',
    mode: 'development',
  },
  output: {
    production: {
      filename: '[name].[chunkhash].js',
    },
    filename: '[name].js',
  },
  devtool: 'source-map',
  name: 'client',
  target: 'web',
  // ... config -> go to example 2
})

devTestReducer

const { devTestReducer } = require('config-reducer')

prodTestReducer

const { prodTestReducer } = require('config-reducer')

devProdTestReducer

const { devProdTestReducer } = require('config-reducer')

Parameters:

Module steps:

  1. find and prepare (deep duplicate) - input
  2. apply to parent - reducer
  3. delete references - cleanup
devProdReducer(input, reducer, cleanup)

input

Type: Object|Array Required: true

Your configuration object or array variants:

[{ development: [] }]
{{ development: {} }}
[{ development: () => [] }]

The function warrant is intended to be postponed until the configurations are reduced. Thanks to this, we do not have to import modules from which we do not use:

1 Install module for development mode

$ yarn add --dev hard-source-webpack-plugin

2 Configuration

{
  plugins: [
    new Dotenv(config.dotenv),
    {development: () => {
      const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
      return [
        new HardSourceWebpackPlugin(),
      ]
    }},
  ],
}

3 Compile production version - work fine, without hard-source-webpack-plugin

$ yarn prod

Useful in Dockerfile.

{{ development: () => {} }}

reducer

Type: string Default: process.env.NODE_ENV

This key will be merged.

cleanup

Type: boolean|string|Array<string> Default: true

These keys will be deleted.

  • false: do nothing
  • true: delete reducer
  • string: delete key
  • array of strings: delete keys

Real examples

TODO

  • array/object reducer
  • reducer in reducer / nested reducers
  • alias ex: test = _test

Issues

If you find a bug, please file an issue on issue tracker on GitHub.

License

MIT