CSS modules support for TypeScript language service providing intelligent type information and auto-completion for CSS module imports
npx @tessl/cli install tessl/npm-typescript-plugin-css-modules@5.2.0TypeScript Plugin CSS Modules is a TypeScript language service plugin that provides comprehensive CSS Modules support for TypeScript development environments. It enables intelligent type information, auto-completion, and go-to-definition functionality for CSS module imports, supporting multiple CSS preprocessors including SASS, SCSS, Less, and Stylus.
npm install -D typescript-plugin-css-modulesThe plugin is configured in tsconfig.json rather than imported directly:
{
"compilerOptions": {
"plugins": [{ "name": "typescript-plugin-css-modules" }]
}
}Note: This package is a TypeScript language service plugin and does not export types for direct import. Types are used internally by the plugin for configuration and custom renderer/template development.
npm install -D typescript-plugin-css-modules{
"compilerOptions": {
"plugins": [
{
"name": "typescript-plugin-css-modules",
"options": {
"classnameTransform": "camelCase",
"customMatcher": "\\.module\\.((c|le|sa|sc)ss|styl)$"
}
}
]
}
}// Import CSS module
import styles from './component.module.css';
// Access class names with full type safety
const className = styles.myClass;
const anotherClass = styles['my-other-class'];
// Named exports for compatible class names
import styles, { myClass } from './component.module.css';declare module '*.module.css' {
const classes: { [key: string]: string };
export default classes;
}The plugin integrates with TypeScript's language service architecture through several key components:
Core plugin configuration interface and options for customizing CSS module processing behavior, file matching patterns, and compilation settings.
interface Options {
additionalData?: string;
allowUnknownClassnames?: boolean;
classnameTransform?: ClassnameTransformOptions;
customMatcher?: string;
customRenderer?: string;
customTemplate?: string;
dotenvOptions?: Omit<DotenvConfigOptions, 'path'> & { path?: string };
goToDefinition?: boolean;
namedExports?: boolean;
noUncheckedIndexedAccess?: boolean;
postcssOptions?: PostcssOptions;
/** @deprecated To align with naming in other projects. */
postCssOptions?: PostcssOptions;
rendererOptions?: RendererOptions;
}
type ClassnameTransformOptions =
| 'asIs'
| 'camelCase'
| 'camelCaseOnly'
| 'dashes'
| 'dashesOnly';CSS file compilation and processing system supporting multiple preprocessors, PostCSS integration, and custom rendering pipelines.
enum FileType {
css = 'css',
less = 'less',
sass = 'sass',
scss = 'scss',
styl = 'styl'
}
type CustomRenderer = (
css: string,
options: CustomRendererOptions
) => string | {
css: string;
sourceMap?: RawSourceMap;
};Automatic TypeScript declaration generation system that creates type-safe interfaces for CSS module class names and handles class name transformations.
type CustomTemplate = (
dts: string,
options: CustomTemplateOptions
) => string;
interface CustomTemplateOptions {
classes: CSSExports;
fileName: string;
logger: Logger;
}File pattern matching system for identifying CSS module files and handling custom file extensions and naming conventions.
type isCSSFn = (fileName: string) => boolean;
type isRelativeCSSFn = (fileName: string) => boolean;
function createIsCSS(customMatcher?: RegExp): isCSSFn;
function createIsRelativeCSS(isCSS: isCSSFn): isRelativeCSSFn;interface PostcssOptions {
excludePlugins?: string[];
useConfig?: boolean;
}
interface RendererOptions {
less?: Partial<Less.Options>;
sass?: Partial<SassOptions<'sync'>>;
stylus?: Partial<StylusRenderOptions>;
}
interface CustomRendererOptions {
fileName: string;
logger: Logger;
compilerOptions: tsModule.CompilerOptions;
}interface Logger {
log: (message: string) => void;
error: (error: unknown) => void;
}
function createLogger(
info: tsModule.server.PluginCreateInfo
): Logger;