babel-plugin-transform-remove-imports
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.default = _default; | ||
exports["default"] = _default; | ||
@@ -15,3 +15,7 @@ function _default() { | ||
ImportDeclaration: function ImportDeclaration(path, state) { | ||
if (state.opts.removeAll) { | ||
var node = path.node; | ||
var source = node.source; | ||
var opts = state.opts; | ||
if (opts.removeAll) { | ||
path.remove(); | ||
@@ -21,6 +25,12 @@ return; | ||
if (!state.opts.test) return; | ||
if (!path.node.source || !path.node.source.value) return; | ||
if (!opts.test) { | ||
console.warn("transform-remove-imports: \"test\" option should be specified"); | ||
return; | ||
} | ||
/** @var {string} importName */ | ||
if (new RegExp(state.opts.test).test(path.node.source.value)) { | ||
var importName = source && source.value ? source.value : undefined; | ||
if (importName && testMatches(importName, opts.test)) { | ||
path.remove(); | ||
@@ -31,2 +41,22 @@ } | ||
}; | ||
} | ||
/** | ||
* Determines if the import matches the specified tests. | ||
* | ||
* @param {string} importName | ||
* @param {RegExp|RegExp[]|string|string[]} test | ||
*/ | ||
function testMatches(importName, test) { | ||
// Normalizing tests | ||
var tests = Array.isArray(test) ? test : [test]; // Finding out if at least one test matches | ||
return tests.some(function (regex) { | ||
if (typeof regex === "string") { | ||
regex = new RegExp(regex); | ||
} | ||
return regex.test(importName); | ||
}); | ||
} |
{ | ||
"name": "babel-plugin-transform-remove-imports", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Remove the specified import declaration when you use the babel transform to build the package.", | ||
@@ -41,3 +41,3 @@ "repository": "https://github.com/uiwjs/babel-plugin-transform-remove-imports", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"jest": "^23.6.0" | ||
"jest": "^24.8.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "files": [ |
@@ -22,3 +22,3 @@ babel-plugin-transform-remove-imports | ||
"babel-plugin-transform-remove-imports", { | ||
"test": "(less|css)$" | ||
"test": "\\.(less|css)$" | ||
} | ||
@@ -51,9 +51,16 @@ ] | ||
## Optiosn | ||
## Options | ||
- `test: string` Matches based on regular expressions. | ||
- `removeAll: boolean` Delete all import packages. | ||
- `test: RegExp | string | (RegExp | string)[]` | ||
A regular expression to match the imports that will be removed. | ||
It could be a string or a RegExp object. | ||
You could also pass an array here. | ||
## Programatic Usage | ||
- `removeAll: boolean` | ||
Deletes all imports. | ||
## Programmatic Usage | ||
```js | ||
@@ -67,3 +74,3 @@ import plugin from 'babel-plugin-transform-remove-imports' | ||
plugins: [ | ||
[plugin, { test: "(less|css)$" }] | ||
[plugin, { test: /\.(less|css)$/ }] | ||
], | ||
@@ -79,2 +86,2 @@ }).code; | ||
MIT © [`Kenny Wong`](https://github.com/jaywcjlove) | ||
MIT © [`Kenny Wong`](https://github.com/jaywcjlove) |
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
4569
46
84