css-modules-content-mapper
v0.3.0
Published
TypeScript content mapper for CSS modules
Readme
css-modules-content-mapper
A TypeScript content mapper for CSS modules.
Table of Contents
Installation
npm install css-modules-content-mapperUsage
In your tsconfig.json, add css-modules-content-mapper to the contentMappers array.
{
"compilerOptions": {
// …
},
"contentMappers": [
{
"package": "css-modules-content-mapper",
"extensions": [".module.css"],
"options": {
// Optional options…
}
}
]
}Now run tsc with the --runExternalCode flag.
tsc --runExternalCodeIf your code uses undefined CSS module classes, this will show an error.
Editor support
In addition to type checking, this content mapper also provides useful editor integrations. This
project uses postcss-safe-parser, a
fault-tolerant CSS parser. That means it will work even if your CSS has syntax errors, for example
while you are typing.
- “Go to Definition” in a TypeScript file will take you to the matching CSS class or ID.
- Hovering an imported class in TypeScript will show the CSS definitions.
- Like TypeScript values, CSS classes and IDs can be documented with JSDoc comments (
/** */).
Options
localsConvention('camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly' | 'all' | 'none'): Style of exported classnames, the keys in your json. For more information, seepostcss-modules.
CSS syntax
This project does not check CSS validity. You can use a linter, such as
Stylelint, for that. This project also doesn’t support CSS variants. For
Sass, use
sass-content-mapper.
For Less, use
less-content-mapper.
Compatibility
This projects works with TypeScript 7.1 or greater and requires Node.js 22 or greater.
Security
Normally, tsc is a self-contained binary that doesn’t execute third party code. By using content
mappers, you are deliberately breaking this trust boundary.
