webpack.config.js 10 KB

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