npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-typings-for-css

v0.13.0

Published

Webpack loader that generates TypeScript typings for CSS modules

Downloads

447

Readme

Webpack Typings for CSS loader

NPM License Downloads Build

Webpack loader that generates TypeScript typings for CSS modules

Installation

npm install webpack-typings-for-css --save-dev

webpack.config.js

module.exports = {
    module: {
        rules: [{
            test: /\.css$/,
            use: [{
                loader: 'style-loader'
            }, {
                loader: 'webpack-typings-for-css'
            }, {
                loader: 'css-loader',
                options: {
                    localsConvention: 'camelCaseOnly',
                    modules: {
                        localIdentName: '[name]__[local]__[hash:base64]'
                    }
                }
            }]
        }]
    }
};

Example

Let's say we are building a simple React button component. And we have this file: ./src/button/style.scss in our project with the following content:

.button {
    color: black;
    background: white;
    font-size: 12px;
    padding: 10px;

    &--small {
        padding: 5px;
        font-size: 10px;
    }

    &--large {
        padding: 20px;
        font-size: 14px;
    }
}

When we add the webpack-typings-for-css loader, this will generate a TypeScript definition file ./src/button/style.scss.d.ts with the following content:

declare const styles = {
    button: 'styles__button__2wmORyJZRJFrl_Mc46Dxnh',
    buttonSmall: 'styles__button--small__1dezcn7Xl7U8em93APXPIU',
    buttonLarge: 'styles__button--large__2GA-bPwea6oWSnVWqjy_ux',
} as const;

export type ClassName = (
    'styles__button__2wmORyJZRJFrl_Mc46Dxnh' |
    'styles__button--small__1dezcn7Xl7U8em93APXPIU' |
    'styles__button--large__2GA-bPwea6oWSnVWqjy_ux'
);

export default styles;

In your Typescript file you will now be able to import this file and get type hints with the available classNames. The values for these properties are the final classNames that are generated by the css-loader. In our example they are hashed:

import styles from './styles.scss';

console.log(styles.button); // styles__button__wzc-BjG34gg0YTmmp71Mh
console.log(styles.buttonSmall); // styles__button--small__Jl0thjpdKMgQgK4SczMV_
console.log(styles.buttonLarge); // styles__button--large__2vSRkXkjXTxZOTMAbhDH_n

In your (React) button component you can now use the same import and use these readable type checked classNames to style your components!

import React from 'react';

import styles from './styles.scss';

export const Button = () => (
    <button className={styles.button}>Hello World!</button>
);

Known issues

As the loader generates typing files, it is wise to tell webpack to ignore them. The fix is luckily very simple. Webpack ships with a "WatchIgnorePlugin" out of the box. Simply add this to your webpack plugins:

plugins: [
    new webpack.WatchIgnorePlugin([
        /css\.d\.ts$/
    ]),
    ...
]

You might see project warnings when you have not yet build the project. This happens because Typescript cannot import SCSS, LESS or CSS files (since they are not official modules). In order to fix this you can add a type definition to your project. You can find the common examples in the typings folder of this project.

Let's say your project uses SCSS files for styling then add the following file to your project:

./typings/scss.d.ts

declare module '*.scss' {

    declare const styles: {
        readonly [key: string]: string;
    };

    export type ClassName = string;

    export default styles;
}

tsconfig.json

{
    ...
    "files": [
        "./typings/scss.d.ts"
    ],
    ...
}