Skip to main content

IIFE filter for webassets

Project description

Build status Coverage status Pypi package https://img.shields.io/pypi/dm/webassets-iife.png

webassets-iife is a webassets filter to wrap a JavaScript bundle in an IIFE to prevent global leaks and improve minification.

Install

pip install webassets-iife

Usage

For example with Flask:

from flask.ext.assets import Environment, Bundle
from webassets_iife import IIFE

# ...

assets = Environment(app)

js = Bundle('myscript1.js',
            'myscript2.js',
            filters=(IIFE, 'closure_js'), output='all.min.js')
assets.register('js_all', js)

This will concat myscript1.js and myscript2.js into one JS chunk, wrap it in an IIFE and minify it.

IIFE?

An IIFE is an Immediately-Invoked Function Expression. It’s an anonymous function that’s declared and invoked immediately after that. It’s used in JavaScript to create a closed environment which can’t be accessed from the outside.

// a and b can be accessed by external code
var a = 3,
    b = 1;
// ... some code ...

// a and b can't be accessed by external code
(function() {
    var a = 3,
        b = 1;
    // ... some code ...
})();

Wrapping code in an IIFE helps the minifier see the dead code, because it knows that these local variables can’t be accessed from the outside and thus can remove them or mangled their name.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webassets-iife-0.1.0.tar.gz (3.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page