babel-plugin-transform-rename-import
Advanced tools
Comparing version 1.1.5 to 2.0.0
@@ -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 @@ |
0
11
41
50
5006
- Removedbabel-types@^6.4.5
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedesutils@2.0.3(transitive)
- Removedlodash@4.17.21(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedto-fast-properties@1.0.3(transitive)