【问题标题】:using babel-polyfill (and not polyfill.io) in nuxt在 nuxt 中使用 babel-polyfill(而不是 polyfill.io)
【发布时间】:2019-06-15 20:43:28
【问题描述】:

我正在使用 nuxt vuetify 模板构建一个静态网站,虽然它非常棒并且已经为我提供了如此多的配置和设置,但我发现让我的网站在 Internet Explorer (11) 上顺利运行非常困难。

由于需要转译 ES6 代码,我正在尝试在我的应用程序中配置 babel,这是我第一次配置 babel 的经验(所以,请忽略我在该领域的知识不足)。互联网上有很多问题/疑问,我几乎尝试了所有但徒劳无功。

TL;DR,我正在使用 Nuxt 2.x(和 Webpack 4,内部),我不知道在哪里以及如何包含 @babel/polyfill,因为,

  1. 我们在 Nuxt 应用程序中没有定义或明确的入口点,我可以在其中导入该文件 import '@babel.polyfill',正如许多地方所述,包括 babel 官方文档

  2. webpack 4 也没有 entry.vendor 字段,我可以在其中注入该 polyfill。

这是我的 nuxt.config.js

babel: {
      presets: [
        ['@babel/preset-env', {
          'useBuiltIns': 'usage',
          'targets': '> 0.25%, not dead'
        }]
      ],
      plugins: ['@babel/plugin-syntax-dynamic-import', [
        '@babel/plugin-transform-runtime',
        {
          'corejs': false,
          'helpers': true,
          'regenerator': true,
          'useESModules': false
        }
      ]],
      'configFile': false,
      'babelrc': false
}

这是我正在使用的软件包版本:

"dependencies": {
    "@babel/polyfill": "^7.2.5"
}

"devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.2.3",
    "@babel/runtime-corejs2": "^7.3.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^10.0.1",
    "babel-loader": "^8.0.5",
    "babel-plugin-dynamic-import-node": "^2.2.0"
}

this thread 之后,我还想将 babel polyfill 作为供应商推送到 webpack 入口对象,但它给了我错误:Cannot set property 'vendor' of undefined 因为显然 webpack 4 配置没有入口字段,就像webpack 3 曾经有。

这是我的 webapack 配置:

{ 
  name: 'client',                                                                                                                                                                                 
  mode: 'production',
  devtool: false,
  optimization: 
   { runtimeChunk: 'single',
     minimize: true,
     minimizer: undefined,
     splitChunks: 
      { chunks: 'all',
        automaticNameDelimiter: '.',
        name: undefined,
        cacheGroups: [Object] } },
  output: 
   { path: '/home/waleed/project/.nuxt/dist/client',
     filename: '[chunkhash].js',
     chunkFilename: '[chunkhash].js',
     publicPath: '/_nuxt/' },
  performance: { maxEntrypointSize: 1024000, hints: 'warning' },
  resolve: 
   { extensions: [ '.wasm', '.mjs', '.js', '.json', '.vue', '.jsx' ],
     alias: 
      { '~': '/home/waleed/project',
        '~~': '/home/waleed/project',
        '@': '/home/waleed/project',
        '@@': '/home/waleed/project',
        assets: '/home/waleed/project/assets',
        static: '/home/waleed/project/static' },
     modules: 
      [ 'node_modules',
        '/home/waleed/projectnode_modules',
        '/home/waleed/project/node_modules/@nuxt/config/node_modules' ] },
  resolveLoader: 
   { modules: 
      [ 'node_modules',
        '/home/waleed/project/node_modules',
        '/home/waleed/project/node_modules/@nuxt/config/node_modules' ] },
  module: 
   { rules: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] },
  plugins: 
   [ VueLoaderPlugin {},
     WarnFixPlugin {},
     WebpackBarPlugin {
       profile: false,
       handler: [Function],
       modulesCount: 500,
       showEntries: false,
       showModules: true,
       showActiveModules: true,
       options: [Object],
       reporters: [Array] },
     MiniCssExtractPlugin { options: [Object] },
     HtmlWebpackPlugin { options: [Object] },
     HtmlWebpackPlugin { options: [Object] },
     VueSSRClientPlugin { options: [Object] },
     DefinePlugin { definitions: [Object] } ] 
}

P.S:我已经看过并尝试过this question,它肯定也有一定的效果,但我的用例要求不要使用 polyfill.io。

【问题讨论】:

    标签: javascript webpack babeljs nuxt.js


    【解决方案1】:

    对于所有像我一样撞头的人,(作为临时解决方案)我决定使用 babel-polyfill CDN 并将其 url 包含在 nuxt.config.js 文件的脚本部分中,如下所示

    script: [
          { src: 'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.2.5/polyfill.min.js' }
        ]
    

    但是,我仍在寻找一种更简洁、更符合 ES6 风格的 importrequire 语法来实现所需的结果。因此,如果您有这样的解决方案,请随时添加更多答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2019-08-20
      • 2020-08-30
      • 1970-01-01
      相关资源
      最近更新 更多