Skip to content

Commit e7d094e

Browse files
committed
feat: rework the compositionAPI option of the preset to support Vue 2.7
1 parent 803eb0a commit e7d094e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

packages/babel-preset-jsx/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ module.exports = {
4444
Options are:
4545

4646
- `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`.
47+
- The default value is `false`;
48+
- 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`;
49+
- When set to `'native'` (or `'naruto'`), it will always import the composition utilities from `vue`
50+
- When set to `plugin`, it will always import the composition utilities from `@vue/composition-api`
4751
- `functional` [@vue/babel-sugar-functional-vue](../babel-sugar-functional-vue/README.md) - Functional components syntactic sugar
4852
- `injectH` [@vue/babel-sugar-inject-h](../babel-sugar-inject-h/README.md) - Automatic `h` injection syntactic sugar
4953
- `vModel` [@vue/babel-sugar-v-model](../babel-sugar-v-model/README.md) - `vModel` syntactic sugar

packages/babel-preset-jsx/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
"rollup-plugin-babel-minify": "^6.2.0"
3131
},
3232
"peerDependencies": {
33-
"@babel/core": "^7.0.0-0"
33+
"@babel/core": "^7.0.0-0",
34+
"vue": "2.x"
35+
},
36+
"peerDependenciesMeta": {
37+
"vue": {
38+
"optional": true
39+
}
3440
}
3541
}

packages/babel-preset-jsx/src/index.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,43 @@ import babelSugarCompositionApiRenderInstance from '@vue/babel-sugar-composition
66
import babelSugarVModel from '@vue/babel-sugar-v-model'
77
import babelSugarVOn from '@vue/babel-sugar-v-on'
88

9-
export default (_, { functional = true, injectH = true, vModel = true, vOn = true, compositionAPI = false } = {}) => {
9+
export default (_, {
10+
functional = true,
11+
injectH = true,
12+
vModel = true,
13+
vOn = true,
14+
compositionAPI = false
15+
} = {}) => {
16+
// compositionAPI: 'auto' | 'native' | 'plugin' | false
17+
// legacy: compositionAPI: true (equivalent to 'auto')
18+
// bonus: compositionAPI: 'naruto' (equivalent to 'native')
19+
let injectHPlugin = babelSugarInjectH
20+
let importSource = '@vue/composition-api'
21+
22+
if (compositionAPI) {
23+
if (compositionAPI === 'native' || compositionAPI === 'naruto') {
24+
importSource = 'vue'
25+
}
26+
27+
if (compositionAPI === 'auto' || compositionAPI === true) {
28+
try {
29+
const vueVersion = require('vue/package.json').version
30+
if (vueVersion.startsWith('2.7')) {
31+
importSource = 'vue'
32+
}
33+
} catch (e) {}
34+
}
35+
36+
injectHPlugin = [babelSugarCompositionApiInjectH, { importSource }]
37+
}
38+
1039
return {
1140
plugins: [
1241
functional && babelSugarFunctionalVue,
13-
injectH && (compositionAPI ? babelSugarCompositionApiInjectH : babelSugarInjectH),
42+
injectH && injectHPlugin,
1443
vModel && babelSugarVModel,
1544
vOn && babelSugarVOn,
16-
compositionAPI && babelSugarCompositionApiRenderInstance,
45+
compositionAPI && [babelSugarCompositionApiRenderInstance, { importSource }],
1746
babelPluginTransformVueJsx,
1847
].filter(Boolean),
1948
}

0 commit comments

Comments
 (0)