webpack.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. /*const HtmlWebpackInlineSourcePlugin=require('html-webpack-inline-source-plugin');*/
  4. const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const CopyWebpackPlugin = require('copy-webpack-plugin');
  7. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  8. const webpack = require('webpack');
  9. const glob = require('glob');
  10. const proxyHost = "http://173.18.12.195:5858";
  11. // const proxyHost = "http://172.16.8.60:5858";
  12. // const proxyHost = "http://172.16.8.64:5858";
  13. // const proxyHost = "http://173.18.12.195:5656";
  14. // const proxyHost = "http://192.168.2.241:5858";
  15. // const proxyHost = "http://192.168.4.222:5858";
  16. // const proxyHost = "http://192.168.3.117:5858";//铁钢
  17. // const proxyHost = "http://192.168.3.113:5858";//王峰
  18. let entries = { vendor: 'lodash' }, plugines = [];
  19. function getentries() {
  20. let entryFiles = glob.sync('./src/js/**/*.js')
  21. let htmls = glob.sync('./src/html/**/*.html');
  22. for (var i = 0; i < entryFiles.length; i++) {
  23. var filePath = entryFiles[i];
  24. var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'));
  25. entries[filename] = filePath;
  26. }
  27. for (var i = 0; i < htmls.length; i++) {
  28. var filePath = htmls[i];
  29. var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'));
  30. let conf = {
  31. filename: filename + '.html',
  32. template: filePath, // html模板路径
  33. inject: true,
  34. chunks: [filename, 'vendor', 'common', 'scrollBar'],
  35. hash: true, //防止缓存
  36. minify: {
  37. removeAttributeQuotes: true, //压缩 去掉引号
  38. removeComments: true, //移除HTML中的注释
  39. collapseWhitespace: true //删除空白符与换行符
  40. }
  41. };
  42. plugines.push(new HtmlWebpackPlugin(conf));
  43. }
  44. }
  45. getentries();
  46. module.exports = {
  47. entry: entries,
  48. output: {
  49. publicPath: '/', //这里要放的是静态资源CDN的地址
  50. path: path.resolve(__dirname, 'dist'),
  51. filename: 'js/[name].js'
  52. },
  53. resolve: {
  54. extensions: [".js", ".css", ".json"],
  55. alias: {
  56. '@': path.resolve('./src'),
  57. '@src': path.resolve('./src'),
  58. '@less': path.resolve('./src/css'),
  59. '@images': path.resolve('./src/images'),
  60. '@js': path.resolve('./src/js'),
  61. } //配置别名可以加快webpack查找模块的速度
  62. },
  63. plugins: [ //多入口的html文件用chunks这个参数来区分
  64. ...plugines,
  65. new CopyWebpackPlugin([{
  66. from: 'src/resource',
  67. to: path.resolve(__dirname, 'dist', 'resource'),
  68. flatten: true, //false会拷贝原始文件夹路径
  69. }]),
  70. new MiniCssExtractPlugin({
  71. filename: 'css/[name].css',
  72. chunkFilename: '[id].css'
  73. }),
  74. new webpack.HotModuleReplacementPlugin(),
  75. new CleanWebpackPlugin()
  76. ],
  77. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  78. minimizer: [
  79. new UglifyJsPlugin({
  80. uglifyOptions: {
  81. ie8: true,
  82. compress: {
  83. properties: false,
  84. warnings: false
  85. },
  86. mangle: {
  87. screw_ie8: false,
  88. except: ['e']
  89. },
  90. output: {
  91. beautify: true
  92. },
  93. sourceMap: false
  94. }
  95. })
  96. ],
  97. splitChunks: {
  98. cacheGroups: {
  99. commons: {
  100. chunks: "initial",
  101. name: "common",
  102. minChunks: 2,
  103. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  104. minSize: 0, // This is example is too small to create commons chunks
  105. reuseExistingChunk: true // 可设置是否重用该chunk
  106. }
  107. }
  108. }
  109. },
  110. module: {
  111. noParse: /WdatePicker/,
  112. rules: [{
  113. test: /.js$/,
  114. enforce: 'post',
  115. loader: 'es3ify-loader'
  116. },
  117. {
  118. test: /\.m?js$/,
  119. exclude: /(node_modules|bower_components)/,
  120. use: {
  121. loader: 'babel-loader',
  122. options: {
  123. presets: ['@babel/preset-env'],
  124. //plugins:["@babel/plugin-transform-runtime"]
  125. }
  126. }
  127. },
  128. {
  129. test: /\.css$/,
  130. use: [{
  131. loader: MiniCssExtractPlugin.loader
  132. },
  133. 'css-loader'
  134. ]
  135. },
  136. {
  137. test: /\.less$/,
  138. use: [{
  139. loader: MiniCssExtractPlugin.loader
  140. },
  141. 'css-loader', 'less-loader'
  142. ]
  143. },
  144. {
  145. test: /\.(png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$/,
  146. use: [{
  147. loader: 'url-loader',
  148. options: {
  149. limit: 10240,
  150. esModule: false,
  151. name: '[name].[ext]',
  152. outputPath: 'images/'
  153. }
  154. }],
  155. exclude: /node_modules/
  156. }, {
  157. test: /.html$/,
  158. use: 'html-withimg-loader'
  159. }
  160. ]
  161. },
  162. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  163. mode: 'development',
  164. devServer: {
  165. contentBase: "./dist", //静态文件根目录
  166. proxy: {
  167. '/': proxyHost
  168. },
  169. hot: true,
  170. openPage: 'login.html'
  171. }
  172. }