import-js icon indicating copy to clipboard operation
import-js copied to clipboard

Add CustomElements support

Open mikabytes opened this issue 5 years ago • 2 comments

Perhaps this idea is a bit esoteric, but consider this situation:

./file1.js

class MyCustomElement extends HTMLElement {
 ...
}

window.customElements.define("my-custom-element", MyCustomElement)

./file2.js

const mydom = html`<my-custom-element>Hello</my-custom-element`
document.body.append(mydom)

I'd like ImportJS to add import './file1.js' to file2.js

The html`` template literal syntax is widely adopted in JavaScript community. Used by LitHtml, with support by Prettier, syntax support is available for Vim, Visual Studio and probably many others. If we could teach ImportJS to understand them as well, it would be a great addition and a huge win for me :)

mikabytes avatar Feb 14 '20 09:02 mikabytes

I think this makes sense. I personally don't have a need for this but I'd be happy to help reviewing a PR! There are two parts to this. First, we need to make sure the "export" syntax in ./file1.js is recognized and indexed by import-js (here I think). Then we need to find custom elements like the ones in ./file2.js, likely here: https://github.com/Galooshi/import-js/blob/master/lib/findUsedIdentifiers.js

trotzig avatar Feb 14 '20 09:02 trotzig

Thanks for the pointers! I might start work on this already tomorrow if nothing else comes up.

I think we also need a configuration option to enable/disable this feature. Some people like to import their custom elements in a separate file (custom elements is a global registry)

mikabytes avatar Feb 14 '20 09:02 mikabytes

It's been 2 years.

While I did implement a solution that worked for my use case, it was evident in my company that it was too opinionated to be made into a general solution. People name their custom elements based on their convention.

In the end, I had an idea for a more general plugin-style extension for ImportJS but never got around to writing it.

Closing this due to it being stale. Might reopen in the future.

mikabytes avatar Jan 18 '24 09:01 mikabytes