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

feat: add @vue/composition-api support #142

Merged
merged 7 commits into from
Oct 16, 2020

Conversation

antfu
Copy link
Member

@antfu antfu commented Jul 7, 2020

Thanks to @luwanquan who made the jsx plugin https://github.com/luwanquan/babel-preset-vca-jsx which is the base of these changes.

#76, This PR provides minimal support for @vue/composition-api.

  • auto inject global h from @vue/composition-api
  • convert this in v-model/v-on usage to getCurrentInstance()

It is disabled by default. To use it, users need to set the config by:

{
  "presets": [
    [
      "@vue/babel-preset-jsx",
      {
        "compositionAPI": true
      }
    ]
  ]
}

@antfu antfu changed the title feat: add babel-sugar-composition-api-inject-h feat: add @vue/composition-api support Jul 11, 2020
antfu and others added 2 commits July 11, 2020 14:19
Co-authored-by: luwanquan <1501474302@qq.com>
Co-authored-by: luwanquan <1501474302@qq.com>
Co-authored-by: Haoqun Jiang <haoqunjiang@gmail.com>
@antfu
Copy link
Member Author

antfu commented Sep 21, 2020

Sorry for the delay. Updated.

@haoqunjiang
Copy link
Member

The example in the README of render-instance is still wrong.

@noor-tg
Copy link

noor-tg commented Oct 14, 2020

is this pull request need any changes to be merged ? @antfu @sodatea

@antfu
Copy link
Member Author

antfu commented Oct 16, 2020

The example in the README of render-instance is still wrong.

Updated.

@haoqunjiang
Copy link
Member

Thanks!

@haoqunjiang haoqunjiang merged commit ecc6ed6 into vuejs:dev Oct 16, 2020
@antfu antfu deleted the feat/composition-api branch October 16, 2020 10:38
@haoqunjiang
Copy link
Member

Released as v1.2.1

https://github.com/vuejs/jsx/releases/tag/v1.2.1

@jvbianchi
Copy link

jvbianchi commented Oct 16, 2020

With this PR, I'm getting this error on the browser console:

[Vue warn]: Error in v-on handler: "TypeError: Cannot read property '$set' of undefined"

TypeError: Cannot read property '$set' of undefined

@haoqunjiang
Copy link
Member

Any reproductions?
It's expected to break some code if the project hasn't turned on the @vue/composition-api plugin. That's the reason why the flag is off by default.

@jvbianchi
Copy link

jvbianchi commented Oct 16, 2020

Reproduction: https://github.com/jvbianchi/error-jsx
You just need to run yarn serve and change the text in the input

@haoqunjiang
Copy link
Member

Got it. The render-instance plugin has only tested for custom component v-model, forgetting <input vModel>

@jvbianchi
Copy link

Great. Can I delete the test repo?

@haoqunjiang
Copy link
Member

Yeah. Feel free to. Thanks for the report!

@haoqunjiang
Copy link
Member

haoqunjiang commented Oct 21, 2020

@antfu The issue here is that in the render-instance plugin, we can't simply replace this with getCurrentInstance() in a callback, because when the callback is invoked, there may not be an actual "currentInstance".

Here we need an approach that works like the classic arrow-functions transformation:


With arrow-functions transformation:

export default {
  render() {
    return h("div", [h("input", {
      "on": {
        "input": ($event) => {
          if ($event.target.composing) return;
          this.$set(a, "value", $event.target.value);
        }
      },
    }])
  }
}

turns into

export default {
  render() {
    var _this = this
    return h("div", [h("input", {
      "on": {
        "input": function input($event) {
          if ($event.target.composing) return;
          _this.$set(a, "value", $event.target.value);
        }
      },
    }])
  }
}

Similarly, with render-instance transformation, the code should be transformed into

export default {
  render() {
    var _this = getCurrentInstance()
    return h("div", [h("input", {
      "on": {
        "input": function input($event) {
          if ($event.target.composing) return;
          _this.$set(a, "value", $event.target.value);
        }
      },
    }])
  }
}

(Implementation of the arrow-functions transform: https://github.com/babel/babel/blob/d51aa6d76177b544590cdfe3868f9f4d33d8813d/packages/babel-traverse/src/path/conversion.js#L101-L155)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants