资讯专栏INFORMATION COLUMN

React+dva+webpack+antd-mobile 实战分享(一)

ziwenxie / 675人阅读

摘要:再看本篇文章之前,本人还是建议想入坑的童鞋可以选有来创建的项目,因为现在和还不成熟,坑相对要多一些,当然如果你已经做好跳坑的准备,那么请继续往下走本文适合对有一定了解的人。

再看本篇文章之前,本人还是建议想入坑react的童鞋可以选有create-react-app来创建react的项目,因为现在dva和roadhog还不成熟,坑相对要多一些,当然如果你已经做好跳坑的准备,那么请继续往下走;
本文适合对 ES6+webpack 有一定了解的人。没有的了解的同学可以先看看下面的我分享的链接,

ES6: http://www.jianshu.com/p/ebfe...
Webpack: https://doc.webpack-china.org...
react: https://facebook.github.io/re...
antd-mobile:https://mobile.ant.design/doc...

扯完啦,接下来就是正题啦,先看效果

今天主要是想给大家说一下怎么用dva来搭建react的项目

第一步
安装 dva 和 roadhog;
    npm i dva-cli roadhog -g 
好啦~现在你已经学会了怎么安装dva和roadhog啦,接下来就可以创建项目啦
第二步
创建项目
dva new projectName
npm install
npm start

打开浏览器输入localhost:8000,看到欢迎界面证明第二步已经成功啦

第三步
添加配置文件和安装webpack

安装 lodash babel-plugin webpack-plugin shim 并添加到package.json文件中

npm install --save-dev webpack 安装本地webpack配置文件

webpack 文件
    // webpack配置
    import glob from "glob";
    import webpack from "webpack";
    import { isRegExp } from "lodash";
    import pxtorem from "postcss-pxtorem";
    import HtmlWebpackPlugin from "html-webpack-plugin";
    import ExtractTextPlugin from "extract-text-webpack-plugin";
    import LodashModuleReplacementPlugin from "lodash-webpack-plugin";
    
    
    const path = require("path");
    export default ( webpackConfig, env ) => {
    
      const loaders = webpackConfig.module.loaders;
      const postcss = webpackConfig.postcss;
      webpackConfig.postcss = function () {
        const postcssArray = postcss();
        postcssArray.push( pxtorem( {
          rootValue: 100,
          propWhiteList: []
        } ) );
        return postcssArray;
      };
      const svgDirs = [
        require.resolve( "antd-mobile" ).replace( /warn.js$/, "" ), // antd-mobile 内置svg    // 引入antd-mobile
        path.resolve(__dirname, "src/assets/icon"),
      ];
    
      loaders.forEach( ( loader ) => {
        if ( loader.test && loader.test.toString() === "/.svg$/" ) {
          loader.exclude = svgDirs;
        }
      } );
    
      loaders.unshift( {
        test: /.svg$/,
        loader: "svg-sprite",
        include: svgDirs
      } );
      const noParse = webpackConfig.module.noParse;
      if ( Array.isArray( noParse ) ) {
        noParse.push( /moment.js/ );
      }
      else if ( noParse ) {
        webpackConfig.module.noParse = [ noParse, /moment.js/ ];
      }
      else {
        webpackConfig.module.noParse = [ /moment.js/ ];
      }
    
      // lodash
      webpackConfig.babel.plugins.push( "lodash" );
      webpackConfig.plugins.push( new LodashModuleReplacementPlugin() );
    
      loaders.push( {
        test: /.(png|jpg|jpeg|gif)(?v=d+.d+.d+)?$/i,
        loader: "file"
      } );
    
      // 打包配置
      if ( env === "production" ) {            
         //添加hash
        webpackConfig.output.filename = "[name].[chunkhash:6].js";
        webpackConfig.output.chunkFilename = "[name].[chunkhash:6].js";
    
        webpackConfig.plugins.forEach( ( plugin, index, plugins ) => {
          if ( plugin instanceof ExtractTextPlugin ) {
            plugins[ index ] = new ExtractTextPlugin( "[name].[chunkhash:6].css", {
              disable: false,
              allChunks: true
            } );
          }
          else if ( plugin instanceof webpack.optimize.CommonsChunkPlugin ) {
            plugins[ index ] = new webpack.optimize.CommonsChunkPlugin(
                "common",
                "common.[chunkhash:6].js"
            );
          }
        } );
    
      }
      //HTML
      webpackConfig.module.loaders = loaders.filter(
              loader => isRegExp( loader.test ) && loader.test.toString() !== "/.html$/"
      );
      webpackConfig.plugins.push(
          new HtmlWebpackPlugin( {
            // favicon: "./src/logo/logo.ico",
            template: "./src/index.html",
            filename: "index.html",
            inject: true
          } )
      );
    
      return webpackConfig;
    };

到现在你已经完成了一半啦 是不是觉得很简单。对啦 这里有一点要注意,复制 es5-shim.min.js es5-sham.min.js console-polyfill/index.js 文件到 public 文件夹console-polyfill/index.js 改名为 console-polyfill.js

第四步 roadhog、proxy配置和antd-mobile引入
废话不说 这步直接上代码(对应的是目录中的.roadhogrc.js,大学按步骤下来的话这应该是.roadhogrc.json的文件,但是本人还是比较喜欢js语法,所以做了修改,此处因人而异)
     import path from "path";
    
    export default {
     "/api": {
        target:"localhost",//这里是你的接口地址,我随便写的
        changeOrigin: true
      },
      multipage: true,
      theme: "antd.config.js",
      entry: [ "src/common.js", "src/index.js" ],
      env: { //下面是在开发环境和生产环境都引入antd-mobile
        development: {
          extraBabelPlugins: [
            "dva-hmr",
            "transform-runtime",
            [ "import", { libraryName: "antd-mobile", style: true }]
          ]
        },
        production: {
          extraBabelPlugins: [
            "transform-runtime",
            [ "import", { libraryName: "antd-mobile", style: true }]
          ]
        }
      }
    };
    

好啦,以上四步差不多就可以用dva把react的项目架子搭建起来,再有就是eslint的配置啦,此处不做讲解(http://eslint.org/docs/user-g...),接下来你可以在src中尝试着运行一下Hello World啦

还有一个点需要注意的是,dva 建项目的时候会默认安装redux和react-router,所以在开发中千万不要在去安装,会因为版本不兼容而导致项目无法运行;

最后给大家分享一些用到的资料
antd主题制定: https://ant.design/docs/react...
roadhog: https://github.com/sorrycc/ro...
webpack中proxy配置: https://webpack.github.io/doc...
redux: http://www.redux.org.cn/
react-router: http://react-guide.github.io/...

项目地址:https://github.com/tengwei30/...

更多精彩敬请期待。。。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/83751.html

相关文章

  • React+dva+webpack+antd-mobile 实战分享(二)

    摘要:第一篇在上一篇文章中教给大家了怎么搭建项目的架子那么今天我们就来说一下项目里的导航和列表的实现导航废话不说啦下面直接给大家讲一下代码项目用的的框架应该没什么难度,我相信大家认真看文档的都能布局出来这个地方是封装的请求请求过来的数据全部存 第一篇 https://segmentfault.com/a/11... 在上一篇文章中教给大家了怎么搭建项目的架子;那么今天我们就来说一下项目里的导...

    eternalshallow 评论0 收藏0
  • 2017-07-01 前端日报

    摘要:前端日报精选腾讯前端团队社区源码分析入门指南一些关于使用的心得基本类型与引用类型知多少掘金中文第期框架选型周刊第期入门系列模块车栈重构基于的网络请求库某熊的全栈之路的那些奇技淫巧的平凡之路模仿写个数组监听掘 2017-07-01 前端日报 精选 Why you shouldn`t use Preact, Fast-React, etc. to replace React today -...

    _DangJin 评论0 收藏0
  • 使用Dva+antd-mobile构建个移动端web

    摘要:整个应用的,由多个小的的以为合成该当前的状态。执行异步函数发出一个,类似于取的值通过函数将需要用到的数据合并到中,再在组件中取修改的值通过被的会自动在中拥有方法请求统一讲用到所有的接口放到中方便管理函数柯里化 项目地址:dva-demo 随手一个小star给予动力,谢谢! Build Setup # install dependencies npm install # serve ...

    Jaden 评论0 收藏0

发表评论

0条评论

ziwenxie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<