onebay / vite-plugin-imp

A vite plugin for import library component style automatic.
MIT License
231 stars 24 forks source link

fix: gracefully handle alternative imports #31

Closed mrcljx closed 2 years ago

mrcljx commented 2 years ago

parseImportModule used a few as casts which would hide a few bugs that would lead to ugly crashes like #10 when alternative imports are used:

import "ant-design-vue";
import Antd from "ant-design-vue";
import * as Antd from "ant-design-vue";

After this PR, the plugin will not crash on these but instead emit a warning (for the first three cases).

It will also treat the following two lines as equivalent:

import Icon from "@ant-design/icons";
import { default as Icon } from "@ant-design/icons";

This allows a custom nameFormatter to rewrite the import properly (in this cases it needs to be @ant-design/icons/es/components/Icon).

Bonus: This PR also improves performance a bit by moving the astNode.type before the libList.find check.

mrcljx commented 2 years ago

Hey @psaren - thank you for the plugin! This PR is a tiny bit "larger" than it needs to be to address my main issue (which is supporting import Icon from "@ant-design/icons";), but as I was debugging the issue, I found some opportunities to use type-narrowing that would have caught this bug earlier. Let me know if this is too much and you'd rather prefer a smaller PR. Thanks!

psaren commented 2 years ago

Thank you very much for your PR, this is a graceful solution