Giter Site home page Giter Site logo

askirmas / postcss-d-ts Goto Github PK

View Code? Open in Web Editor NEW
30.0 3.0 2.0 6.03 MB

PostCSS plugin to generate `.d.ts` of all used CSS classes and ids in imported stylesheets

Home Page: https://www.npmjs.com/package/postcss-d-ts

License: MIT License

TypeScript 82.48% CSS 4.18% JavaScript 7.16% Shell 1.33% SCSS 0.21% Less 0.16% Stylus 0.16% HTML 4.33%
typescript css-modules postcss-plugin dts css sass typing

postcss-d-ts's Introduction

PostCSS plugin to generate .d.ts of all used CSS classes and ids in imported stylesheets

build@ci codecov Quality Gate Status Maintainability Scrutinizer Code Quality DeepScan grade CodeFactor

dependencies Status version license

Goal

Provide contract between JS and CSS.

Installation and setup

npm install postcss-d-ts
// postcss.config.js
module.exports = {
	plugins: [
	  "postcss-preset-env",
  	...
+	  "postcss-d-ts"  // or "postcss-d-ts/dist/7" for postcss v7
	]
}

Check postcss#usage for details.

Features

Languages

Language agnostic because of PostCss philosophy


CSS libraries/frameworks

In ./__typing__/ results of applying to some popular libraries: bootstrap v3, bootstrap v4, material v10, tailwind v2.

Applyment

CSS content:

/* some.css or some.module.css */
.class1 { ... }
.class2 { ... }

Generated declaration from template (i.e. default ./src/_css-template.d.ts):

declare const identifiersMap: CssIdentifiersMap

export default identifiersMap

export type CssIdentifiersMap = {
  "class1": string|undefined
  "class2": string|undefined
}

Thus, in Component (i.e. React): ./__recipes__/pages/module.tsx

import moduleClasses from "./some.module.css"

const {
  class1,
  class2,
  //@ts-expect-error - we have only .class1 and .class2
  class3
} = moduleClasses

const Component = () => <div className={`${class1} ${class2}`}/>

or ./__recipes__/pages/button.tsx

// Ordinary CSS
import type { CssIdentifiersMap } from "./some.css"
// I.e. with help of https://www.npmjs.com/package/react-classnaming
import classNaming from "react-classnaming"

const {
  class1,
  class2,
  //@ts-expect-error - we have only .class1 and .class2
  class3
} = {} as CssIdentifiersMap

const classNames = classNaming()

const Component() => <div {...classNames({class1, class2})} />

npm install react-classnaming

Options

template: string

Local path to a custom template for declarations generating.

declare const identifiersMap: CssIdentifiersMap

export default identifiersMap

export type CssIdentifiersMap = {
  "__identifier__": string|undefined
}
import type { CSSProperties } from "react";
interface Styled {
  __identifier__: Record<string, CSSProperties>;
}
declare const styled: Styled;
export default styled;
export declare const __identifier__: CSSProperties;

identifierKeyword: string

The word in d.ts template to be replaced with CSS classes, ids, etc.

// postcss.config.js
module.exports = {
  plugins: {
    "postcss-d-ts": {
+     identifierKeyword: "data"
    }
  }
}
// _css-template.d.ts
export type CssIdentifiersMap = {
-  "__identifier__": string|undefined
+  "data": string|undefined
}

checkMode: boolean

Throw an error instead of declaration file rewrite. By default, this mode is on for NODE_ENV === 'production'

Other options

Full list in different formats

import { Options } from "postcss-d-ts/dist/options.types"
/** @type {{
 *  plugins: Array<
*    ["postcss-d-ts", import("postcss-d-ts/dist/options.types").Options]
 *  >
 * }}
 */
module.exports = {
  plugins: [
    ["postcss-d-ts", {}]
  ]
}

Additional notes

CLI

Simply install postcss-cli and add it to npm scripts with desired options: example@cra and another:

// package.json
{
  "scripts": {
    "postcss-d-ts": "postcss --use postcss-d-ts styles/index.css --watch > /dev/null"
  }
}

With create-react-app

You need to launch postcss as a separate process. See commit https://github.com/askirmas/postcss-d-ts/commit/f9f07f009a02db69d9332bdd029a95420ce1a6d9 as an additional option how to establish

postcss-d-ts's People

Contributors

monkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

monkin nikifilini

postcss-d-ts's Issues

Can't run via JS API

[email protected]
[email protected]

My code (index.js):

const dts = require('postcss-d-ts')
const postcss = require('postcss')
const fs = require('fs')

fs.readFile('dx.ligt.css', (err, css) => {
  postcss([
    dts(),
  ], { from: 'dx.ligt.css', to: 'dx.out.css' })
    .process(css)
    .then(result => {
      console.log(result.css)
    })
})

Error:

dmitry@DESKTOP-Q74C34Q:~/H/_postcss/_tmp$ node index.js
/home/dmitry/H/_postcss/_tmp/node_modules/postcss/lib/processor.js:61
        throw new Error(i + ' is not a PostCSS plugin')
        ^

Error: [object Object] is not a PostCSS plugin
    at Processor.normalize (/home/dmitry/H/_postcss/_tmp/node_modules/postcss/lib/processor.js:61:15)
    at new Processor (/home/dmitry/H/_postcss/_tmp/node_modules/postcss/lib/processor.js:9:25)
    at postcss (/home/dmitry/H/_postcss/_tmp/node_modules/postcss/lib/postcss.js:25:10)
    at ReadFileContext.callback (/home/dmitry/H/_postcss/_tmp/index.js:6:3)
    at FSReqCallback.readFileAfterOpen [as oncomplete] (fs.js:273:13)

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.