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

[![test](https://github.com/eight04/rollup-plugin-iife/actions/workflows/test.yml/badge.svg)](https://github.com/eight04/rollup-plugin-iife/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/eight04/rollup-plugin-iife/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/rollup-plugin-iife)
[![install size](https://packagephobia.now.sh/badge?p=rollup-plugin-iife)](https://packagephobia.now.sh/result?p=rollup-plugin-iife)

Currently (rollup@0.65), [rollup doesn't support code splitting with IIFE output](https://github.com/rollup/rollup/issues/2072). This plugin would transform ES module output into IIFEs. Powered by [es-iife](https://www.npmjs.com/package/es-iife).

Installation
------------

```
npm install -D rollup-plugin-iife
```

Usage
-----

```js
import iife from "rollup-plugin-iife";

export default {
  input: ["entry.js", "entry2.js"],
  output: {
    dir: "dist",
    format: "es",
    globals: {
      vue: "Vue"
    }
  },
  externals: ["vue"],
  plugins: [iife()]
};
```

Define global variables for external imports
--------------------------------------------

You can define global variables with `output.globals` just like before. You can also specify them with the plugin option `names`.

The plugin would first lookup `names` option then `output.globals`.

API
----

This module exports a single function.

### iife

```js
iife({
  names?: Function|Object,
  sourcemap?: Boolean,
  prefix?: String,
  strict?: Boolean
}) => PluginInstance
```

Create the plugin instance.

If `names` is a function, the signature is:

```js
(moduleId: String) => globalVariableName: String
```

If `names` is an object, it is a `moduleId`/`globalVariableName` map. `moduleId` can be relative to the output folder (e.g. `./entry.js`), the plugin would resolve it to the absolute path.

If the plugin can't find a proper variable name, it would generate one according to its filename with [camelcase](https://www.npmjs.com/package/camelcase).

If `sourcemap` is false then don't generate the sourcemap. Default: `true`.

When `prefix` is defined, it will be used to prefix auto-generated variable names. It doesn't prefix names defined in the `names` option. It doesn't prefix external imports.

If `strict` is true then add `'use strict';` directive to the IIFE. Default: `true`.

Related projects
----------------

* [rollup-plugin-external-globals](https://www.npmjs.com/package/rollup-plugin-external-globals) - transform imports into global variables *at transform hook*.
* [amd-script](https://www.npmjs.com/package/amd-script) - a small AMD module cache that would work with AMD module chunks generated by Rollup. You have to load modules manually (e.g. load modules through `<script>` tags).
* [rollup-plugin-loadz0r](https://github.com/surma/rollup-plugin-loadz0r) - attach each chunk with a module loader so you don't have to include a module loader at runtime. Support workers.

Changelog
---------

* 0.8.0 (Apr 2, 2026)

  - Bump dependencies. Fix export assignment issue.

* 0.7.1 (Feb 16, 2024)

  - Relax peer dependency. Support rollup 4.

* 0.7.0 (Oct 20, 2022)

  - Breaking: bump to rollup@3.
  - Change: throw an error if `output.dir` is not defined.

* 0.6.0 (Feb 11, 2022)

  - Add: handle `import.meta.url`. The plugin also throws and error if it sees unconverted `import.meta`.

* 0.5.0 (Feb 18, 2021)

  - Now `'use strict';` directive is inserted by default.
  - Add: `strict` option.

* 0.4.0 (Dec 28, 2020)

  - Fix: plugin name.
  - **Bump dependencies. Update to rollup@2.35.1, replace camelcase with lodash/camelCase.**

* 0.3.1 (Feb 8, 2020)

  - Fix: prefix local imports.

* 0.3.0 (Aug 14, 2019)

  - Bump dependencies. Update to es-iife@0.2.0. Define default export with `var`.

* 0.2.1 (Jun 23, 2019)

  - Fix: handle empty chunks.

* 0.2.0 (Jan 26, 2019)

  - Breaking: bump rollup version to 1.x.

* 0.1.2 (Jan 25, 2019)

  - Add: `prefix` option.

* 0.1.1 (Sep 28, 2018)

  - Add: support `output.globals` option.

* 0.1.0 (Aug 28, 2018)

  - First release.
