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 details)

Uploaded Source

File details

Details for the file webassets-iife-0.1.0.tar.gz.

File metadata

File hashes

Hashes for webassets-iife-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac9769ecb303e4b2364b2b1da3a300d1118012dc8faeb54f9f6a63b3029e2771
MD5 c6e86447e76df42e201af31f05b4c198
BLAKE2b-256 d78a4da381a1c8dc81d0a23751d6e72cbc47dbee8745fba465717404dd5a6ae9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page