Configurable Babel preset that enables JSX syntax support for Vue.js applications
npx @tessl/cli install tessl/npm-vue--babel-preset-jsx@1.4.0@vue/babel-preset-jsx is a configurable Babel preset that enables JSX syntax support for Vue.js applications. It serves as a meta-package that orchestrates multiple specialized Babel plugins to transform JSX syntax into Vue-compatible render functions, supporting advanced Vue.js features including component props, slots, scoped slots, directives (v-model, v-on), functional components, and composition API integration.
npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props// babel.config.js or .babelrc.js
module.exports = {
presets: ['@vue/babel-preset-jsx']
}For programmatic use:
const babelPresetJsx = require('@vue/babel-preset-jsx');// babel.config.js
module.exports = {
presets: ['@vue/babel-preset-jsx']
}// babel.config.js
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
functional: true,
injectH: true,
vModel: true,
vOn: true,
compositionAPI: false
}
]
]
}// babel.config.js - Auto-detect Vue version
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
compositionAPI: 'auto'
}
]
]
}
// For Vue 3
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
compositionAPI: 'native'
}
]
]
}The main export that creates a Babel preset configuration with selectable Vue JSX transformation plugins.
/**
* Creates a Babel preset configuration for Vue JSX transformation
* @param {Object} babel - The Babel instance (unused but required by Babel preset API)
* @param {PresetOptions} options - Configuration options for the preset
* @returns {PresetConfiguration} Babel preset configuration object
*/
function babelPresetJsx(babel, options = {}) {
// Returns { plugins: Array<Plugin|[Plugin, Options]> }
}/**
* Configuration options for the Vue JSX Babel preset
*/
interface PresetOptions {
/** Enable functional components syntactic sugar (default: true) */
functional?: boolean;
/** Enable automatic h injection syntactic sugar (default: true) */
injectH?: boolean;
/** Enable vModel syntactic sugar (default: true) */
vModel?: boolean;
/** Enable vOn syntactic sugar (default: true) */
vOn?: boolean;
/**
* Enable composition API support (default: false)
* - false: Disabled
* - true | 'auto': Auto-detect Vue version for import source
* - 'native' | 'naruto': Import from 'vue'
* - 'vue-demi': Import from 'vue-demi'
* - { importSource: string }: Custom import source
*/
compositionAPI?: boolean | string | CompositionAPIConfig;
}
/**
* Custom composition API configuration
*/
interface CompositionAPIConfig {
/** Custom import source for composition API utilities */
importSource: string;
}
/**
* Babel preset configuration returned by the factory function
*/
interface PresetConfiguration {
/** Array of Babel plugins with their configurations */
plugins: Array<Plugin | [Plugin, PluginOptions]>;
}The preset conditionally includes these plugins based on configuration:
functional: true)injectH: true)compositionAPI enabled)compositionAPI enabled)vModel: true)vOn: true)// Only enable core JSX transformation
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
functional: false,
injectH: false,
vModel: false,
vOn: false,
compositionAPI: false
}
]
]
}// Vue 3 with native composition API support
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
compositionAPI: 'native'
}
]
]
}// Custom composition API import source
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
compositionAPI: {
importSource: 'my-vue-composition-library'
}
}
]
]
}// Automatic detection for Vue 2.7
module.exports = {
presets: [
[
'@vue/babel-preset-jsx',
{
compositionAPI: 'auto'
}
]
]
}The preset includes automatic Vue version detection when compositionAPI is set to 'auto' or true. If Vue package cannot be found or read, it falls back to the default @vue/composition-api import source without throwing an error.
// Automatic fallback behavior
try {
const vueVersion = require('vue/package.json').version;
if (vueVersion.startsWith('2.7')) {
importSource = 'vue';
}
} catch (e) {
// Falls back to '@vue/composition-api'
}