1.0.2 • Published 2 years ago

dynamic-theme-webpack-plugin v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@dynamic/dynamic-theme-webpack-plugin

添加动态主题的 webpack 插件

Usage

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const DynamicThemeWebpackPlugin = require('@dynamic/dynamic-theme-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  mode: 'development',
  devtool: false,
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'build'),
  },
  module: {
    rules: [
      {
        test: /\.less$/,
        exclude: [/node_modules/],
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true,
              importLoaders: 1,
            },
          },
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true,
              },
            },
          },
        ],
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'public/index.html'),
      filename: 'index.html',
    }),
    new DynamicThemeWebpackPlugin({
      lessVarFile: path.resolve(__dirname, './src/var/var.less'),
    }),
  ],
};

说明

DynamicThemeWebpackPlugin 是基于 css 变量的换肤方案

  • 定义变量的方式一
    • lessVarFile 指定定义 css 变量的 less 文件
  • 定义变量的方式二
    • theme
theme = {
	--background: 'red',
	--fontColor: '#f0f0f0'
}

DynamicThemeWebpackPlugin 会将定义的变量提取到单独的 style 标签里,放在 :root{} 下

:root {
	--background: red,
	--fontColor: #f0f0f0
}

在代码中使用 CSS 变量定义颜色

.box{
	background: var(--background)
}

@dynamic/theme

@dynamic/theme 下提供 updateTheme 可以实现修改变量,进行换肤