webpack.config.js 7.6 KB

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