Skip to content

eight04/rollup-plugin-iife

Repository files navigation

rollup-plugin-iife

Build Status codecov install size

Currently (rollup@0.65), rollup doesn't support code splitting with IIFE output. This plugin would transform ES module output into IIFEs.

Installation

npm install -D rollup-plugin-iife

Usage

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

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

Create the plugin instance.

If names is a function, the signature is:

(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.

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 - transform imports into global variables at transform hook.
  • 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 - attach each chunk with a module loader so you don't have to include a module loader at runtime. Support workers.

Changelog

  • 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.