webpack4 多页面打包

我会带着你远行 2021-07-24 11:00 588阅读 0赞
  1. 方式一:
  2. entry修改为对象形式的多入口方式,每个入口对应对应一个html-webpack-plugin
  3. 通过html-webpack-plugin中的chunks属性,引入entry中对应的chunk
  4. 方式二:
  5. 在方法中通过globfs来匹配指定文件夹下的文件,遍历生成webpack对应的entryhtml-webpack-plugin配置
  6. 方式一:
  7. 通过glob匹配目录后通过正则得到文件夹名称
  8. 方式二:
  9. 通过fs模块来获取文件信息
  10. const files = fs.readdirSync('./src') 获取路径下的所有文件名
  11. files.forEach(function (item, index) {
  12. let stat = fs.lstatSync("./src/" + item) 获取路径中的文件信息
  13. if (stat.isDirectory() === true) {
  14. 是文件夹的情况
  15. }
  16. })
  17. 通过path.dirname解析绝对路径获取到的文件夹名称会包含绝对路径的信息,所以使用fs

代码示例:

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  4. const HTMLInlineCSSWebpackPlugin = require ( "html-inline-css-webpack-plugin" ). default ;
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const glob = require('glob');
  7. const fs = require('fs');
  8. //多页面配置entry和htmlWebpackPlugin
  9. const setOptions = ()=>{
  10. const entry = { };
  11. const htmlWebpackPlugin = [];
  12. const entryFiles = glob.sync(path.resolve(__dirname, './src/*/index.js'))
  13. const fileReg = /src\/(.*)\/index\.js/;
  14. entryFiles.forEach((filePath, index) => {
  15. const fileName = filePath.match(fileReg);
  16. entry[fileName[1]] = filePath;
  17. htmlWebpackPlugin.push(
  18. new HtmlWebpackPlugin({
  19. template: `./src/${ fileName[1]}/index.html`,
  20. filename:fileName[1]+'.html',
  21. chunks:[fileName[1]]
  22. })
  23. )
  24. })
  25. return {
  26. entry,
  27. htmlWebpackPlugin
  28. }
  29. }
  30. module.exports = {
  31. // entry: './src/index.js',
  32. entry: setOptions().entry,
  33. output: {
  34. path: path.resolve(__dirname, 'dist'),
  35. filename:'[name]_[chunkhash:8].js'
  36. },
  37. module: {
  38. rules: [
  39. {
  40. test: /\.js$/,
  41. use:['babel-loader']
  42. },
  43. {
  44. test: /\.css$/,
  45. use: [
  46. MiniCssExtractPlugin.loader,
  47. 'css-loader',
  48. {
  49. loader: "postcss-loader",
  50. options: {
  51. postcssOptions: {
  52. plugins: [
  53. [
  54. "autoprefixer",
  55. {
  56. overrideBrowserslist: [
  57. "Android 4.1",
  58. "iOS 7.1",
  59. "Chrome > 31",
  60. "ff > 31",
  61. "ie >= 8",
  62. "> 1%", // 必须大于 1% 用户使用的浏览器
  63. //'last 2 versions', // 所有主流浏览器最近的 2个版本
  64. ],
  65. },
  66. ],
  67. ],
  68. },
  69. },
  70. },
  71. {
  72. loader: 'px2rem-loader',
  73. options: {
  74. remUnit: 75, //初始稿的10分之一,代表设计稿为750,1rem=75px,
  75. remPrecision:8, //精确的小数点位数
  76. }
  77. }
  78. ]
  79. },
  80. {
  81. test: /\.txt$/i,
  82. use: [
  83. {
  84. loader: 'raw-loader',
  85. options: {
  86. esModule: false,
  87. },
  88. },
  89. ],
  90. }
  91. ]
  92. },
  93. mode: "development",
  94. plugins: [
  95. ...setOptions().htmlWebpackPlugin,
  96. new MiniCssExtractPlugin({ filename:'css/build.css'}),
  97. new HTMLInlineCSSWebpackPlugin(),
  98. new CleanWebpackPlugin()
  99. ]
  100. }

发表评论

表情:
评论列表 (有 0 条评论,588人围观)

还没有评论,来说两句吧...

相关阅读