Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(babel-preset-jsx): Make importSource API configurable #291

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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