You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

1 month ago
  1. ## @vue/babel-sugar-functional-vue
  2. Syntactic sugar for functional components.
  3. ### Babel Compatibility Notes
  4. - This repo is only compatible with Babel 7.x, for 6.x please use [vuejs/babel-plugin-transform-vue-jsx](https://github.com/vuejs/babel-plugin-transform-vue-jsx)
  5. ### Usage
  6. Install the dependencies:
  7. ```sh
  8. # for yarn:
  9. yarn add @vue/babel-sugar-functional-vue
  10. # for npm:
  11. npm install @vue/babel-sugar-functional-vue --save
  12. ```
  13. In your `.babelrc`:
  14. ```json
  15. {
  16. "plugins": ["@vue/babel-sugar-functional-vue"]
  17. }
  18. ```
  19. However it is recommended to use the [configurable preset](../babel-preset-jsx/README.md) instead.
  20. ### Details
  21. This plugin transpiles arrow functions that return JSX into functional components but only if it's an uppercase variable declaration or default export:
  22. ```js
  23. // Original:
  24. export const A = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
  25. export const b = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
  26. export default ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
  27. // Result:
  28. export const A = {
  29. functional: true,
  30. render: (h, {
  31. props,
  32. listeners
  33. }) => <div onClick={listeners.click}>{props.msg}</div>
  34. }
  35. export const b = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
  36. export default {
  37. functional: true,
  38. render: (h, {
  39. props,
  40. listeners
  41. }) => <div onClick={listeners.click}>{props.msg}</div>
  42. }
  43. ```