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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-css-in-js

v1.3.1

Published

Resolves css tagged templates to css files within Javascript

Downloads

19

Readme

Vite CSS in JS

A CSS in JavaScript plugin which allows developers to write their CSS within JavaScript, while outputting to actual CSS files when bundling and avoiding class name conflicts.

Originally designed for use with JSX and React or VueJs, but can be used with any framework.

Installation

1. Install the package:

npm install -D vite-css-in-js

2. Add to Vite:

/* vite.config.ts */
import { createCSSJSImportPlugin } from "vite-css-in-js"

export default defineConfig({
  plugins: [
    // ...your plugins,
    createCSSJSImportPlugin()
  ],
  ...
})

Example

/* MyButton.jsx */
import { css } from "vite-css-in-js"

//Variable will contain the class name of the specified styles
const buttonStyles = css`
  color: white;
  background-color: royalblue;
  border: 1px solid #1c48ce;

  font-size: 1.25rem;
  padding: 0.5rem 2rem;
  border-radius: 1rem;

  cursor: pointer;

  /* Component selectors & nesting is permitted */
  &:hover {
    background-color: #587adf;
  }
`

//React
export const MyButton = () => <button className={buttonStyles}>Click Me!</button>

//Vue
export const MyButton = defineComponent({
  render() {
    return <button class={buttonStyles}>Click Me!</button>
  },
})

Outputs:

import "vite-css-in-js@button_nMvFo3p.css"

//Variable has been assigned the class name of the associated CSS
const buttonStyles = "button_nMvFo3p"

//React
export const MyButton = () => <button className={buttonStyles}>Click Me!</button>

//Vue
export const MyButton = defineComponent({
   render() {
      return <button class={buttonStyles}>Click Me!</button>
   },
})
/* Generated CSS */
.button_nMvFo3p {
   color: white;
   background-color: royalblue;
   border: 1px solid #1c48ce;
   font-size: 1.25rem;
   padding: 0.5rem 2rem;
   border-radius: 1rem;
   cursor: pointer;
}

.button_nMvFo3p:hover {
   background-color: #587adf;
}

VSCode

When using with VSCode, its highly recommended to use the following plugin to enable rich CSS integration: https://marketplace.visualstudio.com/items?itemName=styled-components.vscode-styled-components

Caveats & Edge Cases

1. Variable Substitutions

Only string constants with no variable substitutions can be transformed. Code such as the following is not allowed and will not compile:

//Not allowed!
const color = "green"
const styles = css`
  background-color: ${color};
`

If you need to utilize conditional styles, you may want to consider direct element styling or alternate CSS in JS libraries.

2. Style Ordering

Styles are ordered based on the order they appear in the syntax tree, but should be intuitive if you keep your code organized.

In the following example, the text will be underlined and dark green in color:

import { css } from "vite-css-in-js"

const initialStyles = css`
  text-decoration: underline;
  color: red;
`

//Declared later so will override previous styles
const overrideStyles = css`
  color: darkgreen;
`

//React
export const MyText = () => <p className={initialStyles + " " + overrideStyles}>Hello World!</p>

All styles get imported from the bundler's point of view at the same position as the vite-css-in-js import. Here is an example of how the internals work to illustrate the point:

/* MyText.jsx */
import "themes/material.css"
import { css } from "vite-css-in-js"
import "./company-branding.css"

const textStyles = css`
  color: lime;
  background-color: hotpink;
  font-family: "Comic Sans MS";
`

const extraStyles = css`
  text-decoration: underline;
`

export const MyText = () => <p className={textStyles + " " + extraStyles}>Hello World!</p>

Output:

import "themes/material.css"

//Synthetic CSS imports get placed where the original css import was
import "vite-css-in-js@text_xCUP6R0.css"
import "vite-css-in-js@extra_zewBme7.css"

import "./company-branding.css"

const textStyles = "text_xCUP6R0"
const extraStyles = "extra_zewBme7"

export const MyText = () => <p className={textStyles + " " + extraStyles}>Hello World!</p>

3. CSS Imports are currently synthetic

All css import statements from vite-css-in-js will be removed via the Vite plugin at compile time to facilitate CSS generation. Do not use the import for purposes other than creating styles.

4. Class names are not guaranteed to be stable

Class names for styles are not guaranteed to be stable, and will certainly change whenever the contents of the styles is changed. You should not depend on a specific value for the generated class name