Write CSS modules without leaving your javascript!

Overview

vite-plugin-inline-css-modules

npm Code style: Prettier

Write CSS modules without leaving your javascript!

  • Zero Runtime
  • Contains full feature set of CSS modules (Includes PostCSS if you use that!)
    • Supports @apply and others!
    • Scopes your CSS locally to your component!
  • Supports ANY framework

Usage

npm install vite-plugin-inline-css-modules
import inlineCssModules from 'vite-plugin-inline-css-modules'

export default {
  plugins: [inlineCssModules()],
}
import { css } from 'vite-plugin-inline-css-modules'

const classes = css`
  .root {
    background-color: #1f1;
    @apply rounded-md;
  }
`

export const Root = () => <div class={classes.root}>Hello world</div>

Why is this useful?

This was originally written for writing styles in SolidJS. I came from Vue, which already contained a special <style scoped> tag, and I wanted something just as easy to use as that. If you are using a framework that does not support writing scoped styles natively, this is for you!

Why not one of the hundreds of existing CSS-in-JS solutions?

Every single CSS-in-JS solution i've seen suffers from the same problem: it can't integrate with existing tooling. This plugin simply generates a CSS module using the contents of the string. This allows it to integrate with PostCSS and things like Tailwind or UnoCSS with ease.

In addition, a lot of solutions also have an implicit bundling cost. This differs in that it is completely based on CSS modules. No addition javascript is added when using this plugin.

Caveats

  • This plugin does NOT support string interpolation. It may seem that way from the use of template strings, but I assure you, all this plugin does is move the contents of the string template into a real CSS module, meaning you cannot interpolate strings.

  • You CANNOT manipulate the classes variables as normal JS variables.

    Why? because at compile time, this plugin transforms:

    const classes = css``

    into:

    import classes from 'virtual:inline-css-modules/App-0.module.css'

Plugin Options

  • tagName: The CSS template tag name to match for.
    • Default: css
    • If you are using other CSS-in-JS frameworks, you can use import aliases during destructuring and set the tagName value to the new name to prevent conflicts.
  • fileMatch: The regex pattern used to match files to inline.
    • Default: /\.(tsx|jsx|js|vue|svelte)$/

Help

  • I'm getting an error like inlineCss is not defined!
    • This is probably because you didn't set the tag name correctly in config. This plugin might be deleting your import of inlineCss from this plugin, so please check to make sure that the tagName option is set correctly.
You might also like...

Vite plugin for minifying / obfuscating CSS classes in production builds

Vite plugin for minifying / obfuscating CSS classes in production builds

vite-plugin-class-mangler Vite plugin for minifying / obfuscating classes in production builds. Compatible with Tailwind, inline, or imported styles.

Dec 22, 2022

Collection of CSS box-shadows for every taste contains more than 100 simple, beautiful and airy shadows.

Collection of CSS box-shadows for every taste contains more than 100 simple, beautiful and airy shadows.

Vue Box-shadows Collection of CSS box-shadows for every taste contains more than 100 simple, beautiful and airy shadows. Add beautiful shadow effects

Nov 14, 2022

Make use css module more sense.

vite-plugin-sense-css-module Make use css module more sense. Installation npm npm install --save-dev vite-plugin-sense-css-module yarn yarn add -D vi

Sep 25, 2022

Easily connect your Nuxt3 application to your directus server. 🐰

Easily connect your Nuxt3 application to your directus server. 🐰

nuxt-directus Directus Module for Nuxt 3 ✨ Release Notes 📖 Read the documentation Features Nuxt3 ready Handle authentication RESTful methods TypeScri

Dec 28, 2022

:necktie: :briefcase: Build fast :rocket: and easy multiple beautiful resumes and create your best CV ever! Made with Vue and LESS.

:necktie: :briefcase: Build fast :rocket: and easy multiple beautiful resumes and create your best CV ever! Made with Vue and LESS.

best-resume-ever 👔 💼 Build fast 🚀 and easy multiple beautiful resumes and create your best CV ever! Made with Vue and LESS. Cool Creative Green Pur

Jan 9, 2023

tsParticles - Easily create highly customizable particles animations and use them as animated backgrounds for your website. Ready to use components available for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Inferno.

tsParticles - Easily create highly customizable particles animations and use them as animated backgrounds for your website. Ready to use components available for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Inferno.

tsParticles - TypeScript Particles A lightweight TypeScript library for creating particles. Dependency free (*) and browser ready! Particles.js conver

Jan 4, 2023

A simple, customization star rating component for your vue3 projects

A simple, customization star rating component for your vue3 projects

vue3-star-ratings A simple, customizable component for star ratings Documentation Features Uses svg for the stars, so it can scale without quality los

Oct 7, 2022

Backs up your favourite and recently used gifs/emotes and restores them in case discord clears them after logouts or for other reasons

Persist Favourites This plugin solves the problem of Discord randomly deciding to clear your gifs or emotes by backing both up regularely and restorin

Oct 13, 2022

quickly start your Vue dev server

quickly start your Vue dev server

vue-fast-dev-server 10倍缩短您的vue dev server启动时间 安装 npm install vue-fast-dev-server --save-dev vue.config.js const path = require('path') module.exports

Feb 28, 2022
Comments
  • Issue with npm run build with recent vite versions

    Issue with npm run build with recent vite versions

    Hi,

    Thanks for creating this very elegant css solution. I am encountering the following error when building with vite (3.0.4) using npm run build:

    [vite:load-fallback] Could not load virtual:inline-css-modules/App-0.module.css?used (imported by src/App.tsx): The argument 'path' must be a string or Uint8Array without null bytes. Received '\x00virtual:inline-css-modules/App-0.module.css?used' error during build: TypeError [PLUGIN_ERROR]: Could not load virtual:inline-css-modules/App-0.module.css?used (imported by src/App.tsx): The argument 'path' must be a string or Uint8Array without null bytes. Received '\x00virtual:inline-css-modules/App-0.module.css?used' at open (node:internal/fs/promises:450:10) at Object.readFile (node:internal/fs/promises:795:20) at Object.load (file:///Users/yongm1/my-react-app/node_modules/vite/dist/node/chunks/dep-71eb12cb.js:43230:35) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async file:///Users/yongm1/my-react-app/node_modules/rollup/dist/es/shared/rollup.js:22096:98 at async Queue.work (file:///Users/yongm1/my-react-app/node_modules/rollup/dist/es/shared/rollup.js:22773:32)

    This contains something similar to the example (I am using it in React): import { css } from 'vite-plugin-inline-css-modules'

    const classes = css.root { background-color: #1f1; @apply rounded-md; }

    Please let me know if there is any way i could work around this.

    Thanks.

    opened by mangkang 4
  • CSS sourcemap generation causing issue with JS sourcemap generation

    CSS sourcemap generation causing issue with JS sourcemap generation

    Description Vite currently supports CSS sourcemap only on dev, not on build. It also supports JS sourcemap on both environments. When this plugin is installed in a Vite codebase, the build process is unable generate the CSS sourcemap. This issue also causes the JS sourcemap from being generated fully.

    Error "Sourcemap is likely to be incorrect: a plugin (inline-css-modules) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help"

    Versions: vite 3.0.0 vite-plugin-inline-css-modules 0.0.6

    Steps to reproduce:

    1. Start with a fresh vite create vite@latest app --template react-ts
    2. Make sure build.sourcemap is set to true in vite.config.js
    3. npm run build
    4. Notice the built files in dist has a working JS sourcemap
    5. npm i vite-plugin-inline-css-modules
    6. import { inlineCssModules } fromvite-plugin-inline-css-modulesand call it in theplugins[]invite.config.js`
    7. npm run build
    8. Notice the built files in dist has a broken JS sourcemap (with much lower file size compared to previous)
    opened by kp-entrust 2
  • Adding a demo Image

    Adding a demo Image

    Hello,

    I would like to request is possible to add an image to the 'demo(s) Folder' or the 'readme page' since it provides more information about the contests without actually having to install the repo.

    Thanks.

    opened by moistwrist3 1
  • Sourcemaps are broken

    Sourcemaps are broken

    Thank you for this interesting idea and plugin implementation! I'm trying to migrate from webpack + linaria to vite + css modules and found this plugin very close to make the migration process almost as a drop-in replacement. Here are some of the tweaks made: https://github.com/Dmitra/vite-plugin-inline-css-modules.

    The only thing I cannot make to work are sourcemaps. They are wrong and it looks like for exact amount of removed lines from the original file. Is it because they are not taken care of here: https://github.com/bluskript/vite-plugin-inline-css-modules/blob/master/src/index.ts#L70 ? Do you have any ideas how to fix it?

    opened by Dmitra 3
Owner
Blusk
Go / TypeScript / C++ / C# / Vue / React / Kotlin
Blusk
Automatically detect and import components or modules

vite-plugin-autoimport Automatically detect and import components or modules. Motivation It's very common to have many components in one file as the p

Yuan Chuan 146 Dec 23, 2022
Unified utils for auto importing APIs in modules.

unimport Unified utils for auto importing APIs in modules Install # npm npm install unimport # yarn yarn install unimport # pnpm pnpm install unimpo

unjs 180 Jan 9, 2023
The first truly composable CSS animation library. Built for Vue, React, SCSS, and CSS, AnimXYZ will bring your website to life.

AnimXYZ animxyz.com AnimXYZ helps you create, customize, and compose animations for your website. Powered by CSS variables to allow a nearly limitless

Ingram Projects 2.1k Jan 2, 2023
A Svelte SPA Template for simple SPA Apps without using svelte kit.

Svelte SPA Template A Svelte Single Page Application Template for simple SPA Apps without using Svelte Kit or Sapper. New Project To create a new proj

ADITYA 1 Jan 24, 2022
🌈 CLI tool to scaffold chakra ui projects, without wasting time.

Create Chakra Project (create-chakra) ⚒ Tool to scaffold chakra apps, without wasting time. It uses existing tools like create-next-app and create-vit

Vedant Nandwana 13 Nov 29, 2022
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.

Headless UI A set of completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS. Packages Name Version D

Tailwind Labs 18.4k Jan 4, 2023
Build performant, native and cross-platform desktop applications with native Vue + powerful CSS like styling.🚀

Vue NodeGui Build performant, native and cross-platform desktop applications with Vue. ?? Vue NodeGUI is powered by Vue ?? and Qt5 ?? which makes it C

NodeGui 765 Dec 30, 2022
Vue injects CSS variables

vue-cssvar Vue 注入 CSS 变量控制样式 (兼容 vue2 , vue3) 样例:点击 div。div 以及其 hover伪类会变色。 <template> <div v-css="{a,b}" class="box" @click="changeColor"></div> </t

siluxianren 14 Mar 8, 2022
100 Modern CSS Buttons, Free And Royalty Free.

?? Contributions Currently, there are 100 buttons in this project, but we’d love to have more! If you have new button ideas, create an Issue. If you h

null 2.7k Jan 7, 2023
Mobile UI Components, based on Vue.js and ionic CSS

Mobile UI Components, based on Vue.js and ionic CSS

王大虎 3.4k Dec 23, 2022