webpack.config.js 5.0 KB

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