Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-extend-scope-chain

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-extend-scope-chain

[![NPM version](http://img.shields.io/npm/v/babel-plugin-extend-scope-chain.svg?style=flat-square)](https://www.npmjs.org/package/babel-plugin-extend-scope-chain) [![Travis build status](http://img.shields.io/travis/gajus/babel-plugin-extend-scope-chain/m

  • 2.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

NameTypeDescriptionDefault
globalsArray<string>Names of global variables that must not be namespaced.['window']
exportbooleanWraps the script body in a function and exports the function using module.exports.false

Keywords

FAQs

Package last updated on 14 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc