经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
使用Webpack构建多页面程序
来源:cnblogs  作者:Tuzilow  时间:2021/3/24 9:08:10  对本文有异议

使用webpack搭建单页面程序十分常见,但在实际开发中我们可能还会有开发多页面程序的需求,因此我研究了一下如何使用webpack搭建多页面程序。

原理

将每个页面所在的文件夹都看作是一个单独的单页面程序目录,配置多个entry以及html-webpack-plugin即可实现多页面打包。

下面为本项目目录结构

  1. .
  2. ├─ src
  3. └─ pages
  4. ├─ about
  5. ├─ index.css
  6. ├─ index.html
  7. └─ index.js
  8. └─ index
  9. ├─ index.css
  10. ├─ index.html
  11. └─ index.js
  12. └─ webpack.config.js

单页面打包基础配置

首先我们来看一下单页面程序的 webpack 基础配置

  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. module.exports = {
  3. entry: './src/index.js',
  4. plugins: [
  5. new HtmlWebpackPlugin({
  6. template: './src/index.html',
  7. filename: 'index.html',
  8. }),
  9. ],
  10. output: {
  11. path: path.resolve(__dirname, './dist'),
  12. filename: 'bundle.js',
  13. },
  14. };

要想将其改为多页面程序,就要将它的单入口和单 HTML 模板改为多入口和多 HTML 模板

多页面打包基础配置

改造入口

传统的多入口写法可以写成键值对的形式

  1. module.exports = {
  2. entry: {
  3. index: './src/pages/index/index.js',
  4. about: './src/pages/about/index.js',
  5. },
  6. ...
  7. }

这样写的话,每增加一个页面就需要手动添加一个入口,比较麻烦,因此我们可以定义一个根据目录生成入口的函数来简化我们的操作

  1. const glob = require('glob');
  2. function getEntry() {
  3. const entry = {};
  4. glob.sync('./src/pages/**/index.js').forEach((file) => {
  5. const name = file.match(/\/pages\/(.+)\/index.js/)[1];
  6. entry[name] = file;
  7. });
  8. return entry;
  9. }
  10. module.exports = {
  11. entry: getEntry(),
  12. ...
  13. }

改造输出

在输出的配置项中,再将输出的文件名写死显示已经不合适了,因此我们要将名字改为与源文件相匹配的名字

  1. module.exports = {
  2. ...
  3. output: {
  4. path: path.resolve(__dirname, './dist'),
  5. filename: 'js/[name].[contenthash].js',
  6. },
  7. ...
  8. }

配置多个 html-webpack-plugin

与入口相同,可以将不同的 html 模板直接写入插件配置中,这里我们需要为每个插件配置不同的chunks,防止 js 注入到错误的 html 中

  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. module.exports = {
  3. ...
  4. plugins: [
  5. new HtmlWebpackPlugin({
  6. template: './src/pages/index/index.html',
  7. chunks: ['index'],
  8. filename: 'index.html',
  9. }),
  10. new HtmlWebpackPlugin({
  11. template: './src/pages/about/index.html',
  12. chunks: ['about'],
  13. filename: 'about.html',
  14. }),
  15. ],
  16. ...
  17. };

这样的做法与入口有着同样的毛病,因此我们再定义一个函数来生成这个配置

  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. const glob = require('glob');
  3. function getHtmlTemplate() {
  4. return glob
  5. .sync('./src/pages/**/index.html')
  6. .map((file) => {
  7. return { name: file.match(/\/pages\/(.+)\/index.html/)[1], path: file };
  8. })
  9. .map(
  10. (template) =>
  11. new HtmlWebpackPlugin({
  12. template: template.path,
  13. chunks: [template.name.toString()],
  14. filename: `${template.name}.html`,
  15. })
  16. );
  17. }
  18. module.exports = {
  19. ...
  20. plugins: [...getHtmlTemplate()],
  21. ...
  22. };

这样一个简单的多页面项目就配置完成了,我们还可以在此基础上添加热更新、代码分割等功能,有兴趣的可以自己尝试一下

完整配置

项目地址:xmy6364/webpack-multipage

  1. // webpack.config.js
  2. const path = require('path');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const glob = require('glob');
  5. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  6. // 多页入口
  7. function getEntry() {
  8. const entry = {};
  9. glob.sync('./src/pages/**/index.js').forEach((file) => {
  10. const name = file.match(/\/pages\/(.+)\/index.js/)[1];
  11. entry[name] = file;
  12. });
  13. return entry;
  14. }
  15. // 多页模板
  16. function getHtmlTemplate() {
  17. return glob
  18. .sync('./src/pages/**/index.html')
  19. .map((file) => {
  20. return { name: file.match(/\/pages\/(.+)\/index.html/)[1], path: file };
  21. })
  22. .map(
  23. (template) =>
  24. new HtmlWebpackPlugin({
  25. template: template.path,
  26. chunks: [template.name.toString()],
  27. filename: `${template.name}.html`,
  28. })
  29. );
  30. }
  31. const config = {
  32. mode: 'production',
  33. entry: getEntry(),
  34. output: {
  35. path: path.resolve(__dirname, './dist'),
  36. filename: 'js/[name].[contenthash].js',
  37. },
  38. module: {
  39. rules: [
  40. {
  41. test: /\.css$/,
  42. use: ['style-loader', 'css-loader'],
  43. },
  44. ],
  45. },
  46. plugins: [new CleanWebpackPlugin(), ...getHtmlTemplate()],
  47. devServer: {
  48. contentBase: path.join(__dirname, 'dist'),
  49. compress: true,
  50. port: 3000,
  51. hot: true,
  52. open: true,
  53. },
  54. };
  55. module.exports = config;

原文链接:http://www.cnblogs.com/xueyubao/p/14539869.html

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

本站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号