Babel Preset for TypeScript Vue 3 SFCs

2.1.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `babel-preset-typescript-vue3` within a Webpack configuration to correctly transpile Vue 3 SFCs with TypeScript, alongside `@babel/preset-env` and `@babel/preset-typescript`.

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"
            ]
          }
        }
      }
    ]
  }
};

view raw JSON →