Giter Site home page Giter Site logo

babel-plugin-extend-scope-chain's Introduction

babel-plugin-extend-scope-chain

NPM version Travis build status js-canonical-style

Namespaces global variable declarations.

A lot like using with statement to add window expression to the scope chain, e.g. with (window) { /* your script */ }.

Example transpilations

global foo assignment expression

Input:

foo = "bar";

Output:

window.foo = "bar";

global foo.bar assignment expression

Input:

foo.bar = "bar";

Output:

window.foo.bar = "bar";

assignment in program scope

Input:

var foo = "bar";

Output:

window.foo = "bar";

Motivation

To bundle external scripts.

A specific use case for which this was developed is to bundle external supply-side platform (SSP) scripts into the main script. This enables us to decrease the amount of HTTP requests that are required to start header bidding.

The problem is that all vendor scripts assume that the script is loaded asynchronously, using script tags, e.g.

const scriptElement = document.createElement('script');
scriptElement.async = true;
scriptElement.src = '//script.js';
document.head.appendChild(scriptElement);

This assumption allows them to write code such as:

var foo = foo || 'bar';

In the above example, if foo is not set, window.foo becomes {}.

We want to bundle and delay these script execution, i.e.

function loadVendorFoo () {
  var foo = foo || 'bar';
}

The above code breaks, because now foo is isolated to loadVendorFoo scope, i.e. in the above code foo will always equal "bar".

Using this transpiler, we namespace all global variable declarations using window object, i.e. our script becomes:

function loadVendorFoo () {
  var foo = window.foo = window.foo || 'bar';
}

Configuration

Name Type Description Default
globals Array<string> Names of global variables that must not be namespaced. ['window']
export boolean Wraps the script body in a function and exports the function using module.exports. false

babel-plugin-extend-scope-chain's People

Contributors

gajus avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.