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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rollup-plugin-css-text

v1.0.3

Published

A rollup plugin to generate javascript files that export the text of the css files

Readme

rollup-plugin-css-text

A rollup plugin to generate javascript files that export the text of the css files.

The plugin functionality

This plugin will search for all css files that exist in the output directory (which is specified via output.dir rollup option). then it will generate for each css file a javascript file (with the format specified via output.format and the export strategy specified via output.exports rollup options). and this generated javascript file will export a constant which holds the text of the css file.

The generated javascript file will have the same name as its corresponding css file name with .css-text.js extension (e.g if the css file name is styles.css then the generated javascript file name will be styles.css-text.js).

The plugin will also generate a typescript declaration file for each generated javascript css text file (you can turn off this behavior by setting the tsDeclaration option to false, see the full options of this plugin below).

The plugin will not remove the css files, it will only generate a javascript css text file besides each of them (which gives the consumer the flexibility between choosing to import the css file or the generated javascript css text file).

The plugin use cases

one of the use cases is if you are building a package (for example a react component package) and you are outputting a css file that the consumer has to import besides the component that your package exports to be styled.

And here the problem is as the webpack css loader / rollup css plugin will not inject the css in the server-side.

And the solution is to make your package export a javascript css text file for each css file

So the consumer can get the css text via the constant imported from the javascript css text file then pass it to a CSS-in-JS library tool that supports css rendering for both client-side and server-side like styled-jsx, emotion-js, styled-components, or others.

Installation

using npm

npm install --save-dev rollup-plugin-css-text

using yarn

yarn add --dev rollup-plugin-css-text

Usage

While using this plugin you must specify the following rollup options:

  • output.dir the path of the output directory
  • output.format the format of the generated javascript file. the value can be one of the following commonjs | cjs | es | esm | module | iife | amd | umd | system | systemjs
  • output.exports the export strategy of the generated javascript file. the value must be either named or default

config example:

import cssText from 'rollup-plugin-css-text';

/** @type {import('rollup').RollupOptions} */
const options = {
  input: './src/index.js',
  output: {
    dir: './dist',
    format: 'cjs',
    exports: 'named',
  },
  plugins: [
    cssText({
      /* see the plugin options below */
    }),
  ],
};

export default options;

IMPORTANT: This plugin will generate javascript css text files from css files that are in the output directory only (in other words, it will not let you load, output, or minify css files in your project. so you must place a plugin that does that before this plugin like rollup-plugin-postcss with extract set to true)

config with rollup-plugin-postcss example

import postcss from 'rollup-plugin-postcss';
import cssText from 'rollup-plugin-css-text';

/** @type {import('rollup').RollupOptions} */
const options = {
  input: './src/index.js',
  output: {
    dir: './dist',
    format: 'cjs',
    exports: 'named',
  },
  plugins: [
    postcss({
      /* the 'extract' option must be true */
      extract: true,
      /**
       * since we set 'minimize' to `true`,
       * the comments will not exist in the generated javascript files
       * even if the option 'includeComments' of the rollup-plugin-css-text
       * set to 'in-file-only' or 'in-const'
      */
      minimize: true,
    }),
    cssText({
      /* see the plugin options below */
    }),
  ],
};

export default options;

Options

includeComments

Type: 'in-file-only' | 'in-const' | false

Default: 'in-file-only'

if in-file-only the comments will be in the generated javascript css text file only. if in-const the comments will be in the exported const. if false the comments will be removed.

note: if you are using a plugin that deletes the comments of the outputted css files before this plugin, then the comments will not exist in the generated javascript css text files. even if the value of this option is 'in-file-only' or 'in-const' since this plugin generates these files from the outputted css files.

tsDeclaration

Type: boolean

Default: true

if it is true the plugin will generate a typescript declaration for each generated javascript css text file.

constName

Type: string | ((cssFilePath: string) => string | Promise<string>)

Default: 'CSS_TEXT'

the name of the constant that will be exported in the generated javascript css text file and its typescript declaration file.