terence55 / themes-switch

Toolset for switch multiple themes in application based on webpack
54 stars 18 forks source link

themes-switch

Features

For Hot reload

For Webpack v3

Installation

npm install themes-switch --save-dev

Usage

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'
    })
  ]
};
src
  - themes
    - dark.less
    - default.less
    - light.less
@import 'light.less';
@color-main: #222A42;
@color-text: #FFF;
@import 'default.less';

.main {
  background: @color-main;
}
import { getThemes } from 'themes-switch';

// ...
const themes = getThemes();
// ...

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

changeTheme

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

Options

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.