Gathering detailed insights and metrics for rollup-plugin-preserve-directives
Gathering detailed insights and metrics for rollup-plugin-preserve-directives
Gathering detailed insights and metrics for rollup-plugin-preserve-directives
Gathering detailed insights and metrics for rollup-plugin-preserve-directives
A Rollup plugin to preserve directives like "use client" when preserveModules is true
npm install rollup-plugin-preserve-directives
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
67 Stars
24 Commits
8 Forks
3 Watching
2 Branches
6 Contributors
Updated on 23 Oct 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-43.2%
4,230
Compared to previous day
Last week
-2.5%
31,003
Compared to previous week
Last month
15.5%
130,500
Compared to previous month
Last year
476%
1,005,181
Compared to previous year
2
1
3
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.
Install:
1npm 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:
1import preserveDirectives from "rollup-plugin-preserve-directives"; 2 3export default { 4 output: { 5 preserveModules: true, 6 }, 7 plugins: [preserveDirectives()], 8};
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
.
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.
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:
1import preserveDirectives from "rollup-plugin-preserve-directives"; 2 3export default { 4 output: { 5 preserveModules: false, 6 }, 7 // This suppresses the warning, but the plugin does nothing 8 plugins: [preserveDirectives({ suppressPreserveModulesWarning: true })], 9};
This can be useful when you have a common list of plugins for several builds, where some have preserveModules: false
and some true
.
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:
1import preserveDirectives from "rollup-plugin-preserve-directives"; 2 3export default { 4 output: { 5 preserveModules: true, 6 }, 7 plugins: [preserveDirectives({ exclude: ["**/*.scss", "**/*.pcss"] })], 8};
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.
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:
Here are some random todos:
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)No vulnerabilities found.
No security vulnerabilities found.