webpack.config.js 9.9 KB

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