vite: Unable to support functions such as "optional chain" in QQ browser 10 or chrome 70

Describe the bug

Invalid Babel configuration

// App.vue

const num = window?.num ?? 9;
console.log(num);

// babel.config.js

module.exports = {
  presets: [
    "@vue/cli-plugin-babel/preset",
    "@babel/plugin-proposal-optional-chaining"
  ]
};

// vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
import babel from 'vite-babel-plugin'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    (babel as any)(),
    // legacy({
    //   additionalLegacyPolyfills: ['@babel/plugin-proposal-optional-chaining'],
    //   targets: ['chrome 70']
    // })
  ],
  build: {
    target: ['es2015'],
    // target: ['chrome70']
  },
})

Reproduction

https://github.com/yangdan8/vite2-vue3-ts-demo

System Info

QQ browser 10
Chrome70

Used Package Manager

yarn

Logs

Uncaught SyntaxError: Unexpected token .

Validations

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import checker from 'vite-plugin-checker'
import vitePluginImp from 'vite-plugin-imp'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  return {
    plugins: [
      react({
        babel: {
          plugins: ['@babel/plugin-proposal-optional-chaining'],
        },
      }),
  }
})

@vitejs/plugin-react 或者 @vitejs/plugin-vue 应该支持传入babel配置,实测react可用