webpack.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.18.1.235:8000/open-platform"
  12. // const proxyHost = "http://192.168.3.113:5050";
  13. // const proxyHost = "http://173.18.12.192:5050";
  14. module.exports = {
  15. entry: {
  16. index: path.resolve(__dirname, 'src/js', 'index.js'),
  17. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  18. participle: path.resolve(__dirname, 'src/js', 'participle.js'),
  19. qaPage: path.resolve(__dirname, 'src/js', 'qaPage.js'),
  20. medicalTermMap: path.resolve(__dirname, 'src/js', 'medicalTermMap.js'),
  21. login: path.resolve(__dirname, 'src/js', 'login.js'),
  22. home: path.resolve(__dirname, 'src/js', 'home.js'),
  23. knowledgeUpdate: path.resolve(__dirname, 'src/js', 'knowledgeUpdate.js'),
  24. graphDataStatistics: path.resolve(__dirname, 'src/js', 'graphDataStatistics.js'),
  25. selfKnowledgeGraph: path.resolve(__dirname, 'src/js', 'selfKnowledgeGraph.js'),
  26. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  27. },
  28. output: {
  29. publicPath: '/', //这里要放的是静态资源CDN的地址
  30. path: path.resolve(__dirname, 'dist'),
  31. filename: 'js/[name].js'
  32. },
  33. resolve: {
  34. extensions: [".js", ".css", ".json"],
  35. alias: {} //配置别名可以加快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', 'selfKnowledgeGraph.html'),
  67. filename: 'selfKnowledgeGraph.html',
  68. chunks: ['selfKnowledgeGraph', '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', 'login.html'),
  80. filename: 'login.html',
  81. chunks: ['login', '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', 'home.html'),
  93. filename: 'home.html',
  94. chunks: ['home', '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', 'knowledgeUpdate.html'),
  106. filename: 'knowledgeUpdate.html',
  107. chunks: ['knowledgeUpdate', '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', 'knowledgeTree.html'),
  119. filename: 'knowledgeTree.html',
  120. chunks: ['knowledgeTree', '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', 'participle.html'),
  132. filename: 'participle.html',
  133. chunks: ['participle', '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', 'qaPage.html'),
  145. filename: 'qaPage.html',
  146. chunks: ['qaPage', 'vendor', 'common'],
  147. inject: true,
  148. hash: true, //防止缓存
  149. minify: {
  150. removeAttributeQuotes: true, //压缩 去掉引号
  151. removeComments: true, //移除HTML中的注释
  152. collapseWhitespace: true //删除空白符与换行符
  153. }
  154. }),
  155. new HtmlWebpackPlugin({
  156. title: '医学术语映射',
  157. template: path.resolve(__dirname, 'src/html', 'medicalTermMap.html'),
  158. filename: 'medicalTermMap.html',
  159. chunks: ['medicalTermMap', 'vendor', 'common'],
  160. inject: true,
  161. hash: true, //防止缓存
  162. minify: {
  163. removeAttributeQuotes: true, //压缩 去掉引号
  164. removeComments: true, //移除HTML中的注释
  165. collapseWhitespace: true //删除空白符与换行符
  166. }
  167. }),
  168. //作用:把public 里面的内容全部拷贝到编译目录
  169. new CopyWebpackPlugin([{
  170. from: __dirname + '/src/public',
  171. to: __dirname + '/dist'
  172. }]),
  173. new CopyWebpackPlugin([
  174. {
  175. from: 'src/resources',
  176. to: path.resolve(__dirname, 'dist', 'resources'),
  177. flatten: true, //false会拷贝原始文件夹路径
  178. }
  179. ]),
  180. new MiniCssExtractPlugin({
  181. filename: 'css/[name].css',
  182. chunkFilename: '[id].css'
  183. }),
  184. new webpack.ProvidePlugin({
  185. $: 'jquery',
  186. jQuery: 'jquery',
  187. 'window.jQuery': 'jquery'
  188. }),
  189. new webpack.HotModuleReplacementPlugin(),
  190. new CleanWebpackPlugin(),
  191. new ExtractTextPlugin("stylesheets/[name].css"),
  192. ],
  193. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  194. minimizer: [
  195. new UglifyJsPlugin({
  196. uglifyOptions: {
  197. ie8: true,
  198. compress: {
  199. properties: false,
  200. warnings: false
  201. },
  202. mangle: {
  203. screw_ie8: false,
  204. except: ['e']
  205. },
  206. output: {
  207. beautify: true
  208. },
  209. sourceMap: false
  210. }
  211. })
  212. ],
  213. splitChunks: {
  214. cacheGroups: {
  215. commons: {
  216. chunks: "initial",
  217. name: "common",
  218. minChunks: 2,
  219. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  220. minSize: 0, // This is example is too small to create commons chunks
  221. reuseExistingChunk: true // 可设置是否重用该chunk
  222. }
  223. }
  224. }
  225. },
  226. module: {
  227. rules: [
  228. {
  229. // test: /.js$/,
  230. // enforce: 'post',
  231. // loader: 'es3ify-loader'
  232. },
  233. {
  234. test: /\.m?js$/,
  235. exclude: /(node_modules|bower_components)/,
  236. use: {
  237. loader: 'babel-loader',
  238. options: {
  239. presets: ['@babel/preset-env']
  240. }
  241. }
  242. },
  243. {
  244. test: /\.css$/,
  245. use: ExtractTextPlugin.extract({
  246. use: [
  247. // {
  248. // loader: MiniCssExtractPlugin.loader
  249. // },
  250. 'css-loader'
  251. ]
  252. })
  253. },
  254. {
  255. test: /\.less$/,
  256. use: ExtractTextPlugin.extract({
  257. fallback: 'style-loader',
  258. use: [
  259. // {
  260. // loader: 'style-loader', // creates style nodes from JS strings
  261. // },
  262. {
  263. loader: 'css-loader', // translates CSS into CommonJS
  264. },
  265. {
  266. loader: 'less-loader', // compiles Less to CSS
  267. },
  268. ],
  269. })
  270. },
  271. {
  272. test: /\.(png|jpg|jpeg|gif|svg)$/,
  273. use: {
  274. loader: 'file-loader',
  275. options: {
  276. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  277. limit: 10000,
  278. name: '[name].[ext]'
  279. }
  280. }
  281. }
  282. ]
  283. },
  284. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  285. mode: 'development',
  286. devServer: {
  287. // host: "172.16.8.57",
  288. contentBase: "./dist", //静态文件根目录
  289. proxy: {
  290. '/api': {
  291. target: proxyHost,
  292. pathRewrite: {
  293. '^/api': '' // 重写路径,去掉/api前缀
  294. },
  295. },
  296. '/open-platform': { //知识图谱统计API
  297. target: "http://192.18.1.235:8000",
  298. changeOrigin: true,
  299. }
  300. },
  301. hot: true,
  302. openPage: 'home.html'
  303. }
  304. }