webpack.config.js 5.1 KB

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