webpack.config.js 11 KB

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