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 0.1.3 to 1.0.0

185

index.js

@@ -22,9 +22,9 @@ 'use strict';

* Assigns the given declaration to the appropriate global variable.
* @param {!Object} options
* @param {!Object} state
* @param {!Array} nodes
* @param {!Declaration} declaration
*/
function assignDeclarationToGlobal(options, nodes, declaration) {
var filenameNoExt = getFilenameNoExt(options.filename);
var id = getGlobalIdentifier(options, filenameNoExt, declaration.id.name);
function assignDeclarationToGlobal(state, nodes, declaration) {
var filenameNoExt = getFilenameNoExt(state.file.opts.filename);
var id = getGlobalIdentifier(state, filenameNoExt, declaration.id.name);
assignToGlobal(id, nodes, declaration.id);

@@ -83,3 +83,3 @@ }

* Gets the global identifier for the given information.
* @param {!Object} options Options object passed to babel.
* @param {!Object} state This plugin's current state object.
* @param {string} filePath The path of the module.

@@ -91,4 +91,4 @@ * @param {?string} name The name of the variable being imported or exported from

*/
function getGlobalIdentifier(options, filePath, name, opt_isWildcard) {
var globalName = options._globalName;
function getGlobalIdentifier(state, filePath, name, opt_isWildcard) {
var globalName = state.opts.globalName;
if (name || opt_isWildcard) {

@@ -98,4 +98,4 @@ globalName += 'Named';

assertFilenameRequired(options.filename);
filePath = path.resolve(path.dirname(options.filename), filePath);
assertFilenameRequired(state.file.opts.filename);
filePath = path.resolve(path.dirname(state.file.opts.filename), filePath);
var splitPath = filePath.split(path.sep);

@@ -123,93 +123,98 @@ var moduleName = splitPath[splitPath.length - 1];

return new babel.Transformer('globals', {
/**
* Wraps the program body in a closure, protecting local variables.
* @param {Program} node
*/
Program: function(node) {
createdGlobals = {};
filenameNoExtCache = null;
return {
visitor: {
/**
* Wraps the program body in a closure, protecting local variables.
* @param {!NodePath} nodePath
*/
Program: function(nodePath) {
createdGlobals = {};
filenameNoExtCache = null;
var contents = node.body;
node.body = [t.expressionStatement(t.callExpression(
t.memberExpression(
t.functionExpression(null, [], t.blockStatement(contents)),
t.identifier('call'),
false
),
[t.identifier('this')]
))];
return node;
},
var node = nodePath.node;
var contents = node.body;
node.body = [t.expressionStatement(t.callExpression(
t.memberExpression(
t.functionExpression(null, [], t.blockStatement(contents)),
t.identifier('call'),
false
),
[t.identifier('this')]
))];
},
/**
* Replaces import declarations with assignments from global to local variables.
* @param {ImportDeclaration} node
*/
ImportDeclaration: function(node) {
var self = this;
var replacements = [];
node.specifiers.forEach(function(specifier) {
var id = getGlobalIdentifier(
self.state.opts,
removeExtensions(node.source.value),
specifier.imported ? specifier.imported.name : null,
t.isImportNamespaceSpecifier(specifier)
);
replacements.push(t.variableDeclaration('var', [
t.variableDeclarator(specifier.local, id)
]));
});
return replacements;
},
/**
* Replaces import declarations with assignments from global to local variables.
* @param {!NodePath} nodePath
* @param {!Object} state
*/
ImportDeclaration: function(nodePath, state) {
var replacements = [];
nodePath.node.specifiers.forEach(function(specifier) {
var id = getGlobalIdentifier(
state,
removeExtensions(nodePath.node.source.value),
specifier.imported ? specifier.imported.name : null,
t.isImportNamespaceSpecifier(specifier)
);
replacements.push(t.variableDeclaration('var', [
t.variableDeclarator(specifier.local, id)
]));
});
nodePath.replaceWithMultiple(replacements);
},
/**
* Removes export all declarations.
*/
ExportAllDeclaration: function() {
return [];
},
/**
* Removes export all declarations.
* @param {!NodePath} nodePath
*/
ExportAllDeclaration: function(nodePath) {
nodePath.replaceWithMultiple([]);
},
/**
* Replaces default export declarations with assignments to global variables.
* @param {ExportDefaultDeclaration} node
*/
ExportDefaultDeclaration: function(node) {
var replacements = [];
var id = getGlobalIdentifier(this.state.opts, getFilenameNoExt(this.state.opts.filename));
assignToGlobal(id, replacements, node.declaration);
return replacements;
},
/**
* Replaces default export declarations with assignments to global variables.
* @param {!NodePath} nodePath
* @param {!Object} state
*/
ExportDefaultDeclaration: function(nodePath, state) {
var replacements = [];
var id = getGlobalIdentifier(state, getFilenameNoExt(state.file.opts.filename));
assignToGlobal(id, replacements, nodePath.node.declaration);
nodePath.replaceWithMultiple(replacements);
},
/**
* Replaces named export declarations with assignments to global variables.
* @param {ExportNamedDeclaration} node
*/
ExportNamedDeclaration: function(node) {
var replacements = [];
if (node.declaration) {
replacements.push(node.declaration);
if (t.isVariableDeclaration(node.declaration)) {
node.declaration.declarations.forEach(assignDeclarationToGlobal.bind(null, this.state.opts, replacements));
/**
* Replaces named export declarations with assignments to global variables.
* @param {!NodePath} nodePath
* @param {!Object} state
*/
ExportNamedDeclaration: function(nodePath, state) {
var replacements = [];
var node = nodePath.node;
if (node.declaration) {
replacements.push(node.declaration);
if (t.isVariableDeclaration(node.declaration)) {
node.declaration.declarations.forEach(assignDeclarationToGlobal.bind(null, state, replacements));
} else {
assignDeclarationToGlobal(state, replacements, node.declaration);
}
} else {
assignDeclarationToGlobal(this.state.opts, replacements, node.declaration);
node.specifiers.forEach(function(specifier) {
var idToAssign = specifier.exported;
if (node.source) {
var specifierName = specifier.local ? specifier.local.name : null;
idToAssign = getGlobalIdentifier(state, node.source.value, specifierName);
}
var filenameNoExt = getFilenameNoExt(state.file.opts.filename);
var id = getGlobalIdentifier(state, filenameNoExt, specifier.exported.name);
assignToGlobal(id, replacements, idToAssign);
});
}
} else {
var self = this;
node.specifiers.forEach(function(specifier) {
var idToAssign = specifier.exported;
if (node.source) {
var specifierName = specifier.local ? specifier.local.name : null;
idToAssign = getGlobalIdentifier(self.state.opts, node.source.value, specifierName);
}
var filenameNoExt = getFilenameNoExt(self.state.opts.filename);
var id = getGlobalIdentifier(self.state.opts, filenameNoExt, specifier.exported.name);
assignToGlobal(id, replacements, idToAssign);
});
nodePath.replaceWithMultiple(replacements);
}
return replacements;
}
});
};
};
{
"name": "babel-plugin-globals",
"version": "0.1.3",
"version": "1.0.0",
"description": "A babel plugin that exposes ES6 modules to global variables.",

@@ -25,12 +25,12 @@ "license": "MIT",

"babel",
"babel-plugin",
"es6",
"globals",
"module",
"plugin"
"module"
],
"dependencies": {
"babel-core": "^5.4.7"
"babel-core": "^6.1.2"
},
"devDependencies": {
"istanbul": "^0.3.14",
"istanbul": "^0.4.0",
"jshint": "^2.7.0",

@@ -37,0 +37,0 @@ "nodeunit": "^0.9.1"

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