webpack.config.js 7.7 KB

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