不久前寫了一篇webpack 4 升級(jí)遷移 這里簡(jiǎn)單說下 React 項(xiàng)目的一些配置;
首先我們新建項(xiàng)目 react-web ,然后進(jìn)入項(xiàng)目初始化 package.json ;
cd react-web && npm init
接下來(lái)我們安裝 webpack ,這里需要注意一點(diǎn),是命令行已經(jīng)單獨(dú)提取出來(lái)了,我們不僅需要安裝 webpack 還需要安裝 webpack-cli 。
npm i webpack webpack-cli --save-dev
接下來(lái)我們開始安裝和 react 相關(guān)的依賴包;
npm i @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
接下來(lái)我們初始化 babel 的配置文件。
touch .babelrc
然后把下面內(nèi)容粘貼進(jìn)去;
{ "presets": ["@babel/preset-env", "@babel/preset-react"] }
實(shí)際上 webpack 4 是可以支持無(wú)配置構(gòu)建的,但是我們還是按照傳統(tǒng)的方式寫一下配置文件;我們新建 webpack.config.js
module.exports = { module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader" } } ] } };
接下來(lái)我們開始 做點(diǎn)和 React 組件相關(guān)的東西;
npm i react-dom react prop-types --save-dev
我們?cè)?src 下新建目錄 components ;
我們建立一個(gè) Header.js 和 Footer.js .
Footer.js
import React, { Component } from "react"; class Footer extends Component { constructor() { super(); this.state = { year: "2018" }; } render() { return ( <footer class="ft"> copyright © {this.state.year} </footer> ); } } export default Footer;
Header.js
import React, { Component } from "react"; import ReactDOM from "react-dom"; class Header extends Component { constructor() { super(); this.state = { year: "2018" }; } render() { return ( <header class="hd"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Logo</a> <ul class="pull-right"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Link1</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Link2</a></li> </ul> </header> ); } } export default Header;
然后我們新增文件 src/index.js
import React, { Component } from "react"; import ReactDOM from "react-dom"; import Header from "../src/components/Header"; import Footer from "../src/components/Footer"; class ReactApp extends Component { constructor() { super(); } render() { return ( <div class="main"> <Header></Header> <Footer></Footer> </div> ); } } ReactDOM.render(<ReactApp />, document.getElementById('react-app')); export default ReactApp;
接下來(lái)我們需要預(yù)覽頁(yè)面,我們引入下 html-loader
npm i html-webpack-plugin html-loader --save-dev
修改我們的 webpack 配置:
const HtmlWebPackPlugin = require("html-webpack-plugin"); module.exports = { module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader" } }, { test: /\.html$/, use: [ { loader: "html-loader" } ] } ] }, plugins: [ new HtmlWebPackPlugin({ template: "./src/index.html", filename: "./index.html" }) ] };
我們?cè)?src 下新建 index.html 然后添加下面的內(nèi)容:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" rel="external nofollow" > <title>React & Webpack4</title> </head> <body> <div class="container"> <div class="row mt-5"> <div class="col-md-4 offset-md-1"> <p>Create a new article</p> <div id="react-app"> </div> </div> </div> </div> </body> </html>
接下來(lái)我們安裝 webpack-dev-server 在開發(fā)時(shí)用于起一個(gè)服務(wù)器預(yù)覽;
npm i webpack-dev-server --save-dev
接下來(lái)修改 npm scripts:
"scripts": { "start": "webpack-dev-server --open --mode development", "build": "webpack" }
差不多已經(jīng)完成一部分了,后面你就可以再安裝其他 react 套件了;
當(dāng)然如果覺得麻煩你也可以使用 yarn-react-webpack-seed 。
擴(kuò)展閱讀
https://www.valentinog.com/blog/react-webpack-babel/
https://reacttraining.com/react-router/web/api/HashRouter
https://github.com/JackPu/yarn-react-webpack-seed
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com