babel-preset-typescript-vue3

TypeScript preset for Babel 7.x supporting Vue.js 3 components written in TS

Usage no npm install needed!

<script type="module">
  import babelPresetTypescriptVue3 from 'https://cdn.skypack.dev/babel-preset-typescript-vue3';
</script>

README

babel-preset-typescript-vue3

TypeScript preset for Babel 7.x supporting Vue.js 3 components written in TS. A drop-in replacement for @babel/preset-typescript.

Why?

This preset was created to support TypeScript transpilation using Babel (not Microsoft TypeScript), also for Typescript Support for Vue.js 3, without enforced TS-to-JS transpilation for all files (which can be achieved by adding @babel/plugin-transform-typescript to your Babel config).

Due to architectural limitations of vue-loader the original @babel/preset-typescript preset was always assuming that the <script> section of Vue SFC was written in JavaScript. This wasn't an issue when webpack was using different loaders for .js and .ts files. Since Babel unifies that, vue-loader is always passing this file to babel-loader with an original filename (and a resourceQuery which is not accessible from the transpiling preset). Since preset-typescript was transpiling files with .ts and .tsx extensions, not .vue, SFCs were ignored and reported errors during ES6 to ES5 transpilation.

Most likely in the future, this won't be no longer needed if vue-loader will consider also Babel transpilation. As for this is the only friendly way to resolve the issue.

How?

This preset checks whether the .vue file (SFC) has a lang="ts" attribute set for the <script> tag. If so, @babel/plugin-transform-typescript is applied explicitly to the SFC file.

Options

All the options match the original @babel/preset-typescript preset options.

You can read more about configuring preset options here

Original notes and issues

See @babel/preset-typescript for more information or the issues associated with the original preset package.

Install

Using npm:

npm install --save-dev babel-preset-typescript-vue3

or using yarn:

yarn add babel-preset-typescript-vue3 --dev

Additional Notes

This package is originated from babel-preset-typescript-vue by pawelgabryelewicz and revised to meet the Vue.js 3 requirements using @vue/compiler-sfc, and following tests.

Example Usage

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",
            ],
          },
        },
      },
      ...
    ]
}

Change Log

v2.0.16 - Update Dependencies v2.0.14 - Update Dependencies v2.0.11 - Supports <script setup> (Link) v2.0.8 - Update Dependencies (vue@3.0.3 -> vue@3.0.5)

License

MIT