Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue 2 partial support #28

Merged
merged 2 commits into from
Feb 16, 2022
Merged

Vue 2 partial support #28

merged 2 commits into from
Feb 16, 2022

Conversation

walaszczykm
Copy link
Collaborator

Type of change

  • Docs
  • Bug fix
  • Feature

Packages

  • @livechat/widget-core
  • @livechat/widget-react
  • @livechat/widget-vue
  • @livechat/widget-angular

Issue

Resolves #27

Description

Add partial support for Vue 2 by exporting additional submodule v2 which exports the LiveChatWidget component compatible with Vue 2. The "hooks" using Composition API will still be Vue 3 exclusive feature as the overall Composition API is a new feature since version 3.

The usage in Vue 2 app is as follows:

<template>
  <LiveChatWidget
    license="12345678"
    visibility="maximized"
    v-on:new-event="handleNewEvent"
  />
</template>

<script>
  import { LiveChatWidget } from '@livechat/widget-vue/v2'

  export default {
    name: 'App',
    components: {
      LiveChatWidget,
    },
    methods: {
      handleNewEvent(event) {
        console.log('LiveChatWidget.onNewEvent', event)
      },
    },
  }
</script>

To achieve compatibility with both Vue 2 and Vue 3 for LiveChatWidget component it is defined with an "old" Options API approach (as opposed to Composition API with setup method or setup script).

In order to resolve imports issues and no existence of defineComponent function in vue@2 package but still offer to reach TypeScript support for Vue 3 there is a new dedicated build pipeline for Vue 2 submodule defined in packages/widget-vue/rollup-vue2.config.js. Aside from the typical Rollup configuration for input and output files, it includes a custom Babel plugin for removing imports and usages of defineComponent from the output file leaving plain object component definition as an export. That way it can be consumed by Vue 2 application without compatibility error.

The LiveChatWidget component required some slight adjustments to be both compatible with Vue 2 and 3:

  • Refactor prop types definitions from using PropType to type constructor (String, Object, Boolean etc.)
  • Add render() methods that explicitly return nothing (null)
  • Add emits property which contains a list of events that consumers can listen on this component.
  • Use this.emit$ directly in handlers and let Babel extract this reference binding.

@walaszczykm walaszczykm added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 14, 2022
@walaszczykm walaszczykm requested a review from a team February 14, 2022 15:09
@walaszczykm walaszczykm merged commit 66b4d4c into master Feb 16, 2022
@walaszczykm walaszczykm deleted the feature/vue-2-partial-support branch February 16, 2022 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't apply it on vue 2
3 participants