Valian / live_vue

End-to-end reactivity for Phoenix LiveView and Vue
https://hex.pm/packages/live_vue
MIT License
219 stars 11 forks source link

index.vue as entrypoint #22

Closed Papipo closed 1 month ago

Papipo commented 1 month ago

Is it possible to have components whose entrypoint is /ComponentName/index.vue? and then use ComponentName in v-component?

Thanks!

Valian commented 1 month ago

Hi @Papipo!

LiveVue resolves component names based on components object you passed to the getHooks function.

To accomplish what you want, it should be enough to adjust components object a bit, from:

export default {
  // it might be slightly different in your case
  ...import.meta.glob('./**/*.vue', { eager: true }),
  ...import.meta.glob('../../lib/**/*.vue'),
}

to

const mapKeys = (object, cb) => Object.entries(object).reduce((acc, current) => {
    const newKey = cb(current[1], current[0], object);
    acc[newKey] = current[1];
    return acc;
  }, {});

const components = {
  // it might be slightly different in your case
  ...import.meta.glob('./**/*.vue', { eager: true }),
  ...import.meta.glob('../../lib/**/*.vue'),
}

export default mapKeys(components, (key) => key.replace("/index.vue", "")

Hopefully this solves your problem! Maybe I could do it by default in the library, it seems to make sense 👍

Valian commented 1 month ago

@Papipo released in 0.4.1. Please check, your use case should work out of the box 💜

Papipo commented 1 month ago

Oh my, amazing, thanks!! 🙇🏽