{"id":13671284,"url":"https://github.com/Jimdo/typings-for-css-modules-loader","last_synced_at":"2025-04-27T14:33:16.837Z","repository":{"id":57096938,"uuid":"68445535","full_name":"Jimdo/typings-for-css-modules-loader","owner":"Jimdo","description":"Drop-in replacement for css-loader to generate typings for your CSS-Modules on the fly in webpack","archived":true,"fork":false,"pushed_at":"2019-11-02T07:02:30.000Z","size":45,"stargazers_count":573,"open_issues_count":51,"forks_count":71,"subscribers_count":94,"default_branch":"master","last_synced_at":"2025-04-18T18:32:46.763Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jimdo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-17T10:21:01.000Z","updated_at":"2025-03-08T15:25:56.000Z","dependencies_parsed_at":"2022-08-20T16:50:29.020Z","dependency_job_id":null,"html_url":"https://github.com/Jimdo/typings-for-css-modules-loader","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Ftypings-for-css-modules-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Ftypings-for-css-modules-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Ftypings-for-css-modules-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Ftypings-for-css-modules-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jimdo","download_url":"https://codeload.github.com/Jimdo/typings-for-css-modules-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251154428,"owners_count":21544499,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:05.029Z","updated_at":"2025-04-27T14:33:16.228Z","avatar_url":"https://github.com/Jimdo.png","language":"JavaScript","readme":"# typings-for-css-modules-loader\n\nWebpack loader that works as a css-loader drop-in replacement to generate TypeScript typings for CSS modules on the fly\n\n## Installation\n\nInstall via npm `npm install --save-dev typings-for-css-modules-loader`\n\n## Options\n\nJust like any other loader you can specify options e.g. as query-params\n\n### css-loader options\nAny option that your installed version of css-loader supports can be used and will be passed to it.\n\n### `namedExport`-option\nAs your fellow css-developer may tend to use characters like dashes(`-`) that 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.\ne.g.:\n```ts\nexport interface IExampleCss {\n  'foo': string;\n  'bar-baz': string;\n}\ndeclare const styles: IExampleCss;\n\nexport default styles;\n```\n\nA cleaner way is to expose all classes as named exports, this can be done if you enable the `namedExport`-option.\ne.g.\n```js\n  { test: /\\.css$/, loader: 'typings-for-css-modules-loader?modules\u0026namedExport' }\n```\n\nAs mentioned above, this requires classnames to only contain valid typescript characters, thus filtering out all classnames that do not match /^\\w+$/i. (feel free to improve that regexp)\nIn order to make sure that even classnames with non-legal characters are used it is highly recommended to use the `camelCase`-option as well, that - once passed to the css-loader - makes sure all classnames are transformed to valid variables.\nwith:\n```js\n  { test: /\\.css$/, loader: 'typings-for-css-modules-loader?modules\u0026namedExport\u0026camelCase' }\n```\nusing the following css:\n```css\n.foo {\n  color: white;\n}\n\n.bar-baz {\n  color: green;\n}\n```\n\nwill generate the following typings file:\n```ts\nexport const foo: string;\nexport const barBaz: string;\n```\n\n`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.*\n\n### `silent`-option\nTo 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.\ne.g.:\n\n```js\n  { test: /\\.css$/, loader: 'typings-for-css-modules-loader?silent' }\n```\n\n### `banner`-option\nTo 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.\n\n```js\n  { test: /\\.css$/, loader: 'typings-for-css-modules?banner=\"// This file is automatically generated by typings-for-css-modules.\\n// Please do not change this file!\"' }\n```\n\n## Usage\n\nKeep your `webpack.config` as is just instead of using `css-loader` use `typings-for-css-modules-loader`\n*its important you keep all the params that you used for the css-loader before, as they will be passed along in the process*\n\nbefore:\n```js\nwebpackConfig.module.loaders: [\n    { test: /\\.css$/, loader: 'css?modules' }\n    { test: /\\.scss$/, loader: 'css?modules\u0026sass' }\n];\n```\n\nafter:\n```js\nwebpackConfig.module.loaders: [\n    { test: /\\.css$/, loader: 'typings-for-css-modules-loader?modules' }\n    { test: /\\.scss$/, loader: 'typings-for-css-modules-loader?modules\u0026sass' }\n];\n```\n\n## Example\n\nImagine you have a file `~/my-project/src/component/MyComponent/myComponent.scss` in your project with the following content:\n```css\n.some-class {\n  // some styles\n  \u0026.someOtherClass {\n    // some other styles\n  }\n  \u0026-sayWhat {\n    // more styles\n  }\n}\n```\n\nAdding the `typings-for-css-modules-loader` will generate a file `~/my-project/src/component/MyComponent/myComponent.scss.d.ts` that has the following content:\n```ts\nexport interface IMyComponentScss {\n  'some-class': string;\n  'someOtherClass': string;\n  'some-class-sayWhat': string;\n}\ndeclare const styles: IMyComponentScss;\n\nexport default styles;\n```\n\n### using `namedExport` with the `camelCase`-option\nUsing the `namedExport` as well as the `camelCase` options the generated file will look as follow:\n```ts\nexport const someClass: string;\nexport const someOtherClass: string;\nexport const someClassSayWhat: string;\n```\n\n### Example in Visual Studio Code\n![typed-css-modules](https://cloud.githubusercontent.com/assets/749171/16340497/c1cb6888-3a28-11e6-919b-f2f51a282bba.gif)\n\nIf you encounter the following errors:\n```\nerror TS1192: Module '\"xxxxx/xxxx/src/style.sass\"' has no default export.\n```\nmaybe you should export the styles as following:\n```\nimport * as styles from './style.sass';\n```\n\n## Support\n\nAs the loader just acts as an intermediary it can handle all kind of css preprocessors (`sass`, `scss`, `stylus`, `less`, ...).\nThe only requirement is that those preprocessors have proper webpack loaders defined - meaning they can already be loaded by webpack anyways.\n\n## Requirements\n\nThe loader uses `css-loader`(https://github.com/webpack/css-loader) under the hood. Thus it is a peer-dependency and the expected loader to create CSS Modules.\n\n## Known issues\n\n### Webpack rebuilds / builds slow\n\nAs the loader generates typing files, it is wise to tell webpack to ignore them.\nThe fix is luckily very simple. Webpack ships with a \"WatchIgnorePlugin\" out of the box.\nSimply add this to your webpack plugins:\n```\nplugins: [\n    new webpack.WatchIgnorePlugin([\n      /css\\.d\\.ts$/\n    ]),\n    ...\n  ]\n```\nwhere `css` is the file extension of your style files. If you use `sass` you need to put `sass` here instead. If you use `less`, `stylus` or any other style language use their file ending.\n\n### Typescript doesnt find the typings\n\nAs the webpack process is independent from your typescript \"runtime\" it may take a while for typescript to pick up the typings.\nAny hints on how this could be fixed deterministically are welcome!\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJimdo%2Ftypings-for-css-modules-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJimdo%2Ftypings-for-css-modules-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJimdo%2Ftypings-for-css-modules-loader/lists"}