css-module-dts
v0.1.4
Published
Generate .d.ts files for CSS modules
Readme
css-module-dts
Generate TypeScript declaration (.d.ts) files for CSS Modules.
This small CLI and library scans a directory for CSS/SCSS/LESS module files (by default **/*.module.{css,sass,scss,less}), extracts class names, and generates .d.ts style declaration output that can be written to a file or printed to stdout.
Key features
- Works with CSS, SCSS, and LESS (uses PostCSS + language parsers)
- CLI-friendly: print results or write to a file
- Small, dependency-light implementation
Installation
Install from npm:
npm install --save-dev css-module-dtsOr use npx for a one-off run (when published):
npx css-module-dts <rootDir>Local development
- Install dev deps:
npm install- Build the project:
npm run build- Run the CLI from the built output:
node dist/cli.js <rootDir> -p "**/*.module.{css,sass,scss,less}" -w out.d.tsUsage (CLI)
css-module-dts [options] <rootDir>Positional arguments
rootDirRoot directory to scan for CSS modules (required)
Options
-p,--patternGlob pattern to locate module files (default:**/*.module.{css,sass,scss,less})-w,--writePath to write the generated declaration output. If omitted, output is written to stdout.
Examples
Print declarations for all module files under src
npx css-module-dts srcWrite declarations to types.d.ts (local, after building)
npx css-module-dts src -w types.d.tsLibrary API
The package also exposes small programmatic helpers (ESM):
extractCssModules(rootDir: string, globPattern: string): Promise<string>- Scans files matching the glob within
rootDir, extracts class names, and returns the combined.d.tscontent as a single string.
- Scans files matching the glob within
extractClassNames(css: string): Set
- Extracts class names from a CSS/SCSS/LESS string and returns a Set of names.
createDtsFile(moduleName: string, classNames: Set): string
- Creates the
.d.tsdeclaration string for a single module path and its class names.
- Creates the
You can import and use these from the built files (dist) or the source during development (Node must support ESM):
import extractCssModules from './dist/extract-css-modules.js'; // or during dev after build: // import extractCssModules from './src/extract-css-modules.js';
CI / Release (GitHub Actions)
This repository includes GitHub Actions workflows to run CI and publish releases:
.github/workflows/ci.yml— runs on push and pull requests tomain. It installs dependencies, runsnpm run check, runs tests, and builds the project..github/workflows/release.yml— runs on push tomainand uses Changesets to create releases and publish to npm. It expectsNPM_TOKENand uses the defaultGITHUB_TOKENprovided by Actions..github/workflows/publish-manual.yml— a manualworkflow_dispatchworkflow that builds and publishes the package. It accepts an optionaltaginput for dist tags.
Required repository secrets
NPM_TOKEN— an npm automation token with publish access. Add this in the repository Settings → Secrets and variables → Actions.GITHUB_TOKEN— automatically provided to workflows; no action needed.
Manual publish
To manually publish the package from the Actions UI, go to the Actions tab, select Manual publish, click Run workflow, and optionally set a tag (like next).
Notes
- The release workflow uses Changesets. Ensure you create Changesets (e.g.,
npx changeset) when you want to release new versions. - The workflows assume
npm publish --access public. If you need a different registry or publish command, update the workflow files accordingly.
Tests
Run the test suite with:
npm testContributing
Contributions welcome — please open issues or PRs. A few suggestions:
- Run formatting/lint checks before committing:
npm run fix/npm run check - Add tests for parsing edge-cases when adding features
License
MIT — see the license field in package.json.
Notes and assumptions
- This project uses ESM ("type": "module") and expects a relatively recent Node.js that supports ESM imports (Node 14+ recommended).
- The CLI specified in package.json points to
dist/cli.js; make sure to runnpm run buildbefore using the local CLI.
