We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
创建工作目录, hello-react-webpack:
hello-react-webpack
mkdir hello-react-webpack cd hello-react-webpack yarn init
yarn add webpack --dev
创建 webpack.config.js :
webpack.config.js
touch webpack.config.js
配置基本入口,出口:
const path = require('path'); module.exports = { entry: "./src/index.js", output: { path: path.resolve("dist"), // 根据当前路径生成,dist绝对路径 filename: "bundle.js" } }
yarn add @babel/preset-react @babel/preset-env @babel/core babel-loader@8.0.0-beta.0 --dev
创建 .babelrc 文件,加入如下代码:
.babelrc
{ "presets": ["@babel/preset-env", "@babel/preset-react"] }
配置 webpack.config.js :
const path = require('path'); module.exports = { entry: "./src/index.js", output: { path: path.resolve("dist"), filename: "bundle.js", public: "/" }, + module: { + rules: [ + { + test: /\.js$/, + use: "babel-loader" + } + ] + } }
下载 react 、react-dom:
react
react-dom
yarn add react react-dom
创建 src/index.js
src/index.js
mkdir src touch src/index.js
在 index.js 中,编写如下代码:
index.js
import React from "react"; import ReactDOM from "react-dom"; ReactDOM.render( <h1>测试</h1>, document.getElementById("root") );
下载插件 html-webpack-plugin:
html-webpack-plugin
yarn add html-webpack-plugin --dev
在 webpack.config.js 中配置 plugins:
plugins
const path = require('path'); + const HTMLWebpackPlugin = require("html-webpack-plugin"); module.exports = { /* ... */ + plugins: [ + new HTMLWebpackPlugin({ + template: "index.html", + filename: "index.html", + inject: "body" + }) + ] }
在根目录创建 index.html
index.html
touch index.html
编写如下内容:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="root"></div> </body> </html>
安装 wepack-dev-server:
wepack-dev-server
yarn add webpack-dev-server --dev
因为使用 webpack-dev-server 会涉及到热更新,因此需要在 webpack.config.js 中添加插件:
webpack-dev-server
const path = require('path'); const HTMLWebpackPlugin = require("html-webpack-plugin"); + const webpack = require("webpack"); module.exports = { /* ... */ plugins: [ new HTMLWebpackPlugin({ template: "index.html", filename: "index.html", inject: "body" }), + new webpack.NamedModulesPlugin(), + new webpack.HotModuleReplacementPlugin() ] }
并配置 inline 和 hot
inline
hot
devServer: { inline: true, hot: true }
安装 style-loader 及 css-loader :
style-loader
css-loader
yarn add style-loader css-loader --dev
在 webpack.config.js 中的 rules 添加
rules
module: { rules: [ { test: /\.js$/, use: "babel-loader" }, { test: /\.css$/, use: [ "style-loader", "css-loader" ] } ] }
package.json
scripts
在 package.json 中添加如下代码:
"scripts": { "start": "webpack-dev-server" },
最后, 执行 start 即可:
start
yarn start
The text was updated successfully, but these errors were encountered:
No branches or pull requests
配置
初始化
创建工作目录,
hello-react-webpack
:配置基本 webpack
创建
webpack.config.js
:配置基本入口,出口:
配置 babel v7
创建
.babelrc
文件,加入如下代码:配置 webpack.config.js :
配置 React
下载
react
、react-dom
:创建
src/index.js
在
index.js
中,编写如下代码:配置 html-webpack-plugin
下载插件
html-webpack-plugin
:在
webpack.config.js
中配置plugins
:在根目录创建
index.html
编写如下内容:
配置 webpack-dev-server
安装
wepack-dev-server
:因为使用
webpack-dev-server
会涉及到热更新,因此需要在webpack.config.js
中添加插件:并配置
inline
和hot
配置 CSS
安装
style-loader
及css-loader
:在
webpack.config.js
中的rules
添加配置
package.json
的scripts
在
package.json
中添加如下代码:最后, 执行
start
即可:The text was updated successfully, but these errors were encountered: