All Projects → MartinKavik → postcss-typed-css-classes

MartinKavik / postcss-typed-css-classes

Licence: MIT license
PostCSS plugin that generates typed entities from CSS classes for chosen programming language.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
Handlebars
879 projects
rust
11053 projects

Projects that are alternatives of or similar to postcss-typed-css-classes

postcss-critical-css
PostCSS plugin to define and output critical CSS using custom atRules, and/or custom CSS properties. Critical CSS may be output to one or more files, as defined within the plugin options or within the CSS.
Stars: ✭ 84 (+600%)
Mutual labels:  postcss, postcss-plugin
postcss-prefixwrap
A PostCSS plugin that is used to wrap css styles with a css selector to constrain their affect on parent elements in a page.
Stars: ✭ 54 (+350%)
Mutual labels:  postcss, postcss-plugin
postcss-rename
Replace class names based on a customizable renaming scheme.
Stars: ✭ 77 (+541.67%)
Mutual labels:  postcss, postcss-plugin
postcss-import-url
PostCSS plugin inlines remote files.
Stars: ✭ 47 (+291.67%)
Mutual labels:  postcss, postcss-plugin
postcss-font-pack
PostCSS plugin to simplify font declarations by validating only configured font packs are used and adding fallbacks.
Stars: ✭ 18 (+50%)
Mutual labels:  postcss, postcss-plugin
postcss-inline-media
Media queries shortcut, built on PostCSS, example: font-size: 20px @1200 18px @480 16px;
Stars: ✭ 47 (+291.67%)
Mutual labels:  postcss, postcss-plugin
postcss-purgecss
PostCSS plugin for purgecss
Stars: ✭ 92 (+666.67%)
Mutual labels:  postcss, postcss-plugin
postcss-trash-killer
It is a postcss plugin which wil be remove all unused css
Stars: ✭ 20 (+66.67%)
Mutual labels:  postcss, postcss-plugin
postcss-clean
PostCss plugin to minify your CSS with clean-css
Stars: ✭ 41 (+241.67%)
Mutual labels:  postcss, postcss-plugin
postcss-gtk
Processes GTK+ CSS into browser CSS
Stars: ✭ 23 (+91.67%)
Mutual labels:  postcss, postcss-plugin
postcss-center
PostCSS plugin to center elements.
Stars: ✭ 44 (+266.67%)
Mutual labels:  postcss, postcss-plugin
postcss-lazyimagecss
A PostCSS plugin that generates images's CSS width & height properties automatically.
Stars: ✭ 38 (+216.67%)
Mutual labels:  postcss, postcss-plugin
postcss-input-style
PostCSS plugin that adds new pseudo-elements for easily styling the inner elements of inputs
Stars: ✭ 16 (+33.33%)
Mutual labels:  postcss, postcss-plugin
postcss-styl
PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
Stars: ✭ 15 (+25%)
Mutual labels:  postcss, postcss-plugin
postcss-clearfix
PostCSS plugin that adds a 'fix' argument to the 'clear' property
Stars: ✭ 47 (+291.67%)
Mutual labels:  postcss, postcss-plugin
postcss
No description or website provided.
Stars: ✭ 59 (+391.67%)
Mutual labels:  postcss, postcss-plugin
Postcss Spiffing
PostCSS plugin to use British English
Stars: ✭ 158 (+1216.67%)
Mutual labels:  postcss, postcss-plugin
A17t
An atomic design toolkit for pragmatists
Stars: ✭ 236 (+1866.67%)
Mutual labels:  postcss, tailwindcss
postcss-windicss
PostCSS integrations for Windi CSS
Stars: ✭ 33 (+175%)
Mutual labels:  postcss, postcss-plugin
tailwind-caret-color
Tailwindcss plugin to color caret in input fields
Stars: ✭ 12 (+0%)
Mutual labels:  postcss, tailwindcss

PostCSS Typed Css Classes npm version

PostCSS plugin that generates typed entities from CSS classes for a chosen programming language. You can also use it to filter CSS classes to reduce output size for faster application launch.

Why

I like atomic css libraries like TailwindCSS or Tachyons. I also like statically typed languages like Rust or Elm where compiler is your best friend and teacher.

So this plugin is trying to solve these problems:

  1. How to force a compiler to check if used CSS class is valid (resp. given class exists in included stylesheet)?
  2. I don't remember all classes - autocomplete with a class description would be nice.
  3. How to reduce size of stylesheet?

Solutions:

  1. Generate a file with source code in chosen language that mirrors your stylesheet and use it instead of plain string class names.
  2. Your IDE should autocomplete classes from generated file. You can use CSS attributes as a class description.
  3. Filter out classes from stylesheet that you didn't use. (Just search your source files for used classes.)

Used In Projects:

Do you use it? Create PR!

Install

yarn add postcss-typed-css-classes --dev

Basic Usage

postcss([
  require("postcss-typed-css-classes")({
    generator: "rust",
  }),
]);

See Seed Quickstart Webpack for using with Webpack.

See PostCSS docs for examples for your environment.

Options

  • generator

    • can be:
      • a) name of a built-in generator
      • b) function with one parameter classes
        • it should return string
        • generated file will not be created when function doesn't return string
    • required
    • examples:
      • "rust"
      • function() {}
      • (classes) => `Classes: ${classes.length}`
    • classes example:
    [
      {
        "name": "container",
        "properties": [
          {
            "property": "max-width: 576px",
            "mediaQuery": "@media (min-width: 576px)"
          }
        ]
      }
    ]
  • output_filepath

    • a file path with filename and extension
    • generated code will be saved into this location
    • optional if generator does not provide a default otherwise it is required
    • examples:
      • path.resolve(__dirname, 'css_classes.rs')
  • purge

    • boolean to indicate that the output should be filtered
    • optional
    • default is false
  • content

    • Can be a path string pointing to the location of the files to be processed or an array of config objects
    • optional

    content options

    • path

      • a string path or an array of globs
      • optional if generator has specified a default otherwise required
    • regex

      • valid regex
      • optional if generator has specified a default otherwise required
    • mapper

      • map function
      • transforms class output
      • optional if generator has specified a default otherwise required
    • escape

      • boolean indicating that the output needs to be escaped to meet generator requirements
      • optional
      • default false

examples

  require("postcss-typed-css-classes")({
    generator: "rust",
    content: 'src/**/*.rs'
  })
  require("postcss-typed-css-classes")({
    generator: "rust",
    purge: options.mode === "production",
    content: [
      { path: ['src/**/*.rs'] },
      {
        path: ['static/index.hbs', 'static/templates/**/*.hbs'],
        regex: /class\s*=\s*["'|][^"'|]+["'|]/g,
        mapper: className => {
          return (className.match(/class\s*=\s*["'|]([^"'|]+)["'|]/)[1]).match(/\S+/g)
        },
        escape: true
      }
    ],
  })
  • filter

    • a function with one parameter class_ that will be called when a CSS class is found in your stylesheet
    • optional
    • If a filter function is defined, it takes precedence over any of the content opts that may have been set
    • examples:
      • function() { return true }
      • (class_) => class_ !== "not-this-class"
  • replace

    • object with key:value that is used to replace invalid characters in rust names where key is the regex and value is the replacement
    • allows adding new regex replacements as well as overriding defaults

default

{
    '-': '_',
    ':': '__',
    '/': 'of',
    '@': '_at_',
    '\\.': '_p_',
    '^(?=\\d)': '_'
 }

example

require("postcss-typed-css-classes")({
    generator: "rust",
    replace: {
		"-":"_d_"
	}
  })

Contributing - How To Add A New Built-In Generator

NOTE: Plugin is based on official postcss-plugin-boilerplate. So it uses old JS and very strict linter, but I think that code is clean enough and commented => it shouldn't be problem for a small project like this and we don't have to solve problems with building pipelines.

  1. Fork this repo
  2. Run yarn in project root
  3. Choose a name for a generator - we'll use csharp for this guide
  4. Duplicate file /generators/json_generator.js and rename it to csharp_generator.js
  5. Open csharp_generator.js and change:
// - pretty-print JSON with 4 spaces indentation
// - with a new line at the end of a file
// - see EXAMPLE CODE:
//     `/tests/json_generator_test/json_generator.basic.expected_output`
function generate (classes) {
  return JSON.stringify(classes, undefined, 4) + os.EOL
}

to

// - generate C# class
// - see EXAMPLE CODE:
//     `/tests/csharp_generator_test/csharp_generator.basic.expected_output`
function generate (classes) {
  return "..I'm a c# class with " + classes.length + ' fields..' + os.EOL
}
  1. Open /index.js
  2. Insert line
var csharpGeneratorModule = require('./generators/csharp_generator')

below the line

var jsonGeneratorModule = require('./generators/json_generator')
  1. Insert case
case 'csharp':
    return csharpGeneratorModule.generate

into function getDefaultGenerator

  1. Duplicate folder /tests/json_generator_test and rename it to csharp_generator_test
  2. Rename /tests/csharp_generator_test/json_generator.basic.expected_output to csharp_generator.basic.expected_output
  3. Change content of csharp_generator.basic.expected_output to
..I'm a c# class with 6 fields..

(new line at the end is necessary)

  1. Rename /tests/csharp_generator_test/json_generator.test.js to csharp_generator.test.js
  2. Open csharp_generator.test.js and change GENERATOR_NAME from json to csharp
  3. Run yarn test in the project root
  4. Update README.md if necessary
  5. Update CHANGELOG.md
  6. Create pull request to this repo (squash commits and rebase if necessary)
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].