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

babel-plugin-globals

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-globals - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

67

index.js

@@ -28,4 +28,4 @@ 'use strict';

var filenameNoExt = getFilenameNoExt(state.file.opts.filename);
var id = getGlobalIdentifier(state, filenameNoExt, declaration.id.name);
assignToGlobal(id, nodes, declaration.id);
var expr = getGlobalExpression(state, filenameNoExt, declaration.id.name);
assignToGlobal(expr, nodes, declaration.id);
}

@@ -35,12 +35,12 @@

* Assigns the given expression to a global with the given id.
* @param {string} id
* @param {!MemberExpression} expr
* @param {!Array} nodes
* @param {!Expression} expression
*/
function assignToGlobal(id, nodes, expression) {
createGlobal(id.name, nodes);
function assignToGlobal(expr, nodes, expression) {
createGlobal(expr, nodes);
if (!t.isExpression(expression)) {
expression = t.toExpression(expression);
}
nodes.push(t.expressionStatement(t.assignmentExpression('=', id, expression)));
nodes.push(t.expressionStatement(t.assignmentExpression('=', expr, expression)));
}

@@ -50,20 +50,21 @@

* Creates the global for the given name, if it hasn't been created yet.
* @param {string} name Name of the global to create.
* @param {!MemberExpression} expr
* @param {!Array} nodes Array to add the global creation assignments to.
*/
function createGlobal(name, nodes) {
var keys = name.split('.');
var currentGlobal = createdGlobals;
var currentGlobalName = 'this.' + keys[1];
var id;
for (var i = 2; i < keys.length - 1; i++) {
currentGlobalName += '.' + keys[i];
id = t.identifier(currentGlobalName);
function createGlobal(expr, nodes) {
var exprs = [];
while (t.isMemberExpression(expr)) {
exprs.push(expr);
expr = expr.object;
}
if (!currentGlobal[keys[i]]) {
currentGlobal[keys[i]] = {};
var currGlobalName = '';
for (var i = exprs.length - 2; i > 0; i--) {
currGlobalName += '.' + exprs[i].property.value;
if (!createdGlobals[currGlobalName]) {
createdGlobals[currGlobalName] = true;
nodes.push(t.expressionStatement(
t.assignmentExpression('=', id, t.logicalExpression(
t.assignmentExpression('=', exprs[i], t.logicalExpression(
'||',
id,
exprs[i],
t.objectExpression([])

@@ -73,3 +74,2 @@ ))

}
currentGlobal = currentGlobal[keys[i]];
}

@@ -98,5 +98,5 @@ }

* @param {boolean=} opt_isWildcard If the import or export declaration is using a wildcard.
* @return {!Specifier}
* @return {!MemberExpression}
*/
function getGlobalIdentifier(state, filePath, name, opt_isWildcard) {
function getGlobalExpression(state, filePath, name, opt_isWildcard) {
assertFilenameRequired(state.file.opts.filename);

@@ -120,3 +120,8 @@ var globalName = state.opts.globalName;

return t.identifier(id);
var parts = id.split('.');
var expr = t.identifier(parts[0]);
for (var i = 1; i < parts.length; i++) {
expr = t.memberExpression(expr, t.stringLiteral(parts[i]), true);
}
return expr;
}

@@ -168,3 +173,3 @@

nodePath.node.specifiers.forEach(function(specifier) {
var id = getGlobalIdentifier(
var expr = getGlobalExpression(
state,

@@ -176,3 +181,3 @@ removeExtensions(nodePath.node.source.value),

replacements.push(t.variableDeclaration('var', [
t.variableDeclarator(specifier.local, id)
t.variableDeclarator(specifier.local, expr)
]));

@@ -198,3 +203,3 @@ });

var replacements = [];
var id = getGlobalIdentifier(state, getFilenameNoExt(state.file.opts.filename));
var expr = getGlobalExpression(state, getFilenameNoExt(state.file.opts.filename));
var expression = nodePath.node.declaration;

@@ -206,3 +211,3 @@ if (expression.id &&

}
assignToGlobal(id, replacements, expression);
assignToGlobal(expr, replacements, expression);
nodePath.replaceWithMultiple(replacements);

@@ -228,11 +233,11 @@ },

node.specifiers.forEach(function(specifier) {
var idToAssign = specifier.exported;
var exprToAssign = specifier.local;
if (node.source) {
var specifierName = specifier.local ? specifier.local.name : null;
idToAssign = getGlobalIdentifier(state, node.source.value, specifierName);
exprToAssign = getGlobalExpression(state, node.source.value, specifierName);
}
var filenameNoExt = getFilenameNoExt(state.file.opts.filename);
var id = getGlobalIdentifier(state, filenameNoExt, specifier.exported.name);
assignToGlobal(id, replacements, idToAssign);
var expr = getGlobalExpression(state, filenameNoExt, specifier.exported.name);
assignToGlobal(expr, replacements, exprToAssign);
});

@@ -239,0 +244,0 @@ }

{
"name": "babel-plugin-globals",
"version": "1.1.1",
"version": "2.0.0",
"description": "A babel plugin that exposes ES6 modules to global variables.",

@@ -5,0 +5,0 @@ "license": "MIT",

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