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

css-var-extract

v1.2.0

Published

Use CSS vars type-safely for CSS-in-JS

Downloads

1,167

Readme

CSS Var Extract

npm version

Use CSS variables type-safely for CSS-in-JS.

Generate at build time, has no runtime dependencies, and can be optimized.

🤔 Why?

Type-safely access CSS variables created by yourself or by external component libraries from TypeScript.

:root {
    --primary: #d3a3a3;
    --secondary: #5aa68a;
    --white: #fff;
}

Automatically generate .ts files based on .css.

// :root: #d3a3a3
export const primary = "var(--primary)"

// :root: #5aa68a
export const secondary = "var(--secondary)"

// :root: #fff
export const white = "var(--white)"

It can be used in styles and also in CSS-in-JS such as vanilla-extract.

import "./theme.css";
import * as vars from "./cssVar.gen";

const App = () => {
    return (
        <div style={{ backgroundColor: vars.primary }}>
            <h1 style={{ color: vars.primary }}>
                Primary color
            </h1>
            <p style={{ color: vars.secondary }}>
                Secondary color
            </p>
        </div>
    );
};

export default App;
import { style } from "@vanilla-extract/css";
import * as vars from "./cssVar.gen.ts";

export const buttonStyle = style({
    backgroundColor: vars.primary,
});

export const messageStyle = style({
    color: vars.secondary,
});

📦 Installation

You need to configure your project's bundler to use the CssVarExtract Plugin or the CssVarExtract CLI.

The plugin will automatically generate .ts through your bundler's dev and build processes.

Configuration with Vite

You need to install the css-var-extract-plugin package.

npm i -D css-var-extract-plugin

Once installed, you'll need to add the plugin to your Vite configuration.

// vite.config.ts
import react from "@vitejs/plugin-react";
import CssVarExtractVite from "css-var-extract-plugin/vite";
import { defineConfig } from "vite";

export default defineConfig({
    plugins: [
        CssVarExtractVite({
            files: ["src/theme.css"],
        }),
        react(),
    ],
});

Configuration with Rspack/Rsbuild

You need to install the css-var-extract-plugin package.

npm i -D css-var-extract-plugin

Once installed, you'll need to add the plugin to your configuration.

// rsbuild.config.ts
import { defineConfig } from "@rsbuild/core";
import { pluginReact } from "@rsbuild/plugin-react";
import CssVarExtractRspack from "css-var-extract-plugin/rspack";

export default defineConfig({
    plugins: [pluginReact()],
    tools: {
        rspack: {
            plugins: [
                CssVarExtractRspack({
                    files: ["src/theme.css"],
                }),
            ],
        },
    },
});

Configuration with Webpack

You need to install the css-var-extract-plugin package.

npm i -D css-var-extract-plugin

Once installed, you'll need to add the plugin to your configuration.

// webpack.config.ts
import CssVarExtractWebpack from "css-var-extract-plugin/webpack"

export default {
    plugins: [CssVarExtractWebpack({
        files: ["src/theme.css"],
    })],
}

Configuration with the CSS Var Extract CLI

You need to install the css-var-extract-cli package.

npm i -D css-var-extract-cli

Once installed, you'll need to amend your scripts in your package.json for the CLI to watch and generate files.

{
  "scripts": {
    "generate-css-var": "cve generate",
    "watch-css-var": "cve watch",
    "build": "npm run generate-css-var && ...",
    "dev": "npm run watch-css-var && ..."
  }
}

With the CLI installed, the following commands are made available via the cve command

Using the generate command

Generates the .ts file based on the provided configuration.

cve generate

Using the watch command

Continuously watches the specified files and regenerates the .ts file as needed.

cve watch

🔧 Configuration

You can create cve.config.json and change settings. If you are using a plugin, it also supports inline configuration.

cve.config.json

{
  "files": [],
  "output": "./src/cssVar.gen.ts",
  "fileHeader": [
    "/* prettier-ignore-start */",
    "/* eslint-disable */",
    "// @ts-nocheck",
    "// noinspection JSUnusedGlobalSymbols"
  ],
  "fileFooter": [
    "/* prettier-ignore-end */"
  ]
}

👏 Acknowledgments

This project was inspired by and references implementation patterns from the TanStack Router package. We extend our gratitude to the developers and maintainers of TanStack Router for their excellent work.