react全家桶實現招聘app-專案準備(一)

而今才到當時錯發表於2018-08-22

1.1 專案技術

a. 使用react全家桶 + es6 + webpack + antd-mobile

b. 採用模組化、元件化、工程化

c. Node + express + mongodb + socketIO

1.2 開始專案

專案原始碼設計

react全家桶實現招聘app-專案準備(一)

1.2.1 移動端在處理點選事件的時候,會有300毫秒的延遲。恰恰是這300毫秒的延遲,會讓人有一種卡頓的體驗。所以在index.html中引入外掛fastclick
<script>
  if ('addEventListener' in document) {
    document.addEventListener('DOMContentLoaded', function() {
      FastClick.attach(document.body);
    }, false);
  }
  if(!window.Promise) {
    document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>');
  }
複製程式碼

2 元件打包

2.1 安裝依賴

npm install --save-dev babel-plugin-import react-app-rewired
複製程式碼

2.2 在更目錄下建立 config-overrides.js

const {injectBabelPlugin} = require('react-app-rewired');

module.exports = function override(config, env) {

config = injectBabelPlugin(['import', {libraryName: 'antd-mobile', style: 'css'}],

config);

return config;

}
複製程式碼

修改配置: package.json

"scripts": {

"start": "react-app-rewired start",

"build": "react-app-rewired build",

"test": "react-app-rewired test --env=jsdom",

"eject": "react-scripts eject"

}
複製程式碼

在index.js中寫入

import React from 'react'

import ReactDOM from 'react-dom'

import {Button} from 'antd-mobile'

ReactDOM.render(

<Buttontype='primary'>招聘</Button>,

document.getElementById('root')

)
複製程式碼

2.3 將主體的背景顏色從 blue 變為 green

新增less-loader要加上版本@2.7.3,否則會載入最新版本, 電腦無法執行

npm install --save-dev less@2.7.3 less-loader
複製程式碼

配置: config-overrides.js

const {injectBabelPlugin, getLoader} = require('react-app-rewired');

const fileLoaderMatcher = function (rule) {
  return rule.loader && rule.loader.indexOf(`file-loader`) != -1;
}

module.exports = function override(config, env) {
  // babel-plugin-import
  config = injectBabelPlugin(['import', {
    libraryName: 'antd-mobile',
    //style: 'css',
    style: true, // use less for customized theme
  }], config);

  // customize theme
  config.module.rules[1].oneOf.unshift(
    {
      test: /\.less$/,
      use: [
        require.resolve('style-loader'),
        require.resolve('css-loader'),
        {
          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',
              }),
            ],
          },
        },
        {
          loader: require.resolve('less-loader'),
          options: {
            // theme vars, also can use theme.js instead of this.
            modifyVars: {
              "@brand-primary": "#1cae82", // 正常
              "@brand-primary-tap": "#1DA57A", // 按下
            },
          },
        },
      ]
    }
  );

  // css-modules
  config.module.rules[1].oneOf.unshift(
    {
      test: /\.css$/,
      exclude: /node_modules|antd-mobile\.css/,
      use: [
        require.resolve('style-loader'),
        {
          loader: require.resolve('css-loader'),
          options: {
            modules: true,
            importLoaders: 1,
            localIdentName: '[local]___[hash:base64:5]'
          },
        },
        {
          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',
              }),
            ],
          },
        },
      ]
    }
  );

  // file-loader exclude
  let l = getLoader(config.module.rules, fileLoaderMatcher);
  l.exclude.push(/\.less$/);

  return config;
};

複製程式碼

結語 後續更新中...

完整專案地址:github.com/hongzhengzh…

相關文章