Skip to content

Commit 60da266

Browse files
authoredAug 25, 2022
feat: Make importSource API configurable (#291)
1 parent de293a3 commit 60da266

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed
 

‎.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ typings/
6161
.next
6262

6363
# vuepress build output
64-
.vuepress/dist
64+
.vuepress/dist
65+
66+
# webstorm
67+
.idea

‎packages/babel-preset-jsx/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ Options are:
4747
- The default value is `false`;
4848
- 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`;
4949
- 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`
50+
- When set to `plugin`, it will always import the composition utilities from `@vue/composition-api`, but it will redirect to `'vue'` itself when the vue version is `2.7.x`
51+
- When set to `vue-demi`, it will always import the composition utilities from `vue-demi`
52+
- When set to an object like `{ importSource: string; }`, it will always import the composition utilities from the importSource you set
5153
- `functional` [@vue/babel-sugar-functional-vue](../babel-sugar-functional-vue/README.md) - Functional components syntactic sugar
5254
- `injectH` [@vue/babel-sugar-inject-h](../babel-sugar-inject-h/README.md) - Automatic `h` injection syntactic sugar
5355
- `vModel` [@vue/babel-sugar-v-model](../babel-sugar-v-model/README.md) - `vModel` syntactic sugar

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,32 @@ export default (_, {
1313
vOn = true,
1414
compositionAPI = false
1515
} = {}) => {
16-
// compositionAPI: 'auto' | 'native' | 'plugin' | false
16+
// compositionAPI: 'auto' | 'native' | 'plugin' | 'vue-demi' | false | { importSource: string; }
1717
// legacy: compositionAPI: true (equivalent to 'auto')
1818
// bonus: compositionAPI: 'naruto' (equivalent to 'native')
1919
let injectHPlugin = babelSugarInjectH
2020
let importSource = '@vue/composition-api'
2121

2222
if (compositionAPI) {
23-
if (compositionAPI === 'native' || compositionAPI === 'naruto') {
23+
if (['native', 'naruto'].includes(compositionAPI)) {
2424
importSource = 'vue'
2525
}
2626

27-
if (compositionAPI === 'auto' || compositionAPI === true) {
27+
if (compositionAPI === 'vue-demi') {
28+
importSource = 'vue-demi'
29+
}
30+
31+
if (['auto', true].includes(compositionAPI)) {
2832
try {
2933
const vueVersion = require('vue/package.json').version
3034
if (vueVersion.startsWith('2.7')) {
3135
importSource = 'vue'
3236
}
33-
} catch (e) {}
37+
} catch (e) { }
38+
}
39+
40+
if (typeof compositionAPI === 'object' && compositionAPI.importSource) {
41+
importSource = compositionAPI.importSource
3442
}
3543

3644
injectHPlugin = [babelSugarCompositionApiInjectH, { importSource }]

0 commit comments

Comments
 (0)
Please sign in to comment.