Skip to content

Commit

Permalink
feat: rework the compositionAPI option of the preset to support Vue…
Browse files Browse the repository at this point in the history
… 2.7
  • Loading branch information
sodatea committed Jul 6, 2022
1 parent 803eb0a commit e7d094e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/babel-preset-jsx/README.md
Expand Up @@ -44,6 +44,10 @@ module.exports = {
Options are:

- `compositionAPI` - Enables [@vue/babel-sugar-composition-api-inject-h](../babel-sugar-composition-api-inject-h) and [@vue/babel-sugar-composition-api-render-instance](../babel-sugar-composition-api-render-instance), support returning render function in `setup`.
- The default value is `false`;
- When set to `'auto'` (or `true`), it will detect the Vue version in the project. If it's >= 2.7, it will import the composition utilities from `vue`, otherwise from `@vue/composition-api`;
- When set to `'native'` (or `'naruto'`), it will always import the composition utilities from `vue`
- When set to `plugin`, it will always import the composition utilities from `@vue/composition-api`
- `functional` [@vue/babel-sugar-functional-vue](../babel-sugar-functional-vue/README.md) - Functional components syntactic sugar
- `injectH` [@vue/babel-sugar-inject-h](../babel-sugar-inject-h/README.md) - Automatic `h` injection syntactic sugar
- `vModel` [@vue/babel-sugar-v-model](../babel-sugar-v-model/README.md) - `vModel` syntactic sugar
Expand Down
8 changes: 7 additions & 1 deletion packages/babel-preset-jsx/package.json
Expand Up @@ -30,6 +30,12 @@
"rollup-plugin-babel-minify": "^6.2.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
"@babel/core": "^7.0.0-0",
"vue": "2.x"
},
"peerDependenciesMeta": {
"vue": {
"optional": true
}
}
}
35 changes: 32 additions & 3 deletions packages/babel-preset-jsx/src/index.js
Expand Up @@ -6,14 +6,43 @@ import babelSugarCompositionApiRenderInstance from '@vue/babel-sugar-composition
import babelSugarVModel from '@vue/babel-sugar-v-model'
import babelSugarVOn from '@vue/babel-sugar-v-on'

export default (_, { functional = true, injectH = true, vModel = true, vOn = true, compositionAPI = false } = {}) => {
export default (_, {
functional = true,
injectH = true,
vModel = true,
vOn = true,
compositionAPI = false
} = {}) => {
// compositionAPI: 'auto' | 'native' | 'plugin' | false
// legacy: compositionAPI: true (equivalent to 'auto')
// bonus: compositionAPI: 'naruto' (equivalent to 'native')
let injectHPlugin = babelSugarInjectH
let importSource = '@vue/composition-api'

if (compositionAPI) {
if (compositionAPI === 'native' || compositionAPI === 'naruto') {
importSource = 'vue'
}

if (compositionAPI === 'auto' || compositionAPI === true) {
try {
const vueVersion = require('vue/package.json').version
if (vueVersion.startsWith('2.7')) {

This comment has been minimized.

Copy link
@kuoruan

kuoruan Jul 6, 2022

@sodatea what if the vue version > 2.7?

This comment has been minimized.

Copy link
@sodatea

sodatea Jul 6, 2022

Author Member
importSource = 'vue'
}
} catch (e) {}
}

injectHPlugin = [babelSugarCompositionApiInjectH, { importSource }]
}

return {
plugins: [
functional && babelSugarFunctionalVue,
injectH && (compositionAPI ? babelSugarCompositionApiInjectH : babelSugarInjectH),
injectH && injectHPlugin,
vModel && babelSugarVModel,
vOn && babelSugarVOn,
compositionAPI && babelSugarCompositionApiRenderInstance,
compositionAPI && [babelSugarCompositionApiRenderInstance, { importSource }],
babelPluginTransformVueJsx,
].filter(Boolean),
}
Expand Down

0 comments on commit e7d094e

Please sign in to comment.