Galooshi / import-js

A tool to simplify importing JS modules
MIT License
526 stars 70 forks source link

Add CustomElements support #550

Closed mikabytes closed 7 months ago

mikabytes commented 4 years ago

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 :)

trotzig commented 4 years ago

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

mikabytes commented 4 years ago

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 commented 7 months ago

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.