diff --git a/.gitignore b/.gitignore index 760dc4f..c78d044 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,7 @@ typings/ .next # vuepress build output -.vuepress/dist \ No newline at end of file +.vuepress/dist + +# webstorm +.idea diff --git a/packages/babel-preset-jsx/README.md b/packages/babel-preset-jsx/README.md index dbc5e82..bbaf0a4 100644 --- a/packages/babel-preset-jsx/README.md +++ b/packages/babel-preset-jsx/README.md @@ -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 diff --git a/packages/babel-preset-jsx/src/index.js b/packages/babel-preset-jsx/src/index.js index e80ffe4..691ade7 100644 --- a/packages/babel-preset-jsx/src/index.js +++ b/packages/babel-preset-jsx/src/index.js @@ -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 }]