react工程搭建系列之---基于create-react-app使用antd-mobile

news/2024/11/13 20:35:21 标签: javascript, webpack, json

一、引入antd-mobile

1.安装

npm install antd-mobile --save

2.使用

/* src/App.js */
import {Button} from 'antd-mobile';
import 'antd-mobile/lib/button/style/css'  需要手动引入样式
class App extends Component {
  render() {
    return (
      <div className="App">
        <Button type='primary'>primary</Button>
      </div>
    );
  }
}

二、安装及配置react-app-rewired

1.安装react-app-rewired

npm install react-app-rewired --save-dev

2.修改package.json

/* package.json */
"scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
}

3.在项目根目录创建 config-overrides.js文件

由于使用creact-react-app创建的项目所以修改默认配置没那么方便,需要通过此文件修改默认配置

module.exports = function override(config, env) {
    // do stuff with the webpack config...
    console.log(config);
    return config;
};

运行npm run start打印出来的config,可以看出这是webpack的开发环境配置

  mode: 'development',
  devtool: 'cheap-module-source-map',
  entry: 
   [ '/Users/surui/Desktop/my-react-app/node_modules/react-dev-utils/webpackHotDevClient.js',
     '/Users/surui/Desktop/my-react-app/src/index.js' ],
  output: 
   { pathinfo: true,
     filename: 'static/js/bundle.js',
     chunkFilename: 'static/js/[name].chunk.js',
     publicPath: '/',
     devtoolModuleFilenameTemplate: [Function: devtoolModuleFilenameTemplate] },
  optimization: 
   { splitChunks: { chunks: 'all', name: false },
     runtimeChunk: true },
  resolve: 
   { modules: [ 'node_modules' ],
     extensions: [ '.mjs', '.web.js', '.js', '.json', '.web.jsx', '.jsx' ],
     alias: { 'react-native': 'react-native-web' },
     plugins: [ [Object], [Object] ] },
  resolveLoader: { plugins: [ [Object] ] },
  module: 
   { strictExportPresence: true,
     rules: [ [Object], [Object], [Object] ] },
  plugins: 
   [ HtmlWebpackPlugin {
       options: [Object],
       childCompilerHash: undefined,
       childCompilationOutputName: undefined,
       assetJson: undefined,
       hash: undefined,
       version: 4 },
     InterpolateHtmlPlugin { htmlWebpackPlugin: [Object], replacements: [Object] },
     ModuleNotFoundPlugin {
       appPath: '/Users/surui/Desktop/my-react-app',
       yarnLockFile: undefined,
       useYarnCommand: [Function: bound useYarnCommand],
       getRelativePath: [Function: bound getRelativePath],
       prettierError: [Function: bound prettierError] },
     DefinePlugin { definitions: [Object] },
     HotModuleReplacementPlugin {
       options: {},
       multiStep: undefined,
       fullBuildTimeout: 200,
       requestTimeout: 10000 },
     CaseSensitivePathsPlugin { options: {}, pathCache: {}, fsOperations: 0, primed: false },
     WatchMissingNodeModulesPlugin {
       nodeModulesPath: '/Users/surui/Desktop/my-react-app/node_modules' },
     IgnorePlugin {
       options: [Object],
       checkIgnore: [Function: bound checkIgnore] },
     ManifestPlugin { opts: [Object] } ],

4.使用babel-plugin-import用于按需加载组件代码和样式

第一步:安装

npm install babel-plugin-import --save-dev

第二步:修改config-overrides.js文件

const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
    // do stuff with the webpack config...
    config = injectBabelPlugin(['import', {
        libraryName: 'antd-mobile',
        style: 'css',
        // style: true, // use less for customized theme
    }], config);
    return config;
};

如果样式用css那么style:'css,如果用less那么style: true

第三步:然后只需从 antd-mobile 引入模块即可,无需单独引入样式

/* src/App.js */
import {Button} from 'antd-mobile';
class App extends Component {
  render() {
    return (
      <div className="App">
        <Button type='primary'>primary</Button>
      </div>
    );
  }
}

5.样式与定制主题

less

antd-mobile 的样式使用了 Less 作为开发语言

第一步:安装less less-loader

npm install --save-dev less less-loader

第二步:在 package.json 文件中添加一个 theme 字段,里面将包含所有我们想要修改的主题样式

"theme": {
    "brand-primary": "red",
    "color-text-base": "#333"
  },

第三步:修改config-overrides.js文件,覆盖webpack配置

const { injectBabelPlugin, getLoader } = require('react-app-rewired');
const autoprefixer = require('autoprefixer');
const theme = require('./package.json').theme;
const fileLoaderMatcher = function (rule) {
    return rule.loader && rule.loader.indexOf(`file-loader`) != -1;
}
module.exports = function override(config, env) {
    // do stuff with the webpack config...
    config = injectBabelPlugin(['import', {
        libraryName: 'antd-mobile',
        // style: 'css',
        style: true, // use less for customized theme
    }], config);
    console.log(config.module.rules[2].oneOf);
    config.module.rules[2].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: theme,
                    },
                },
            ]
        }
    );

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

    return config;
};

scss

第一步:安装sass-loader

npm install --save-dev sass-loader

第二步:修改config-overrides.js文件,覆盖webpack配置

const { injectBabelPlugin, getLoader } = require('react-app-rewired');
const autoprefixer = require('autoprefixer');
const theme = require('./package.json').theme;
const fileLoaderMatcher = function (rule) {
    return rule.loader && rule.loader.indexOf(`file-loader`) != -1;
}
module.exports = function override(config, env) {
    // do stuff with the webpack config...
    config = injectBabelPlugin(['import', {
        libraryName: 'antd-mobile',
        // style: 'css',
        style: true, // use less for customized theme
    }], config);
    console.log(config.module.rules[2].oneOf);

    // sass
    config.module.rules[2].oneOf.unshift(
        {
            test: /\.scss$/,
            use: [
                require.resolve('style-loader'),
                require.resolve('css-loader'),
                require.resolve('sass-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',
                            })
                        ],
                    },
                }
            ]
        }
    );
    //less
    config.module.rules[2].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: theme,
                    },
                },
            ]
        }
    );

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

项目地址:https://github.com/SuRuiGit/m...


http://www.niftyadmin.cn/n/1494303.html

相关文章

python添加pythonhome参数,如何在python中向烧瓶烧瓶添加参数?

如何创建一个类似以下内容的api调用&#xff1a;api.add_resource(MyResource, /myresource/)目前尚不清楚如何从内部进行处理&#xff1a;class MyResource(restful.Resource):def get(self):print myurlparameterreturn ""另外,我注意到我只能将add_resource提升到…

TypeScript基础入门之装饰器(二)

2019独角兽企业重金招聘Python工程师标准>>> 转发 TypeScript基础入门之装饰器(二) 装饰器求值 如何应用装饰器应用于类内的各种声明的顺序&#xff1a; 1. 对每个实例成员应用参数装饰器&#xff0c;后跟Method&#xff0c;Accessor或Property Decorators。 2. 对每…

怎样在max系统运行Java,如何在mac系统运行servlet helloworld项目

一、写一个项目1.新建项目假设你已经安装了eclipse,新建一个命名为&#xff1a;servletHelloworld 的web项目2.创建类创建一个命名为&#xff1a; Hello 的类&#xff0c;写上包名&#xff1a;com.servletHelloworld添加代码如下&#xff1a;package com.servletHelloworld;imp…

php定义数据库配置,thinkphp3.2.2 没有定义数据库配置

出现这个问题&#xff0c;温习下tp配置多个数据库return array(//默认数据库‘DB_TYPE‘ > ‘mysql‘, // 数据库类型‘DB_HOST‘ > ‘localhost‘, // 服务器地址‘DB_NAME‘ > ‘thinkphp‘, // 数据库名‘DB_USER‘ > ‘root‘, // 用户名‘DB_PWD‘ > ‘roo…

【基础】RandomAccess

在List集合中&#xff0c;我们经常会用到ArrayList以及LinkedList集合&#xff0c;但是通过查看源码&#xff0c;就会发现ArrayList实现RandomAccess接口&#xff0c;但是RandomAccess接口里面是空的&#xff01;Linked并没有实现RandomAccess接口。 RandomAccess接口是一个标志…

oracle无法跟踪web发送的sql,如何通过跟踪客户端程序发出的sql的方法来优化SQL

简要说来&#xff0c;跟踪一个客户程序发出的SQL主要分成下面几步&#xff1a;1) 识别要跟踪的客户端程序到数据库的连接(后面都用session代替)&#xff0c;主要找出能唯一识别一个session的sid与serial#.2) 设定相应的参数&#xff0c;如打开时间开关(可以知道一个sql执行了多…

oracle的默认编码是,ORACLE 默认编码 GBK -UTF8编码

查看oracle数据库字符集&#xff1a;select userenv(language) from dual;SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 135337420 bytesFixed Size …

nodemailer + express + h5 拖拽文件上传 实现发送邮件

一、部署 1、部署Express 2、准备一个邮箱并开始SMTP服务 二、服务器端 三、客户端 四、效果&#xff1a; 转载于:https://www.cnblogs.com/cwxwdm/p/10601646.html