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

weblo

v1.0.0

Published

Webpack Environment Based Loader Options

Downloads

4

Readme

Webpack Environment Based Loader Options

WEBLO allows you to add custom keys to your loader options based on the environment you are currently running in.

NOTE this will only work with Webpack 2 which (at the time of writing) is currently only available as a release candidate.

In the past developers have sometimes needed to define their webpack loaders twice (or more) once for development and once for production often with repeated options, like so:

webpack.dev.config:

rules: [
    test: /\.css$/,
    use: [
        {
            loader: 'css-loader',
            options: {
                url: false,
                minimize: false,
                sourceMap: true,
            },
        },
    ],
],

webpack.prod.config:

rules: [
    test: /\.css$/,
    use: [
        {
            loader: 'css-loader',
            options: {
                url: false,
                minimize: true,
                sourceMap: false,
            },
        },
    ],
],

Or alternatively one could use ugly ternaries that require a development or production environment to be defined, there can also only be two environments when using this kind of boolean logic, like so:

webpack.config:

rules: [
    test: /\.css$/,
    use: [
        {
            loader: 'css-loader',
            options: {
                url: false,
                minimize: development ? false : true,
                sourceMap: development ? true : false,
            },
        },
    ],
],

WEBLO fixes this by adding two additional keys to the loader object (development and production), additional keys can be defined in the package.json if needed.

So the new syntax looks like this.

webpack.config:

const weblo = require('weblo');

let config = {
    module: {
        rules: [
            test: /\.css$/,
            use: [
                {
                    loader: 'css-loader',
                    options: {
                        url: false,    
                    },
                    development: {
                        sourceMap: true
                    },
                    production: {
                        minimize: true,
                    },
                },
            ],
        ],    
    },
},

weblo('development', config);

The above code will merge the development object with the options object. If the options key does not exist it will create it.

Or if you like environment variables you can initiate like below:

weblo(JSON.stringify(process.env.NODE_ENV), config);

If you wish to add your own keys you can add a weblo key to your projects package.json, like so:

"weblo": [
	"testing",
	"development",
	"production"
]

NOTE if you do this you will need to difine all of your keys i.e. it will not retain the existing development and production keys.

This plugin might not be the best implementation to solve this problem, but it works so far, I do plan to intergrate it more tightly as a webpack plugin and perhaps match the syntax better but for now PR's are more than welcome.