babel-plugin-transform-imports
Advanced tools
Comparing version 1.2.0 to 1.3.0
17
index.js
var types = require('babel-types'); | ||
var camel = require('lodash.camelcase'); | ||
var kebab = require('lodash.kebabcase'); | ||
var snake = require('lodash.snakecase'); | ||
@@ -9,7 +11,8 @@ function barf(msg) { | ||
function transform(transformOption, importName) { | ||
if (/\.js$/i.test(transformOption)) { | ||
var isFunction = typeof transformOption === 'function'; | ||
if (/\.js$/i.test(transformOption) || isFunction) { | ||
var transformFn; | ||
try { | ||
transformFn = require(transformOption); | ||
transformFn = isFunction ? transformOption : require(transformOption); | ||
} catch (error) { | ||
@@ -80,10 +83,18 @@ barf('failed to require transform file ' + transformOption); | ||
// import gird from 'react-bootstrap/lib/Grid'; | ||
// or this, if skipDefaultConversion = true: | ||
// import { Grid as gird } from 'react-bootstrap/lib/Grid'; | ||
var importName = memberImport.imported.name; | ||
if (opts.camelCase) importName = camel(importName); | ||
if (opts.kebabCase) importName = kebab(importName); | ||
if (opts.snakeCase) importName = snake(importName); | ||
var replace = transform(opts.transform, importName); | ||
var newImportSpecifier = (opts.skipDefaultConversion) | ||
? memberImport | ||
: types.importDefaultSpecifier(types.identifier(memberImport.local.name)); | ||
transforms.push(types.importDeclaration( | ||
[types.importDefaultSpecifier(types.identifier(memberImport.local.name))], | ||
[newImportSpecifier], | ||
types.stringLiteral(replace) | ||
@@ -90,0 +101,0 @@ )); |
{ | ||
"name": "babel-plugin-transform-imports", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x')", | ||
@@ -25,3 +25,5 @@ "keywords": [ | ||
"babel-types": "^6.6.0", | ||
"lodash.kebabcase": "^4.0.1" | ||
"lodash.camelcase": "^4.3.0", | ||
"lodash.kebabcase": "^4.1.1", | ||
"lodash.snakecase": "^4.1.1" | ||
}, | ||
@@ -28,0 +30,0 @@ "devDependencies": { |
@@ -123,2 +123,33 @@ # babel-plugin-transform-imports | ||
## Webpack | ||
This can be used as a plugin with babel-loader. | ||
webpack.config.js: | ||
```js | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /(node_modules|bower_components)/, | ||
use: { | ||
loader: 'babel-loader', | ||
query: { | ||
plugins: [ | ||
[require('babel-plugin-transform-imports'), { | ||
"my-library": { | ||
"transform": function(importName) { | ||
return 'my-library/etc/' + importName.toUpperCase(); | ||
}, | ||
preventFullImport: true | ||
} | ||
}] | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
## Options | ||
@@ -130,2 +161,5 @@ | ||
| `preventFullImport` | `boolean` | no | `false` | Whether or not to throw when an import is encountered which would cause the entire module to be imported. | | ||
| `kebabCase` | `boolean` | no | `false` | When set to true, runs ${member} through _.kebabCase. | | ||
| `camelCase` | `boolean` | no | `false` | When set to true, runs ${member} through _.camelCase. | | ||
| `kebabCase` | `boolean` | no | `false` | When set to true, runs ${member} through _.kebabCase. | | ||
| `snakeCase` | `boolean` | no | `false` | When set to true, runs ${member} through _.snakeCase. | | ||
| `skipDefaultConversion` | `boolean` | no | `false` | When set to true, will preserve `import { X }` syntax instead of converting to `import X`. | |
import assert from 'assert'; | ||
import * as babel from 'babel-core'; | ||
function createOptions({ preventFullImport = false, transform = 'react-bootstrap/lib/${member}', kebabCase = false }) { | ||
function createOptions({ | ||
preventFullImport = false, | ||
transform = 'react-bootstrap/lib/${member}', | ||
camelCase = false, | ||
kebabCase = false, | ||
snakeCase = false, | ||
skipDefaultConversion = false | ||
}) { | ||
return { | ||
'react-bootstrap': { transform, preventFullImport, kebabCase } | ||
'react-bootstrap': { transform, preventFullImport, camelCase, kebabCase, snakeCase, skipDefaultConversion } | ||
}; | ||
@@ -13,3 +20,3 @@ }; | ||
function occurances(regex, test) { | ||
function occurrences(regex, test) { | ||
return (test.match(regex) || []).length; | ||
@@ -29,4 +36,4 @@ } | ||
assert.equal(occurances(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurances(memberImportRegex, code), 0, 'number of member imports should be 0'); | ||
assert.equal(occurrences(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurrences(memberImportRegex, code), 0, 'number of member imports should be 0'); | ||
}); | ||
@@ -37,4 +44,4 @@ | ||
assert.equal(occurances(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurances(memberImportRegex, code), 0, 'number of member imports should be 0'); | ||
assert.equal(occurrences(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurrences(memberImportRegex, code), 0, 'number of member imports should be 0'); | ||
}); | ||
@@ -45,4 +52,4 @@ | ||
assert.equal(occurances(fullImportRegex, code), 0, 'number of full imports should be 0'); | ||
assert.equal(occurances(memberImportRegex, code), 2, 'number of member imports should be 2'); | ||
assert.equal(occurrences(fullImportRegex, code), 0, 'number of full imports should be 0'); | ||
assert.equal(occurrences(memberImportRegex, code), 2, 'number of member imports should be 2'); | ||
}); | ||
@@ -53,7 +60,17 @@ | ||
assert.equal(occurances(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurances(memberImportRegex, code), 2, 'number of member imports should be 2'); | ||
assert.equal(occurrences(fullImportRegex, code), 1, 'number of full imports should be 1'); | ||
assert.equal(occurrences(memberImportRegex, code), 2, 'number of member imports should be 2'); | ||
}); | ||
}); | ||
describe('camelCase plugin option', function() { | ||
it('should use camel casing when set', function() { | ||
let options = createOptions({ camelCase: true }); | ||
let code = transform(`import { CamelMe } from 'react-bootstrap';`, options); | ||
assert.notEqual(code.indexOf('camelMe'), -1, 'member name CamelMe should be transformed to camelMe'); | ||
}); | ||
}); | ||
describe('kebabCase plugin option', function() { | ||
@@ -69,2 +86,12 @@ it('should use kebab casing when set', function() { | ||
describe('snakeCase plugin option', function() { | ||
it('should use snake casing when set', function() { | ||
let options = createOptions({ snakeCase: true }); | ||
let code = transform(`import { SnakeMe } from 'react-bootstrap';`, options); | ||
assert.notEqual(code.indexOf('snake_me'), -1, 'member name SnakeMe should be transformed to snake_me'); | ||
}); | ||
}); | ||
describe('transform as function', function() { | ||
@@ -90,2 +117,10 @@ it('should throw when provided filename is invalid', function() { | ||
}); | ||
it('should call the transform as a function when provided as so', function() { | ||
let options = createOptions({ transform: function(input) { return `path/${input}`; } }); | ||
let code = transform(`import { somePath } from 'react-bootstrap';`, options); | ||
assert.notEqual(code.indexOf('path/somePath'), -1, 'function should transform somePath to path/somePath'); | ||
}); | ||
}); | ||
@@ -96,3 +131,3 @@ | ||
let options = createOptions({ preventFullImport: true }); | ||
assert.throws(() => {transform(`import Bootstrap from 'react-bootstrap';`, options)}); | ||
@@ -103,3 +138,3 @@ }); | ||
let options = createOptions({ preventFullImport: true }); | ||
assert.throws(() => {transform(`import * as Bootstrap from 'react-bootstrap';`, options)}); | ||
@@ -110,3 +145,3 @@ }); | ||
let options = createOptions({ preventFullImport: true }); | ||
assert.doesNotThrow(() => {transform(`import { Grid, Row as row } from 'react-bootstrap';`, options)}); | ||
@@ -116,2 +151,12 @@ }); | ||
describe('skipDefaultConversion plugin option', function() { | ||
it('should retain named import syntax when enabled', function() { | ||
let options = createOptions({ skipDefaultConversion: true }); | ||
let code = transform(`import { Grid, Row as row } from 'react-bootstrap';`, options); | ||
assert.equal(code.indexOf('_interopRequireDefault'), -1, 'skipDefaultConversion should not allow conversion to default import'); | ||
}) | ||
}); | ||
describe('edge cases', function() { | ||
@@ -123,2 +168,2 @@ it('should throw when transform plugin option is missing', function() { | ||
}); | ||
}); | ||
}); |
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
18028
204
164
4
+ Addedlodash.camelcase@^4.3.0
+ Addedlodash.snakecase@^4.1.1
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedlodash.snakecase@4.1.1(transitive)
Updatedlodash.kebabcase@^4.1.1