webpack.config.js 5.4 KB

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