webpack.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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: {} //配置别名可以加快webpack查找模块的速度
  53. },
  54. plugins: [ //多入口的html文件用chunks这个参数来区分
  55. ...plugines,
  56. new CopyWebpackPlugin([{
  57. from: 'src/resource',
  58. to: path.resolve(__dirname, 'dist', 'resource'),
  59. flatten: true, //false会拷贝原始文件夹路径
  60. }]),
  61. new MiniCssExtractPlugin({
  62. filename: 'css/[name].css',
  63. chunkFilename: '[id].css'
  64. }),
  65. new webpack.HotModuleReplacementPlugin(),
  66. new CleanWebpackPlugin()
  67. ],
  68. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  69. minimizer: [
  70. new UglifyJsPlugin({
  71. uglifyOptions: {
  72. ie8: true,
  73. compress: {
  74. properties: false,
  75. warnings: false
  76. },
  77. mangle: {
  78. screw_ie8: false,
  79. except: ['e']
  80. },
  81. output: {
  82. beautify: true
  83. },
  84. sourceMap: false
  85. }
  86. })
  87. ],
  88. splitChunks: {
  89. cacheGroups: {
  90. commons: {
  91. chunks: "initial",
  92. name: "common",
  93. minChunks: 2,
  94. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  95. minSize: 0, // This is example is too small to create commons chunks
  96. reuseExistingChunk: true // 可设置是否重用该chunk
  97. }
  98. }
  99. }
  100. },
  101. module: {
  102. noParse: /WdatePicker/,
  103. rules: [{
  104. test: /.js$/,
  105. enforce: 'post',
  106. loader: 'es3ify-loader'
  107. },
  108. {
  109. test: /\.m?js$/,
  110. exclude: /(node_modules|bower_components)/,
  111. use: {
  112. loader: 'babel-loader',
  113. options: {
  114. presets: ['@babel/preset-env']
  115. }
  116. }
  117. },
  118. {
  119. test: /\.css$/,
  120. use: [{
  121. loader: MiniCssExtractPlugin.loader
  122. },
  123. 'css-loader'
  124. ]
  125. },
  126. {
  127. test: /\.less$/,
  128. use: [{
  129. loader: MiniCssExtractPlugin.loader
  130. },
  131. 'css-loader', 'less-loader'
  132. ]
  133. },
  134. {
  135. test: /\.(png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$/,
  136. use: [{
  137. loader: 'url-loader',
  138. options: {
  139. limit: 10240,
  140. esModule: false,
  141. name: '[name]_[hash:6].[ext]',
  142. outputPath: 'images/'
  143. }
  144. }],
  145. exclude: /node_modules/
  146. }, {
  147. test: /.html$/,
  148. use: 'html-withimg-loader'
  149. }
  150. ]
  151. },
  152. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  153. mode: 'development',
  154. devServer: {
  155. contentBase: "./dist", //静态文件根目录
  156. proxy: {
  157. '/': proxyHost
  158. },
  159. hot: true,
  160. openPage: 'login.html'
  161. }
  162. }