webpack.config.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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', 'homeMini.html'),
  52. filename: 'homeMini.html',
  53. chunks: ['homeMini', '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', 'login.html'),
  65. filename: 'login.html',
  66. chunks: ['login', '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', 'home.html'),
  78. filename: 'home.html',
  79. chunks: ['home', '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', 'knowledgeUpdate.html'),
  91. filename: 'knowledgeUpdate.html',
  92. chunks: ['knowledgeUpdate', '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', 'knowledgeTree.html'),
  104. filename: 'knowledgeTree.html',
  105. chunks: ['knowledgeTree', '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', 'participle.html'),
  117. filename: 'participle.html',
  118. chunks: ['participle', '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', 'qaPage.html'),
  130. filename: 'qaPage.html',
  131. chunks: ['qaPage', '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', 'medicalTermMap.html'),
  143. filename: 'medicalTermMap.html',
  144. chunks: ['medicalTermMap', 'vendor', 'common'],
  145. inject: true,
  146. hash: true, //防止缓存
  147. minify: {
  148. removeAttributeQuotes: true, //压缩 去掉引号
  149. removeComments: true, //移除HTML中的注释
  150. collapseWhitespace: true //删除空白符与换行符
  151. }
  152. }),
  153. //作用:把public 里面的内容全部拷贝到编译目录
  154. new CopyWebpackPlugin([{
  155. from: __dirname + '/src/public',
  156. to: __dirname + '/dist'
  157. }]),
  158. new CopyWebpackPlugin([
  159. {
  160. from: 'src/resources',
  161. to: path.resolve(__dirname, 'dist', 'resources'),
  162. flatten: true, //false会拷贝原始文件夹路径
  163. }
  164. ]),
  165. new MiniCssExtractPlugin({
  166. filename: 'css/[name].css',
  167. chunkFilename: '[id].css'
  168. }),
  169. new webpack.ProvidePlugin({
  170. $: 'jquery',
  171. jQuery: 'jquery',
  172. 'window.jQuery': 'jquery'
  173. }),
  174. new webpack.HotModuleReplacementPlugin(),
  175. new CleanWebpackPlugin(),
  176. new ExtractTextPlugin("stylesheets/[name].css"),
  177. ],
  178. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  179. minimizer: [
  180. new UglifyJsPlugin({
  181. uglifyOptions: {
  182. ie8: true,
  183. compress: {
  184. properties: false,
  185. warnings: false
  186. },
  187. mangle: {
  188. screw_ie8: false,
  189. except: ['e']
  190. },
  191. output: {
  192. beautify: true
  193. },
  194. sourceMap: false
  195. }
  196. })
  197. ],
  198. splitChunks: {
  199. cacheGroups: {
  200. commons: {
  201. chunks: "initial",
  202. name: "common",
  203. minChunks: 2,
  204. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  205. minSize: 0, // This is example is too small to create commons chunks
  206. reuseExistingChunk: true // 可设置是否重用该chunk
  207. }
  208. }
  209. }
  210. },
  211. module: {
  212. rules: [
  213. {
  214. // test: /.js$/,
  215. // enforce: 'post',
  216. // loader: 'es3ify-loader'
  217. },
  218. {
  219. test: /\.m?js$/,
  220. exclude: /(node_modules|bower_components)/,
  221. use: {
  222. loader: 'babel-loader',
  223. options: {
  224. presets: ['@babel/preset-env']
  225. }
  226. }
  227. },
  228. {
  229. test: /\.css$/,
  230. use: ExtractTextPlugin.extract({
  231. use: [
  232. // {
  233. // loader: MiniCssExtractPlugin.loader
  234. // },
  235. 'css-loader'
  236. ]
  237. })
  238. },
  239. {
  240. test: /\.less$/,
  241. use: ExtractTextPlugin.extract({
  242. fallback: 'style-loader',
  243. use: [
  244. // {
  245. // loader: 'style-loader', // creates style nodes from JS strings
  246. // },
  247. {
  248. loader: 'css-loader', // translates CSS into CommonJS
  249. },
  250. {
  251. loader: 'less-loader', // compiles Less to CSS
  252. },
  253. ],
  254. })
  255. },
  256. {
  257. test: /\.(png|jpg|jpeg|gif|svg)$/,
  258. use: {
  259. loader: 'file-loader',
  260. options: {
  261. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  262. limit: 10000,
  263. name: '[name].[ext]'
  264. }
  265. }
  266. }
  267. ]
  268. },
  269. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  270. mode: 'development',
  271. devServer: {
  272. host: "172.16.8.57",
  273. contentBase: "./dist", //静态文件根目录
  274. proxy: {
  275. '/api': {
  276. target: proxyHost,
  277. pathRewrite: {
  278. '^/api': '' // 重写路径,去掉/api前缀
  279. },
  280. }
  281. },
  282. hot: true,
  283. openPage: 'home.html'
  284. }
  285. }