Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

webpack.prod.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const path = require("path");
  2. const webpack = require('webpack');
  3. const TerserPlugin = require("terser-webpack-plugin");
  4. const CopyWebpackPlugin = require("copy-webpack-plugin");
  5. const {
  6. CleanWebpackPlugin
  7. } = require("clean-webpack-plugin");
  8. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  9. const HtmlWebpackPlugin = require("html-webpack-plugin");
  10. const HappyPack = require('happypack');
  11. const os = require('os');
  12. const happyThreadPool = HappyPack.ThreadPool({
  13. size: os.cpus().length
  14. });
  15. const common = require("./common");
  16. const pathVars = require("./pathVars");
  17. const pageVars = require("./pageVars");
  18. //---------------------------------- output -----------------------------------//
  19. const packageJson = require("./../package.json");
  20. const entryConfig = {};
  21. let pages = pageVars.getPages();
  22. console.log("pages", pages);
  23. pages.forEach(function (page) {
  24. entryConfig[page["chunkName"]] = page["entryJs"];
  25. });
  26. //---------------------------------- output -----------------------------------//
  27. let outputConfig = {
  28. path: pathVars.distPath,
  29. publicPath: "./", // 表示资源的发布地址,当配置过该属性后,打包文件中所有通过相对路径引用的资源都会被配置的路径所替换(如:css中背景图的路径)
  30. filename: "[name]/index.js",
  31. assetModuleFilename: 'images/[name]_[hash][ext][query]'
  32. //chunkFilename: '[name]/index_[hash].chunk.js'
  33. };
  34. let pluginConfig = [
  35. new CleanWebpackPlugin({
  36. cleanOnceBeforeBuildPatterns: [pathVars.distPath + '/*'],
  37. }),
  38. // new webpack.ProvidePlugin({
  39. // 'THREE': require.resolve('three')
  40. // }),
  41. new MiniCssExtractPlugin({
  42. filename: "[name]/index.css",
  43. }),
  44. new HappyPack({
  45. id: 'babel',
  46. loaders: ["babel-loader"],
  47. threadPool: happyThreadPool
  48. }),
  49. new CopyWebpackPlugin({
  50. patterns: [{
  51. from: pathVars.devPath,
  52. to: pathVars.distPath
  53. }]
  54. })
  55. ];
  56. pages.forEach(function (page) {
  57. let htmlPlugin = new HtmlWebpackPlugin({
  58. title: 'page1',
  59. template: page["filePath"],
  60. filename: page["htmlName"],
  61. inject: 'body',
  62. chunks: [page["chunkName"]]
  63. //chunks: ["manifest", "vendor", "common", page["chunkName"]],
  64. //chunksSortMode: 'dependency'
  65. });
  66. pluginConfig.push(htmlPlugin);
  67. });
  68. module.exports = {
  69. mode: 'production',
  70. entry: entryConfig,
  71. output: outputConfig,
  72. resolve: common.resolve,
  73. module: {
  74. rules: [{
  75. test: /\.(css|less)$/,
  76. include: [pathVars.srcPath],
  77. exclude: [pathVars.nodeModulesPath],
  78. use: [{
  79. loader: MiniCssExtractPlugin.loader,
  80. options: {
  81. /* 解决css背景图路径问题 由于图片目录已经定好了,因此这里可以用固定的相对路径*/
  82. publicPath: '../'
  83. },
  84. },
  85. //"css-loader?modules&localIdentName=[name]__[local]-[hash:base64:5]",
  86. "css-loader",
  87. "less-loader"
  88. ],
  89. },
  90. {
  91. test: /\.tpl$/i,
  92. use: [{
  93. loader: 'raw-loader',
  94. options: {
  95. esModule: false,
  96. },
  97. }]
  98. },
  99. {
  100. test: /\.js$/,
  101. exclude: pathVars.nodeModulesPath,
  102. include: pathVars.srcPath,
  103. //use: ['babel-loader'],
  104. use: ["happypack/loader?id=babel"]
  105. },
  106. {
  107. test: /\.(png|jpg|gif|woff|svg|eot|ttf|woff2)$/,
  108. type: 'asset/resource',
  109. parser: {
  110. dataUrlCondition: {
  111. maxSize: 10 * 1024
  112. }
  113. }
  114. //利用HappyPack打包,图片会损坏。暂时不使用
  115. //use: ["happypack/loader?id=url"]
  116. },
  117. {
  118. test: /\.(glsl|vs|fs|vert|frag)$/,
  119. exclude: [pathVars.nodeModulesPath, pathVars.dllPath],
  120. use: ["raw-loader", "glslify-loader"]
  121. }
  122. ]
  123. },
  124. plugins: pluginConfig,
  125. optimization: {
  126. //minimize: true,
  127. minimizer: [
  128. new TerserPlugin()
  129. ],
  130. usedExports: true,
  131. runtimeChunk: 'single',
  132. splitChunks: {
  133. chunks: "all",
  134. cacheGroups: {
  135. vendor: {
  136. test: /[\\/]node_modules[\\/]/,
  137. name: 'vendor',
  138. chunks: 'all',
  139. priority: -10,
  140. reuseExistingChunk: true,
  141. },
  142. // 其他不是node_modules中的模块
  143. default: {
  144. name:"common",
  145. minChunks: 2, //如果有被引用不少于2次
  146. priority: -20,
  147. reuseExistingChunk: true,
  148. }
  149. }
  150. }
  151. }
  152. };