webpack.config.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 CopyWebpackPlugin = require('copy-webpack-plugin');
  7. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  8. const webpack = require('webpack');
  9. // const proxyHost = "http://173.18.12.195:5050";
  10. const proxyHost = "http://173.18.12.205:8086/healsphere";
  11. // const proxyHost = "http://192.168.3.113:5050";
  12. // const proxyHost = "http://173.18.12.192:5050";
  13. module.exports = {
  14. entry: {
  15. index: path.resolve(__dirname, 'src/js', 'index.js'),
  16. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  17. participle: path.resolve(__dirname, 'src/js', 'participle.js'),
  18. qaPage: path.resolve(__dirname, 'src/js', 'qaPage.js'),
  19. medicalTermMap: path.resolve(__dirname, 'src/js', 'medicalTermMap.js'),
  20. login: path.resolve(__dirname, 'src/js', 'login.js'),
  21. home: path.resolve(__dirname, 'src/js', 'home.js'),
  22. knowledgeUpdate: path.resolve(__dirname, 'src/js', 'knowledgeUpdate.js'),
  23. graphDataStatistics: path.resolve(__dirname, 'src/js', 'graphDataStatistics.js'),
  24. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  25. },
  26. output: {
  27. publicPath: '/', //这里要放的是静态资源CDN的地址
  28. path: path.resolve(__dirname, 'dist'),
  29. filename: 'js/[name].js'
  30. },
  31. resolve: {
  32. extensions: [".js", ".css", ".json"],
  33. alias: {
  34. "vue$": "vue/dist/vue.esm.js", // 确保使用 ESM 版本
  35. } //配置别名可以加快webpack查找模块的速度
  36. },
  37. plugins: [// 多入口的html文件用chunks这个参数来区分
  38. new HtmlWebpackPlugin({
  39. title: '医学知识图谱',
  40. template: path.resolve(__dirname, 'src/html', 'knowledgeGraph.html'),
  41. filename: 'knowledgeGraph.html',
  42. chunks: ['index', 'vendor', 'common'],
  43. inject: true,
  44. hash: true, //防止缓存
  45. minify: {
  46. removeAttributeQuotes: true, //压缩 去掉引号
  47. removeComments: true, //移除HTML中的注释
  48. collapseWhitespace: true //删除空白符与换行符
  49. }
  50. }),
  51. new HtmlWebpackPlugin({
  52. title: '图谱数据统计',
  53. template: path.resolve(__dirname, 'src/html', 'graphDataStatistics.html'),
  54. filename: 'graphDataStatistics.html',
  55. chunks: ['graphDataStatistics', 'vendor', 'common'],
  56. inject: true,
  57. hash: true, //防止缓存
  58. minify: {
  59. removeAttributeQuotes: true, //压缩 去掉引号
  60. removeComments: true, //移除HTML中的注释
  61. collapseWhitespace: true //删除空白符与换行符
  62. }
  63. }),
  64. new HtmlWebpackPlugin({
  65. title: '登录界面',
  66. template: path.resolve(__dirname, 'src/html', 'login.html'),
  67. filename: 'login.html',
  68. chunks: ['login', 'vendor', 'common'],
  69. inject: true,
  70. hash: true, //防止缓存
  71. minify: {
  72. removeAttributeQuotes: true, //压缩 去掉引号
  73. removeComments: true, //移除HTML中的注释
  74. collapseWhitespace: true //删除空白符与换行符
  75. }
  76. }),
  77. new HtmlWebpackPlugin({
  78. title: '主页',
  79. template: path.resolve(__dirname, 'src/html', 'home.html'),
  80. filename: 'home.html',
  81. chunks: ['home', 'vendor', 'common'],
  82. inject: true,
  83. hash: true, //防止缓存
  84. minify: {
  85. removeAttributeQuotes: true, //压缩 去掉引号
  86. removeComments: true, //移除HTML中的注释
  87. collapseWhitespace: true //删除空白符与换行符
  88. }
  89. }),
  90. new HtmlWebpackPlugin({
  91. title: '知识更新',
  92. template: path.resolve(__dirname, 'src/html', 'knowledgeUpdate.html'),
  93. filename: 'knowledgeUpdate.html',
  94. chunks: ['knowledgeUpdate', 'vendor', 'common'],
  95. inject: true,
  96. hash: true, //防止缓存
  97. minify: {
  98. removeAttributeQuotes: true, //压缩 去掉引号
  99. removeComments: true, //移除HTML中的注释
  100. collapseWhitespace: true //删除空白符与换行符
  101. }
  102. }),
  103. new HtmlWebpackPlugin({
  104. title: '描述框架',
  105. template: path.resolve(__dirname, 'src/html', 'knowledgeTree.html'),
  106. filename: 'knowledgeTree.html',
  107. chunks: ['knowledgeTree', 'vendor', 'common'],
  108. inject: true,
  109. hash: true, //防止缓存
  110. minify: {
  111. removeAttributeQuotes: true, //压缩 去掉引号
  112. removeComments: true, //移除HTML中的注释
  113. collapseWhitespace: true //删除空白符与换行符
  114. }
  115. }),
  116. new HtmlWebpackPlugin({
  117. title: '电子病历信息抽取',
  118. template: path.resolve(__dirname, 'src/html', 'participle.html'),
  119. filename: 'participle.html',
  120. chunks: ['participle', 'vendor', 'common'],
  121. inject: true,
  122. hash: true, //防止缓存
  123. minify: {
  124. removeAttributeQuotes: true, //压缩 去掉引号
  125. removeComments: true, //移除HTML中的注释
  126. collapseWhitespace: true //删除空白符与换行符
  127. }
  128. }),
  129. new HtmlWebpackPlugin({
  130. title: '智能问答',
  131. template: path.resolve(__dirname, 'src/html', 'qaPage.html'),
  132. filename: 'qaPage.html',
  133. chunks: ['qaPage', 'vendor', 'common'],
  134. inject: true,
  135. hash: true, //防止缓存
  136. minify: {
  137. removeAttributeQuotes: true, //压缩 去掉引号
  138. removeComments: true, //移除HTML中的注释
  139. collapseWhitespace: true //删除空白符与换行符
  140. }
  141. }),
  142. new HtmlWebpackPlugin({
  143. title: '医学术语映射',
  144. template: path.resolve(__dirname, 'src/html', 'medicalTermMap.html'),
  145. filename: 'medicalTermMap.html',
  146. chunks: ['medicalTermMap', 'vendor', 'common'],
  147. inject: true,
  148. hash: true, //防止缓存
  149. minify: {
  150. removeAttributeQuotes: true, //压缩 去掉引号
  151. removeComments: true, //移除HTML中的注释
  152. collapseWhitespace: true //删除空白符与换行符
  153. }
  154. }),
  155. //作用:把public 里面的内容全部拷贝到编译目录
  156. new CopyWebpackPlugin([{
  157. from: __dirname + '/src/public',
  158. to: __dirname + '/dist'
  159. }]),
  160. new CopyWebpackPlugin([
  161. {
  162. from: 'src/resources',
  163. to: path.resolve(__dirname, 'dist', 'resources'),
  164. flatten: true, //false会拷贝原始文件夹路径
  165. }
  166. ]),
  167. new MiniCssExtractPlugin({
  168. filename: 'css/[name].css',
  169. chunkFilename: '[id].css'
  170. }),
  171. new webpack.ProvidePlugin({
  172. $: 'jquery',
  173. jQuery: 'jquery',
  174. 'window.jQuery': 'jquery'
  175. }),
  176. new webpack.HotModuleReplacementPlugin(),
  177. new CleanWebpackPlugin(),
  178. new ExtractTextPlugin("stylesheets/[name].css"),
  179. ],
  180. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  181. minimizer: [
  182. new UglifyJsPlugin({
  183. uglifyOptions: {
  184. ie8: true,
  185. compress: {
  186. properties: false,
  187. warnings: false
  188. },
  189. mangle: {
  190. screw_ie8: false,
  191. except: ['e']
  192. },
  193. output: {
  194. beautify: true
  195. },
  196. sourceMap: false
  197. }
  198. })
  199. ],
  200. splitChunks: {
  201. cacheGroups: {
  202. commons: {
  203. chunks: "initial",
  204. name: "common",
  205. minChunks: 2,
  206. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  207. minSize: 0, // This is example is too small to create commons chunks
  208. reuseExistingChunk: true // 可设置是否重用该chunk
  209. }
  210. }
  211. }
  212. },
  213. module: {
  214. rules: [
  215. {
  216. // test: /.js$/,
  217. // enforce: 'post',
  218. // loader: 'es3ify-loader'
  219. },
  220. {
  221. test: /\.m?js$/,
  222. exclude: /(node_modules|bower_components)/,
  223. use: {
  224. loader: 'babel-loader',
  225. options: {
  226. presets: ['@babel/preset-env']
  227. }
  228. }
  229. },
  230. {
  231. test: /\.css$/,
  232. use: ExtractTextPlugin.extract({
  233. use: [
  234. // {
  235. // loader: MiniCssExtractPlugin.loader
  236. // },
  237. 'css-loader'
  238. ]
  239. })
  240. },
  241. {
  242. test: /\.less$/,
  243. use: ExtractTextPlugin.extract({
  244. fallback: 'style-loader',
  245. use: [
  246. // {
  247. // loader: 'style-loader', // creates style nodes from JS strings
  248. // },
  249. {
  250. loader: 'css-loader', // translates CSS into CommonJS
  251. },
  252. {
  253. loader: 'less-loader', // compiles Less to CSS
  254. },
  255. ],
  256. })
  257. },
  258. {
  259. test: /\.(png|jpg|jpeg|gif|svg)$/,
  260. use: {
  261. loader: 'file-loader',
  262. options: {
  263. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  264. limit: 10000,
  265. name: '[name].[ext]'
  266. }
  267. }
  268. }
  269. ]
  270. },
  271. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  272. mode: 'development',
  273. devServer: {
  274. // host: "172.16.8.57",
  275. contentBase: "./dist", //静态文件根目录
  276. proxy: {
  277. '/api': {
  278. target: proxyHost,
  279. pathRewrite: {
  280. '^/api': '' // 重写路径,去掉/api前缀
  281. },
  282. },
  283. '/kg': { //知识图谱统计API
  284. target: "http://173.18.12.205:8086/healsphere",
  285. changeOrigin: true,
  286. }
  287. },
  288. hot: true,
  289. openPage: 'home.html'
  290. }
  291. }