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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-native-css-modules

v1.1.1

Published

Use native CSS modules with import assertions in Rollup. This plugin is intended to be used if you want to use import assertions in your build _output_, either in browsers that already support it, or because you're using something like [es-module-shims](h

Downloads

7

Readme

Rollup-plugin-native-css-modules

Use native CSS modules with import assertions in Rollup. This plugin is intended to be used if you want to use import assertions in your build output, either in browsers that already support it, or because you're using something like es-module-shims to support native CSS modules.

This plugin does not transform any CSS module imports to JavaScript, it leaves the import statements and imports in tact.

Example

Checkout the example on Stackblitz.

Or take a look at the example project for a more elaborate example.

Input

src/index.js:

import styles from './styles.css' assert { type: 'css' };

Output:

dist/index.js:

import styles from './styles-3275f665.css' assert { type: 'css' };

Usage

npm i -S rollup-plugin-native-css-modules
import css from 'rollup-plugin-native-css-modules';

export default {
  input: 'index.js',
  output: {
    dir: 'dist',
    format: 'esm'
  },
  plugins: [
    css()
    /**
     * Or:
     */
    css({
      transform: (code) => {
        // modify the CSS code, minify, post-process, etc
        return code;
      }
    })
  ]
};

Features

At time of writing Rollup V3 supports import assertion syntax, however, Rollup will still try to parse any module that gets imported in your source code and expect it to be JavaScript. This will cause Rollup to throw an error, because it'll try to parse CSS files expecting it to be JavaScript. This plugin fixes that.

Support

This plugin supports:

import styles from './styles.css' assert { type: 'css' };
import styles from 'bare-module-specifier/styles.css' assert { type: 'css' };
import('./styles.css', { assert: { type: 'css'} });

This plugin does NOT support:

import(`./styles-${i}.css`, { assert: { type: 'css'} });
import('./styles-' + i + '.css', { assert: { type: 'css'} });

The reason for this is that imports with dynamic variables are hard to statically analyze, because they rely on runtime code.

External stylesheets are ignored:

import styles from 'http://styles.com/index.css' assert { type: 'css' };
import styles from 'https://styles.com/index.css' assert { type: 'css' };

Data uri's are also ignored.

Deduplication

This plugin also deduplicates imports for the same module. If foo.js and bar.js both import my-styles.css, only one CSS file will be output in the output directory, as opposed to two.

Hashing

CSS modules output by this plugin receive a hash based on the contents of the CSS file. E.g.:

input:

import styles from './styles.css' assert { type: 'css' };

output:

import styles from './styles-3275f665.css' assert { type: 'css' };

Transform

You can use the transform hook to modify the CSS that gets output to, for example, minify your CSS using a tool like lightning CSS or something like postcss.

import css from 'rollup-plugin-native-css-modules';
import { transform } from 'lightningcss';

export default {
  input: 'demo/index.js',
  output: {
    dir: 'demo/dist',
    format: 'esm'
  },
  plugins: [
    css({
      transform: (css) => transform({
        code: Buffer.from(css),
        minify: true
      }).code.toString()
    })
  ]
};

FAQ

Polyfilling

At the time of writing, browser support for import assertions is still low, so you're probably going to need to polyfill them. You can do this via es-module-shims, note that you'll also need a polyfill for constructable stylesheets, which you can polyfill via construct-style-sheets-polyfill.

Why are CSS files not combined and bundled?

Because you can't. Consider the following example:

my-app/
├─ index.js
├─ element-a.js
├─ element-b.js
├─ blue-styles.css
├─ red-styles.css
import './element-a.js';
import './element-b.js';
import blueStyles from './blue-styles.css' assert { type: 'css' };

class ElementA extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [blueStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>blue</h1>';
  }
}

customElements.define('element-a', ElementA);
import redStyles from './red-styles.css' assert { type: 'css' };

class ElementB extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [redStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>red</h1>';
  }
}

customElements.define('element-b', ElementB);
h1 {
  color: blue;
}
h1 {
  color: red;
}

Bundling this would lead to the following build output:

import blueStyles from './styles-3275f665.css' assert { type: 'css' };
import redStyles from './styles-3a3f9686.css' assert { type: 'css' };

class ElementA extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [blueStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>blue</h1>';
  }
}

customElements.define('element-a', ElementA);

class ElementB extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [redStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>red</h1>';
  }
}

customElements.define('element-b', ElementB);
h1 {
  color: red;
}
h1 {
  color: blue;
}

If you would combine the CSS files for blueStyles and redStyles into one, and use that stylesheet in both components, it would lead to style clashes; you would only have red <h1>s, instead of one blue <h1> for <element-a> and one red <h1> for <element-b>.

To illustrate:

import bundledStyles from './styles-f32a2851.css' assert { type: 'css' };

class ElementA extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [bundledStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>blue</h1>';
  }
}

customElements.define('element-a', ElementA);

class ElementB extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.adoptedStyleSheets = [bundledStyles];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<h1>red</h1>';
  }
}

customElements.define('element-b', ElementB);
h1 {
  color: blue;
}

h1 {
  color: red;
}