# rollup-plugin-body-iife

> Transform a script file with imports and early returns into valid ES

Latest version **1.0.0** (published 2020-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install rollup-plugin-body-iife
pnpm add rollup-plugin-body-iife
yarn add rollup-plugin-body-iife
bun add rollup-plugin-body-iife
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-06-01 |
| First published | 2020-06-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ted Driggs |
| Maintainers | teddriggs |
| Keywords | rollup, plugin, iife |

## Links

- npm: https://www.npmjs.com/package/rollup-plugin-body-iife
- Repository: https://github.com/TedDriggs/rollup-plugin-body-iife
- Homepage: https://github.com/TedDriggs/rollup-plugin-body-iife#readme
- Issues: https://github.com/TedDriggs/rollup-plugin-body-iife/issues
- npm.io page: https://npm.io/package/rollup-plugin-body-iife

## Dependencies (1)

- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^3.0.10

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-06-01
- 1.0.0-beta4 — 2020-06-01
- 1.0.0-beta3 — 2020-06-01
- 1.0.0-beta2 — 2020-06-01
- 1.0.0-beta — 2020-06-01

## README

rollup-plugin-body-iife
=======================

This plugin is designed to convert scripts that use static imports and early returns into valid JS or TS.
This is needed because this combination of language elements won't parse normally; both returns outside function bodies and non-top-level static imports fail parsing.

# Input

Here is an example of a file that this plugin will transform:

```javascript
import { checkGeoLocation } from '../util';

if (!checkGeoLocation(Flow.client.ipaddr)) return;

Application('suspiciousGeo').commit();
```

In this case, the desired output is:

```javascript
import { checkGeoLocation } from '../util';

(function () {
    if (!checkGeoLocation(Flow.client.ipaddr)) return;
    Application('suspiciousGeo').commit();
})();
```

This transformation allows build-time dependency resolution using existing tools by making the file valid JS.
Because the "main" function is immediately invoked, this is conceptually equivalent to leaving the main body outside a function declaration.

# Build-Time Invocation

Here is an example of how to invoke this plugin when using the [`rollup` module bundler](https://rollupjs.org).
In `rollup.config.js` in the project root, include the following:

```javascript
import * as glob from 'glob';
import bodyIife from 'rollup-plugin-body-iife';

/**
 * Glob pattern to match each trigger's script file.
 */
const TRIGGER_SCRIPT_GLOB = 'triggers/**/script.js';

/**
 * Inline dependencies and perform tree-shaking to convert an in-repo trigger
 * to one that is understood by the in-product trigger editor and runtime.
 *
 * @param {string} inputPath File path to the trigger script
 */
export const buildOneTrigger = inputPath => ({
    input: inputPath,
    output: {
        file: 'output/' + inputPath,
        format: 'cjs',
    },
    plugins: [
        bodyIife({
            // Only transform the triggers; transforming other JS files will
            // cause build errors.
            include: TRIGGER_SCRIPT_GLOB
        }),
    ],
});

/**
 * Rollup a JS "bundle" for each trigger.
 */
export default glob.sync(TRIGGER_SCRIPT_GLOB).map(buildOneTrigger);
```

It is recommended that this plugin run before others to ensure that later transforms get valid input.
The input and output paths can be changed freely.

---
_Source: https://npm.io/package/rollup-plugin-body-iife · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
