码无界前端

记录一次es6转es5的填坑历程

2017-11-30  本文已影响0人  eternalshallow

背景

随着前端的技术的不断发展,JavaScript也在不断地版本迭代,但是浏览器厂商如果不支持新版的js,那么对于前端同学来说是一场灾难,最近小飞就遇到了这样的一个问题,好好的Vue项目在低版本安卓6.0一下,还有IE浏览器上白屏,于是乎就有了这次es6转es5的探索之旅。

解决过程

step1

遇到问题当然是问google了,这一查不要紧,babel已经到7.x版本了,可是我还是停留在6.x版本啊,vue-cli已经是3.x了,可是我用的还是vue-2.x,这可让我如何是好。

解决这个问题最重要的是两个插件

babel-polyfill
es6-promise

[图片上传失败...(image-e98044-1595217156359)]

使用方式呢,也很简单在入口文件main.js 顶部引入这两个插件

第一步:

import 'babel-polyfill'
import Es6Promise from 'es6-promise'
require('es6-promise').polyfill()
Es6Promise.polyfill()

[图片上传失败...(image-b24684-1595217156359)]

第二步:

在webpack.base.conf.js里边配置

  entry: {
    // app: "./src/main.js"
    app: ["babel-polyfill", "./src/main.js"]
  },

欢天喜地的以为这不是so easy嘛,就像 “外事不决问google,内事不决问baidu”一样,但是打包上传服务器之后,为何我的还是白屏,什么鬼,这是在玩儿我么,但是广大网友也不至于给我开这个玩笑啊

匹夫!!!

step2

是不是可能是我babel-core是6.x的原因呢,那我就升级一下试试吧,于是把所有以babel开头的给卸载了,我要重新配置babel环境,你没有听错,我就是要重新配置

一、介绍一下babel7库的作用域(scope)

  1. Babel 7 用了 npm 的 private scope,把全部的包都挂在在 @babel 下

  2. Babel 7 提供多种配置

    1. .babelrc

    2. babel.config.js

    3. webpack babel loader

二、babel7配置后的文件

babel.config.js

const plugins = [];
if (['production', 'prod'].includes(process.env.NODE_ENV)) {
  plugins.push("transform-remove-console")
}
module.exports = {
  presets: [
    [
      "@vue/app",
      {
        "useBuiltIns": "entry",
        polyfills: [
          'es6.promise',
          'es6.symbol'
        ]
      }
    ]
  ],
  plugins: plugins
};

[图片上传失败...(image-b7661c-1595217156359)]

.babelrc的配置文件

[图片上传失败...(image-9f5eb8-1595217156359)]

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false,
        "useBuiltIns": "entry",
        "corejs": 3,
        "targets": {
          "browsers": [
            "> 0.25%",
            "last 2 versions",
            "ie >= 8"
          ]
        }
      }
    ]
  ],
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 3
      }
    ],
    "transform-vue-jsx",
    "@babel/plugin-transform-arrow-functions",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    "@babel/plugin-proposal-json-strings",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions"
  ],
  "env": {
    "test": {
      "presets": ["env", "stage-2", "es2015"],
      "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}

[图片上传失败...(image-b48ec1-1595217156359)]

升级步骤:

1.babel-core卸载掉,从新安装@babel/core

npm un babel-core
npm i -D @babel/core
npx babel-upgrade --write --install

[图片上传失败...(image-fa9d45-1595217156359)]

2. .babelrc 引入插件解析

"plugins": [ 
  "@babel/plugin-transform-runtime", 
  "@babel/plugin-syntax-dynamic-import", 
  "@babel/plugin-proposal-object-rest-spread",
   "transform-vue-jsx"
 ]

[图片上传失败...(image-2d3bb8-1595217156359)]

3. 在package.json文件中 删除babel6 相关的依赖

4. 删除文件夹 node_module 然后重新安装

至此完成了babel6->babel7的升级

于是我兴致勃勃的去重新打包服务构建到服务器,什么鬼怎么还不好使呢,这不是逗我呢,我都升级到7了,然后打开IE浏览器我又看了看报错信息

SCRIPT5007: 无法获取未定义或 null 引用的属性“call”
SCRIPT1002: 语法错误

点进去看了看压缩之后的代码的报错,看压缩之后的代码真是头疼的事情啊

!e.kind;class c{constructor(e,t){this.buffer="",this.classPrefix=t.classPrefix,e.walk(this)}addText(e){this.buffer+=r(e)}openNode(e){if(!l(e))return;let t=e.kind;e.sublanguage||(t=`${this.classPrefix}${t}`),this.span(t)}closeNode(e){l(e)&&(this.buffer+=s)}span(e){this.buffer+=`<span class="${e}">`}value(){return this.buffer}}class u{constructor(){this.rootNode=

[图片上传失败...(image-69595-1595217156359)]

什么鬼还有“constructor”这肯定是es6转es5没转好啊,想了想,我的babel都重新安装了应该不会有这个问题,那么肯定就是某个插件没有转成功,于是在node_modules里边全局搜索this.classPrefix 果然是这个原因

step3

是引用的一个highlight.js用来做代码高亮的插件引起的,我滴个亲娘啊,终于找到这个作祟的鬼了,然后IE也好了,低版本的问题也解决了,所以这个就告诉我们遇到问题不要慌,输入google来解决问题。

解决方式:

webpack.base.conf.js配置

{
        test: /\.js$/,
        loader: 'babel-loader',
        include: [
          resolve('src'),
          resolve('test'),
          resolve('node_modules/webpack-dev-server/client'),
          resolve('node_modules/highlight.js'),
          resolve('node_modules/highlight.js/lib/highlight.js'),
          resolve('node_modules/cli-highlight')
        ],
      }

你以为这样就结束了???

no!no!no!

你知道变态的IE又出现了了新的问题

CSS3114: @font-face 未能完成 OpenType 嵌入权限检查。权限必须是可安装的

什么?解决了babel还会出现新的问题?,当然啊,当然会出现啊,你解决的是js的问题,那么css3的问题呢

当时我就不淡定了,发现是Vue的 weui的字体库的问题

@font-face {
    font-weight: normal;
    font-style: normal;
    font-family: "weui";
    src: url('data:application/octet-stream;base64,AAEAAAALAIAAAwAwR1NVQrD+s+0AAAE4AAAAQk9TLzJAKEx+AAABfAAAAFZjbWFw65cFHQAAAhwAAAJQZ2x5ZvCRR/EAAASUAAAKtGhlYWQMPROtAAAA4AAAADZoaGVhCCwD+gAAALwAAAAkaG10eEJo//8AAAHUAAAASGxvY2EYqhW4AAAEbAAAACZtYXhwASEAVQAAARgAAAAgbmFtZeNcHtgAAA9IAAAB5nBvc3T6bLhLAAARMAAAAOYAAQAAA+gAAABaA+j/////A+kAAQAAAAAAAAAAAAAA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8uY29tAHcAZQB1AGkAUgBlAGcAdQBsAGEAcgB3AGUAdQBpAHcAZQB1AGkAVgBlAHIAcwBpAG8AbgAgADEALgAwAHcAZQB1AGkARwBlAG4AZQByAGEAdABlAGQAIABiAHkAIABzAHYAZwAyAHQAdABmACAAZgByAG8AbQAgAEYAbwBuAHQAZQBsAGwAbwAgAHAAcgBvAGoAZQBjAHQALgBoAHQAdABwADoALwAvAGYAbwBuAHQAZQBsAGwAbwAuAGMAbwBtAAAAAgAAAAAAAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASAQIBAwEEAQUBBgEHAQgBCQEKAQsBDAENAQ4BDwEQAREBEgETAAZjaXJjbGUIZG93bmxvYWQEaW5mbwxzYWZlX3N1Y2Nlc3MJc2FmZV93YXJuB3N1Y2Nlc3MOc3VjY2Vzcy1jaXJjbGURc3VjY2Vzcy1uby1jaXJjbGUHd2FpdGluZw53YWl0aW5nLWNpcmNsZQR3YXJuC2luZm8tY2lyY2xlBmNhbmNlbAZzZWFyY2gFY2xlYXIEYmFjawZkZWxldGUAAAAA') format('truetype');
}

[图片上传失败...(image-23d48c-1595217156359)]

这个是因为IE上边不支持字体库,既然不支持字体库那怎么办呢,那就用自己的样式覆盖吧,首先复制src下边的database64的字节流就是 “data:application/octet-stream;base64,” 之后的纯base64字节流

。。。

。。。

。。。

。。。

经过以上四步我们就生成了

WechatIMG363.png

然后我们在APP.vue里边进行配置

@font-face {
  font-weight: normal !important;
  font-style: normal !important;
  font-family: "weui" !important;
  src: url('../fonts/weui-webfont.eot') !important; /* IE9 Compat Modes */
  src: url('../fonts/weui-webfont.woff') format('woff'), /* Modern Browsers */
  url('../fonts/weui-webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
  url('../fonts/weui-webfont.svg#weiui') format('svg') !important; /* Legacy iOS */
}

这样就针对IE做好了字体库的配置,完美的解决了问题(自欺欺人一下,虽然报这个错误但是页面可以正常显示,只是字体的问题,所以 以上配置并没有解决我的问题,但是白屏问题已经解决了,字体的问题稍后补充。

以上的字体的解决方式参考:把vux中的@font-face为base64格式的字体信息解码成可用的字体文件

一把辛酸泪,以上历程是三天的解决问题的过程,事后回忆了一下大概有一下三个思路:

至此白屏问题告一段落了,等有时间再更新vux中的@font-face的base64的字体库的问题

不说了,产品又开始改需求了

上一篇下一篇

猜你喜欢

热点阅读