webpack.config.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 proxyHost = "http://192.168.2.236:5858";
  10. module.exports = {
  11. entry: {
  12. index: path.resolve(__dirname, 'src/js', 'index.js'),
  13. qcScore:path.resolve(__dirname, 'src/js', 'qcScore.js'),
  14. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  15. },
  16. output: {
  17. publicPath: '/', //这里要放的是静态资源CDN的地址
  18. path: path.resolve(__dirname, 'dist'),
  19. filename: 'js/[name].js'
  20. },
  21. resolve: {
  22. extensions: [".js", ".css", ".json"],
  23. alias: {} //配置别名可以加快webpack查找模块的速度
  24. },
  25. plugins: [// 多入口的html文件用chunks这个参数来区分
  26. new HtmlWebpackPlugin({
  27. title: 'index',
  28. template: path.resolve(__dirname, 'src/html', 'index.html'),
  29. filename: 'index.html',
  30. chunks: ['index', 'vendor', 'common'],
  31. hash: true, //防止缓存
  32. inject: true,
  33. minify: {
  34. removeAttributeQuotes: true, //压缩 去掉引号
  35. removeComments: true, //移除HTML中的注释
  36. collapseWhitespace: true //删除空白符与换行符
  37. }
  38. }),
  39. new HtmlWebpackPlugin({
  40. title: 'console.html',
  41. template: path.resolve(__dirname, 'src/html', 'console.html'),
  42. filename: 'console.html',
  43. chunks: ['index', 'vendor', 'common'],
  44. hash: true, //防止缓存
  45. inject: true,
  46. minify: {
  47. removeAttributeQuotes: true, //压缩 去掉引号
  48. removeComments: true, //移除HTML中的注释
  49. collapseWhitespace: true //删除空白符与换行符
  50. }
  51. }),
  52. new HtmlWebpackPlugin({
  53. title: 'itemManager.html',
  54. template: path.resolve(__dirname, 'src/html', 'itemManager.html'),
  55. filename: 'itemManager.html',
  56. chunks: ['index', 'vendor', 'common'],
  57. hash: true, //防止缓存
  58. inject: true,
  59. minify: {
  60. removeAttributeQuotes: true, //压缩 去掉引号
  61. removeComments: true, //移除HTML中的注释
  62. collapseWhitespace: true //删除空白符与换行符
  63. }
  64. }),
  65. new HtmlWebpackPlugin({
  66. title: 'moduleManager.html',
  67. template: path.resolve(__dirname, 'src/html', 'moduleManager.html'),
  68. filename: 'moduleManager.html',
  69. chunks: ['index', 'vendor', 'common'],
  70. hash: true, //防止缓存
  71. inject: true,
  72. minify: {
  73. removeAttributeQuotes: true, //压缩 去掉引号
  74. removeComments: true, //移除HTML中的注释
  75. collapseWhitespace: true //删除空白符与换行符
  76. }
  77. }),
  78. new HtmlWebpackPlugin({
  79. title: 'login.html',
  80. template: path.resolve(__dirname, 'src/html', 'login.html'),
  81. filename: 'login.html',
  82. chunks: ['index', 'vendor', 'common'],
  83. hash: true, //防止缓存
  84. inject: true,
  85. minify: {
  86. removeAttributeQuotes: true, //压缩 去掉引号
  87. removeComments: true, //移除HTML中的注释
  88. collapseWhitespace: true //删除空白符与换行符
  89. }
  90. }),
  91. new HtmlWebpackPlugin({
  92. title: 'qcList.html',
  93. template: path.resolve(__dirname, 'src/html', 'qcList.html'),
  94. filename: 'qcList.html',
  95. chunks: ['index', 'vendor', 'common'],
  96. hash: true, //防止缓存
  97. inject: true,
  98. minify: {
  99. removeAttributeQuotes: true, //压缩 去掉引号
  100. removeComments: true, //移除HTML中的注释
  101. collapseWhitespace: true //删除空白符与换行符
  102. }
  103. }),
  104. new HtmlWebpackPlugin({
  105. title: 'qcScore.html',
  106. template: path.resolve(__dirname, 'src/html', 'qcScore.html'),
  107. filename: 'qcScore.html',
  108. chunks: ['index', 'vendor', 'common','qcScore'],
  109. hash: true, //防止缓存
  110. inject: true,
  111. minify: {
  112. removeAttributeQuotes: true, //压缩 去掉引号
  113. removeComments: true, //移除HTML中的注释
  114. collapseWhitespace: true //删除空白符与换行符
  115. }
  116. }),
  117. new HtmlWebpackPlugin({
  118. title: 'statistics.html',
  119. template: path.resolve(__dirname, 'src/html', 'statistics.html'),
  120. filename: 'statistics.html',
  121. chunks: ['index', 'vendor', 'common'],
  122. hash: true, //防止缓存
  123. inject: true,
  124. minify: {
  125. removeAttributeQuotes: true, //压缩 去掉引号
  126. removeComments: true, //移除HTML中的注释
  127. collapseWhitespace: true //删除空白符与换行符
  128. }
  129. }),
  130. new CopyWebpackPlugin([
  131. {
  132. from:'src/resource',
  133. to:path.resolve(__dirname,'dist','resource'),
  134. flatten:true, //false会拷贝原始文件夹路径
  135. }
  136. ]),
  137. new MiniCssExtractPlugin({
  138. filename: 'css/[name].css',
  139. chunkFilename: '[id].css'
  140. }),
  141. new webpack.HotModuleReplacementPlugin(),
  142. new CleanWebpackPlugin()
  143. ],
  144. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  145. minimizer: [
  146. new UglifyJsPlugin({
  147. uglifyOptions: {
  148. ie8: true,
  149. compress: {
  150. properties: false,
  151. warnings: false
  152. },
  153. mangle: {
  154. screw_ie8: false,
  155. except: ['e']
  156. },
  157. output: {
  158. beautify: true
  159. },
  160. sourceMap: false
  161. }
  162. })
  163. ],
  164. splitChunks: {
  165. cacheGroups: {
  166. commons: {
  167. chunks: "initial",
  168. name: "common",
  169. minChunks: 2,
  170. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  171. minSize: 0, // This is example is too small to create commons chunks
  172. reuseExistingChunk: true // 可设置是否重用该chunk
  173. }
  174. }
  175. }
  176. },
  177. module: {
  178. rules: [
  179. {
  180. test: /.js$/,
  181. enforce: 'post',
  182. loader: 'es3ify-loader'
  183. },
  184. {
  185. test: /\.m?js$/,
  186. exclude: /(node_modules|bower_components)/,
  187. use: {
  188. loader: 'babel-loader',
  189. options: {
  190. presets:['@babel/preset-env']
  191. }
  192. }
  193. },
  194. {
  195. test: /\.less$/,
  196. use: [
  197. {
  198. loader: 'style-loader', // creates style nodes from JS strings
  199. },
  200. {
  201. loader: 'css-loader', // translates CSS into CommonJS
  202. },
  203. {
  204. loader: 'less-loader', // compiles Less to CSS
  205. },
  206. ],
  207. },
  208. {
  209. test:/\.(png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$/,
  210. use:[{
  211. loader:'url-loader',
  212. options:{
  213. limit:10240,
  214. esModule:false,
  215. name:'[name]_[hash:6].[ext]',
  216. outputPath:'images/'
  217. }
  218. }],
  219. exclude:/node_modules/
  220. },{
  221. test:/.html$/,
  222. use:'html-withimg-loader'
  223. }
  224. ]
  225. },
  226. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  227. mode: 'development',
  228. devServer: {
  229. contentBase: "./dist", //静态文件根目录
  230. proxy: {
  231. '/': proxyHost
  232. },
  233. hot: true,
  234. openPage:'index.html'
  235. }
  236. }