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

再看本篇文章之前,本人照样发起想入坑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…

扯完啦,接下来就是正题啦,先看结果
《React+dva+webpack+antd-mobile 实战分享(一)》

本日主如果想给人人说一下怎样用dva来搭建react的项目

第一步

装置 dva 和 roadhog;
    npm i dva-cli roadhog -g 
好啦~如今你已学会了怎样装置dva和roadhog啦,接下来就能够建立项目啦

第二步

建立项目
dva new projectName
npm install
npm start

翻开浏览器输入localhost:8000,看到迎接界面证实第二步已胜利啦

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

第三步

增加设置文件和装置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://segmentfault.com/a/1190000010002714
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞