基于 react vue 的移动端终极适配方案

先上github地址 https://github.com/gaohan1994/react-vw-layout 有空点个赞蛤~~

2018-4-19日更新适配到安卓低版本的插件buggyfill(是我疏忽导致大家以为vw解决方案兼容范围过小,原第六步css-modules改为buggyfill,css-modules顺延为第七步)

2018-4-16日更新css-modules配置,前面步骤不变,可直接跳到第七步。

写在前面的话

在接触到大漠先生牵头开发的vw解决方案之前,我使用的是阿里的第一代适配解决方案 lib-flexible 在使用vw解决方案开发一套H5之后,我真正的被vw的威力所折服。
由于大漠先生只给出了vue-cli的配置方式,并未给出react系列对应脚手架create-react-app配置版本,在看过大漠先生的配置之后,我在create-react-app脚手架生成的项目上进行了一套配置,使得使用react的各位师兄弟也可以使用vw解决方案!
话不多说开工

vue使用方式:《如何在Vue项目中使用vw实现移动端适配》
关于具体如何使用请参考
再聊移动端页面的适配
使用Flexible实现手淘H5页面的终端适配

移动端适配最接近完美的解决方案在react中的使用方式。本文只讲create-react-app创建的项目如何配置,具体每个插件的用途和使用方法请先查阅大漠先生的文章,我相信大漠先生的文章已经讲的很明白。

《如何在Vue项目中使用vw实现移动端适配》

《如何在Vue项目中使用vw实现移动端适配》

《如何在Vue项目中使用vw实现移动端适配》

重要的事情说三遍。一定要先大概看一下大漠先生的文章再往下看,否则可能会一头雾水。

1.创建项目

create-react-app react-vw-layout
cd react-vw-layout
npm start

打开http://localhost:3000/ 可以看到react欢迎页面,第一步成功。

2.打开配置选项

由于react默认隐藏webpack配置需要手动显示。

npm run eject
//Are you sure you want to eject? This action is permanent. (y/N) 
y

运行完eject之后项目结构如下


项目结构.png

第二步收工,第三部开始配置各种插件。

3.增加配置

安装postCss插件

npm i --save postcss-aspect-ratio-mini postcss-px-to-viewport postcss-write-svg postcss-cssnext postcss-viewport-units cssnano

config/webpack.config.dev.js文件中进行如下修改

1.引入postCss插件

const postcssAspectRatioMini = require('postcss-aspect-ratio-mini');
const postcssPxToViewport = require('postcss-px-to-viewport');
const postcssWriteSvg = require('postcss-write-svg');
const postcssCssnext = require('postcss-cssnext');
const postcssViewportUnits = require('postcss-viewport-units');
const cssnano = require('cssnano');

2.加入postCss配置

加入配置代码位置如下

{
    test: /\.css$/,
    use: [
      require.resolve('style-loader'),
      {
        loader: require.resolve('css-loader'),
        options: {
          importLoaders: 1,
        },
      },
      {
        loader: require.resolve('postcss-loader'),
        options: {
          // Necessary for external CSS imports to work
          // https://github.com/facebookincubator/create-react-app/issues/2677
          ident: 'postcss',
          plugins: () => [
            require('postcss-flexbugs-fixes'),
            autoprefixer({
              browsers: [
                '>1%',
                'last 4 versions',
                'Firefox ESR',
                'not ie < 9', // React doesn't support IE8 anyway
              ],
              flexbox: 'no-2009',
            }),
            //加入地点
            //加入地点
            //加入地点
          ],
        },
      },
    ],
},  

需要加入的代码如下


postcssAspectRatioMini({}),
postcssPxToViewport({ 
  viewportWidth: 750, // (Number) The width of the viewport. 
  viewportHeight: 1334, // (Number) The height of the viewport. 
  unitPrecision: 3, // (Number) The decimal numbers to allow the REM units to grow to. 
  viewportUnit: 'vw', // (String) Expected units. 
  selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px. 
  minPixelValue: 1, // (Number) Set the minimum pixel value to replace. 
  mediaQuery: false // (Boolean) Allow px to be converted in media queries. 
}),
postcssWriteSvg({
  utf8: false
}),
postcssCssnext({}),
postcssViewportUnits({}),
cssnano({
  preset: "advanced", 
  autoprefixer: false, 
  "postcss-zindex": false 
})

第三步收工。

4.测试

修改App.js

import React, { Component } from 'react';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        hello vw-layout
      </div>
    );
  }
}
export default App;

修改App.css

.App {
  width: 750px;
  height: 200px;
  background: #f27a7a;
  color: #ffffff;
  line-height: 200px;
  text-align: center;
}

重新npm start页面显示如下

测试demo.png

可以说是非常OK,剩下最后一个问题,配置生产环境webpack配置文件。

5.配置生产环境webpack.config.js

操作与配置测试环境文件相同先引入插件,在相同的位置配置postCss插件
配置完成后执行npm run build
打开static/css/main.********.css

打包后的css.png

可以看到已经成功编译,打完收工

6.加入viewport-units-buggyfill配置

这一步不过在大漠先生的文章中或是我自己的项目中都已经配置,系我自己的疏忽忘记写在文章中导致大家以为vw兼容范围小。抱歉!!!

打开public/index.html

首先在<head></head>中引入阿里cdn

<script src="//g.alicdn.com/fdilab/lib3rd/viewport-units-buggyfill/0.6.2/??viewport-units-buggyfill.hacks.min.js,viewport-units-buggyfill.min.js"></script>

body中,加入如下js代码:

 <script>
  window.onload = function () {
    window.viewportUnitsBuggyfill.init({
      hacks: window.viewportUnitsBuggyfillHacks
    });
  }
</script>

最终index.html如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
    <script src="//g.alicdn.com/fdilab/lib3rd/viewport-units-buggyfill/0.6.2/??viewport-units-buggyfill.hacks.min.js,viewport-units-buggyfill.min.js"></script>
  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
    <script>
      window.onload = function () {
        window.viewportUnitsBuggyfill.init({
          hacks: window.viewportUnitsBuggyfillHacks
        });
      }
    </script>
  </body>
</html>

重新执行npm start打开页面发现:

image

如果遇到img无法显示,则添加全局css

img { 
    content: normal !important; 
}

OK配置成功。这样就适配了低版本安卓机型

7.加入css-modules配置

一般的小项目不使用css-modules已经可以hold住了,但是页面多起来还是建议使用css-modules,下面介绍一下用法:

执行npm i --save react-css-modules

App.js文件中引入插件
import CSSModules from 'react-css-modules';

修改css文件的引入方式
import './App.css';修改为import styles from './App.css';

修改引用Css方式
className=>styleName

修改导出方式
export default App=>export default CSSModules(App, styles);

保存,从新执行npm start查看页面发现失败

clipboard.png

原因是未打开css import配置,此时import styles from './App.css';该语句并未成功引入css文件。

打开webpack.config.dev.js加入modules: true
找到如下位置

{
    test: /\.css$/,
    use: [
      require.resolve('style-loader'),
      {
        loader: require.resolve('css-loader'),
        options: {
          //看这里看这里看这里看这里
          modules: true,
          
          importLoaders: 1,
        },
      },
      {
        loader: require.resolve('postcss-loader'),
        options: {
           //.....省略
        }
      }
    ],
  },

保存,再次执行npm start查看页面

clipboard.png

成功!但是这个class名太过乱码不适于调试,再次打开webpack.config.dev.js
找到如下位置加入语句localIdentName:'[name]_[local]_[hash:base64:5]'

{
    test: /\.css$/,
    use: [
      require.resolve('style-loader'),
      {
        loader: require.resolve('css-loader'),
        options: {
          modules: true,
          importLoaders: 1,
          //看这里看这里看这里
          localIdentName: '[name]_[local]_[hash:base64:5]'
        },
      },
      {
        loader: require.resolve('postcss-loader'),
        options: {
           //.....省略
        }
      }
    ],
  },

再次执行npm start查看页面

clipboard.png

OJBK大功告成!

最后相同步骤加入到webpack.config.prod.js
执行npm run build 查看打包文件

clipboard.png

彳亍吧,OK了。

END.其他想说的话

git地址再发一次,希望有空能帮忙点个赞谢谢~!! https://github.com/gaohan1994/react-vw-layout 没有配置成功的可以参考一下。尤其是css-modules可能改的地方比较多。

当初看到大漠先生的vw适配方案真的是眼前一亮,在尝试了之后觉得这套方案的生产力非常强悍,其实按照本文进行配置已经可以满足相当一部分项目,<del>除了一点就是没有使用css-modules,当然我自己已经成功配置了css-modules要修改的地方比较多,以后会出一篇文章来再继续分享,</del>同时我是个Typescript重度患者!我极度作死的成功配置了create-react-app typescript versionvw + css-modules版本,现在回想起来配置的那几天真的生不如死。。。各种
踩坑。 等如果有人需要ts + react + vw 解决方案的时候我再写一篇文章吧。
那就到这里了,希望大家使用vw解决方案玩的愉快!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容