webpack.config.js 7.8 KB

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