
Security News
Frontier AI Is Now Critical Infrastructure
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.
css-modules-typing-loader
Advanced tools
Webpack loader to generate typings for your CSS modules on the fly.
Webpack loader to generate Typescript typings for your CSS modules on the fly. This does not replace your existing CSS loader.
Originally modified from css-modules-typing-loader-loader.
npm install --save-dev css-modules-typing-loader
You can specify the following options as query parameters:
Makes sure all classnames are transformed to valid variables (often used in combination with namedExport,
as described below).
{ test: /\.css$/, loader: 'css-modules-typing-loader?camelCase' }
namedExportSince class names with characters like dashes (-) are not valid characters for a Typescript variable,
the default behaviour for this loader is to export an interface as the default export that contains
the classnames as properties.
export interface IExampleCss {
'foo': string;
'bar-baz': string;
}
declare const styles: IExampleCss;
export default styles;
A cleaner way is to expose all classes as named exports. This can be done by enabling the namedExport option:
{ test: /\.css$/, loader: 'css-modules-typing-loader?namedExport' }
As mentioned above, this requires class names to only contain valid typescript characters,
thus filtering out all classnames that do not match the regex /^\w+$/i.
In order to make sure that even class names with non-legal characters are used it is highly recommended to use
the camelCase option as well:
{ test: /\.css$/, loader: 'css-modules-typing-loader?namedExport&camelCase' }
Note: css-loader exports mappings to exports.locals which is incompatible with the namedExport-option unless paired
with extract-text-webpack-plugin or style-loader. They move the exported properties from exports.locals to exports
making them required for namedExport to work, and namedExport required for them to work. Always combine usage of
extract-text-webpack-plugin or style-loader with the namedExport-option.
silentTo silence the loader because you get annoyed by its warnings or for other reasons, you can simply pass the "silent" query to the loader and it will shut up.
{ test: /\.css$/, loader: 'css-modules-typing-loader?silent' }
bannerTo add a "banner" prefix to each generated *.d.ts file, you can pass a string to this option as shown below.
The prefix is quite literally prefixed into the generated file, so please ensure it conforms to the type definition syntax.
{ test: /\.css$/, loader: 'css-modules-typing-loader?banner="// This file is automatically generated by css-modules-typing-loader.\n// Please do not change this file!"' }
Modify your Webpack config to pass your CSS module files through css-modules-typing-loader.
before:
{
...webpackConfig,
module: {
rules: [
{ test: /\.css$/, loader: 'css?modules' }
]
}
}
after:
{
...webpackConfig,
module: {
rules: [
{ test: /\.css$/, use: ['css?modules', 'css-modules-typing-loader'] }
]
}
}
Imagine you have a file ~/my-project/src/component/MyComponent/myComponent.scss in your project with the following content:
.some-class {
// some styles
&.someOtherClass {
// some other styles
}
&-sayWhat {
// more styles
}
}
Using css-modules-typing-loader will generate ~/my-project/src/component/MyComponent/myComponent.scss.d.ts
with the following content:
export interface IMyComponentScss {
'some-class': string;
'someOtherClass': string;
'some-class-sayWhat': string;
}
declare const styles: IMyComponentScss;
export default styles;
If you are using the namedExport and camelCase options, the generated file will look something more like this:
export const someClass: string;
export const someOtherClass: string;
export const someClassSayWhat: string;
Since this loader will be generating type definition files on the fly, it is recommended that
tell Webpack you ignore these. This can be done using the built-in WatchIgnorePlugin:
plugins: [
new webpack.WatchIgnorePlugin([
/css\.d\.ts$/
]),
...
]
FAQs
Webpack loader to generate typings for your CSS modules on the fly.
We found that css-modules-typing-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.