webpack.config.js 10.0 KB

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