JSPM

  • Created
  • Published
  • Downloads 127004
  • Score
    100M100P100Q171194F
  • License MIT

Creates .d.ts files from CSS Modules .css files

Package Exports

  • typed-css-modules
  • typed-css-modules/lib/fileSystemLoader

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typed-css-modules) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

typed-css-modules Build Status npm version

Creates TypeScript definition files from CSS Modules .css files.

If you have the following css,

/* styles.css */
.myClass {
  color: red;
}

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

/* styles.css.d.ts */
export const myClass: string;

So, you can import CSS modules in your TypeScript sources:

/* app.ts */
import * as styles from './styles.css';
console.log(`<div class="${styles.myClass}"></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.

tcm -p src/**/*.icss

watch

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

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

create(filepath) => Promise(dtsContent)

Returns DtsContent instance.

  • filepath: path of target .css file.

class DtsContent

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

writeFile() => Promise(dtsContent)

Writes the DtsContent instance's content to a file.

  • dtsContent: the DtsContent instance itself.

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 followng class names, these invalid tokens are not written to output .d.ts file.

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

/* invalid TypeScript variable */
.my-class{
  color: red;
}

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

License

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