Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-imports

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-imports - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

test/invalidTransform.js

36

index.js
var types = require('babel-types');
var kebab = require('lodash.kebabcase');
var pluginName = 'babel-plugin-transform-imports';
function barf(msg) {
throw new Error('babel-plugin-transform-imports: ' + msg);
}
function transform(transformOption, importName) {
if (/\.js$/i.test(transformOption)) {
var transformFn;
try {
transformFn = require(transformOption);
} catch (error) {
barf('failed to require transform file ' + transformOption);
}
if (typeof transformFn !== 'function') {
barf('expected transform function to be exported from ' + transformOption);
}
return transformFn(importName);
}
return transformOption.replace(/\$\{\s?member\s?\}/ig, importName);
}
module.exports = function() {

@@ -21,3 +43,3 @@ return {

if (!opts.transform) {
throw new Error(pluginName + ': transform option is required for module ' + source);
barf('transform option is required for module ' + source);
}

@@ -35,4 +57,5 @@

if (opts.preventFullImport)
throw new Error(pluginName + ': import of entire module ' + source + ' not allowed due to preventFullImport setting');
if (opts.preventFullImport) {
barf('import of entire module ' + source + ' not allowed due to preventFullImport setting');
}

@@ -62,3 +85,3 @@ if (memberImports.length > 0) {

var replace = opts.transform.replace(/\$\{\s?member\s?\}/ig, importName);
var replace = transform(opts.transform, importName);

@@ -71,4 +94,5 @@ transforms.push(types.importDeclaration(

if (transforms.length > 0)
if (transforms.length > 0) {
path.replaceWithMultiple(transforms);
}
}

@@ -75,0 +99,0 @@ }

2

package.json
{
"name": "babel-plugin-transform-imports",
"version": "1.1.0",
"version": "1.2.0",
"description": "Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x')",

@@ -5,0 +5,0 @@ "keywords": [

@@ -18,2 +18,5 @@ # babel-plugin-transform-imports

*Note: this plugin is not restricted to the react-bootstrap and lodash
libraries. You may use it with any library.*
## That's stupid, why would you do that?

@@ -85,9 +88,38 @@

That's it!
## Advanced Transformations
*Note: this plugin is not restricted to the react-bootstrap and lodash
libraries. You may use it with any library, where the options keys
(**react-bootstrap** and **lodash** above) are the actual names of the
libraries.*
In cases where the provided default string replacement transformation is not
sufficient (for example, needing to execute a RegExp on the import name), you
may instead provide a path to a .js file which exports a function to run
instead. Keep in mind that the .js file will be `require`d relative from this
plugin's path, likely located in `/node_modules/babel-plugin-transform-imports`.
You may provide any filename, as long as it ends with `.js`.
.babelrc:
```json
{
"plugins": [
["transform-imports", {
"my-library": {
"transform": "../../path/to/transform.js",
"preventFullImport": true
}
}]
]
}
```
/path/to/transform.js:
```js
module.exports = function(importName) {
return 'my-library/etc/' + importName.toUpperCase();
};
```
This is a little bit hacky, but options are a bit limited due to .babelrc being
a JSON5 file which does not support functions as a type. In Babel 7.0, it
appears .babelrc.js files will be supported, at which point this plugin will be
updated to allow transform functions directly in the configuration file.
See: https://github.com/babel/babel/pull/4892
## Options

@@ -97,4 +129,4 @@

| --- | --- | --- | --- | --- |
| `transform` | `string` | yes | `undefined` | The library name to use instead of the one specified in the import statement. ${member} will be replaced with the member, aka Grid/Row/Col/etc. |
| `transform` | `string` | yes | `undefined` | The library name to use instead of the one specified in the import statement. ${member} will be replaced with the member, aka Grid/Row/Col/etc. Alternatively, pass a path to a .js file which exports a function to process the transform (see Advanced Transformations) |
| `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. |

@@ -58,3 +58,3 @@ import assert from 'assert';

let code = transform(`import { KebabMe } from 'react-bootstrap'; LocalName.test = null;`, options);
let code = transform(`import { KebabMe } from 'react-bootstrap';`, options);

@@ -65,2 +65,24 @@ assert.notEqual(code.indexOf('kebab-me'), -1, 'member name KababMe should be transformed to kebab-me');

describe('transform as function', function() {
it('should throw when provided filename is invalid', function() {
let options = createOptions({ transform: 'missingFile.js' });
assert.throws(() => {transform(`import { Row } from 'react-bootstrap';`, options)});
});
it('should throw when provided filename does not resolve to a function', function() {
let options = createOptions({ transform: './test/invalidTransform.js' });
assert.throws(() => {transform(`import { Row } from 'react-bootstrap';`, options)});
});
it('should properly execute transform function when provided', function() {
let options = createOptions({ transform: './test/transform.js' });
let code = transform(`import { upperCaseMe } from 'react-bootstrap';`, options);
assert.notEqual(code.indexOf('UPPERCASEME'), -1, 'member name upperCaseMe should be transformed to UPPERCASEME');
});
});
describe('preventFullImport plugin option', function() {

@@ -67,0 +89,0 @@ it('should throw on default imports when truthy', function() {

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