webpack.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  6. const webpack = require('webpack');
  7. const proxyHost = "http://192.168.2.236:5050";
  8. module.exports = {
  9. entry: {
  10. index: path.resolve(__dirname, 'src/js', 'index.js'),
  11. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  12. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  13. },
  14. output: {
  15. publicPath: '/', //这里要放的是静态资源CDN的地址
  16. path: path.resolve(__dirname, 'dist'),
  17. filename: 'js/[name].js'
  18. },
  19. resolve: {
  20. extensions: [".js", ".css", ".json"],
  21. alias: {} //配置别名可以加快webpack查找模块的速度
  22. },
  23. plugins: [// 多入口的html文件用chunks这个参数来区分
  24. new HtmlWebpackPlugin({
  25. title: 'knowledgeMap',
  26. template: path.resolve(__dirname, 'src/html', 'knowledgeMap.html'),
  27. filename: 'knowledgeMap.html',
  28. chunks: ['index', 'vendor', 'common'],
  29. inject: true,
  30. hash: true, //防止缓存
  31. minify: {
  32. removeAttributeQuotes: true, //压缩 去掉引号
  33. removeComments: true, //移除HTML中的注释
  34. collapseWhitespace: true //删除空白符与换行符
  35. }
  36. }),
  37. new HtmlWebpackPlugin({
  38. title: 'knowledgeTree',
  39. template: path.resolve(__dirname, 'src/html', 'knowledgeTree.html'),
  40. filename: 'knowledgeTree.html',
  41. chunks: ['knowledgeTree', 'vendor', 'common'],
  42. inject: true,
  43. hash: true, //防止缓存
  44. minify: {
  45. removeAttributeQuotes: true, //压缩 去掉引号
  46. removeComments: true, //移除HTML中的注释
  47. collapseWhitespace: true //删除空白符与换行符
  48. }
  49. }),
  50. new MiniCssExtractPlugin({
  51. filename: 'css/[name].css',
  52. chunkFilename: '[id].css'
  53. }),
  54. new webpack.ProvidePlugin({
  55. $: 'jquery',
  56. jQuery: 'jquery',
  57. 'window.jQuery': 'jquery'
  58. }),
  59. new webpack.HotModuleReplacementPlugin(),
  60. new CleanWebpackPlugin()
  61. ],
  62. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  63. minimizer: [
  64. new UglifyJsPlugin({
  65. uglifyOptions: {
  66. ie8: true,
  67. compress: {
  68. properties: false,
  69. warnings: false
  70. },
  71. mangle: {
  72. screw_ie8: false,
  73. except: ['e']
  74. },
  75. output: {
  76. beautify: true
  77. },
  78. sourceMap: false
  79. }
  80. })
  81. ],
  82. splitChunks: {
  83. cacheGroups: {
  84. commons: {
  85. chunks: "initial",
  86. name: "common",
  87. minChunks: 2,
  88. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  89. minSize: 0, // This is example is too small to create commons chunks
  90. reuseExistingChunk: true // 可设置是否重用该chunk
  91. }
  92. }
  93. }
  94. },
  95. module: {
  96. rules: [
  97. {
  98. test: /.js$/,
  99. enforce: 'post',
  100. loader: 'es3ify-loader'
  101. },
  102. {
  103. test: /\.m?js$/,
  104. exclude: /(node_modules|bower_components)/,
  105. use: {
  106. loader: 'babel-loader',
  107. options: {
  108. presets:['@babel/preset-env']
  109. }
  110. }
  111. },
  112. {
  113. test: /\.css$/,
  114. use: [{
  115. loader: MiniCssExtractPlugin.loader
  116. },
  117. 'css-loader'
  118. ]
  119. },
  120. {
  121. test: /\.less$/,
  122. use: [
  123. {
  124. loader: 'style-loader', // creates style nodes from JS strings
  125. },
  126. {
  127. loader: 'css-loader', // translates CSS into CommonJS
  128. },
  129. {
  130. loader: 'less-loader', // compiles Less to CSS
  131. },
  132. ],
  133. },
  134. {
  135. test: /\.(png|jpg|jpeg|gif|svg)$/,
  136. use: {
  137. loader: 'file-loader',
  138. options: {
  139. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  140. limit: 10000,
  141. name: '[name].[ext]'
  142. }
  143. }
  144. }
  145. ]
  146. },
  147. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  148. mode: 'development',
  149. devServer: {
  150. contentBase: "./dist", //静态文件根目录
  151. proxy: {
  152. '/api': proxyHost
  153. },
  154. hot: true,
  155. openPage:'knowledgeMap.html'
  156. }
  157. }