(Translated by https://www.hiragana.jp/)
GitHub - m-Ryan/css-color-extract-plugin: webpack 主题颜色提取插件
Skip to content

webpack しゅ题颜しょくひっさげ插件

Notifications You must be signed in to change notification settings

m-Ryan/css-color-extract-plugin

Repository files navigation

css-color-extract-plugin


Install

npm install css-color-extract-plugin

yarn add css-color-extract-plugin

  • 该插けん主要しゅようよう于提しゅ题颜しょく
  • ひっさげいたてきcssすうすえかい挂载到window
  • つう过颜しょくがえ换再插入そうにゅういた<style>,达到动态おさむあらためぬし题的目的もくてき

演示图

Usage


// webpack.config.js

const CssColorExtractPlugin = require('css-color-extract-plugin').default;
const PRIMARY_COLOR = '#1890ff';
module.exports = {
    ...
    module: {
        rules: [
          {
            test: /\.css$/,
            exclude: '/\.module\.css$/',
            use: [
                "style-loader", 
                "css-loader", 
                {
                  loader: CssColorExtractPlugin.loader,
                  options: {
                    colors: [ PRIMARY_COLOR ]
                  }
              },
           ]
        },
        {
            test:  /\.module\.css$/,
            use: [
                "style-loader", 
                 {
                   loader: "css-loader", 
                   options: {
                     modules: true,
                     localIdentName: '[path][name]__[local]',
                   }
								},
                {
                    loader: CssColorExtractPlugin.loader,
                    options: {
                    colors: [ PRIMARY_COLOR ],
                    modules: true,
                    localIdentName: '[path][name]__[local]',
                  }
               },
            ]
        }
      ]
    }
   ...
   	plugins: [
      ...
      new CssColorExtractPlugin({ fileName: 'theme' }),
     ]
};

编译きさきかいざいhtmlちゅう插入そうにゅうtheme.js,其内容ないよう类似以下いか

window.CSS_EXTRACT_COLOR_PLUGIN = [
  {"source":".src-App-module__example {  background: #1890ff;}","fileName":"App.module.scss","matchColors":["#1890ff"]},
  {"source":".src-controller-blog-components-Header-Header-module__theme {  color: #067785;}.src-controller-blog-components-Header-Header-module__pc_header {  background: #1890ff;}.src-controller-blog-components-Header-Header-module__mb_header {  background: #1890ff;}.src-controller-blog-components-Header-Header-module__mb_header .src-controller-blog-components-Header-Header-module__mb_nav {  background: #1890ff;}","fileName":"Header.module.scss","matchColors":["#1890ff"]}
];

接着せっちゃくただよう使用しよう简单てきせい则即がえ换主题色

import React, { Component } from 'react';
import styles from './App.module.scss';
import { SketchPicker } from 'react-color';

function replaceColor(source, color, replaceColor) {
	return source.replace(new RegExp(`(:.*?\\s*)(${color})(\\b.*?)(?=})`, 'mig'), (group) => {
		return group.replace(new RegExp(`${color}`, 'mig'), replaceColor);
	});
}

const PRIMARY_COLOR = '#1890ff';

class App extends Component {
	
	async setColor(color) {
		const styleData = window.CSS_EXTRACT_COLOR_PLUGIN || [];
		const cssText = styleData.map((item) => item.source).join('');
		const styleText = replaceColor(cssText, PRIMARY_COLOR, color);
		const style = document.createElement('style');
		style.innerHTML = styleText;
		document.body.appendChild(style);
	}
	render() {
		return (
			<div className={styles['example']}>
				<SketchPicker onChangeComplete={(colorResult) => this.setColor(colorResult.hex)} />
			</div>
		);
	}
}

export default App;

loader Options

 {
	colors: string[]; // ひきはいてき颜色すう组,如果现颜しょく层次错误覆盖的じょう况,需要じゅよう选上被覆ひふく盖的颜色,どおり过该选项ざい不同ふどうてきぶんけんひっさげ不同ふどうてき颜色
	only?: boolean = true; // 仅提选中颜色规则,いや则会はたせい个文けんひっさげ进去
	modules?: boolean = false; 
	localIdentName?: string = '';
}

plugin Options

  {
    fileName?: string; // ひっさげ颜色てきぶん件名けんめい,提供ていきょう直接ちょくせつはまざい script标签ちゅう
    json?: boolean = false; // 提起ていきいたjson
	  variableName?: string = 'CSS_EXTRACT_COLOR_PLUGIN'; // 挂载到windowてき变量めいだま认 CSS_EXTRACT_COLOR_PLUGIN
}

example

一个更复杂的例子-RyanCMS内容ないよう管理かんりけい

About

webpack しゅ题颜しょくひっさげ插件

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published