Babel Preset for TypeScript Vue 3 SFCs
This Babel preset, `babel-preset-typescript-vue3`, addresses a specific limitation in `vue-loader` when transpiling Vue 3 Single File Components (SFCs) with `<script lang='ts'>` using Babel 7.x. Unlike the standard `@babel/preset-typescript`, which historically ignored `.vue` files, this preset explicitly applies TypeScript transformation to SFCs where `lang='ts'` is detected. It serves as a drop-in replacement or addition to `@babel/preset-typescript` specifically for Vue 3 projects. The current stable version is 2.1.1, with recent updates including Babel 7.27.x, Vue.js 3.5.13, ES module support, and `<script setup>` support. It differentiates itself by resolving the `vue-loader` and Babel integration challenge for TypeScript SFCs, a problem the official preset did not originally handle due to filename assumptions. While its necessity might diminish with future `vue-loader` updates, it remains a crucial solution for this specific use case.
Common errors
-
SyntaxError: Unexpected token ':' (or similar for TypeScript syntax errors)
cause Vue SFC `<script lang='ts'>` content is being parsed by a JavaScript transpiler (e.g., `@babel/preset-env`) without prior TypeScript stripping, as `babel-preset-typescript-vue3` is missing or misconfigured.fixConfirm `babel-preset-typescript-vue3` is listed in your Babel `presets` array within your `babel.config.js` or `.babelrc`. Ensure it is placed before `@babel/preset-env` and `@babel/preset-typescript` to prioritize TypeScript transformation for Vue SFCs. Also verify `babel-loader` is correctly applied to `.vue` files. -
Error: You gave us a parser plugin that doesn't exist yet...
cause This error typically indicates an outdated `@babel/core` or related `@babel/*` package, or an issue where the `babel-preset-typescript-vue3`'s internal plugins are not being loaded correctly due to an incompatible Babel environment.fixUpdate your `@babel/core` to meet the `babel-preset-typescript-vue3`'s peer dependency requirements (e.g., `^7.0.0-0`). Run `npm install` or `yarn install` to ensure all Babel dependencies are correctly resolved. Double-check your Babel configuration for any conflicting or old plugin declarations.
Warnings
- gotcha Without `babel-preset-typescript-vue3`, Vue 3 Single File Components (SFCs) using `<script lang='ts'>` may not be correctly processed by Babel, leading to syntax errors or unexpected behavior. The standard `@babel/preset-typescript` historically ignores `.vue` files due to filename assumptions, causing transpilation failures for TypeScript within SFCs.
- deprecated The package documentation suggests that this preset might become unnecessary in the future if `vue-loader` natively integrates Babel transpilation for TypeScript SFCs. Developers should be aware of this potential for future deprecation.
- gotcha Ensure all Babel-related dependencies, especially `@babel/core` and `@vue/compiler-sfc`, are compatible with the specific version of `babel-preset-typescript-vue3` you are using. Incompatibilities can lead to cryptic errors or unexpected transpilation issues, particularly after major dependency updates.
Install
-
npm install babel-preset-typescript-vue3 -
yarn add babel-preset-typescript-vue3 -
pnpm add babel-preset-typescript-vue3
Imports
- babel-preset-typescript-vue3
presets: ['babel-preset-typescript-vue3']
- Preset Options
presets: [['babel-preset-typescript-vue3', { /* options */ }]] - Babel Config (ESM)
module.exports = { presets: [...] };export default { presets: [...] };
Quickstart
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
},
{
test: /\.(j|t)sx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"babel-preset-typescript-vue3",
"@babel/preset-typescript"
]
}
}
}
]
}
};