Code Monkey home page Code Monkey logo

vite-plugin-inline-css-modules's Introduction

vite-plugin-inline-css-modules

npm Code style: Prettier

Note

This package is a fork of bluskript/vite-plugin-inline-css-modules with a few incompatible changes.

  • Explicit tag function name for preprocessor
  • Runtime error on accessing undefined class names

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

pnpm add -D @cueaz/vite-plugin-inline-css-modules

vite.config.ts

import inlineCssModules from '@cueaz/vite-plugin-inline-css-modules';

export default {
  plugins: [inlineCssModules()],
};

src/env.d.ts

/// <reference types="@cueaz/vite-plugin-inline-css-modules" />

src/Root.tsx

// use sass`` scss`` less`` styl`` for preprocessors
const classes = css`
  .root {
    background-color: #1f1;
    @apply rounded-md;
  }
`

// accessing undefined classes.abc will throw a runtime error
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.

Caveats

  • This plugin does not support string interpolation. It may seem that way from the use of template strings, but all this plugin really does is move the contents of the string template into a real CSS module, meaning string interpolation can't work.

  • You can't manipulate the classes variables as normal JS variables.

    Why? because at compile time, this plugin transforms:

    const classes = css``;

    into:

    import classes from './[basename]/classes.inline.module.css.wrapper';

Plugin Options

None

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.

vite-plugin-inline-css-modules's People

Contributors

bluskript avatar cueaz avatar mrfoxpro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.