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

themes-switch

v1.1.0

Published

Toolset for switch multiple themes in application based on webpack

Downloads

387

Readme

themes-switch

Features

  • Multiple themes supported via custom variables.
  • Generating themes via webpack.
  • Themes switch without page reload.
  • Supported formats: css, less, postcss, sass.

For Hot reload

  • Hot-reload is supported in v1.1.x, new theme files will be generated when a reload is triggered.
  • When hot-reload is enabled, the temp directory will not be cleared if webpack-dev-server was running.
  • To improve performance, dynamic addition of new theme files is not supported, please restart the server when adding new files.

For Webpack v3

  • Webpack v3 is no longer supported from v1.1.x, please use v1.0.x instead.
  • In the version v1.0.7 themes-switch replaces extract-text-webpack-plugin with mini-css-extract-plugin, and upgrade peerDependency to Webpack 4.3.x. Now the option themesLoader is deprecated. If you are using Webpack 3.x and extract-text-webpack-plugin, view the docs here.

Installation

npm install themes-switch --save-dev

Usage

  • Config themes-switch in webpack.config.js, and put MiniCssExtractPlugin.loader in your less/sass/postcss/css loaders.
const ThemesGeneratorPlugin = require('themes-switch/ThemesGeneratorPlugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: {
    main: './src/main.js'
  },
  output: {
    filename: '[name]-[contenthash].js',
    chunkFilename: '[name]-[contenthash].js',
    path: `${__dirname}/build`,
    publicPath: ''
  },
  module: {
    rules: [
      // ...
      {
        test: /\.(less|css)$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
      }
    ]
  },
  plugins: [
    new ThemesGeneratorPlugin({
      srcDir: 'src',
      themesDir: 'src/assets/themes',
      outputDir: 'static/css',
      defaultStyleName: 'default.less'
    })
  ]
};
  • Create following directory for themes, and set it to option themesDir:
src
  - themes
    - dark.less
    - default.less
    - light.less
  • Import your default theme into default.less:
@import 'light.less';
  • Specify theme variables:
@color-main: #222A42;
@color-text: #FFF;
  • Import default.less when using theme variables:
@import 'default.less';

.main {
  background: @color-main;
}
  • ThemesGeneratorPlugin scans files in themesDir and files that import default.less, then generates separated files for all themes automatically.

  • You can access the themes info via process.themes in your code, value such as { 'theme-dark': 'css/dark.css', 'theme-light': 'css/light.css' }, or call getThemes method directly.

import { getThemes } from 'themes-switch';

// ...
const themes = getThemes();
// ...
  • Call switchTheme method to switch to new theme by pass theme name.

Switch themes in your code

import { switchTheme } from 'themes-switch';

// ...
switchTheme({ theme: 'themes-dark' });
// ...

Options

| Name | Description | Type | Default Value | | -------- | ----------- | ---- | ------------- | | srcDir | Souce code directory | {String} | | | themesDir | Directory of themes | {String} | | | outputDir | Directory of generated files | {String} | | | defaultStyleName | File name of default style, specify it when you use different style formats | {String} | default | | clearTemp | Delete temp directory when webpack was done | {Boolean} | true | | disable | Disable the plugin | {Boolean} | false | | useStaticThemeName | Whether to add random number to file names of themes | {Boolean} | false | | ignoredFilesInThemesDir | Files that will be ignored in themes directory | {String} | | | usePureCSS | If you only use pure CSS, you need to explicitly declare | {Boolean} | false | | enableHotReload | Whether to generate new files for webpack hot reload | {Boolean} | false |

Methods

switchTheme

switchTheme({ theme: 'themes-dark', onLoad: onLoadFunc });

Options

  • theme: new theme name, such as theme-dark.
  • onLoad: callback when new link was loaded.

changeTheme

changeTheme('themes-dark', 'css/dark.css', onLoadFunc);

Options

  • theme: new theme name, such as theme-dark.
  • themeUrl: new theme url, such as css/dark.css. You can get the value from process.themes
  • onLoad: callback when new link was loaded.

getThemes

const themes = getThemes();
// { 'theme-dark': 'css/dark.css', 'theme-light': 'css/light.css' }

ThemesGeneratorPlugin.clearTemp

It will delete temp directory. You can call the method as needed, e.g. when you stop webpack-dev-server in development mode.