webpack.config.js 8.9 KB

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