Vue 3 CLI - How to add babel polyfill for Object.entries

假如想象 提交于 2019-12-07 04:59:02

问题


I have a dependency (vue2-google-maps) which is causing issues with my Vue app in older browsers, throwing an error with Object.entries.

Reading the Vue CLI Docs on polyfills, I see it mention trying to load the polyfill in babel.config.js.

My babel.config.js:

module.exports = {
  presets: [
    ['@vue/app', {
      polyfills: [
        'es6.object'
      ]
    }]
  ]
}

Throws an error:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: [BABEL] /PROJECT_DIR/src/main.js: Cannot read property 'android' of undefined (While processing: "/PROJECT_DIR/node_modules/@vue/babel-preset-app/index.js")
    at targetEnvironments.filter.environment (/PROJECT_DIR/node_modules/@babel/preset-env/lib/index.js:75:16)
    at Array.filter (<anonymous>)
    at isPluginRequired (/PROJECT_DIR/node_modules/@babel/preset-env/lib/index.js:74:56)
    at includes.filter.item (/PROJECT_DIR/node_modules/@vue/babel-preset-app/index.js:23:12)
    at Array.filter (<anonymous>)
    at getPolyfills (/PROJECT_DIR/node_modules/@vue/babel-preset-app/index.js:22:19)
    at module.exports (/PROJECT_DIR/node_modules/@vue/babel-preset-app/index.js:94:17)
    at loadDescriptor (/PROJECT_DIR/node_modules/@babel/core/lib/config/full.js:163:14)
    at cachedFunction (/PROJECT_DIR/node_modules/@babel/core/lib/config/caching.js:42:19)
    at loadPresetDescriptor (/PROJECT_DIR/node_modules/@babel/core/lib/config/full.js:233:63)
    at config.presets.map.descriptor (/PROJECT_DIR/node_modules/@babel/core/lib/config/full.js:68:19)
    at Array.map (<anonymous>)
    at recurseDescriptors (/PROJECT_DIR/node_modules/@babel/core/lib/config/full.js:66:38)
    at loadFullConfig (/PROJECT_DIR/node_modules/@babel/core/lib/config/full.js:106:6)
    at process.nextTick (/PROJECT_DIR/node_modules/@babel/core/lib/transform.js:28:33)
    at process._tickCallback (internal/process/next_tick.js:112:11)

 @ multi (webpack)-dev-server/client?http://192.168.1.201:8080/sockjs-node (webpack)/hot/dev-server.js ./src/main.js

What am I doing wrong? I created the project with Vue CLI 3.0.3


回答1:


You need to change es6.object to es7.object.entries to make Object.entries work on older browsers.

So your /babel.config.js should look like the following:

module.exports = {
  presets: [
    [
      "@vue/app",
      {
        polyfills: ["es7.object.entries"]
      }
    ]
  ]
};

I have found the polyfills name here: built-in-features.js#L153



来源:https://stackoverflow.com/questions/52475423/vue-3-cli-how-to-add-babel-polyfill-for-object-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!