babel-plugin-iife-wrap
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript
A Babel plugin that wraps the entire file content in an Immediately Invoked Function Expression (IIFE) to avoid polluting the global scope. Current stable version 1.1.0 (latest, 2019). Simple and focused: no configuration options, works with Babel 7+. Differentiator: extremely minimal and does one thing—file-level IIFE wrapping, unlike more complex scope isolators.
Common errors
error Error: Could not find plugin 'babel-plugin-iife-wrap' ↓
cause Plugin not installed or not listed in .babelrc plugins array correctly.
fix
Run: npm install babel-plugin-iife-wrap --save-dev. Then in .babelrc: { "plugins": ["iife-wrap"] }
error TypeError: this.cache is not a function ↓
cause Potential compatibility issue with older Babel versions (likely <7).
fix
Ensure @babel/core version 7+ is installed as peer dependency.
Warnings
gotcha Plugin does not add semicolon before IIFE; output may break if previous file lacks semicolon. Always ensure semicolons or use a separate tool. ↓
fix Manually add semicolon or use a plugin like 'babel-plugin-add-import-extension' but note that this plugin does not add prefix ';'.
gotcha Only wraps the entire file; does not handle modules or imports. If used with ES modules, output may be invalid JavaScript. ↓
fix Do not use with ES module syntax (import/export). For modules, consider different scoping techniques.
gotcha No configuration options—cannot customize IIFE parameters or use strict mode. The plugin always wraps in an anonymous function with no arguments. ↓
fix No workaround; choose another plugin if customization is needed.
Install
npm install babel-plugin-iife-wrap yarn add babel-plugin-iife-wrap pnpm add babel-plugin-iife-wrap Imports
- default wrong
import iifeWrap from 'babel-plugin-iife-wrap';correctmodule.exports = require('babel-plugin-iife-wrap'); - babel.config.js wrong
module.exports = { plugins: ['babel-plugin-iife-wrap'] };correctmodule.exports = { plugins: ['iife-wrap'] }; - Babel CLI wrong
babel --plugins babel-plugin-iife-wrap script.jscorrectbabel --plugins iife-wrap script.js
Quickstart
// Install: npm i babel-plugin-iife-wrap --save-dev
// Then add to babel.config.js:
module.exports = {
plugins: ['iife-wrap']
};
// Input (input.js):
var x = 1;
window.a = x;
// Output:
;(function () {
var x = 1;
window.a = x;
}());