最近遇到一個(gè)問題,用 Vue 開發(fā)的項(xiàng)目在最近兩年新出的安卓手機(jī)上沒問題,在三四年前的舊手機(jī)上出現(xiàn)白屏問題。分析一下應(yīng)該是安卓系統(tǒng)版本的原因,目前已知的是Android 6.0 以上都 OK,6.0 以下就不行了。
低版本安卓系統(tǒng)內(nèi)置的 webview 不支持 ES6 語法等一些新特性,所以報(bào)錯(cuò)。但在手機(jī)上調(diào)試不方便,受一篇文章的啟發(fā), IE 瀏覽器也是同樣的問題,所以可以在 IE 上調(diào)試,一個(gè)調(diào)好了兩個(gè)就都好了。突然發(fā)現(xiàn)萬惡的 IE 還是有點(diǎn)用的…
網(wǎng)上的文章大部分是 Vue-cli 2.x 版本的解決方案,但 Vue-cli 3 跟之前的版本還是有很大差異的,可能是我比較菜,看了 n 篇文章還是不知道怎么配置。經(jīng)過努力,終于梳理出了基于 Vue-cli 3 的項(xiàng)目如何做兼容性配置的步驟:
1. 根目錄下新建 .babelrc 文件
在項(xiàng)目根目錄下新建 .babelrc 文件,跟 package.json 同級(jí)。 將以下代碼復(fù)制到 .babelrc 文件中
1
2
3
4
5
6
|
{ "presets": ["@babel/preset-env"], "plugins": [ "@babel/plugin-transform-runtime" ] } |
2. 修改 babel.config.js
將以下代碼復(fù)制到 babel.config.js 文件中,其中最上面四行是打包時(shí)刪除 console 的配置,如不需要可以刪除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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 }; |
3. 修改 vue.config.js
用 vue-cli 3 新建項(xiàng)目時(shí),默認(rèn)是沒有這個(gè)配置文件的,沒有則在項(xiàng)目根目錄下新建一個(gè) vue.config.js ,也是跟 package.json 同級(jí)。
解決白屏問題需要添加的代碼:
1
2
3
4
5
6
|
module.exports = { transpileDependencies: [ 'webpack-dev-server/client' ], chainWebpack: config => { config.entry.app = [ 'babel-polyfill' , './src/main.js' ]; } } |
4. 修改 main.js 文件
找到 項(xiàng)目根目錄/src/main.js ,添加以下代碼
1
2
3
|
import '@babel/polyfill' ; import Es6Promise from 'es6-promise' Es6Promise.polyfill() |
5. 安裝依賴
在根目錄下執(zhí)行以下語句。如果在第二步不需要配置生產(chǎn)環(huán)境刪除 console 可以不要最后一個(gè) babel-plugin-transform-remove-console 。
1
|
npm install --save-dev @babel /core @babel /plugin-transform-runtime @babel /preset-env es6-promise babel-polyfill babel-plugin-transform-remove-console |
以上五步配置完就可以解決 Vue 項(xiàng)目在低版本安卓系統(tǒng)和 IE 瀏覽器下顯示空白的問題了。
附完整的 vue.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
const path = require( 'path' ) const resolve = dir => path.resolve(__dirname, dir) const IS_PROD = [ 'production' , 'prod' ].includes(process.env.NODE_ENV) const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' ).BundleAnalyzerPlugin; const CompressionWebpackPlugin = require( 'compression-webpack-plugin' ); const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; module.exports = { transpileDependencies: [ 'webpack-dev-server/client' ], // 基本路徑 // baseUrl: './', publicPath: './' , // 輸出文件目錄 outputDir: 'dist' , // eslint-loader 是否在保存的時(shí)候檢查 lintOnSave: false , assetsDir: '' , // 相對(duì)于outputDir的靜態(tài)資源(js、css、img、fonts)目錄 runtimeCompiler: true , // 是否使用包含運(yùn)行時(shí)編譯器的 Vue 構(gòu)建版本 // 生產(chǎn)環(huán)境是否生成 sourceMap 文件 productionSourceMap: false , chainWebpack: config => { config.entry.app = [ 'babel-polyfill' , './src/main.js' ]; // 修復(fù)HMR config.resolve.symlinks( true ); //修復(fù) Lazy loading routes Error config.plugin( 'html' ).tap(args => { args[0].chunksSortMode = 'none' ; return args; }); // 添加別名 config.resolve.alias .set( '@' , resolve( 'src' )) .set( 'assets' , resolve( 'src/assets' )) .set( 'components' , resolve( 'src/components' )) .set( 'layout' , resolve( 'src/layout' )) .set( 'base' , resolve( 'src/base' )) .set( 'static' , resolve( 'src/static' )); //壓縮圖片 config.module .rule( "images" ) .use( "image-webpack-loader" ) .loader( "image-webpack-loader" ) .options({ mozjpeg: {progressive: true , quality: 65}, optipng: {enabled: false }, pngquant: {quality: "65-90" , speed: 4}, gifsicle: {interlaced: false }, webp: {quality: 75} }); // 打包分析 if (process.env.IS_ANALYZ) { config.plugin( 'webpack-report' ) .use(BundleAnalyzerPlugin, [{ analyzerMode: 'static' , }]); } }, configureWebpack: config => { if (IS_PROD) { const plugins = []; //開啟 gzip 壓縮 plugins.push( new CompressionWebpackPlugin({ filename: '[path].gz[query]' , algorithm: 'gzip' , test: productionGzipExtensions, threshold: 10240, minRatio: 0.8 }) ); config.plugins = [ ...config.plugins, ...plugins ]; } }, // css相關(guān)配置 css: { extract: true , sourceMap: false , loaderOptions: {}, modules: false }, parallel: require( 'os' ).cpus().length > 1, pwa: {}, devServer: { open: process.platform === 'darwin' , host: '0.0.0.0' , port: 8080, https: false , hotOnly: false , proxy: null , // 設(shè)置代理 before: app => { } }, // 第三方插件配置 pluginOptions: {} }; |
這里引了幾個(gè)依賴,如果運(yùn)行報(bào)錯(cuò)則根據(jù)提示安裝相應(yīng)依賴即可。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://juejin.im/post/5cb1dc4c5188251b1f4d95ed