babel-plugin-fake-import-specifiers
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "babel-plugin-fake-import-specifiers", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Allow importing pieces while only exporting the whole object", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,0 +0,0 @@ # babel-plugin-fake-import-specifiers |
'use strict'; | ||
function convertDasherizedToCamelized(str) { | ||
return str.split('-').reduce(function(str, chunk) { | ||
if (str === '') { | ||
str = chunk; | ||
} else { | ||
str += chunk.substr(0, 1).toUpperCase() + chunk.substr(1); | ||
} | ||
return str; | ||
}, ''); | ||
} | ||
module.exports = function(babel) { | ||
@@ -9,7 +20,19 @@ var Plugin = babel.Plugin; | ||
var defaultSpecifierLookup; | ||
var newIdentifierLookup; | ||
var visitor = { | ||
ImportDeclaration: function(node, parent, scope, file) { | ||
if (sourcesToFake.indexOf(node.source.value) === -1) { | ||
return; | ||
let value = node.source.value; | ||
let sourceMinusOne; | ||
let dasherizedPackageName; | ||
if (sourcesToFake.indexOf(value) === -1) { | ||
// try to match a partial import | ||
// ex: source = 'my-lib' | ||
// import aPackage from 'my-lib/a-package'; | ||
let lastSlashIndex = value.lastIndexOf('/'); | ||
sourceMinusOne = value.substr(0, lastSlashIndex); | ||
if (sourcesToFake.indexOf(sourceMinusOne) === -1) { | ||
return; | ||
} | ||
dasherizedPackageName = value.substr(lastSlashIndex + 1); | ||
} | ||
@@ -30,22 +53,46 @@ | ||
defaultSpecifier = specifier; | ||
break; | ||
} else if (dasherizedPackageName) { | ||
// ignore partial match imports with non default specifiers | ||
// ex: source = 'my-lib' | ||
// import { anotherPackage } from 'my-lib/a-package'; | ||
return; | ||
} | ||
} | ||
function generateNewDefaultSpecifier() { | ||
defaultSpecifierIndentifier = scope.generateUidIdentifier(); | ||
defaultSpecifier = t.importDefaultSpecifier(defaultSpecifierIndentifier); | ||
} | ||
var hasSpecifiers; | ||
specifiers.forEach(function(specifier) { | ||
if (!t.isImportSpecifier(specifier)) { | ||
return; | ||
} | ||
function addSpecifierToLookup(name) { | ||
defaultSpecifierLookup[name] = defaultSpecifierIndentifier; | ||
hasSpecifiers = true; | ||
} | ||
if (!defaultSpecifierIndentifier) { | ||
defaultSpecifierIndentifier = scope.generateUidIdentifier(); | ||
defaultSpecifier = t.importDefaultSpecifier(defaultSpecifierIndentifier); | ||
} | ||
if (dasherizedPackageName) { | ||
node.source.value = sourceMinusOne; | ||
defaultSpecifierLookup[specifier.local.name] = defaultSpecifierIndentifier; | ||
hasSpecifiers = true; | ||
}); | ||
let oldName = defaultSpecifierIndentifier.name; | ||
let name = convertDasherizedToCamelized(dasherizedPackageName); | ||
newIdentifierLookup[oldName] = name; | ||
generateNewDefaultSpecifier(); | ||
addSpecifierToLookup(oldName); | ||
} else { | ||
specifiers.forEach(function(specifier) { | ||
if (!t.isImportSpecifier(specifier)) { | ||
return; | ||
} | ||
if (!defaultSpecifierIndentifier) { | ||
generateNewDefaultSpecifier(); | ||
} | ||
addSpecifierToLookup(specifier.local.name); | ||
}); | ||
} | ||
if (hasSpecifiers) { | ||
@@ -67,2 +114,7 @@ node.specifiers = [defaultSpecifier]; | ||
var newIdentifier = newIdentifierLookup[name]; | ||
if (newIdentifier) { | ||
name = newIdentifier; | ||
} | ||
return this.replaceWith( | ||
@@ -81,2 +133,3 @@ t.callExpression( | ||
defaultSpecifierLookup = {}; | ||
newIdentifierLookup = {}; | ||
} | ||
@@ -83,0 +136,0 @@ }; |
Sorry, the diff of this file is not supported yet
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
6350
116