webpack.config.js 5.0 KB

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