1.0.0-beta.2 • Published 6 years ago

templatize-css v1.0.0-beta.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Do you want to generate dynamic CSS and you can't use CSS in JS?

:root {
  --main-primary-color: brown; /* templatize-css: track */
  --main-mobile-primary-color: brown; /* templatize-css: track */
  --main-text-color: #fff; /* templatize-css: track */
  --main-link-color: #000;
}

.btn {
  background-color: var(--main-primary-color);
  color: var(--main-text-color);
  border: 0;
  padding: 10px 40px;
}

.btn-link {
  background-color: var(--main-link-color);
  border: 0;
  padding: 10px 40px;
}

@media (min-width: 992px) {
  .btn::after {
    background-color: var(--main-mobile-primary-color);
    padding: 5px 10px;
  }
}
const defaults = {
  mainPrimaryColor: 'brown',
  mainMobilePrimaryColor: 'brown',
  mainTextColor: '#fff'
};

const templatize = locals => `.btn {
  background-color: ${locals.mainPrimaryColor || defaults.mainPrimaryColor};
  color: ${locals.mainTextColor || defaults.mainTextColor}
}
@media (min-width: 992px) {
  .btn::after {
    background-color: ${locals.mainMobilePrimaryColor || defaults.mainMobilePrimaryColor}
  }
}`;

module.exports = { defaults, templatize };

Node.js v8 or newer is required.

Via the yarn client:

$ yarn add --dev templatize-css

Via the npm client:

$ npm install --save-dev templatize-css

Compile

import { compile } from 'templatize-css';

const css = `:root {
  --main-bg-color: brown; /* templatize-css: track */
}

.btn {
  background-color: var(--main-bg-color);
}
`;

const template = compile(css);

Compile File

import path from 'path';
import { compileFile } from 'templatize-css';

const cssFilePath = path.resolve(__dirname, 'input.css');
const templateFilePath = path.resolve(__dirname, 'template.js');

compileFile(cssFilePath, templateFilePath).then(() => {
  console.log('File Compiled! 🍕');
});

Compile File (--out-file/-o)

templatize-css src/main.css --out-file src/template-main-css.js

Feel free to dive in! Open an issue or submit PRs.

templatize-css follows the Contributor Covenant Code of Conduct.

MIT