使用webpack搭建React

安装全局包

1
2
3
npm install babel -g
npm install webpack -g
npm install webpack-dev-server -g

创建根目录

创建一个根目录,目录名为:reactApp,再使用 npm init 初始化,生成 package.json 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 mkdir reactApp
cd reactApp/
npm init
name: (reactApp) youj-react-test
version: (1.0.0)
description: 成为厉害男人之React篇
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/laolan/www/reactApp/package.json:

{
"name": "youj-react-test",
"version": "1.0.0",
"description": "成为厉害男人之React篇",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}


Is this ok? (yes)

添加依赖包及插件

因为我们要使用 React, 所以我们需要先安装它,—save 命令用于将包添加至 package.json 文件。

1
2
npm install react --save
npm install react-dom --save

同时我们也要安装一些 babel 插件

1
2
3
4
npm install babel-core
npm install babel-loader
npm install babel-preset-react
npm install babel-preset-es2015

创建文件

1
2
3
4
touch index.html
touch App.jsx
touch main.js
touch webpack.config.js

注:touch是linux下创建新文件的命令,在Windows中可以使用ni命令替换touch命令。即

1
2
3
4
ni index.html
ni App.jsx
ni main.js
ni webpack.config.js

设置编译器,服务器,载入器

打开 webpack.config.js 文件添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var config = {
entry: './main.js',

output: {
path:'./',
filename: 'index.js',
},

devServer: {
inline: true,
port: 7777
},

module: {
loaders: [ {
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',

query: {
presets: ['es2015', 'react']
}
}]
}

}

module.exports = config;
  • entry: 指定打包的入口文件 main.js。
  • output:配置打包结果,path定义了输出的文件夹,filename则定义了打包结果文件的名称。
  • devServer:设置服务器端口号为 7777,端口后你可以自己设定 。
  • module:定义了对模块的处理逻辑,这里可以用loaders定义了一系列的加载器,以及一些正则。当需要加载的文件匹配test的正则时,就会调用后面的loader对文件进行处理,这正是webpack强大的原因。

现在打开 package.json 文件,找到 ​”scripts” ​中的​ “test” “echo \”Error: no test specified\” && exit 1”​ 使用以下代码替换:

1
"start": "webpack-dev-server --hot"

替换后的 package.json 文件 内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"name": "youj-react-test",
"version": "1.0.0",
"description": "W3Cschool教程 react 测试",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}

现在我们可以使用​ npm start ​命令来启动服务。​—hot ​命令会在文件变化后重新载入,这样我们就不需要在代码修改后重新刷新浏览器就能看到变化。

index.html

设置​<div id = "app">​ 为我们应用的根元素,并引入 index.js 脚本文件。

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>React App - W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<div id = "app"></div>
<script src = "index.js"></script>
</body>
</html>

App.jsx 和 main.js

App.jsx文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from 'react';

class App extends React.Component {
render() {
return (
<div>
Hello World!!!<br />
YYBK 启动!!!
</div>
);
}
}

export default App;

我们需要引入组件并将其渲染到根元素 App 上,这样我们才可以在浏览器上看到它。

main.js文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from 'react';

class App extends React.Component {
render() {
return (
<div>
Hello World!!!<br />
YYBK
</div>
);
}
}

export default App;

注意:
如果想要组件可以在任何的应用中使用,需要在创建后使用 export 将其导出,在使用组件的文件使用 import 将其导入。

运行服务

完成以上配置后,我们即可运行该服务:

1
npm start

如果以上报错 可以下载脚手架(推荐)

安装脚手架

1
npm install -g create-react-app

创建脚手架 (TS版)

1
npx create-react-app <项目名称> -template typescript

创建完成后 进入目录

1
npm install 

安装完成后 React 启动!!

1
npm start