babel-plugin-transform-remove-imports
babel-plugin-transform-remove-imports copied to clipboard
Removing only side effects imports
Hello!
Thank you for this great plugin!
However, I want to remove only side-effects imports that looks like this:
import 'something',
but not like these:
import { something } from 'something'import something from 'something'
Is it possible?
Thanks!
@slavafomin
You should choose prettier and ESLint to solve your problem.
Can meet your needs, but which one is correct to delete?
@jaywcjlove I'm not sure I understand what linting tools have to do with built-time code transformations.
Right now, this plugin would delete imports in all formats, however, I want to delete only imports that looks like this: import 'something', i.e. imports that doesn't import any specific symbols.
Following your notation it would be:
// Input Code
import 'foo';
import { Foo } from 'foo';
import Foo from 'foo';
// Output ↓ ↓ ↓ ↓ ↓ ↓
import { Foo } from 'foo';
import Foo from 'foo';
@slavafomin Upgrade + [email protected]
I don't know if this is the feature you want.
@jaywcjlove wow, that was quick, thank you. I will try it out.
I've tried to use the following configuration:
{
loader: "babel-loader",
options: {
plugins: [
[
"babel-plugin-transform-remove-imports",
{
test: /^core-js\//,
remove: 'effects',
},
],
],
},
}
to remove imports that look like: import 'core-js/something';, but I'm getting the following built-time error message: SyntaxError: NodePath has been removed so is read-only, and I'm not sure why. Do you have any thoughts on this?
@jaywcjlove actually, looking at your implementation I can see a small misunderstanding. I was hoping that remove: 'effects' would work with test option, so both criteria should be met in order for import to be removed.
If you don't mind I would propose the following API:
{
loader: "babel-loader",
options: {
plugins: [
[
"babel-plugin-transform-remove-imports",
{
cases: [ // multiple cases could be specified
{
test: /^core-js\//,
remove: "side-effects-only",
},
{
test: "foo", // string could be used
remove: "everything", // default
},
],
},
],
],
},
}
Such configuration format would allow to specify multiple cases and will allow extensibility in the future by extending the remove option.
A removeAll option could be used instead of cases.
When the removeAll option exists, cases does not work? @slavafomin
@jaywcjlove I believe that when removeAll option is used, the usage of cases should throw error to signify that those two options are mutually exclusive.
@slavafomin Keep removeAll is compatible with previous versions
is there away to prevent the remove of the import in certain files