Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esbuild-ssr-css-modules-plugin

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esbuild-ssr-css-modules-plugin

esbuild plugin for bundling CSS module files for client-side and server-side logic

  • 0.1.14
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
231
decreased by-50.64%
Maintainers
0
Weekly downloads
 
Created
Source

npm version

esbuild SSR CSS Modules Plugin

This esbuild Plugin bundles CSS module files for Server-Side Rendering (SSR) use cases.

Using import directives such as the following in your React components:

import styles from './Panel.module.css';

Results in exceptions when trying to render your React components on the server-side, since these will be executed in Node.js and Node.js cannot load the .css files. (Note: if you absolutely require this, such as for local testing, have a look at node-css-require.)

This plugin will:

  • Replace all CSS files imported into React source files with JavaScript files. This allows looking up the generated style names during server-side rendering.
  • Provide the generated CSS source.
  • (Optional) Inject JavaScript snippets into JavaScript source files that will dynamically load a style tag with the generated CSS. This can be useful for local testing but is not recommended for production deployments.

Usage

(1) Install this plugin as a dependency for your project:

npm i esbuild-ssr-css-modules-plugin
# -- or
yarn add esbuild-ssr-css-modules-plugin

(2) Add this plugin to the list of plugins supplied to esbuild:

import cssPlugin from 'esbuild-ssr-css-modules-plugin';

const res = await build({
  plugins: [cssPlugin()],
});

Options

onCSSGenerated

The plugin accepts the parameter onCSSGenerated. This parameter accepts a callback function that receives one argument css that contains snippets of CSS generated.

import cssPlugin from 'esbuild-ssr-css-modules-plugin';

const generatedCss: string[] = [];
const res = await build({
  plugins: [
    cssPlugin({
      onCSSGenerated: (css) => {
        generatedCss.push(css);
      },
    }),
  ],
});
console.log(generatedCss.join('\n'));

This can be useful for creating a CSS bundle that can be shipped with the other files packaged for the server. When generating a client-side bundle using esbuild-css-modules-client-plugin, the option excludeCSSInject can then be set to true for a better page load experience (no flicker of the page once CSS is injected).

jsCSSInject

The plugin supports the option jsCSSInject. This defaults to false. When set to true, <script> tags are injected to be run during script load.

import cssPlugin from 'esbuild-ssr-css-modules-plugin';

const res = await build({
  plugins: [
    cssPlugin({
      jsCSSInject: true,
    }),
  ],
});

How does it work?

When no JS is injected

This plugin will transform .css files into JavaScript source files that contain:

  • A default export with a map of original class names to transformed class names

Thus, when server-side rendering is performed, class names can be resolved to the transformed counter-parts and the HTML rendered with the correct class names.

When JS is injected

This plugin will transform .css files into JavaScript source files that contain:

  • A small snippet of JavaScript that will inject a <script> tag when the script is loaded that contains the CSS (with transformed class names as per CSS module specification)
  • A default export with a map of original class names to transformed class names

Thus, when the client-side bundle is loaded, all required CSS will be injected into the page and we can use the styles object to resolve original class names to the transformed ones.

See also

Keywords

FAQs

Package last updated on 25 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc