Skip to content

Commit

Permalink
feat: Make importSource API configurable (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
emosheeep committed Aug 25, 2022
1 parent de293a3 commit 60da266
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -61,4 +61,7 @@ typings/
.next

# vuepress build output
.vuepress/dist
.vuepress/dist

# webstorm
.idea
4 changes: 3 additions & 1 deletion packages/babel-preset-jsx/README.md
Expand Up @@ -47,7 +47,9 @@ Options are:
- 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`
- 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`
- When set to `vue-demi`, it will always import the composition utilities from `vue-demi`
- When set to an object like `{ importSource: string; }`, it will always import the composition utilities from the importSource you set
- `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
16 changes: 12 additions & 4 deletions packages/babel-preset-jsx/src/index.js
Expand Up @@ -13,24 +13,32 @@ export default (_, {
vOn = true,
compositionAPI = false
} = {}) => {
// compositionAPI: 'auto' | 'native' | 'plugin' | false
// compositionAPI: 'auto' | 'native' | 'plugin' | 'vue-demi' | false | { importSource: string; }
// 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') {
if (['native', 'naruto'].includes(compositionAPI)) {
importSource = 'vue'
}

if (compositionAPI === 'auto' || compositionAPI === true) {
if (compositionAPI === 'vue-demi') {
importSource = 'vue-demi'
}

if (['auto', true].includes(compositionAPI)) {
try {
const vueVersion = require('vue/package.json').version
if (vueVersion.startsWith('2.7')) {
importSource = 'vue'
}
} catch (e) {}
} catch (e) { }
}

if (typeof compositionAPI === 'object' && compositionAPI.importSource) {
importSource = compositionAPI.importSource
}

injectHPlugin = [babelSugarCompositionApiInjectH, { importSource }]
Expand Down

0 comments on commit 60da266

Please sign in to comment.