经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » webpack » 查看文章
webpack4+react多页面架构的实现
来源:jb51  时间:2018/10/26 9:46:19  对本文有异议

webpack在单页面打包上应用广泛,以create-react-app为首的脚手架众多,单页面打包通常是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口,但也有许多业务需要多个页面不同的入口,比如不同的h5活动,或者需要支持seo的官方网站,都需要多个不同的html,webpack-react-multi-page架构让你可以实现多页面架构,在项目开发中保证每个页面都可以热更新并且打包后有清晰的文件层次结构。

Github地址

项目架构

技术使用

  • 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

目录结构github

  1. |-- webpack-react-multi-page //项目
  2. |-- dist //编译生产目录
  3. |-- index
  4. |-- index.css
  5. |-- index.js
  6. |-- about
  7. |-- about.css
  8. |-- about.js
  9. |-- images
  10. |-- index.html
  11. |-- about.html
  12. |-- node_modules //node包
  13. |-- src //开发目录
  14. |-- index //index页面打包入口
  15. |-- images/
  16. |-- app.js// index业务js
  17. |-- index.scss
  18. |-- index.js //index页面js入口
  19. |-- about //about页面打包入口
  20. |-- images/
  21. |-- app.js// about业务js
  22. |-- index.scss
  23. |-- index.js //about页面js入口
  24. |-- template.html // html模板
  25. |-- style.scss //公共scss
  26. |-- webpackConfig //在webpack中使用
  27. |-- getEntry.js //获取入口
  28. |-- getFilepath.js //遍历文件夹
  29. |-- htmlconfig.js //每个页面html注入数据
  30. |-- package.json
  31. |-- .gitignore
  32. |-- webpack.config.js //webpack配置文件
  33. |-- www.js //生产启动程序

wiki

webpack单页面打包配置

webpack.config.js

  1. module.exports = (env, argv) => ({
  2. entry: ".src/index.js",
  3. output: {
  4. path: path.join(__dirname, "dist"),
  5. filename: "bundle.js"
  6. },
  7. module: {
  8. rules: [
  9. ...
  10. ],
  11. },
  12. plugins: [
  13. new HtmlWebpackPlugin({
  14. title: "首页",
  15. filename:"index.html",
  16. favicon:"",
  17. template: "./src/template.html",
  18. })
  19. ]
  20. });

这样就可以在dist文件夹下打包出一个下面这样的文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>首页</title>
  5. <body>
  6. <div id="root"></div>
  7. <script type="text/javascript" src="bundle.js"></script>
  8. </body>
  9. </html>

webpack多页面打包配置

webpack 的entry支持两种种格式

打包单个文件

  1. module.exports = {
  2. entry: '.src/file.js',
  3. output: {
  4. path: path.resolve(__dirname, 'dist'),
  5. filename: 'bundle.js'
  6. }
  7. };

在dist下打包出一个bundle.js

打包出多个文件

  1. module.exports = {
  2. entry: {
  3. index:"./src/index.js",
  4. about:"./src/about.js"
  5. },
  6. output: {
  7. path: path.resolve(__dirname, 'dist'),
  8. filename: '[name].js'
  9. }
  10. };

上面在dist下打包出两个与entry属性名对应的index.js,about.js这两个文件

将每个js挂载到相应的html文件上

这里我们需要用到html-webpack-plugin这个webpack插件,每添加一个页面就需要在plugins添加一个new HtmlWebpackPlugin({....})

  1. const HtmlWebpackPlugin = require("html-webpack-plugin");
  2. module.exports = (env, argv) => ({
  3. entry: {
  4. index:"./src/index.js",
  5. about:"./src/about.js"
  6. },
  7. output: {
  8. path: path.resolve(__dirname, 'dist'),
  9. filename: '[name].js'
  10. }
  11. ....//其他配置
  12. plugins: [
  13. new HtmlWebpackPlugin(
  14. {
  15. filename:"index.html",//生成的index.html
  16. template: "./src/template.html",}) //模板
  17. chunks:["index"]
  18. }),
  19. new HtmlWebpackPlugin(
  20. {
  21. filename:"about.html",//生成的index.html
  22. template: "./src/template.html",}) //模板
  23. chunks:["about"]
  24. })
  25. ]
  26. })

html-webpack-plugin会通过template.html模板生成对应的filename名的html文件,并一并打包到output中对应的文件夹下,注意,所有打包的文件都是对应到output中path这个目录下,也包括html。这里的chunks需要注意,它是确定该html需要引入哪个js,如果没写的话,默认会引出所有打包的js,当然这不是我们想要的。
上面的配置最终可以在dist下打包出下面的文件结构

  1. |-- dist
  2. |-- index.js
  3. |-- about.js
  4. |-- index.html //内挂载index.js
  5. |-- about.html //内挂载about.js

通过上面这样的配置,再加上devServer,我们已经可以实现多页面的配置开发了,但这样很不智能,因为你每增加一个页面,就要在wepback里面配置一次,会非常繁琐,所以我们来优化下,让我们只专注于开发页面,配置交给webpack.

wehbpack多页面配置优化

我们看下src下面的文件结构

  1. |-- src
  2. |-- index
  3. |-- app.js
  4. |-- index.scss
  5. |-- index.js
  6. |-- about
  7. |-- app.js
  8. |-- index.scss
  9. |-- index.js

src下面每个文件夹对应一个html页面的js业务,如果我们直接把文件夹对应入口js找到并把他们合并生成对应的entry,那是不是就不用手动写entry了呢,再把对应的html-webpack-plugin通过src下目录遍历出来,就可以生成对应的页面。

这样一个完整的多页面架构配置就完成了,完整代码参考项目code

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号