webpack.config.js 5.5 KB

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