Use babel-polyfill in vue to achieve compatibility with low-version browsers

Use babel-polyfill in vue to achieve compatibility with low-version browsers


The problem encountered in the work, the client's browser is chrome49 version, which can only support ES6, but my project is a new project, and ES8 or even newer syntax is used in many places, resulting in higher version browsers being able to work normally after front-end deployment Access, low-version browsers are blank. It is unrealistic to rewrite the code, so after checking a lot of information, I stumbled to solve this problem, and I record it here. The correct solution to the problem is to use babel-polyfill and go directly
to code:

  1. 首先安装babel-polyfill
    
npm install --save-dev babel-polyfill
  1. After installation, your package.json will have such data:
"babel-polyfill": "^6.26.0",
  1. Import the module at the beginning of the main.js file
import 'babel-polyfill'
  1. Add the .babelrc file to the root directory of the project, and the contents of the file are as follows
{
    
    
  "presets": [
    ["env", {
    
    
      "modules": false,
      "targets": {
    
    
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-vue-jsx", "transform-runtime"]
}

  1. Add the following code to 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')
        ]
      },

The complete webpack.conf.js is as follows:

'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'
  }
}

It is worth mentioning that
if your code cannot run because the syntax of new features is used in the third-party module used, then you must list it in the new code babel-loader of webpack.conf.js

For example: resolve('node_modules/vue-json-excel'),

My project cannot run because of the use of the vue-json-excel component

Guess you like

Origin blog.csdn.net/geshi201028/article/details/121772455