webpack.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // const proxyHost = "http://192.168.2.241:5858";
  11. // const proxyHost = "http://192.168.3.117:5858";//铁钢
  12. // const proxyHost = "http://192.168.3.113:5858";//王峰
  13. module.exports = {
  14. entry: {
  15. index: path.resolve(__dirname, 'src/js', 'index.js'),
  16. qcScore:path.resolve(__dirname, 'src/js', 'qcScore.js'),
  17. login:path.resolve(__dirname, 'src/js', 'login.js'),
  18. console:path.resolve(__dirname, 'src/js', 'console.js'),
  19. deptConsole:path.resolve(__dirname, 'src/js', 'deptConsole.js'),
  20. moduleManager: path.resolve(__dirname, 'src/js', 'moduleManager.js'),
  21. itemManager: path.resolve(__dirname, 'src/js', 'itemManager.js'),
  22. qcList: path.resolve(__dirname, 'src/js', 'qcList.js'),
  23. qcListDept:path.resolve(__dirname, 'src/js', 'qcListDept.js'),
  24. qcListPerson:path.resolve(__dirname, 'src/js', 'qcListPerson.js'),
  25. userManager: path.resolve(__dirname, 'src/js', 'userManager.js'),
  26. roleManager: path.resolve(__dirname, 'src/js', 'roleManager.js'),
  27. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  28. },
  29. output: {
  30. publicPath: '/', //这里要放的是静态资源CDN的地址
  31. path: path.resolve(__dirname, 'dist'),
  32. filename: 'js/[name].js'
  33. },
  34. resolve: {
  35. extensions: [".js", ".css", ".json"],
  36. alias: {} //配置别名可以加快webpack查找模块的速度
  37. },
  38. plugins: [//多入口的html文件用chunks这个参数来区分
  39. new HtmlWebpackPlugin({
  40. title: 'index',
  41. template: path.resolve(__dirname, 'src/html', 'index.html'),
  42. filename: 'index.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: 'console.html',
  54. template: path.resolve(__dirname, 'src/html', 'console.html'),
  55. filename: 'console.html',
  56. chunks: ['console', '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: 'deptConsole.html',
  67. template: path.resolve(__dirname, 'src/html', 'deptConsole.html'),
  68. filename: 'deptConsole.html',
  69. chunks: ['deptConsole', '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: 'itemManager.html',
  80. template: path.resolve(__dirname, 'src/html', 'itemManager.html'),
  81. filename: 'itemManager.html',
  82. chunks: ['itemManager', '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: 'moduleManager.html',
  93. template: path.resolve(__dirname, 'src/html', 'moduleManager.html'),
  94. filename: 'moduleManager.html',
  95. chunks: ['moduleManager', '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: 'login.html',
  106. template: path.resolve(__dirname, 'src/html', 'login.html'),
  107. filename: 'login.html',
  108. chunks: ['login', 'vendor', 'common'],
  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: 'qcList.html',
  119. template: path.resolve(__dirname, 'src/html', 'qcList.html'),
  120. filename: 'qcList.html',
  121. chunks: ['qcList', 'vendor', 'common'],
  122. hash: true, //防止缓存
  123. inject: true,
  124. minify: {
  125. removeAttributeQuotes: true, //压缩 去掉引号
  126. removeComments: true, //移除HTML中的注释
  127. collapseWhitespace: true //删除空白符与换行符
  128. }
  129. }),
  130. new HtmlWebpackPlugin({
  131. title: 'qcListPerson.html',
  132. template: path.resolve(__dirname, 'src/html', 'qcListPerson.html'),
  133. filename: 'qcListPerson.html',
  134. chunks: ['qcListPerson', 'vendor', 'common'],
  135. hash: true, //防止缓存
  136. inject: true,
  137. minify: {
  138. removeAttributeQuotes: true, //压缩 去掉引号
  139. removeComments: true, //移除HTML中的注释
  140. collapseWhitespace: true //删除空白符与换行符
  141. }
  142. }),
  143. new HtmlWebpackPlugin({
  144. title: 'qcListDept.html',
  145. template: path.resolve(__dirname, 'src/html', 'qcListDept.html'),
  146. filename: 'qcListDept.html',
  147. chunks: ['qcListDept', 'vendor', 'common'],
  148. hash: true, //防止缓存
  149. inject: true,
  150. minify: {
  151. removeAttributeQuotes: true, //压缩 去掉引号
  152. removeComments: true, //移除HTML中的注释
  153. collapseWhitespace: true //删除空白符与换行符
  154. }
  155. }),
  156. new HtmlWebpackPlugin({
  157. title: 'qcScore.html',
  158. template: path.resolve(__dirname, 'src/html', 'qcScore.html'),
  159. filename: 'qcScore.html',
  160. chunks: [ 'qcScore','vendor', 'common'],
  161. hash: true, //防止缓存
  162. inject: true,
  163. minify: {
  164. removeAttributeQuotes: true, //压缩 去掉引号
  165. removeComments: true, //移除HTML中的注释
  166. collapseWhitespace: true //删除空白符与换行符
  167. }
  168. }),
  169. new HtmlWebpackPlugin({
  170. title: 'statistics.html',
  171. template: path.resolve(__dirname, 'src/html', 'statistics.html'),
  172. filename: 'statistics.html',
  173. chunks: ['index', 'vendor', 'common'],
  174. hash: true, //防止缓存
  175. inject: true,
  176. minify: {
  177. removeAttributeQuotes: true, //压缩 去掉引号
  178. removeComments: true, //移除HTML中的注释
  179. collapseWhitespace: true //删除空白符与换行符
  180. }
  181. }),
  182. new HtmlWebpackPlugin({
  183. title: 'userManager.html',
  184. template: path.resolve(__dirname, 'src/html', 'userManager.html'),
  185. filename: 'userManager.html',
  186. chunks: ['userManager', 'vendor', 'common'],
  187. hash: true, //防止缓存
  188. inject: true,
  189. minify: {
  190. removeAttributeQuotes: true, //压缩 去掉引号
  191. removeComments: true, //移除HTML中的注释
  192. collapseWhitespace: true //删除空白符与换行符
  193. }
  194. }),
  195. new HtmlWebpackPlugin({
  196. title: 'roleManager.html',
  197. template: path.resolve(__dirname, 'src/html', 'roleManager.html'),
  198. filename: 'roleManager.html',
  199. chunks: ['roleManager', 'vendor', 'common'],
  200. hash: true, //防止缓存
  201. inject: true,
  202. minify: {
  203. removeAttributeQuotes: true, //压缩 去掉引号
  204. removeComments: true, //移除HTML中的注释
  205. collapseWhitespace: true //删除空白符与换行符
  206. }
  207. }),
  208. new CopyWebpackPlugin([
  209. {
  210. from:'src/resource',
  211. to:path.resolve(__dirname,'dist','resource'),
  212. flatten:true, //false会拷贝原始文件夹路径
  213. }
  214. ]),
  215. new MiniCssExtractPlugin({
  216. filename: 'css/[name].css',
  217. chunkFilename: '[id].css'
  218. }),
  219. new webpack.HotModuleReplacementPlugin(),
  220. new CleanWebpackPlugin()
  221. ],
  222. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  223. minimizer: [
  224. new UglifyJsPlugin({
  225. uglifyOptions: {
  226. ie8: true,
  227. compress: {
  228. properties: false,
  229. warnings: false
  230. },
  231. mangle: {
  232. screw_ie8: false,
  233. except: ['e']
  234. },
  235. output: {
  236. beautify: true
  237. },
  238. sourceMap: false
  239. }
  240. })
  241. ],
  242. splitChunks: {
  243. cacheGroups: {
  244. commons: {
  245. chunks: "initial",
  246. name: "common",
  247. minChunks: 2,
  248. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  249. minSize: 0, // This is example is too small to create commons chunks
  250. reuseExistingChunk: true // 可设置是否重用该chunk
  251. }
  252. }
  253. }
  254. },
  255. module: {
  256. noParse: /WdatePicker/,
  257. rules: [
  258. {
  259. test: /.js$/,
  260. enforce: 'post',
  261. loader: 'es3ify-loader'
  262. },
  263. {
  264. test: /\.m?js$/,
  265. exclude: /(node_modules|bower_components)/,
  266. use: {
  267. loader: 'babel-loader',
  268. options: {
  269. presets:['@babel/preset-env']
  270. }
  271. }
  272. },
  273. {
  274. test: /\.css$/,
  275. use: [{
  276. loader: MiniCssExtractPlugin.loader
  277. },
  278. 'css-loader'
  279. ]
  280. },
  281. {
  282. test: /\.less$/,
  283. use: [{
  284. loader: MiniCssExtractPlugin.loader
  285. },
  286. 'css-loader', 'less-loader'
  287. ]
  288. },
  289. {
  290. test:/\.(png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$/,
  291. use:[{
  292. loader:'url-loader',
  293. options:{
  294. limit:10240,
  295. esModule:false,
  296. name:'[name]_[hash:6].[ext]',
  297. outputPath:'images/'
  298. }
  299. }],
  300. exclude:/node_modules/
  301. },{
  302. test:/.html$/,
  303. use:'html-withimg-loader'
  304. }
  305. ]
  306. },
  307. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  308. mode: 'development',
  309. devServer: {
  310. contentBase: "./dist", //静态文件根目录
  311. proxy: {
  312. '/': proxyHost
  313. },
  314. hot: true,
  315. openPage:'login.html'
  316. }
  317. }