webpack.config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // 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. qaPage:path.resolve(__dirname,'src/js', 'qaPage.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: '医学知识图谱',
  30. template: path.resolve(__dirname, 'src/html', 'knowledgeGraph.html'),
  31. filename: 'knowledgeGraph.html',
  32. chunks: ['index', 'vendor', 'common'],
  33. inject: true,
  34. hash: true, //防止缓存
  35. minify: {
  36. removeAttributeQuotes: true, //压缩 去掉引号
  37. removeComments: true, //移除HTML中的注释
  38. collapseWhitespace: true //删除空白符与换行符
  39. }
  40. }),
  41. new HtmlWebpackPlugin({
  42. title: '描述框架',
  43. template: path.resolve(__dirname, 'src/html', 'knowledgeTree.html'),
  44. filename: 'knowledgeTree.html',
  45. chunks: ['knowledgeTree', 'vendor', 'common'],
  46. inject: true,
  47. hash: true, //防止缓存
  48. minify: {
  49. removeAttributeQuotes: true, //压缩 去掉引号
  50. removeComments: true, //移除HTML中的注释
  51. collapseWhitespace: true //删除空白符与换行符
  52. }
  53. }),
  54. new HtmlWebpackPlugin({
  55. title: '电子病历信息抽取',
  56. template: path.resolve(__dirname, 'src/html', 'participle.html'),
  57. filename: 'participle.html',
  58. chunks: ['participle', 'vendor', 'common'],
  59. inject: true,
  60. hash: true, //防止缓存
  61. minify: {
  62. removeAttributeQuotes: true, //压缩 去掉引号
  63. removeComments: true, //移除HTML中的注释
  64. collapseWhitespace: true //删除空白符与换行符
  65. }
  66. }),new HtmlWebpackPlugin({
  67. title: '智能问答',
  68. template: path.resolve(__dirname, 'src/html', 'qaPage.html'),
  69. filename: 'qaPage.html',
  70. chunks: ['qaPage', 'vendor', 'common'],
  71. inject: true,
  72. hash: true, //防止缓存
  73. minify: {
  74. removeAttributeQuotes: true, //压缩 去掉引号
  75. removeComments: true, //移除HTML中的注释
  76. collapseWhitespace: true //删除空白符与换行符
  77. }
  78. }),
  79. new MiniCssExtractPlugin({
  80. filename: 'css/[name].css',
  81. chunkFilename: '[id].css'
  82. }),
  83. new webpack.ProvidePlugin({
  84. $: 'jquery',
  85. jQuery: 'jquery',
  86. 'window.jQuery': 'jquery'
  87. }),
  88. new webpack.HotModuleReplacementPlugin(),
  89. new CleanWebpackPlugin(),
  90. new ExtractTextPlugin("stylesheets/[name].css"),
  91. ],
  92. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  93. minimizer: [
  94. new UglifyJsPlugin({
  95. uglifyOptions: {
  96. ie8: true,
  97. compress: {
  98. properties: false,
  99. warnings: false
  100. },
  101. mangle: {
  102. screw_ie8: false,
  103. except: ['e']
  104. },
  105. output: {
  106. beautify: true
  107. },
  108. sourceMap: false
  109. }
  110. })
  111. ],
  112. splitChunks: {
  113. cacheGroups: {
  114. commons: {
  115. chunks: "initial",
  116. name: "common",
  117. minChunks: 2,
  118. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  119. minSize: 0, // This is example is too small to create commons chunks
  120. reuseExistingChunk: true // 可设置是否重用该chunk
  121. }
  122. }
  123. }
  124. },
  125. module: {
  126. rules: [
  127. {
  128. test: /.js$/,
  129. enforce: 'post',
  130. loader: 'es3ify-loader'
  131. },
  132. {
  133. test: /\.m?js$/,
  134. exclude: /(node_modules|bower_components)/,
  135. use: {
  136. loader: 'babel-loader',
  137. options: {
  138. presets:['@babel/preset-env']
  139. }
  140. }
  141. },
  142. {
  143. test: /\.css$/,
  144. use:ExtractTextPlugin.extract({
  145. use: [
  146. // {
  147. // loader: MiniCssExtractPlugin.loader
  148. // },
  149. 'css-loader'
  150. ]
  151. })
  152. },
  153. {
  154. test: /\.less$/,
  155. use: ExtractTextPlugin.extract({
  156. fallback: 'style-loader',
  157. use: [
  158. // {
  159. // loader: 'style-loader', // creates style nodes from JS strings
  160. // },
  161. {
  162. loader: 'css-loader', // translates CSS into CommonJS
  163. },
  164. {
  165. loader: 'less-loader', // compiles Less to CSS
  166. },
  167. ],
  168. })
  169. },
  170. {
  171. test: /\.(png|jpg|jpeg|gif|svg)$/,
  172. use: {
  173. loader: 'file-loader',
  174. options: {
  175. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  176. limit: 10000,
  177. name: '[name].[ext]'
  178. }
  179. }
  180. }
  181. ]
  182. },
  183. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  184. mode: 'development',
  185. devServer: {
  186. contentBase: "./dist", //静态文件根目录
  187. proxy: {
  188. '/api': proxyHost
  189. },
  190. hot: true,
  191. openPage:'knowledgeGraph.html'
  192. }
  193. }