webpack在单页面打包上应用普遍,以create-react-app为首的脚手架众多,单页面打包一般是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口,但也有许多业务须要多个页面不一样的入口,好比不一样的h5活动,或者须要支持seo的官方网站,都须要多个不一样的html,webpack-react-multi-page架构让你能够实现多页面架构,在项目开发中保证每一个页面均可以热更新而且打包后有清晰的文件层次结构。javascript
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
git
|-- webpack-react-multi-page //项目 |-- dist //编译生产目录 |-- index |-- index.css |-- index.js |-- about |-- about.css |-- about.js |-- images |-- index.html |-- about.html |-- node_modules //node包 |-- src //开发目录 |-- index //index页面打包入口 |-- images/ |-- app.js// index业务js |-- index.scss |-- index.js //index页面js入口 |-- about //about页面打包入口 |-- images/ |-- app.js// about业务js |-- index.scss |-- index.js //about页面js入口 |-- template.html // html模板 |-- style.scss //公共scss |-- webpackConfig //在webpack中使用 |-- getEntry.js //获取入口 |-- getFilepath.js //遍历文件夹 |-- htmlconfig.js //每一个页面html注入数据 |-- package.json |-- .gitignore |-- webpack.config.js //webpack配置文件 |-- www.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
文件夹下打包出一个下面这样的文件css
<!DOCTYPE html> <html lang="en"> <head> <title>首页</title> <body> <div id="root"></div> <script type="text/javascript" src="bundle.js"></script> </body> </html>
webpack 的entry支持两种种格式html
module.exports = { entry: '.src/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' } };
在dist下打包出一个bundle.jsjava
module.exports = { entry: { index:"./src/index.js", about:"./src/about.js" }, output: { path: path.resolve(__dirname, 'dist'), filename: '[name].js' } };
上面在dist下打包出两个与entry属性名对应的index.js,about.js这两个文件node
这里咱们须要用到html-webpack-plugin
这个webpack插件,每添加一个页面就须要在plugins添加一个new HtmlWebpackPlugin({....})react
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' } ....//其余配置 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:["about"] }) ] })
html-webpack-plugin
会经过template.html
模板生成对应的filename名的html文件,并一并打包到output中对应的文件夹下,注意,全部打包的文件都是对应到output中path
这个目录下,也包括html。这里的chunks
须要注意,它是肯定该html须要引入哪一个js,若是没写的话,默认会引出全部打包的js,固然这不是咱们想要的。webpack
上面的配置最终能够在dist下打包出下面的文件结构git
|-- dist |-- index.js |-- about.js |-- index.html //内挂载index.js |-- about.html //内挂载about.js
经过上面这样的配置,再加上devServer,咱们已经能够实现多页面的配置开发了,但这样很不智能,由于你每增长一个页面,就要在wepback里面配置一次,会很是繁琐,因此咱们来优化下,让咱们只专一于开发页面,配置交给webpack.es6
咱们看下src下面的文件结构github
|-- src |-- index |-- app.js |-- index.scss |-- index.js |-- about |-- app.js |-- index.scss |-- index.js
src下面每一个文件夹对应一个html页面的js业务,若是咱们直接把文件夹对应入口js找到并把他们合并生成对应的entry,那是否是就不用手动写entry了呢,再把对应的html-webpack-plugin经过src下目录遍历出来,就能够生成对应的页面。
这样一个完整的多页面架构配置就完成了,完整代码参考项目code