JuniorTour / vue-template-babel-compiler

Enable Optional Chaining(?.), Nullish Coalescing(??) and many new ES syntax for Vue.js SFC based on Babel
https://www.npmjs.com/package/vue-template-babel-compiler
118 stars 9 forks source link

[Bug] Call typeof in template return Reference Error #23

Open dimar-hanung opened 2 years ago

dimar-hanung commented 2 years ago

Version

image

What is expected?

Can call typeof in template

What is actually happening?

Return ReferenceError: _typeof is not defined

Steps to Reproduce

  1. Set config following docs image
  2. Call typeof for debugging in template image
  3. This error appear in console, and page doesn't load because this error image
JuniorTour commented 2 years ago

It is a bug when working with vue-cli.


Workaround

1. Exclude @babel/plugin-transform-typeof-symbol from @vue/cli-plugin-babel/preset

// babel.config.js
module.exports = {
    presets: [
        [
            '@vue/cli-plugin-babel/preset',
            {
                exclude: [
                    '@babel/plugin-transform-typeof-symbol'
                ]
            }
        ]
    ],
}

2. Only exclude result of this compiler

// babel.config.js
module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  exclude: 'VueTemplateBabelCompiler'
}

This compiler take the @vue/cli-plugin-babel/preset as a babel preset by default.

So the typeof keyword is transformed into:

var _typeof = require(".../node_modules/@babel/runtime/helpers/typeof")

// your code...
_typeof example

It is unexpected.

May be we can ignore the default babel.config.js of vue-cli.


Do you have interest to fix this issue? You can:

  1. Try to reproduce this code snippet and error by setup DEMO Project.

  2. Follow CONTRIBUTING.md to make a Pull Request.

dimar-hanung commented 2 years ago

Thanks for solution and explanation , it's work after exclude '@babel/plugin-transform-typeof-symbol'