css-modules-typescript-loader is a Webpack loader designed to generate TypeScript declaration files (.d.ts) for CSS Modules. This enables developers to achieve type-safe access to CSS class names within their TypeScript projects, preventing runtime errors due to mistyped or non-existent class references. The current stable version, 4.0.1, was released in September 2020. While lacking a rigid release schedule, previous major updates occurred roughly annually. A key differentiator of this loader is its emphasis on checking the generated TypeScript declarations into source control. This approach facilitates parallel execution of `webpack` and `tsc` commands in Continuous Integration (CI) pipelines, optimizing build times. Furthermore, it offers a unique `verify` mode, which, when enabled, ensures that the committed TypeScript declarations remain in sync with the dynamically generated types, providing an additional layer of consistency assurance.
npm install css-modules-typescript-loaderVerified import paths — ran on the pinned version, not inferred.
This Webpack configuration demonstrates how to integrate `css-modules-typescript-loader` to generate TypeScript declaration files for CSS Modules, ensuring type safety for CSS class names within a TypeScript project.
Remove existing `.d.ts` files, upgrade the loader, and rebuild your project to generate new, compatible declarations. Review generated types for any unexpected changes.
Upgrade `css-modules-typescript-loader` to v4.0.1 or newer if you are using `css-loader` v4 or higher. Ensure your `css-loader` options are correctly configured for CSS Modules (`modules: true`).
Ensure all generated `.d.ts` files are committed to source control. During local development, consider setting `mode: 'emit'` or only enabling `verify` mode conditionally (e.g., `process.env.CI ? 'verify' : 'emit'`). Regularly run a build in `emit` mode to update types.
Review your `webpack.config.js` and ensure `css-modules-typescript-loader` appears immediately after the `css-loader` instance that is configured for `modules: true`.
Upgrade `css-modules-typescript-loader` to v2.0.4 or higher to resolve the missing dependency.
Run your Webpack build in `emit` mode (e.g., `webpack --env mode=emit`) to regenerate the `.d.ts` files, then commit the updated files to version control. Alternatively, disable `verify` mode for local development.
Ensure `css-modules-typescript-loader` is placed *after* `css-loader` in your `webpack.config.js` rules. Also, verify that `css-loader` has `options: { modules: true }` enabled.Confirm that `css-modules-typescript-loader` is correctly configured and running in `emit` mode in your Webpack setup. Ensure your `tsconfig.json` includes the directory where `.d.ts` files are emitted (e.g., `"include": ["./src/**/*.ts", "./src/**/*.tsx", "./src/**/*.d.ts"]`).
Upgrade `css-modules-typescript-loader` to v4.0.1 or newer. If on an older version, ensure it's at least v2.0.4 to resolve the `loader-utils` dependency issue.