An open API service indexing awesome lists of open source software.

https://github.com/danielroe/rollup-plugin-pure

Annotate functions as pure for Rollup
https://github.com/danielroe/rollup-plugin-pure

Last synced: about 1 year ago
JSON representation

Annotate functions as pure for Rollup

Awesome Lists containing this project

README

          

# `rollup-plugin-pure`

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]

> Automatically add `/* #__PURE__ */` annotations before definition functions

- [✨  Changelog](https://github.com/danielroe/rollup-plugin-pure/blob/main/CHANGELOG.md)
- [▶️  Online playground](https://stackblitz.com/github/danielroe/rollup-plugin-pure/tree/main/playground)

## Features

- ⚡️ avoids end-users bundling unused code

## How it works

Definition functions (for example, in Vue with `defineComponent`) are increasingly common but do not play nice with tree-shaking. It's not possible to tell whether or not a function call which receives an object can be tree-shaken from a build, as it's possible there might be side effects.

Rollup supports `/* @__PURE__ */` annotations to declare this from a library author's point of view, but it can be tricky when we know that _every_ occurrence of a function call is pure.

This plugin will automatically inject the annotation before any occurrence of the function call, as well as injecting `/* @__NO_SIDE_EFFECTS__ */` annotations in front of function declarations.

## Installation

Install and add `rollup-plugin-pure` to your Vite or Rollup config.

```bash
pnpm add -D rollup-plugin-pure
```

```js
import { PluginPure } from 'rollup-plugin-pure'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
PluginPure({
functions: ['defineComponent', /^define(Page|Meta)$/],
include: [/(?

[npm-version-src]: https://img.shields.io/npm/v/rollup-plugin-pure?style=flat-square
[npm-version-href]: https://npmjs.com/package/rollup-plugin-pure
[npm-downloads-src]: https://img.shields.io/npm/dm/rollup-plugin-pure?style=flat-square
[npm-downloads-href]: https://npm.chart.dev/rollup-plugin-pure
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/danielroe/rollup-plugin-pure/ci.yml?branch=main
[github-actions-href]: https://github.com/danielroe/rollup-plugin-pure/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/danielroe/rollup-plugin-pure/main?style=flat-square
[codecov-href]: https://codecov.io/gh/danielroe/rollup-plugin-pure