All Projects → Quramy → Typed Css Modules

Quramy / Typed Css Modules

Licence: mit
Creates .d.ts files from CSS Modules .css files

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typed Css Modules

create-class-names
A utility to extend the values of a classNames object.
Stars: ✭ 16 (-97.59%)
Mutual labels:  css-modules
Reshadow
Markup and styles that feel right
Stars: ✭ 343 (-48.27%)
Mutual labels:  css-modules
Nyancss
🌈 Write plain CSS while reaping benefits of CSS-in-JS
Stars: ✭ 544 (-17.95%)
Mutual labels:  css-modules
React Flexbox Grid
A set of React components implementing flexboxgrid with the power of CSS Modules.
Stars: ✭ 2,858 (+331.07%)
Mutual labels:  css-modules
Kratos Boilerplate
🔥 A simple boilerplate for creating statics PWA using Webpack, Pug, PostCSS and CSS Modules
Stars: ✭ 308 (-53.54%)
Mutual labels:  css-modules
Sku
Front-end development toolkit
Stars: ✭ 403 (-39.22%)
Mutual labels:  css-modules
typed-css-modules-loader
💠 Webpack loader for typed-css-modules auto-creation
Stars: ✭ 62 (-90.65%)
Mutual labels:  css-modules
React Css Themr
Easy theming and composition for CSS Modules.
Stars: ✭ 582 (-12.22%)
Mutual labels:  css-modules
Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (-52.04%)
Mutual labels:  css-modules
Rollup Plugin Postcss
Seamless integration between Rollup and PostCSS.
Stars: ✭ 507 (-23.53%)
Mutual labels:  css-modules
Ember Css Modules
CSS Modules for ambitious applications
Stars: ✭ 274 (-58.67%)
Mutual labels:  css-modules
React Redux Boilerplate
Awesome React Redux Workflow Boilerplate with Webpack 4
Stars: ✭ 307 (-53.7%)
Mutual labels:  css-modules
Must Watch Css
A useful list of must-watch talks about CSS
Stars: ✭ 3,966 (+498.19%)
Mutual labels:  css-modules
edgestack
[UNMAINTAINED] A Universal React Stack with deeply integrated localization Support, semi-automatic route-based code splitting, Hot Module Reloading (HMR), Redux, Apollo GraphQL and more...
Stars: ✭ 77 (-88.39%)
Mutual labels:  css-modules
Redux Webpack Es6 Boilerplate
A starter project for modern React apps with Redux
Stars: ✭ 566 (-14.63%)
Mutual labels:  css-modules
movies
Real world isomorphic application for movies search, based on Webpack 5 / Express / React 17 + Redux-Saga / Bootstrap 4.6 + CSS Modules / i18next / SSR
Stars: ✭ 20 (-96.98%)
Mutual labels:  css-modules
Webpack React Boilerplate
Minimal React 16 and Webpack 4 boilerplate with babel 7, using the new webpack-dev-server, react-hot-loader, CSS-Modules
Stars: ✭ 358 (-46%)
Mutual labels:  css-modules
React Native Style Tachyons
Better styling for React Native
Stars: ✭ 640 (-3.47%)
Mutual labels:  css-modules
Csjs
✨ Modular, scoped CSS with ES6
Stars: ✭ 578 (-12.82%)
Mutual labels:  css-modules
Augur Ui
Augur UI
Stars: ✭ 412 (-37.86%)
Mutual labels:  css-modules

typed-css-modules github actions npm version

Creates TypeScript definition files from CSS Modules .css files.

If you have the following css,

/* styles.css */

@value primary: red;

.myClass {
  color: primary;
}

typed-css-modules creates the following .d.ts files from the above css:

/* styles.css.d.ts */
declare const styles: {
  readonly "primary": string;
  readonly "myClass": string;
};
export = styles;

So, you can import CSS modules' class or variable into your TypeScript sources:

/* app.ts */
import styles from './styles.css';
console.log(`<div class="${styles.myClass}"></div>`);
console.log(`<div style="color: ${styles.primary}"></div>`);

CLI

npm install -g typed-css-modules

And exec tcm <input directory> command. For example, if you have .css files under src directory, exec the following:

tcm src

Then, this creates *.css.d.ts files under the directory which has original .css file.

(your project root)
- src/
    | myStyle.css
    | myStyle.css.d.ts [created]

output directory

Use -o or --outDir option.

For example:

tcm -o dist src
(your project root)
- src/
    | myStyle.css
- dist/
    | myStyle.css.d.ts [created]

file name pattern

By the default, this tool searches **/*.css files under <input directory>. If you can customize glob pattern, you can use --pattern or -p option. Note the quotes around the glob to -p (they are required, so that your shell does not perform the expansion).

tcm -p 'src/**/*.icss' .

watch

With -w or --watch, this CLI watches files in the input directory.

camelize CSS token

With -c or --camelCase, kebab-cased CSS classes(such as .my-class {...}) are exported as camelized TypeScript varibale name(export const myClass: string).

You can pass --camelCase dashes to only camelize dashes in the class name. Since version 0.27.1 in the webpack css-loader. This will keep upperCase class names intact, e.g.:

.SomeComponent {
  height: 10px;
}

becomes

declare const styles: {
  readonly "SomeComponent": string;
};
export = styles;

See also webpack css-loader's camelCase option.

named exports (enable tree shaking)

With -e or --namedExports, types are exported as named exports as opposed to default exports. This enables support for the namedExports css-loader feature, required for webpack to tree shake the final CSS (learn more here).

Use this option in combination with https://webpack.js.org/loaders/css-loader/#namedexport and https://webpack.js.org/loaders/style-loader/#namedexport (if you use style-loader).

When this option is enabled, the type definition changes to support named exports.

NOTE: this option enables camelcase by default.

.SomeComponent {
  height: 10px;
}

Standard output:

declare const styles: {
  readonly "SomeComponent": string;
};
export = styles;

Named exports output:

export const someComponent: string;

API

npm install typed-css-modules
import DtsCreator from 'typed-css-modules';
let creator = new DtsCreator();
creator.create('src/style.css').then(content => {
  console.log(content.tokens);          // ['myClass']
  console.log(content.formatted);       // 'export const myClass: string;'
  content.writeFile();                  // writes this content to "src/style.css.d.ts"
});

class DtsCreator

DtsCreator instance processes the input CSS and create TypeScript definition contents.

new DtsCreator(option)

You can set the following options:

  • option.rootDir: Project root directory(default: process.cwd()).
  • option.searchDir: Directory which includes target *.css files(default: './').
  • option.outDir: Output directory(default: option.searchDir).
  • option.camelCase: Camelize CSS class tokens.
  • option.namedExports: Use named exports as opposed to default exports to enable tree shaking. Requires import * as style from './file.module.css'; (default: false)
  • option.EOL: EOL (end of line) for the generated d.ts files. Possible values '\n' or '\r\n'(default: os.EOL).

create(filepath, contents) => Promise(dtsContent)

Returns DtsContent instance.

  • filepath: path of target .css file.
  • contents(optional): the CSS content of the filepath. If set, DtsCreator uses the contents instead of the original contents of the filepath.

class DtsContent

DtsContent instance has *.d.ts content, final output path, and function to write file.

writeFile(postprocessor) => Promise(dtsContent)

Writes the DtsContent instance's content to a file. Returns the DtsContent instance.

  • postprocessor (optional): a function that takes the formatted definition string and returns a modified string that will be the final content written to the file.

    You could use this, for example, to pass generated definitions through a formatter like Prettier, or to add a comment to the top of generated files:

    dtsContent.writeFile(
      definition => `// Generated automatically, do not edit\n${definition}`
    )
    

tokens

An array of tokens retrieved from input CSS file. e.g. ['myClass']

contents

An array of TypeScript definition expressions. e.g. ['export const myClass: string;'].

formatted

A string of TypeScript definition expression.

e.g.

export const myClass: string;

messageList

An array of messages. The messages contains invalid token information. e.g. ['my-class is not valid TypeScript variable name.'].

outputFilePath

Final output file path.

Remarks

If your input CSS file has the following class names, these invalid tokens are not written to output .d.ts file.

/* TypeScript reserved word */
.while {
  color: red;
}

/* invalid TypeScript variable */
/* If camelCase option is set, this token will be converted to 'myClass' */
.my-class{
  color: red;
}

/* it's ok */
.myClass {
  color: red;
}

Example

There is a minimum example in this repository example folder. Clone this repository and run cd example; npm i; npm start.

Or please see https://github.com/Quramy/typescript-css-modules-demo. It's a working demonstration of CSS Modules with React and TypeScript.

License

This software is released under the MIT License, see LICENSE.txt.

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].