vue で babel-polyfill を使用して、低バージョンのブラウザとの互換性を実現する

vue で babel-polyfill を使用して、低バージョンのブラウザとの互換性を実現する


作業中に発生した問題。クライアントのブラウザは chrome49 バージョンであり、ES6 のみをサポートしていますが、私のプロジェクトは新しいプロジェクトであり、ES8 またはさらに新しい構文が多くの場所で使用されており、結果として、より高いバージョンのブラウザはその後正常に動作します。フロントエンド展開 Access、低バージョンのブラウザでは空白です。コードを書き直すのは現実的ではないため、多くの情報を確認した結果、この問題の解決につまずいたので、ここに記録します。問題の正しい解決策は次のとおりです。 babel-polyfill を使用して
コードに直接移動します。

  1. 首先安装babel-polyfill
    
npm install --save-dev babel-polyfill
  1. インストール後、package.json には次のようなデータが含まれます。
"babel-polyfill": "^6.26.0",
  1. main.js ファイルの先頭にモジュールをインポートします。
import 'babel-polyfill'
  1. .babelrc ファイルをプロジェクトのルート ディレクトリに追加します。ファイルの内容は次のとおりです。
{
    
    
  "presets": [
    ["env", {
    
    
      "modules": false,
      "targets": {
    
    
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-vue-jsx", "transform-runtime"]
}

  1. 次のコードを webpack.conf.js に追加します。
      {
    
    
        test: /\.js$/,
        loader: 'babel-loader',
        include: [
          resolve('src'),
          resolve('test'),
          resolve('node_modules/vue-json-excel'),
          resolve('node_modules/webpack-dev-server/client')
        ]
      },

完全な webpack.conf.js は次のとおりです。

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
    
    
  return path.join(__dirname, '..', dir)
}

const createLintingRule = () => ({
    
    
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    
    
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

module.exports = {
    
    
  context: path.resolve(__dirname, '../'),
  entry: {
    
    
    app: './src/main.js'
  },
  output: {
    
    
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    
    
    extensions: ['.js', '.vue', '.json'],
    alias: {
    
    
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },
  module: {
    
    
    rules: [
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
    
    
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
    
    
        test: /\.js$/,
        loader: 'babel-loader',
        include: [
          resolve('src'),
          resolve('test'),
          resolve('node_modules/vue-json-excel'),
          resolve('node_modules/webpack-dev-server/client')
        ]
      },
      {
    
    
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
    
    
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
    
    
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
    
    
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
    
    
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
    
    
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  node: {
    
    
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

使用されているサードパーティ モジュールで新機能の構文が使用されているためにコードが実行できない場合は、それを webpack.conf.js の新しいコード babel-loader にリストする必要があることに注意してください

例如:resolve(‘node_modules/vue-json-excel’),

vue-json-excel コンポーネントの使用が原因でプロジェクトを実行できません

おすすめ

転載: blog.csdn.net/geshi201028/article/details/121772455