webpack.config.js 5.0 KB

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