babel-plugin-globals
Advanced tools
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" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9780
194
1
+ Addedbabel-code-frame@6.26.0(transitive)
+ Addedbabel-core@6.26.3(transitive)
+ Addedbabel-generator@6.26.1(transitive)
+ Addedbabel-helpers@6.24.1(transitive)
+ Addedbabel-messages@6.23.0(transitive)
+ Addedbabel-register@6.26.0(transitive)
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedbabel-template@6.26.0(transitive)
+ Addedbabel-traverse@6.26.0(transitive)
+ Addedbabel-types@6.26.0(transitive)
+ Addedbabylon@6.18.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addeddetect-indent@4.0.0(transitive)
+ Addedglobals@9.18.0(transitive)
+ Addedhome-or-tmp@2.0.0(transitive)
+ Addedinvariant@2.2.4(transitive)
+ Addedjs-tokens@3.0.2(transitive)
+ Addedjsesc@1.3.0(transitive)
+ Addedjson5@0.5.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)
+ Addedrepeating@2.0.1(transitive)
+ Addedsource-map-support@0.4.18(transitive)
- Removedacorn@5.7.4(transitive)
- Removedalign-text@0.1.4(transitive)
- Removedalter@0.2.0(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedast-traverse@0.1.1(transitive)
- Removedast-types@0.8.120.9.6(transitive)
- Removedbabel-core@5.8.38(transitive)
- Removedbabel-plugin-constant-folding@1.0.1(transitive)
- Removedbabel-plugin-dead-code-elimination@1.0.2(transitive)
- Removedbabel-plugin-eval@1.0.1(transitive)
- Removedbabel-plugin-inline-environment-variables@1.0.1(transitive)
- Removedbabel-plugin-jscript@1.0.4(transitive)
- Removedbabel-plugin-member-expression-literals@1.0.1(transitive)
- Removedbabel-plugin-property-literals@1.0.1(transitive)
- Removedbabel-plugin-proto-to-assign@1.0.4(transitive)
- Removedbabel-plugin-react-constant-elements@1.0.3(transitive)
- Removedbabel-plugin-react-display-name@1.0.3(transitive)
- Removedbabel-plugin-remove-console@1.0.1(transitive)
- Removedbabel-plugin-remove-debugger@1.0.1(transitive)
- Removedbabel-plugin-runtime@1.0.7(transitive)
- Removedbabel-plugin-undeclared-variables-check@1.0.2(transitive)
- Removedbabel-plugin-undefined-to-void@1.1.6(transitive)
- Removedbabylon@5.8.38(transitive)
- Removedbluebird@2.11.0(transitive)
- Removedbreakable@1.0.0(transitive)
- Removedcamelcase@1.2.1(transitive)
- Removedcenter-align@0.1.3(transitive)
- Removedcliui@2.1.0(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcommoner@0.10.8(transitive)
- Removedcore-js@1.2.7(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddefined@1.0.1(transitive)
- Removeddefs@1.1.1(transitive)
- Removeddetect-indent@3.0.1(transitive)
- Removeddetective@4.7.1(transitive)
- Removedesprima@2.7.33.1.3(transitive)
- Removedesprima-fb@15001.1001.0-dev-harmony-fb(transitive)
- Removedfs-readdir-recursive@0.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedglob@5.0.15(transitive)
- Removedglobals@6.4.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhome-or-tmp@1.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinvert-kv@1.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-core-module@2.16.1(transitive)
- Removedis-integer@1.0.7(transitive)
- Removedjs-tokens@1.0.1(transitive)
- Removedjsesc@0.5.0(transitive)
- Removedjson5@0.4.0(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.4(transitive)
- Removedlcid@1.0.0(transitive)
- Removedleven@1.0.2(transitive)
- Removedlodash@3.10.1(transitive)
- Removedlongest@1.0.1(transitive)
- Removedminimatch@2.0.10(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-locale@1.4.0(transitive)
- Removedoutput-file-sync@1.1.2(transitive)
- Removedpath-exists@1.0.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedq@1.5.1(transitive)
- Removedrecast@0.10.330.11.23(transitive)
- Removedregenerate@1.4.2(transitive)
- Removedregenerator@0.8.40(transitive)
- Removedregexpu@1.3.0(transitive)
- Removedregjsgen@0.2.0(transitive)
- Removedregjsparser@0.1.5(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedrepeating@1.1.3(transitive)
- Removedresolve@1.22.10(transitive)
- Removedright-align@0.1.3(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedsimple-fmt@0.1.0(transitive)
- Removedsimple-is@0.2.0(transitive)
- Removedsource-map@0.1.32(transitive)
- Removedsource-map-support@0.2.10(transitive)
- Removedstable@0.1.8(transitive)
- Removedstringmap@0.2.2(transitive)
- Removedstringset@0.2.1(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedthrough@2.3.8(transitive)
- Removedtry-resolve@1.0.1(transitive)
- Removedtryor@0.1.2(transitive)
- Removeduser-home@1.1.1(transitive)
- Removedwindow-size@0.1.4(transitive)
- Removedwordwrap@0.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedy18n@3.2.2(transitive)
- Removedyargs@3.27.0(transitive)
Updatedbabel-core@^6.1.2