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-preserve-directives

v0.4.0

Published

A Rollup plugin to preserve directives like "use client" when preserveModules is true.

Downloads

123,425

Readme

rollup-plugin-preserve-directives

Note: This plugin is still largely untested. It should work fine, but use at your own risk and please help me test it!

This plugin preserves directives when preserveModules: true is set in the Rollup config.

Rollup by default always removes directives like 'use client' from the top of files. This makes sense when bundling files because directives should be applied per file, which is not possible when bundling.

When preserveModules: true is set, because each module is a separate output file, it's possible to keep directives, which is exactly what this plugin does. If you want to add a directive to an entire bundle, use something like rollup-plugin-banner2.

Getting started

Install:

npm install -D rollup-plugin-preserve-directives

Add the plugin to rollup.config.js and make sure you are using preserveModules: true, or the plugin will output a warning and do nothing:

import preserveDirectives from "rollup-plugin-preserve-directives";

export default {
  output: {
    preserveModules: true,
  },
  plugins: [preserveDirectives()],
};

If you are using a minifier like terser, make sure that is not removing the directive again. In terser, you can set compress.directives to false.

Disabling warnings

Rollup warning

This plugin is currently not silencing the warning that Rollup outputs when it comes across directives. It looks something like this:

(!) Module level directives cause errors when bundled, 'use client' was ignored.

Maybe this plugin could hide that warning by default in the future, but for now you can add an onwarn handler in your Rollup config to ignore the warning there, see the docs.

rollup-plugin-preserve-directives warning

This plugin currently warns when it's used in a build that has preserveModules false, since it does nothing then. If you for some reason want to suppress this warning, you can pass the option suppressPreserveModulesWarning: true in the config:

import preserveDirectives from "rollup-plugin-preserve-directives";

export default {
  output: {
    preserveModules: false,
  },
  // This suppresses the warning, but the plugin does nothing
  plugins: [preserveDirectives({ suppressPreserveModulesWarning: true })],
};

This can be useful when you have a common list of plugins for several builds, where some have preserveModules: false and some true.

Include / exclude

To exclude certain files from being processed by this plugin, you can use the include and exclude options. These options take minimatch globs, and can be used like this:

import preserveDirectives from "rollup-plugin-preserve-directives";

export default {
  output: {
    preserveModules: true,
  },
  plugins: [preserveDirectives({ exclude: ["**/*.scss", "**/*.pcss"] })],
};

Motivation and usecase

While this plugin is generic and works with any directive, the motivator was to be able to build libraries that wants to provide both React Server Components and Client Components without having to use separate entrypoints.

This will never be possible in bundled builds, but with this plugin, libraries can support it for unbundled versions.

Contributing, status and todos

This is currently an early and somewhat untested plugin, please help me try it out in more projects and report bugs! This is my first Rollup plugin, so code review and feedback is also very welcome. This plugin:

  • Generates correct sourcemaps (but help me verify)
  • Supports multiple directives (though I have never seen this)

Here are some random todos:

  • Tests
  • Prettier/Linting/EditorConfig
  • If possible: Suppress the Rollup warning when this plugin is used
    • Perhaps we should instead/also export a custom onwarn handler that suppresses the warning if people want to add that to any builds that are not using this plugin? (If this plugin is used in one build, the directive is probably not a problem in the other build)
  • Versioning, release pipeline, changelog etc