Giter Site home page Giter Site logo

1596944197 / js-var-to-css-var Goto Github PK

View Code? Open in Web Editor NEW

This project forked from solidzoro/js-var-to-css-var

1.0 0.0 0.0 27 KB

Generate CSS/LESS variables and TS Types from JS/TS

License: MIT License

JavaScript 44.01% TypeScript 41.13% CSS 7.43% Less 7.43%

js-var-to-css-var's Introduction

js-var-to-css-var

Generate CSS/LESS variables and TS Types from JS/TS.

version license size download

Installation

yarn add js-var-to-css-var

Usage

for Js

const jsVarToCssVar = require('js-var-to-css-var');

jsVarToCssVar({
  inputPath: `${CUR_DIR}/styles/style--js-1.js`,
  //
  outputCssPath: `${CUR_DIR}/_output--js/style--css-1.css`,
  outputCssScopeTag: ':root',
  //
  outputLessPath: `${CUR_DIR}/_output--js/style--less-1.less`,  // [Optional]
  outputLessHeaderImport: `@import './variables.less';`, // [Optional]
  //
  outputTypePath: `${CUR_DIR}/_output--js/style--type-1.ts`,  // [Optional]
  outputTypeName: 'ITheme1',  // [Optional]
});

for Node Cil

TODO

Result

Input

export const THEME_JS_COLOR = {
  '--color-red': '#f99',
  '--color-blue': '#1f9cff',
};

export const THEME_JS_FONT = {
  '--font-size-xs': '12px',
  '--font-size-md': '18px',
  '--font-size-lg': '24px',
};

Output

css

:root {
  --color-red: #f99;
  --color-blue: #1f9cff;
  --font-size-xs: 12px;
  --font-size-md: 18px;
  --font-size-lg: 24px;
}

less

@import './variables.less';

@color-red: #f99;
@color-blue: #1f9cff;
@font-size-xs: 12px;
@font-size-md: 18px;
@font-size-lg: 24px;

type

export type ITheme1 =
  | '--color-red'
  | '--color-blue'
  | '--theme-light'
  | '--theme-dark'
  | '--font-size-xs'
  | '--font-size-md'
  | '--font-size-lg'

Real World

sample

import { IGlobalCssVars } from '@/styles/vars/global-css-vars--type';
import { IOverwriteAntdVars } from '@/styles/vars/overwrite-antd-vars--type';

type ICssVars = IGlobalCssVars | IOverwriteAntdVars | string;

export const getCssVar = (cssVar: ICssVars): string => {
  return getComputedStyle(document.documentElement).getPropertyValue(cssVar);
};

// use getCssVar Fn
// getCssVar('--font    <-- here have IDE hint all css vars

watch style.ts file cahnge (use webpack plugin)

const WatchFileAndRunCallbackWebpackPlugin = require('watch-file-change-and-run-callback-webpack-plugin');

webpackConfig.plugins.push(
  new WatchFileAndRunCallbackWebpackPlugin({
    matchs: [
      {
        filePath: `${SRC_DIR}/styles/vars/global-css-vars.ts`,
        callback: () => {
          syncStyleCssVar(SRC_DIR);
        },
      },
    ],
  }),
);

License

MIT © Jason Feng

js-var-to-css-var's People

Contributors

solidzoro avatar

Stargazers

 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.