New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-rename-import

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-rename-import - npm Package Compare versions

Comparing version 1.1.5 to 2.0.0

license

32

lib/index.js

@@ -7,15 +7,8 @@ 'use strict';

exports.default = visitor;
var _babelTypes = require('babel-types');
var t = _interopRequireWildcard(_babelTypes);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
/* eslint-disable no-param-reassign */
function isModule(value, original) {
var pattern = new RegExp('^(' + original + '|' + original + '/.*)$');
return pattern.test(value);
} /* eslint-disable no-param-reassign */
}
function replace(value, original, replacement) {

@@ -27,6 +20,5 @@ var pattern = new RegExp('^' + original);

function visitor(_ref) {
var original = _ref.original;
var replacement = _ref.replacement;
var t = _ref.types;
var source = function source(value) {
var source = function source(value, original, replacement) {
return t.stringLiteral(replace(value, original, replacement));

@@ -36,12 +28,20 @@ };

visitor: {
ImportDeclaration: function ImportDeclaration(path) {
ImportDeclaration: function ImportDeclaration(path, state) {
var _state$opts = state.opts;
var original = _state$opts.original;
var replacement = _state$opts.replacement;
var value = path.node.source.value;
if (isModule(value, original)) {
path.node.source = source(value);
path.node.source = source(value, original, replacement);
}
},
CallExpression: function CallExpression(path) {
CallExpression: function CallExpression(path, state) {
var _state$opts2 = state.opts;
var original = _state$opts2.original;
var replacement = _state$opts2.replacement;
var node = path.node;
if (node.callee.name === 'require' && node.arguments && node.arguments.length === 1 && t.isStringLiteral(node.arguments[0]) && isModule(node.arguments[0].value, original)) {
path.node.arguments = [source(node.arguments[0].value)];
path.node.arguments = [source(node.arguments[0].value, original, replacement)];
}

@@ -48,0 +48,0 @@ }

{
"name": "babel-plugin-transform-rename-import",
"version": "1.1.5",
"version": "2.0.0",
"description": "Replace import sources",

@@ -28,3 +28,2 @@ "main": "lib/index",

"babel-core": "^6.4.5",
"babel-generator": "^6.7.2",
"babel-preset-es2015": "^6.3.13",

@@ -41,11 +40,3 @@ "babel-register": "^6.4.3",

"lib"
],
"babel": {
"presets": [
"es2015"
]
},
"dependencies": {
"babel-types": "^6.4.5"
}
]
}

@@ -15,14 +15,22 @@ # babel-plugin-transform-rename-import [![Build Status][travis-image]][travis-url] [![npm][npm-image]][npm-url]

## Usage
## babelrc
```js
{
"plugins": [["transform-rename-import", { original: 'assert', replacement: 'power-assert' }]]
}
```
## Programatic Usage
```javascript
import visitor from 'babel-plugin-transform-rename-import'
import traverse from 'babel-traverse'
import generate from 'babel-generator'
import plugin from 'babel-plugin-transform-rename-import'
import { transform } from 'babel-core'
function replace (code, original, replacement) {
const { ast } = transform(code)
traverse(ast, visitor({ replacement, original }).visitor)
return generate(ast, {}, code).code.trim()
return transform(code, {
babelrc: false,
plugins: [
[plugin, { original, replacement} ],
],
}).code;
}

@@ -29,0 +37,0 @@

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