What is vite-plugin-css-injected-by-js?
vite-plugin-css-injected-by-js is a Vite plugin that allows you to inject CSS directly into the JavaScript bundle. This can be useful for scenarios where you want to avoid separate CSS files and ensure that styles are loaded synchronously with your JavaScript.
What are vite-plugin-css-injected-by-js's main functionalities?
Inject CSS into JavaScript
This feature allows you to configure Vite to inject CSS directly into the JavaScript bundle. By using the `cssInjectedByJsPlugin` in the Vite configuration, the CSS will be included in the JavaScript output, ensuring that styles are applied as soon as the JavaScript is executed.
import { defineConfig } from 'vite';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
export default defineConfig({
plugins: [cssInjectedByJsPlugin()]
});
Other packages similar to vite-plugin-css-injected-by-js
vite-plugin-style-import
vite-plugin-style-import is a Vite plugin that allows you to import styles on demand. Unlike vite-plugin-css-injected-by-js, which injects CSS into the JavaScript bundle, vite-plugin-style-import focuses on importing styles only when they are needed, reducing the initial load time.
vite-plugin-css-modules
vite-plugin-css-modules is a Vite plugin that provides support for CSS Modules. It allows you to scope CSS by automatically generating unique class names. While vite-plugin-css-injected-by-js injects CSS into JavaScript, vite-plugin-css-modules focuses on modularizing CSS to avoid style conflicts.
vite-plugin-css-injected-by-js
A Vite plugin that takes the CSS and adds it to the page through the JS. For those who want a single JS file.
How does it work
Essentially what it does is take all the CSS generated by the build process and add it through javascript. The CSS file is therefore not generated and the declaration in the generated HTML file is also removed.
Usage
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
export default {
plugins: [
cssInJsPlugin(),
]
}
Contributions
This is one of the first Vite plugins I do, feel free to open issues and PR. The important thing is that the PRs are created by the develop branch and that everything is explained through the comment and correlated issue.