前言
create-react-app为首的脚手架众多,单页面打包通常指的是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口,但也有许多业务需要多个页面不同的入口,比如不同的h5活动,或者需要支持seo的官方网站,都需要多个不同的html
react16
-
webpack4
html-webpack-plugin 生成html文件
mini-css-extract-plugin css分离打包
uglifyjs-webpack-plugin js压缩
optimize-css-assets-webpack-plugin css压缩
es6
babel
-
node
opn 打开浏览器
compression 开启gzip压缩
express
fs
git
webpack单页面打包配置
webpack.config.js
module.exports = (env, argv) => ({ entry: ".src/index.js", output: { path: path.join(__dirname, "dist"), filename: "bundle.js" }, module: { rules: [ ... ], }, plugins: [ new HtmlWebpackPlugin({ title: "首页", filename:"index.html", favicon:"", template: "./src/template.html", }) ] });
这样就可以在dist
文件夹下打包出一个下面这样的文件
<!DOCTYPE html> <html lang="en"> <head> <title>首页</title> <body> <div id="root"></div> <script type="text/javascript" src="bundle.js"></script></body> </html>
webpack多页面打包配置
webpack 的entry支持两种种格式
打包单个文件
module.exports = { entry: '.src/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' } };
这样就会在dist下打包出一个bundle.js
打包出多个文件
module.exports = { entry: { index:"./src/index.js", about:"./src/about.js" }, output: { path: path.resolve(__dirname, 'dist'), filename: '[name].js' index.js,about.js这两个文件 } };
将每个js挂载到相应的html文件上
这里我们需要用到html-webpack-plugin
这个webpack插件,每添加一个页面就需要在plugins添加一个new HtmlWebpackPlugin({....})
const HtmlWebpackPlugin = require("html-webpack-plugin"); module.exports = (env, argv) => ({ entry: { index:"./src/index.js", about:"./src/about.js" }, output: { path: path.resolve(__dirname, 'dist'), filename: '[name].js' index.js,about.js这两个文件 } ....//其他配置 plugins: [ new HtmlWebpackPlugin( { filename:"index.html",//生成的index.html template: "./src/template.html",}) //模板 chunks:["index"] }), new HtmlWebpackPlugin( { filename:"about.html",//生成的index.html template: "./src/template.html",}) //模板 chunks:["index"] }) ] })
html-webpack-plugin
会通过 template.html
模板生成对应的filename名的html文件,并一并打包到output中对应的文件夹下,注意,在没有特殊配置的情况下所有打包的文件都是对应到output中 path
这个目录下,也包括html。这里的 chunks
需要注意,它是确定该html需要引入哪个js,如果没写的话,默认会引出所有打包的js,当然这不是我们想要的。
上面的配置最终可以在dist下打包出下面的文件结构
|-- dist
|-- index.js
|-- about.js
|-- index.html //内挂载index.js
|-- about.html //内挂载about.js
通过上面这样的配置,再加上devServer,我们已经可以实现多页面的配置开发了,但这样很不智能,因为你每增加一个页面,就要在wepback里面配置一次,会非常繁琐,所以我们来优化下,让我们只专注于开发页面,配置交给webpack自己
html-webpack-plugin自动配置
因为每个页面都需要配置一个html,而且每个页面的标题,关键字,描述等信息可能不同,所以我们在每个页面文件夹下创建一个pageinfo.json,通过fs模块获取到json里信息再遍历到对应得html-webpack-plugin中生成一个html插件数组。
index/pageinfo.json 生成index.html页面信息 { "title":"首页", "keywords":"webpack多页面" } about/pageinfo.json 生成about.html页面信息供 { "title":"关于页面", "keywords":"webpack多页面关于页面" }
通过fs遍历读取并生成HtmlWebpackPlugin数组供webpack使用
const fs = require("fs"); const HtmlWebpackPlugin = require("html-webpack-plugin");//生成html文件 const getFilePath = require("./getFilepath"); let htmlArr = []; getFilePath("./src").map((item)=>{ let infoJson ={},infoData={}; try{ // 读取pageinfo.json文件内容,如果在页面目录下没有找到pageinfo.json 捕获异常 infoJson = fs.readFileSync(`src/${item}/pageinfo.json`,"utf-8");// infoData = JSON.parse(infoJson); }catch(err){ infoData = {}; } htmlArr.push(new HtmlWebpackPlugin({ title:infoData.title ? infoData.title : "webpack,react多页面架构", meta:{ keywords: infoData.keywords ? infoData.keywords : "webpack,react,github", description:infoData.description ? infoData.description : "这是一个webpack,react多页面架构" }, chunks:[`${item}/${item}`], //引入的js template: "./src/template.html", filename : item == "index" ? "index.html" : `${item}/index.html`, //html位置 minify:{//压缩html collapseWhitespace: true, preserveLineBreaks: true }, })); }); module.exports = htmlArr;
wbpack终极配置
const path = require("path"); const getEntry = require("./webpackConfig/getEntry"); //入口配置 const entry = getEntry("./src"); const htmlArr =require("./webpackConfig/htmlConfig");// html配置 module.exports = (env, argv) => ({ entry: entry output: { path: path.resolve(__dirname, 'dist'), filename: '[name].js' } ....//其他配置 devServer: { port: 3100, open: true, }, plugins: [ ...htmlArr ] })
这样一个自动化完整的多页面架构配置就完成了,如果我们要新创建一个页面
- 在src下创建一个文件目录
- 在新创建的文件目录下添加
index.js
(必须,因为是webpack打包入口文件)
- 在新创建的文件目录下添加
- 在新创建文件夹下添加
pageinfo.json
(非必须) 供html插件使用
- 在新创建文件夹下添加
-
npm run dev
开发
-
这样就可以是多页面的形成了