webpack學習(十一):構建多頁面應用
阿新 • • 發佈:2018-12-11
demo地址: https://github.com/Lkkkkkkg/webpack-demo
繼上一次配置完壓縮分離css : https://blog.csdn.net/qq593249106/article/details/84933978
先前單頁面目錄結構:
|- /dist
|- /css
|- main.css //分離出來的css檔案
|- bundle.js //入口檔案
|- index.html
|- /node_modules
|- /node_modules
|- /src //用於放原始檔的資料夾
|- /components
|- /index //模板檔案
| - index.html //模板檔案
|- index.js //入口檔案
|- App.js //react主檔案
|- package.json
|- webpack.config.js //webpack配置檔案
新增兩個頁面, 修改成如下多頁面結構:
|- /dist
|- /css
|- main.css //分離出來的css檔案
|- bundle.js //入口檔案
|- index.html
|- /node_modules
|- /src //用於放原始檔的資料夾
|- components //存放頁面的目錄
|- index //主頁
|- App. js
|- index.html
|- index.js
|- index.scss
|- otherA //另一個頁面A
|- App.js
|- otherA.html
|- otherA.js
|- otherA.scss
|- otherB //另一個頁面B
|- App.js
|- otherB.html
|- otherB.js
|- otherB.scss
|- package.json
|- webpack.config.js //webpack配置檔案
每個頁面添加了一個 App.js :
index頁面
App.js
import React from ‘react’
const App = () => (
Index
)
export default App
**index.html**
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
index.js
import React from 'react'
import { render } from 'react-dom'
import App from './App'
import './index.scss'
render(<App />, document.getElementById("root"))
index.scss
$color: green;
body {
background-color: $color;
}
otherA頁面
App.js
import React from 'react'
const App = () => (
<h1>
otherA<br />
</h1>
)
export default App
otherA.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>otherA</title>
</head>
<body>
<div id="root">
</div>
</body>
</html>
otherA.js
import React from 'react'
import { render } from 'react-dom'
import App from './App'
import './otherA.scss'
render(<App />, document.getElementById("root"))
otherA.scss
$color: red;
body {
background-color: $color;
}
otherB頁面
App.js
import React from 'react'
const App = () => (
<h1>
otherB<br />
</h1>
)
export default App
otherB.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>otherB</title>
</head>
<body>
<div id="root">
</div>
</body>
</html>
otherB.js
import React from 'react'
import { render } from 'react-dom'
import App from './App'
import './otherB.scss'
render(<App />, document.getElementById("root"))
otherB.scss
$color: yellow;
body {
background-color: $color;
}
配置 webpack.config.js
增加入口檔案, 同時修改出口檔案, 然後配置出口檔案新增一個 js 目錄專門用來存放 js 檔案, css 同理, 在 plugins 裡的 MiniCssExtractPlugin 外掛裡配置, 因為是多個頁面, 還需要新增 HtmlWebpackPlugin , 設定各個頁面的引數:
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
var devMode = false; //標誌是否開發模式
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
mode: 'development',
entry: { //入口檔案
index: "./src/components/index/index.js",
otherA: "./src/components/otherA/otherA.js",
otherB: "./src/components/otherB/otherB.js",
},
output: {
path: path.join(__dirname, 'dist'),
filename: 'js/[name].js', //根據入口檔案分為不同出口檔案
},
devtool: 'inline-source-map', // 不同選項適用於不同環境
devServer: {
contentBase: './dist', //將dist目錄下的檔案(index.html)作為可訪問檔案, 如果不寫這個引數則預設與webpack.cofig.js的同級目錄
port: 8080 //埠號設為8080, 預設也是8080
},
module: {
rules: [ //配置載入器, 用來處理原始檔, 可以把es6, jsx等轉換成js, sass, less等轉換成css
{
exclude: /node_modules|packages/, //路徑
test: /\.js$/, //配置要處理的檔案格式,一般使用正則表示式匹配
use: 'babel-loader', //使用的載入器名稱
},
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
]
},
plugins: [ //webpack 通過 plugins 實現各種功能, 比如 html-webpack-plugin 使用模版生成 html 檔案
new CleanWebpackPlugin(['dist']), //設定清除的目錄
new HtmlWebpackPlugin({
template: "./src/components/index/index.html", //設定生成的HTML檔案的名稱, 支援指定子目錄,如:assets/admin.html
chunks: ['index'], //指定入口檔案
filename: "index.html" //指定模板檔案的位置
}),
new HtmlWebpackPlugin({
template: "./src/components/otherA/otherA.html",
chunks: ['otherA'],
filename: "otherA.html"
}),
new HtmlWebpackPlugin({
template: "./src/components/otherB/otherB.html",
chunks: ['otherB'],
filename: "otherB.html"
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css', //類似出口檔案
chunkFilename: 'css/[id].css'
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g, //一個正則表示式,指示應優化\最小化的資產的名稱。提供的正則表示式針對配置中ExtractTextPlugin例項匯出的檔案的檔名執行,而不是源CSS檔案的檔名。預設為/\.css$/g
cssProcessor: require('cssnano'), //用於優化\最小化CSS的CSS處理器,預設為cssnano。這應該是一個跟隨cssnano.process介面的函式(接收CSS和選項引數並返回一個Promise)。
cssProcessorOptions: { safe: true, discardComments: { removeAll: true } }, //傳遞給cssProcessor的選項,預設為{}
canPrint: true //一個布林值,指示外掛是否可以將訊息列印到控制檯,預設為true
})
]
};
打包
終端輸入 npm run build, 可以看到目錄結構的變化:
|- /dist
|- /css //分離出來的css檔案
|- index.css
|- otherA.css
|- otherB.css
|- /js
|- index.js
|- otherA.js
|- otherB.js
|- index.html
|- otherA.html
|- otherB.html
|- /node_modules
|- /src //用於放原始檔的資料夾
|- components //存放頁面的目錄
|- index //主頁
|- App.js
|- index.html
|- index.js
|- index.scss
|- otherA //另一個頁面A
|- App.js
|- otherA.html
|- otherA.js
|- otherA.scss
|- otherB //另一個頁面B
|- App.js
|- otherB.html
|- otherB.js
|- otherB.scss
|- package.json
|- webpack.config.js //webpack配置檔案
分別開啟 index.html, otherA.html, otherB.html, 各自頁面設定的 scss 都生效了, 構建成功:
在這裡插入圖片描述