From e4e9f013efe1c187fd81916f122493756190819b Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 18 Jun 2020 23:53:45 +0800 Subject: [PATCH] feat: Add remove options. #3 --- README.md | 21 +++++++++++++++---- src/index.js | 9 +++++++- test/fixtures/remove-effects-import/input.js | 4 ++++ test/fixtures/remove-effects-import/output.js | 3 +++ test/index.test.js | 6 ++++++ 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/remove-effects-import/input.js create mode 100644 test/fixtures/remove-effects-import/output.js diff --git a/README.md b/README.md index 6ddeb080..72473787 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.js b/src/index.js index fba2be7c..18cb6a12 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); diff --git a/test/fixtures/remove-effects-import/input.js b/test/fixtures/remove-effects-import/input.js new file mode 100644 index 00000000..1e35eaa9 --- /dev/null +++ b/test/fixtures/remove-effects-import/input.js @@ -0,0 +1,4 @@ +import 'uiw'; +import { Foo } from 'foo'; +import { Button } from 'uiw-admin'; +import { Select } from '@uiw/core'; \ No newline at end of file diff --git a/test/fixtures/remove-effects-import/output.js b/test/fixtures/remove-effects-import/output.js new file mode 100644 index 00000000..096e19f7 --- /dev/null +++ b/test/fixtures/remove-effects-import/output.js @@ -0,0 +1,3 @@ +import { Foo } from 'foo'; +import { Button } from 'uiw-admin'; +import { Select } from '@uiw/core'; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 96a2ca09..1e52eccc 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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 = [ @@ -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);