webpack構(gòu)建react多頁面應(yīng)用詳解
寫這個的初衷是很難找一個簡潔的項目腳手架,很多腳手架都有很多依賴,光看依賴就要很久,所以自己參照網(wǎng)上的內(nèi)容,弄個這么一個簡單的多頁面的腳手架。
利用creat-react-app 新建一個react應(yīng)用
npm install -g create-react-app
然后創(chuàng)建一個項目
create-react-app demo
create-react-app會自動初始化一個腳手架并安裝 React 項目的各種必要依賴,如果在過程中出現(xiàn)網(wǎng)絡(luò)問題,請用cnpm淘寶鏡像安裝。
然后我們進入項目并啟動。
cd demo
然后啟動項目
npm start
那就會看到如下頁面

然后進入src/App.js,用下面代碼替換App.js中的代碼(因為在webpack中暫時不想處理圖片和icon)
import React, { Component } from 'react';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<h2>Welcome to App</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App
然后將 index.js 中的 內(nèi)容替換為如下代碼(刪除registerServiceWorker)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
然后刪除src下面的registerServiceWorker.js(該文件用于構(gòu)建pwa應(yīng)用用的,暫時我們用不了)和 logo.svg文件(不想處理圖片文件)和 App.test.js(用于測試用的)。
現(xiàn)在src/下面有四個文件。接下來,在src下面新建兩個文件夾 app1和 app2,分別將原來的四個文件拷貝進入app1和app2。文件目錄如下:

接下來,進入public文件下,刪除favicon.ico(不想處理)和 manifest.json(構(gòu)建pwa用的),然后將index.html中的內(nèi)容用如下內(nèi)容替換
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="theme-color" content="#000000"> <title>React App</title> </head> <body> <noscript> You need to enable JavaScript to run this app. </noscript> <div id="root"></div> </body> </html>
這個index.html就是我們的模版html。
進入正題,開始install webpack和配置webpack
1.安裝依賴。將package.json文件用下面的文件替代
{
"name": "demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.0.0",
"glob": "^7.1.2",
"html-webpack-plugin": "^2.30.1",
"postcss-loader": "^2.0.6",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.5.6",
"webpack-dev-server": "^2.8.1"
},
"scripts": {
"start": "webpack-dev-server --open",
"build": "webpack"
}
}
2.刪除當(dāng)前目錄中的node_modules,然后重新在控制臺執(zhí)行
npm i
3.在根目錄下也就是/demo下新建一個webpack.config.js文件,寫入如下代碼
const webpack = require('webpack');
const glob = require('glob');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpackConfig = {
entry: {},
output:{
path:path.resolve(__dirname, './dist/'),
filename:'[name].[chunkhash:6].js'
},
//設(shè)置開發(fā)者工具的端口號,不設(shè)置則默認(rèn)為8080端口
devServer: {
inline: true,
port: 8181
},
module:{
rules:[
{
test:/\.js?$/,
exclude:/node_modules/,
loader:'babel-loader',
query:{
presets:['es2015','react']
}
},
{
test: /\.(scss|sass|css)$/,
loader: ExtractTextPlugin.extract({fallback: "style-loader", use: "css-loader"})
},
]
},
plugins: [
new ExtractTextPlugin("[name].[chunkhash:6].css"),
new CleanWebpackPlugin(
['dist'],
{
root: __dirname,
verbose: true,
dry: false
}
)
],
};
// 獲取指定路徑下的入口文件
function getEntries(globPath) {
const files = glob.sync(globPath),
entries = {};
files.forEach(function(filepath) {
const split = filepath.split('/');
const name = split[split.length - 2];
entries[name] = './' + filepath;
});
return entries;
}
const entries = getEntries('src/**/index.js');
Object.keys(entries).forEach(function(name) {
webpackConfig.entry[name] = entries[name];
const plugin = new HtmlWebpackPlugin({
filename: name + '.html',
template: './public/index.html',
inject: true,
chunks: [name]
});
webpackConfig.plugins.push(plugin);
})
module.exports = webpackConfig;
4.然后用直接執(zhí)行如下代碼
npm run build
成功在dist中看到你的兩個頁面app1和app2.
如果要自己調(diào)試用直接啟用npm run start,然后在localhost:8181/app1.html查看頁面進行調(diào)試。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React引入css的幾種方式及應(yīng)用小結(jié)
這篇文章主要介紹了React引入css的幾種方式及應(yīng)用小結(jié),操作styled組件的樣式屬性,可在組件標(biāo)簽上定義屬性、也可以通過attrs定義屬性,本文通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
React特征學(xué)習(xí)Form數(shù)據(jù)管理示例詳解
這篇文章主要為大家介紹了React特征學(xué)習(xí)Form數(shù)據(jù)管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
React調(diào)度系統(tǒng)Scheduler工作原理詳解
這篇文章主要為大家介紹了React調(diào)度系統(tǒng)Scheduler工作原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
React?Native中8081端口號被占用問題的原因及解決方案
隨著移動應(yīng)用開發(fā)的興起,React?Native?已成為一種流行的開發(fā)框架,然而,在開發(fā)過程中,常常會遇到?8081?端口被占用的問題,導(dǎo)致無法訪問?Metro?Bundler,本文將詳細(xì)探討如何解決這一問題,包括原因、解決方案及相關(guān)最佳實踐,需要的朋友可以參考下2025-06-06
React項目配置axios和反向代理和process.env環(huán)境配置等問題
這篇文章主要介紹了React項目配置axios和反向代理和process.env環(huán)境配置等問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

