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