webpack.config.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 webpack = require('webpack');
  8. // const proxyHost = "http://173.18.12.195:5050";
  9. // const proxyHost = "http://173.18.12.205:8086/healsphere";
  10. // const proxyHost = "http://192.168.3.113:5050";
  11. // const proxyHost = "http://173.18.12.192:5050";
  12. const proxyHost = 'http://173.18.12.191:1388/healsphere'
  13. // 移除 extract-text-webpack-plugin 引入
  14. // const ExtractTextPlugin = require('extract-text-webpack-plugin');
  15. module.exports = {
  16. entry: {
  17. index: path.resolve(__dirname, 'src/js', 'index.js'),
  18. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  19. participle: path.resolve(__dirname, 'src/js', 'participle.js'),
  20. qaPage: path.resolve(__dirname, 'src/js', 'qaPage.js'),
  21. medicalTermMap: path.resolve(__dirname, 'src/js', 'medicalTermMap.js'),
  22. login: path.resolve(__dirname, 'src/js', 'login.js'),
  23. home: path.resolve(__dirname, 'src/js', 'home.js'),
  24. knowledgeUpdate: path.resolve(__dirname, 'src/js', 'knowledgeUpdate.js'),
  25. homeMini: path.resolve(__dirname, 'src/js', 'homeMini.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: [
  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. // 移除 ExtractTextPlugin 实例
  172. // new ExtractTextPlugin({
  173. // filename: 'css/[name].css',
  174. // allChunks: true
  175. // }),
  176. new webpack.ProvidePlugin({
  177. $: 'jquery',
  178. jQuery: 'jquery',
  179. 'window.jQuery': 'jquery'
  180. }),
  181. new webpack.HotModuleReplacementPlugin(),
  182. new CleanWebpackPlugin(),
  183. // new ExtractTextPlugin("stylesheets/[name].css"),
  184. ],
  185. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  186. minimizer: [
  187. new UglifyJsPlugin({
  188. uglifyOptions: {
  189. ie8: true,
  190. compress: {
  191. properties: false,
  192. warnings: false
  193. },
  194. mangle: {
  195. screw_ie8: false,
  196. except: ['e']
  197. },
  198. output: {
  199. beautify: true
  200. },
  201. sourceMap: false
  202. }
  203. })
  204. ],
  205. splitChunks: {
  206. cacheGroups: {
  207. commons: {
  208. chunks: "initial",
  209. name: "common",
  210. minChunks: 2,
  211. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  212. minSize: 0, // This is example is too small to create commons chunks
  213. reuseExistingChunk: true // 可设置是否重用该chunk
  214. }
  215. }
  216. }
  217. },
  218. module: {
  219. rules: [
  220. {
  221. // test: /.js$/,
  222. // enforce: 'post',
  223. // loader: 'es3ify-loader'
  224. },
  225. {
  226. test: /\.m?js$/,
  227. exclude: /(node_modules|bower_components)/,
  228. use: {
  229. loader: 'babel-loader',
  230. options: {
  231. presets: ['@babel/preset-env']
  232. }
  233. }
  234. },
  235. {
  236. test: /\.css$/,
  237. use: [
  238. // 生产环境使用 MiniCssExtractPlugin.loader,开发环境使用 style-loader
  239. process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader',
  240. 'css-loader'
  241. ]
  242. },
  243. {
  244. test: /\.less$/,
  245. use: [
  246. process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader',
  247. 'css-loader',
  248. 'less-loader'
  249. ]
  250. },
  251. // 移除使用 extract-text-webpack-plugin 的规则
  252. // {
  253. // test: /\.css$/,
  254. // use: ExtractTextPlugin.extract({
  255. // fallback: 'style-loader',
  256. // use: 'css-loader'
  257. // })
  258. // },
  259. {
  260. test: /\.(png|jpg|jpeg|gif|svg)$/,
  261. use: {
  262. loader: 'file-loader',
  263. options: {
  264. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  265. limit: 10000,
  266. name: '[name].[ext]'
  267. }
  268. }
  269. }
  270. ]
  271. },
  272. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  273. mode: 'development',
  274. devServer: {
  275. // host: "172.16.8.57",
  276. contentBase: "./dist", //静态文件根目录
  277. proxy: {
  278. '/api': {
  279. target: proxyHost,
  280. pathRewrite: {
  281. '^/api': '' // 重写路径,去掉/api前缀
  282. },
  283. },
  284. '/kg': { //知识图谱统计API
  285. target: "http://173.18.12.205:8086/healsphere",
  286. changeOrigin: true,
  287. }
  288. },
  289. hot: true,
  290. openPage: 'home.html'
  291. }
  292. }