Skip to content

Commit

Permalink
feat: Add remove options. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 18, 2020
1 parent d6b64d0 commit e4e9f01
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,26 @@ import { Select } from '@uiw/core';

- `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.
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.

- `removeAll: boolean`

Deletes all imports.
Deletes all imports.

- `remove?: 'effects'`

Removing only side effects imports

```js
// Input Code
import 'foo';
import Foo from 'foo';

// Output Code ↓ ↓ ↓ ↓ ↓ ↓
import Foo from 'foo';
```

## Programmatic Usage

Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ export default function () {

const { node } = path;
const { source } = node;
const { opts } = state;
const { opts = {} } = state;

// https://github.com/uiwjs/babel-plugin-transform-remove-imports/issues/3
if (opts.remove === 'effects') {
if (node.specifiers && node.specifiers.length === 0) {
path.remove();
}
}

if (opts.removeAll) {
path.remove();
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/remove-effects-import/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'uiw';
import { Foo } from 'foo';
import { Button } from 'uiw-admin';
import { Select } from '@uiw/core';
3 changes: 3 additions & 0 deletions test/fixtures/remove-effects-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Foo } from 'foo';
import { Button } from 'uiw-admin';
import { Select } from '@uiw/core';
6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ fixtures.map((caseName) => {
pluginBaseOpts.plugins = [
[plugin, { removeAll: true }]
]
} else if (caseName === 'remove-effects-import') {
pluginBaseOpts.presets = [["@babel/preset-env", { "modules": false }]];
pluginBaseOpts.plugins = [
[plugin, { remove: 'effects' }]
]
} else if (caseName === 'options-empty') {
pluginBaseOpts.presets = [["@babel/preset-env", { "modules": false }]];
pluginBaseOpts.plugins = [
Expand All @@ -48,6 +53,7 @@ fixtures.map((caseName) => {
} else {
pluginBaseOpts.presets = [["@babel/preset-env", { "modules": false }]];
}

const code = transformSync(readFileSync(inputFile), pluginBaseOpts).code;
const expected = readFileSync(outputFile).toString();
expect(code).toBe(expected);
Expand Down

0 comments on commit e4e9f01

Please sign in to comment.