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

branding-webpack-plugin

v1.1.0

Published

Cross platform theming with CSS variables

Downloads

5

Readme

Branding plugin 🎨 Build Status

NPM

Use CSS custom properties to theme your app with fallbacks for browsers that don't support it (tested on IE11).

What it does

Branding plugin allows you to import CSS variables that get applied to the page. The CSS variables are defined in a .branding file. When imported, they get applied to the page, therefore any usage of the CSS variable gets the theme's colour. You also get access to the colour values in JavaScript.

Branding plugin creates a nice fallback for IE 11 by creating an overrides stylesheet that gets loaded only when the browser doesn't support CSS variables.

The end result is that you can easily import a theme's colours without worrying about browser support.

📃 Blog post: You can read more about how it works here

Installation and configuration

You'll need webpack 4.

npm i --save-dev branding-webpack-plugin

A non-supported webpack 3 version also exists. You can install it by running npm i --save-dev branding-webpack3-plugin

In your webpack config, create a new instance of BrandingPlugin and add its CSS loader to your CSS pipeline, and its loader for *.branding files.

const BrandingPlugin = require('webpack-branding-plugin');
const brandingPlugin = new BrandingPlugin();
module.exports = {
    module: {
        rules: [
            {test: /\.branding$/, use: [brandingPlugin.createLoader()]},
            {test: /\.css$/, use: [{loader: 'style-loader'}, {loader: 'css-loader'}, brandingPlugin.createCSSLoader()]}
        ]
    },
    plugins: [brandingPlugin]
};

Usage

Say you have your app's CSS in app.css and some branding/theming colours in theme.branding.

app.css

body {
    color: black;
    background: var(--my-theme-color);
}

theme.branding

:root {
    --my-theme-color: red;
}

Then, you can import theme.branding in your entry JS file:

index.js

import './app.css'; // uses CSS and style loader

// uses branding plugin and loader to apply the CSS variables to the page,
// with fallbacks for browsers that don't support it!
import brandingVars from './theme.branding';

console.log(brandingVars); // Output: {'--my-theme-color': 'red'}

This also works with dynamic imports import('./theme.branding'), which means you can dynamically import a theme at runtime e.g. import(`./theme-${confirm('Enter theme name')}.branding`). You can read the blog post to learn more about a real use case.

Example

Take a look at the examples folder for a working example of how it works.

Caveats

  • CSS overrides are used to override app CSS when the browser doesn't support custom properties. This has specificity implications.
  • There is a known issue where Edge (which supports custom properties) doesn't load images from the correct relative root.

Contributions

Contributions welcome via issues and pull requests! Read our contribution guide here.

There is a basic test to ensure the plugin works with and without CSS variable support.

To run the tests, run:

npm test

You can also run the test server in the background using npm run serve-test and run jest directly in another terminal using npm run jest.