npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-typed

v0.7.6

Published

TypeScript declaration generator for CSS files

Downloads

118

Readme

css-typed

TypeScript declaration generator for CSS files.

Usage

Install

Install the CLI tool as a dev dependency.

npm install --save-dev css-typed

Run

Run css-typed and pass it a glob targeting your CSS files.

npx css-typed 'src/**/*.css'

This will generate .d.css.ts files next to the original source files.

Note

A CSS module file with the name foo.module.css will emit foo.module.d.css.ts.

Configure

Configure TypeScript to allow arbitrary extensions (TS 5+).

{
	"compilerOptions": {
		"allowArbitraryExtensions": true
	}
}

Add *.d.css.ts to your .gitignore if appropriate. (See #4 for more information about alternative output directory.)

echo '*.d.css.ts' >> .gitignore

Options

The following table lists the options css-typed supports. Also run css-typed -h on the command line.

| CLI option | Default | Description | | :------------------: | :----------: | :------------------------------------- | | -c or --config | Heuristics | Custom path to the configuration file. | | --localsConvention | dashesOnly | Style of exported class names. |

config

css-typed supports loading options from a configuration file instead of using command line arguments. To load from a custom path, use the -c or --config option. By default, css-typed looks in the following locations. Extensionless "rc" files can have JSON or YAML format.

  • Package file: css-typed property in package.json or package.yaml
  • Root rc files: .csstypedrc with no extension or one of json, yaml, yml, js, cjs, or mjs
  • Config folder rc files: .config/csstypedrc with no extension or one of json, yaml, yml, js, cjs, or mjs
  • Root config files: css-typed.config with an extension of js, cjs, or mjs

Under the hood, css-typed uses lilconfig to load configuration files. It supports YAML files via js-yaml.

See config.ts for the implementation.

localsConvention

Inspired by postcss localsConvention. Adds none option value to use the class name as-is.

The --localsConvention option changes the style of exported class names, the exports in your TS (i.e., the JS names).

css-typed will only camelize dashes in class names by default (the dashesOnly option value). It will not preserve the original class name. For example, my-class becomes myClass and you cannot use my-class in JS/TS code.

Modern bundlers or build system such as Vite and Gatsby support this transformation. The default matches CSS naming practices (kebab-case).

IMPORTANT

Note that camelCase and dashes MAY have TypeScript bugs. TypeScript 5.6 may help with the named exports for these.

If you encounter a bug, please file an issue. In the mean-time, consider using camelCaseOnly instead. (Or dashesOnly which is the default.)

Recipes

Run script

To run it as part of your build, you will likely include it as a run script, maybe as codegen or pretsc.

{
	"scripts": {
		"codegen": "css-typed \"src/**/*.css\"",
		"pretsc": "css-typed \"src/**/*.css\"",
		"tsc": "tsc"
	}
}

Watch

The CLI does not have built-in watch support. Feel free to nodemon or similar.

{
	"scripts": {
		"codegen": "css-typed \"src/**/*.css\"",
		"codegen:watch": "nodemon -x \"npm run codegen\" -w src -e css"
	}
}

Motivation

typescript-plugin-css-modules provides a great IDE experience, but cannot perform build-failing type-checking. Furthermore, the traditional TypeScript ambient module definition fails the noUncheckedIndexedAccess strict check and causes issues with typed ESLint rules.

// This does not provide strict typing
declare module "*.module.css" {
	const classes: { [key: string]: string };
	export default classes; // It also uses default export 😿
}

typed-css-modules and typed-scss-modules exist, but the former does not have recent activity and the latter focuses on SCSS. (My current (2023/2024) interests involve modern CSS only.) Both depend on css-modules-loader-core, which appears abandoned.

Therefore, I wrote my own (very basic) implementation. See §Implementation details for more information.

Contributing

See CONTRIBUTING.md.

Implementation details

This (very basic) implementation uses glob for file matching and css-tree for CSS parsing. It extracts CSS classes (ClassSelector in CSS Tree’s AST) and exports them as string constants (named exports).

The CSS-file class name is modified for JS export according to the localsConvention option. The implementation matches PostCSS.

I chose CSS Tree after a brief search because it had a nice API, good documentation, and supported CSS nesting (a requirement for my original use case).

css-typed uses Commander.js for command line parsing and lilconfig for configuration file loading.

The “brand” image/logo combines the public CSS 3 and TypeScript logos with a basic plus icon in between. See css-typed.svg.