All Projects → intoli → Antd Scss Theme Plugin

intoli / Antd Scss Theme Plugin

Licence: other
A Webpack plugin for customizing Ant Design with an SCSS theme file and using Ant Design's compiled variables in SCSS files throughout your project.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Antd Scss Theme Plugin

dva-typescript-antd-starter-kit
A admin dashboard application demo based on antd by typescript and dva
Stars: ✭ 61 (-65.34%)
Mutual labels:  webpack-plugin, antd
Antd Dayjs Webpack Plugin
⏰ Day.js webpack plugin for antd
Stars: ✭ 215 (+22.16%)
Mutual labels:  webpack-plugin, antd
Clean Webpack Plugin
By default, this plugin will remove all files inside webpack's output.path directory, as well as all unused webpack assets after every successful rebuild.
Stars: ✭ 1,888 (+972.73%)
Mutual labels:  webpack-plugin
React Native Responsive Styles
React Native styles that respond to orientation change
Stars: ✭ 173 (-1.7%)
Mutual labels:  stylesheets
Multipage Webpack Plugin
A plugin that makes handling templates and asset distribution for multi-page applications using webpack trivial
Stars: ✭ 168 (-4.55%)
Mutual labels:  webpack-plugin
React Webpack4 Cook
💯The most powerful webpack4 tutorial in the universe
Stars: ✭ 152 (-13.64%)
Mutual labels:  webpack-plugin
React Core Boilerplate
Powerful ASP.NET Core 3 templates with React, true server-side rendering and Docker support
Stars: ✭ 169 (-3.98%)
Mutual labels:  webpack-plugin
Nodemon Webpack Plugin
A webpack plugin that uses Nodemon to start and reload the server.
Stars: ✭ 150 (-14.77%)
Mutual labels:  webpack-plugin
Scout
可能是东半球最灵活的 URL 监控系统
Stars: ✭ 174 (-1.14%)
Mutual labels:  antd
Webpack.js.org
Repository for webpack documentation and more!
Stars: ✭ 2,049 (+1064.2%)
Mutual labels:  webpack-plugin
Fc Angular
快速搭建angular后台管理系统的admin template。Fast development platform based on angular8, ng.ant.design built multi-tab page background management system (continuous upgrade) ^_^
Stars: ✭ 171 (-2.84%)
Mutual labels:  antd
Virtual Module Webpack Plugin
Adds the contents of a virtual file to webpack's cached file system without writing it to disk
Stars: ✭ 165 (-6.25%)
Mutual labels:  webpack-plugin
React Refresh Webpack Plugin
A Webpack plugin to enable "Fast Refresh" (also previously known as Hot Reloading) for React components.
Stars: ✭ 2,413 (+1271.02%)
Mutual labels:  webpack-plugin
Html Res Webpack Plugin
plugin for generating html in webpack
Stars: ✭ 170 (-3.41%)
Mutual labels:  webpack-plugin
React Admin
基于[email protected]的react动态权限后台管理系统模板
Stars: ✭ 151 (-14.2%)
Mutual labels:  antd
Pro Layout
easy use `Ant Design Vue` layout
Stars: ✭ 172 (-2.27%)
Mutual labels:  antd
Easyfun
a project using react antd webpack es6
Stars: ✭ 150 (-14.77%)
Mutual labels:  antd
Optimize Js Plugin
Webpack plugin to optimize a JavaScript file for faster initial load by wrapping eagerly-invoked functions.
Stars: ✭ 163 (-7.39%)
Mutual labels:  webpack-plugin
Cssjanus
↔️ Convert CSS stylesheets between left-to-right and right-to-left.              Made by Wikimedia.
Stars: ✭ 168 (-4.55%)
Mutual labels:  stylesheets
Abp React Antd
一个基于 ABP + React + Ant Design Pro 的快速开发框架
Stars: ✭ 175 (-0.57%)
Mutual labels:  antd

antd-scss-theme-plugin Tweet Share on Facebook Share on Reddit Share on Hacker News

Build Status License NPM Version

antd-scss-theme-plugin is a Webpack plugin which allows you to effectively use Ant Design in a project with SCSS styling. With it you can:

  1. Customize Ant Design by specifying theme variable overrides through a single theme.scss file.
  2. @import Ant Design's theme and color variables from your theme.scss file.
  3. Hot reload your project when theme.scss changes.

📖 For a more detailed overview of the plugin and its usage, check out the Using Ant Design in Sass-Styled Projects article on Intoli's blog.

Table of Contents

Installation

This plugin is published as antd-scss-theme-plugin on npm:

npm install --save-dev antd-scss-theme-plugin

It extends the functionality of a antd, less-loader and sass-loader to accomplish its goals. These are listed as peerDependencies in package.json, and you can install them with:

npm install --save antd
npm install --save-dev less-loader sass-loader

Configuration

To use antd-scss-theme-plugin, you need to configure Ant Design to use Less stylesheets when loading components, and to connect a few loaders with the plugin in your Webpack setup. You can find a fully configured project in the example directory.

Step 1. Configure Ant Design to Use Less Stylesheets

In order to customize Ant Design's theme, you need to configure antd to load its components with Less stylesheets instead of with pre-compiled CSS. The official documentation explains this to some degree, but here are the explicit steps you should take.

  1. Install babel-plugin-import, a package published by the makers of antd.

  2. Modify the plugin section of your Babel setup to use this package with antd:

    "plugins": [
      ["import", {
        "libraryName": "antd",
        "style": true
      }]
    ]
    

    The "style": true option is what enables the use of Less components.

  3. Configure less-loader to compile antd components. This can be done by adding something like the following to your Webpack config's loaders array:

    {
      test: /\.less$/,
      use: [
        {
          loader: 'style-loader',
          options: {
            sourceMap: process.env.NODE_ENV !== 'production',
          },
        },
        {
          loader: 'css-loader',
          options: {
            importLoaders: 1,
            sourceMap: process.env.NODE_ENV !== 'production',
          },
        },
        'less-loader',
      ],
    }
    

    Obviously, this also requires you to install style-loader and css-loader.

With that setup, you can import self-contained antd components with lines like following:

import { Button } from 'antd';

So, in addition to enabling styling customizations, this has the potential to reduce the size of your Webpack bundle.

Step 2. Use antd-scss-theme-plugin in Your Webpack Setup

First, initialize the plugin by passing your theme file's path to the plugin's constructor, and add the plugin to your Webpack config's plugins array:

import AntdScssThemePlugin from 'antd-scss-theme-plugin';

const webpackConfig =  {
  // ...
  plugins: [
    new AntdScssThemePlugin('./theme.scss'),
  ],
};

Second, wrap your less-loader and sass-loader Webpack configs with AntdScssThemeFile.themify(). For example, in the config from Step 1, you would change the line

'less-loader',

to

AntdScssThemePlugin.themify('less-loader'),

This works even when your loader has options, e.g., you would change

{
  loader: 'sass-loader',
  options: {
    sourceMap: process.env.NODE_ENV !== 'production',
  },
}

to

AntdScssThemePlugin.themify({
  loader: 'sass-loader',
  options: {
    sourceMap: process.env.NODE_ENV !== 'production',
  },
})

Usage

Customize Ant Design's Theme

With the project configured, you can customize Ant Design's theme by specifying Ant Design theme variables in your SCSS theme file (e.g., theme.scss). For example, if theme.scss has the following contents

/* theme.scss */
$primary-color: #fe8019;

then the interface will no longer be based off of the default blue #1890ff, but rather a louder orange #fe8019:

Effects of Changing Primary Color to #fe8019

You can customize any Less variable that antd uses in this way: just relace @ with a $, e.g., @info-color becomes $info-color.

Use Ant Design's Customized Color and Theme Variables

Importing theme.scss in some SCSS file gives it access all of Ant Design's theme and color variables. This is true even if you specify only a subset of the available theme variables in theme.scss. For instance, with theme.scss containing only a $primary-color definition as above, you would still be able to do something like:

@import '../theme.scss';

.header {
  color: $blue-10; /* From colors.less */
  padding: $padding-lg; /* From themes/default.less */
}

Live Reload Components when Ant Design Styles Change

Since antd-scss-theme-plugin registers your theme file as a watched dependency with Webpack, changes in the theme file will result in recompilations of components that use it. To learn how to set up your project to use live reloading, see the working example.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].