Giter VIP home page Giter VIP logo

postcss-css-vars-to-less-antd's Introduction

postcss-css-vars-to-less-antd

NPM version npm license codecov

npm

Introduction

For conversion css variables to less

Installation

npm install postcss-css-vars-to-less-antd --save
or
yarn add postcss-css-vars-to-less-antd

example

πŸ’… file globals.css
:root {
  --primary: #00ff73;
  --black: #000000;
  --red: #ff0018;
  --white: #ffffff;
}
:root {
  --bg-black: #000000;
  --bg-red: #ff0018;
  --bg-white: #ffffff;
  --color-text-black: var(--black, @black);
  --color-text-red: var(--red, @red);
  --color-text-white: var(--white, @white);
  --color-text-todo1: var(--color-text-white-n,var(--color-text-red-n, #ff0018));
  --color-text-todo2: var(--color-text-white-n, var(--color-text-red-n, @red));
  --color-text-todo3: var(--color-text-white-n, var(--color-text-red));
}
html.dark {
  --prism-foreground: #d4d4d4;
  --prism-background: #1e1e1e;
}
a.link {
  color: var(--color-text-black);
}
πŸ’… file antd-custom.less
@primary-color: var(--primary, @primary-color);
@info-color: var(--primary);
@info-text: @info-color;
@error-color: var(--color-text-red-n1, var(--color-text-red-n2, var(--red)));
@white: var(--white);
@black: var(--black);
@red: #ff0018;
πŸ‘¨β€πŸ’» custom
πŸ˜… note => you may need to install more.
----------
npm install less-vars-to-js --save
npm install postcss --save
----------

const fs = require("fs")
const path = require('path')
const { parse } = require("postcss")
const lessToJS = require('less-vars-to-js')
const cssVarsToLessAntd = require('postcss-css-vars-to-less-antd')

or

import fs from 'fs'
import path from 'path'
import { parse } from 'postcss'
import lessToJS from 'less-vars-to-js'
import cssVarsToLessAntd from 'postcss-css-vars-to-less-antd'

const themeVariables = lessToJS(
  fs.readFileSync(path.resolve(__dirname, './antd-custom.less'), 'utf8'),
)

πŸš€ output themeVariables

{
  '@primary-color': 'var(--primary, @primary-color)',
  '@info-color': 'var(--primary)',
  '@info-text': '@info-color',
  '@error-color': 'var(--color-text-red-n1, var(--color-text-red-n2, var(--red)))',
  '@white': 'var(--white)',
  '@black': 'var(--black)',
  '@red': '#ff0018'
}

case read css file

const process = parse(fs.readFileSync("./globals.css", "utf8"));
const newThemeVariables = cssVarsToLessAntd(process, themeVariables)
console.log(newThemeVariables)

case read typescript file and styled-components

πŸ’… file globals.ts (styled-components)
import { createGlobalStyle, css } from 'styled-components'

export const rootStyling = css`
  :root {
    --primary: #00ff73;
    --black: #000000;
    --red: #ff0018;
    --white: #ffffff;

    --bg-black: #000000;
    --bg-red: #ff0018;
    --bg-white: #ffffff;
    --color-text-black: var(--black, @black);
    --color-text-red: var(--red, @red);
    --color-text-white: var(--white, @white);
    --color-text-todo1: var(--color-text-white-n, var(--color-text-red-n, #ff0018));
    --color-text-todo2: var(--color-text-white-n, var(--color-text-red-n, @red));
    --color-text-todo3: var(--color-text-white-n, var(--color-text-red));
  }
  html.dark {
    --prism-foreground: #d4d4d4;
    --prism-background: #1e1e1e;
  }
`
const GlobalStyle = createGlobalStyle`
${rootStyling};

@font-face {
  font-family: 'CSChatThaiUI';
  font-weight: normal;
  font-style: normal;
  font-display: swap;
  src: url('/fonts/CSChatThaiUI/CSChatThaiUI.ttf');
}
`
npm install @babel/core --save
npm install @babel/preset-typescript --save
npm install node-eval --save
----------

const babelCore = require('@babel/core')
const nodeEval = require('node-eval')
const path = require('path')
const fs = require("fs")

const requireTypescript = (file) => {
  const fileContent = fs.readFileSync(file, 'utf8')
  const compiled = babelCore.transform(fileContent, {
    filename: file,
    presets: ['@babel/preset-typescript'],
  })
  return nodeEval(compiled.code, file)
}

const themeVariables = lessToJS(
  fs.readFileSync(path.resolve(__dirname, './antd-custom.less'), 'utf8'),
)
const rootStyling = requireTypescript('./globals.ts')
const newThemeVariables = cssVarsToLessAntd(parse(rootStyling[0]), themeVariables)
console.log(newThemeVariables)

πŸš€ output newThemeVariables

{
  '@primary-color': '#00ff73',
  '@info-color': '#00ff73',
  '@info-text': '@info-color',
  '@error-color': '#ff0018',
  '@white': '#ffffff',
  '@black': '#000000',
  '@red': '#ff0018'
}

πŸ₯° with nextjs next.config.js https://github.com/vercel/next.js/blob/canary/examples/with-ant-design-less/next.config.js

{
  ...
  lessLoaderOptions: {
    javascriptEnabled: true,
    modifyVars: newThemeVariables,
  },
  ...
}

postcss-css-vars-to-less-antd's People

Contributors

pla2aroi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.