Git Product home page Git Product logo

react-dome's Introduction

react-dome

react项目搭建过程

1.安装webpack

yarn add webpack --save

1.1 新建 webpack.config.js、新建入口文件src/app.js、配置 webpack.config.js 入口及出口

const path = require('path');

	module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.js'
    }
};

1.2 执行 node_modules/.bin/webpack 即可在dist文件下查看app.js的打包文件

+ var HtmlWebpackPlugin = require('html-webpack-plugin');
  const path = require('path');

  module.exports = {
    ......
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
     ]
 };

1.3 添加loader,并添加module

yarn add [email protected] [email protected] [email protected] --dev

module.exports = {
...
module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env']
                    }
                }
            }
        ]
    }
...

2. react安装与配置

2.1 安装相关依赖

yarn add [email protected]

yarn add [email protected] [email protected]

2.2 修改文件 app.js-->app.jsx并添加配置文件

import React from 'react';
import ReactDOM from 'react-dom'
ReactDOM.render(
    <h1>hello</h1>,
    document.getElementById('app')
)
  • 修改webpack.config.js
 entry: './src/app.jsx',//修改入口文件
 module: {
        rules: [
            {
                test: /\.jsx$/,//匹配规则由js->jsx
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react']//添加react
                    }
                }
            }
        ]
    },
  • 重新打包 node_modules/.bin/webpack,在浏览器中查看 dist/index.html

页面访问

3. 添加样式解析配置webpack.config.js

3.1 配置css

yarn add [email protected] [email protected] --dev

 module: {
        rules: [
			...,
			{//添加样式loader
                test: /\.css$/,
                use: [
                    'style-loader', 'css-loader',
                ]
            }
        ]
    }

新建 src/index.css,在app.jsx引入import './index.css'

#app { color: pink; }

重新打包 node_modules/.bin/webpack刷新页面

添加样式打包

重新打包 node_modules/.bin/webpack刷新页面

查看打包后文件css的渲染要等所有js执行完成才加载,则有一段等待时间,怎么解决这个问题

css加载位置

按照官方配置修改webpack.config.js

//引入
const ExtractTextPlugin = require("extract-text-webpack-plugin");
//替换module->css
 rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
 //添加plugins
 plugins: [
	 ...,
  +  new ExtractTextPlugin("index.css"),
  ]

重新打包 node_modules/.bin/webpack,dist下新增了index.css文件,

添加了css引入<link href="index.css" rel="stylesheet">在浏览器中查看 dist/index.html样式已生效

3.2 配置sass

//添加scss解析
 rules: [
	 ...,
       {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ["css-loader", "sass-loader"]
                })
            }
    ]
  • 新增index.sass, 在index.jsx引入sass,重新打包 node_modules/.bin/webpack
//index.scss
body {
    background: beige;

    #app {
        font-size: 100px;
    }
}

效果如下:

sass

4.图片处理url-loader

温馨提示:使用url-loader处理小图片比较方便,会自动处理成base64

 rules: [
	 ...,
	 	{
                test: /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                        },
                    },
                ],
            },
	 ]
  • 添加image引用
body {
    background: url('./banner.jpg');
}

效果如下:

image

5. 添加字体库

  • 添加字体库 yarn add font-awesome 并在 app.jsx 中引入 font-awesome/css/font-awesome.min.css
import 'font-awesome/css/font-awesome.min.css'
ReactDOM.render(
    <div>
        <i className='fa fa-address-book'></i>
        <h1>hello</h1>
    </div>
    ,
    document.getElementById('app')
)
  • 添加字体打包配置
 rules: [
	 ...,
	 //字体图表的处理
            {
                test: /\.(woff|woff2|svg|ttf|eot|otf)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                        },
                    },
                ],
            },
	 ]

效果如下:

icon

6. 提出公共模块分出文件类型

var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const path = require('path');
const webpack = require('webpack')

module.exports = {
    entry: './src/app.jsx',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/app.js'
    },
    module: {
        rules: [
            //react语法处理
            {
                test: /\.jsx$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react']
                    }
                }
            },
            //css文件处理
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            //sass文件处理
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ["css-loader", "sass-loader"]
                })
            },
            //图片处理
            {
                test: /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: 'resourse/[name].[ext]'
                        },
                    },
                ],
            },
            //字体图表的处理
            {
                test: /\.(woff|woff2|svg|ttf|eot|otf)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: 'resourse/[name].[ext]'
                        },
                    },
                ],
            },

        ]
    },
    plugins: [
        //处理html文件
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        //独立css文件
        new ExtractTextPlugin("css/[name].css"),
        //提出公共模块
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',//手动指定的公共模块
            filename: 'js/base.js'
        })
    ]
};

发现在每次更新时都要执行 node_modules/.bin/webpack,很繁琐,则加入webpack-dev-server 时时更新修改, 步骤如下:

output: {
        path: path.resolve(__dirname, 'dist'),
     +   publicPath: '/dist/', //解决启动后,字体文件404错误
        filename: 'js/app.js'
    },
		...,
devServer: {

    }
  • 使用node_modules/.bin/webpack-dev-server 进入 http://localhost:8080/dist/ 则可以时时更新修改
  • 配置端口号
devServer: {
        port: 8022,
    }
  • package.json 配置打包命令
"scripts": {
    "dev": "node_modules/.bin/webpack-dev-server",
	//配置线上打包命令
    "dist": "node_modules/.bin/webpack -p"
  },

react-dome's People

Contributors

xinyuexiao avatar

Stargazers

xyz avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.