babel-plugin-globals
Advanced tools
Comparing version
26
index.js
@@ -60,3 +60,7 @@ 'use strict'; | ||
nodes.push(t.expressionStatement( | ||
t.assignmentExpression('=', id, t.objectExpression([])) | ||
t.assignmentExpression('=', id, t.logicalExpression( | ||
'||', | ||
id, | ||
t.objectExpression([]) | ||
)) | ||
)); | ||
@@ -91,13 +95,19 @@ } | ||
function getGlobalIdentifier(state, filePath, name, opt_isWildcard) { | ||
assertFilenameRequired(state.file.opts.filename); | ||
var globalName = state.opts.globalName; | ||
if (name || opt_isWildcard) { | ||
globalName += 'Named'; | ||
var id; | ||
if (typeof globalName === 'function') { | ||
id = globalName(state, filePath, name, opt_isWildcard); | ||
} | ||
else { | ||
if (name || opt_isWildcard) { | ||
globalName += 'Named'; | ||
} | ||
assertFilenameRequired(state.file.opts.filename); | ||
filePath = path.resolve(path.dirname(state.file.opts.filename), filePath); | ||
var splitPath = filePath.split(path.sep); | ||
var moduleName = splitPath[splitPath.length - 1]; | ||
filePath = path.resolve(path.dirname(state.file.opts.filename), filePath); | ||
var splitPath = filePath.split(path.sep); | ||
var moduleName = splitPath[splitPath.length - 1]; | ||
var id = 'this.' + globalName + '.' + moduleName + (name ? '.' + name : ''); | ||
id = 'this.' + globalName + '.' + moduleName + (name ? '.' + name : ''); | ||
} | ||
@@ -104,0 +114,0 @@ return t.identifier(id); |
{ | ||
"name": "babel-plugin-globals", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A babel plugin that exposes ES6 modules to global variables.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -25,5 +25,16 @@ babel-plugin-globals | ||
### Plugin options | ||
- `globalName` **{string}** The name of the global variable that the modules should be exported to. | ||
#### `globalName` **{string|!function()}** | ||
The name of the global variable that the modules should be exported to. | ||
Default exports will be exported as `<globalName>.<filename>` whilst named exports will be exported as | ||
`<globalName>Named.<fileName>.<exportName>`. | ||
`globalName` can also receive a function that returns the whole variable path for each export (e.g. `(state, filePath, name, isWildcard) => 'this.MyModule.Views' + (name ? '.' + name : '')`) | ||
### Babel options | ||
- `filename` **{string}** This is an optional existing babel option, but is required for this plugin, since the plugin uses the file name to decide the name of the keys that will be exported in the global variable. | ||
#### `filename` **{string}** | ||
This is an optional existing babel option, but is required for this plugin, since the plugin uses the file name to decide the name of the keys that will be exported in the global variable. |
10407
6.06%204
5.15%40
37.93%