webpack.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 ExtractTextPlugin = require("extract-text-webpack-plugin");
  7. const webpack = require('webpack');
  8. const proxyHost = "http://192.168.2.241:5050";
  9. // const proxyHost = "http://192.168.3.113:5050";
  10. module.exports = {
  11. entry: {
  12. index: path.resolve(__dirname, 'src/js', 'index.js'),
  13. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  14. participle: path.resolve(__dirname,'src/js', 'participle.js'),
  15. qaPage:path.resolve(__dirname,'src/js', 'qaPage.js'),
  16. medicalTermMap:path.resolve(__dirname,'src/js', 'medicalTermMap.js'),
  17. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  18. },
  19. output: {
  20. publicPath: '/', //这里要放的是静态资源CDN的地址
  21. path: path.resolve(__dirname, 'dist'),
  22. filename: 'js/[name].js'
  23. },
  24. resolve: {
  25. extensions: [".js", ".css", ".json"],
  26. alias: {} //配置别名可以加快webpack查找模块的速度
  27. },
  28. plugins: [// 多入口的html文件用chunks这个参数来区分
  29. new HtmlWebpackPlugin({
  30. title: '医学知识图谱',
  31. template: path.resolve(__dirname, 'src/html', 'knowledgeGraph.html'),
  32. filename: 'knowledgeGraph.html',
  33. chunks: ['index', 'vendor', 'common'],
  34. inject: true,
  35. hash: true, //防止缓存
  36. minify: {
  37. removeAttributeQuotes: true, //压缩 去掉引号
  38. removeComments: true, //移除HTML中的注释
  39. collapseWhitespace: true //删除空白符与换行符
  40. }
  41. }),
  42. new HtmlWebpackPlugin({
  43. title: '描述框架',
  44. template: path.resolve(__dirname, 'src/html', 'knowledgeTree.html'),
  45. filename: 'knowledgeTree.html',
  46. chunks: ['knowledgeTree', 'vendor', 'common'],
  47. inject: true,
  48. hash: true, //防止缓存
  49. minify: {
  50. removeAttributeQuotes: true, //压缩 去掉引号
  51. removeComments: true, //移除HTML中的注释
  52. collapseWhitespace: true //删除空白符与换行符
  53. }
  54. }),
  55. new HtmlWebpackPlugin({
  56. title: '电子病历信息抽取',
  57. template: path.resolve(__dirname, 'src/html', 'participle.html'),
  58. filename: 'participle.html',
  59. chunks: ['participle', 'vendor', 'common'],
  60. inject: true,
  61. hash: true, //防止缓存
  62. minify: {
  63. removeAttributeQuotes: true, //压缩 去掉引号
  64. removeComments: true, //移除HTML中的注释
  65. collapseWhitespace: true //删除空白符与换行符
  66. }
  67. }),
  68. new HtmlWebpackPlugin({
  69. title: '智能问答',
  70. template: path.resolve(__dirname, 'src/html', 'qaPage.html'),
  71. filename: 'qaPage.html',
  72. chunks: ['qaPage', 'vendor', 'common'],
  73. inject: true,
  74. hash: true, //防止缓存
  75. minify: {
  76. removeAttributeQuotes: true, //压缩 去掉引号
  77. removeComments: true, //移除HTML中的注释
  78. collapseWhitespace: true //删除空白符与换行符
  79. }
  80. }),
  81. new HtmlWebpackPlugin({
  82. title: '医学术语映射',
  83. template: path.resolve(__dirname, 'src/html', 'medicalTermMap.html'),
  84. filename: 'medicalTermMap.html',
  85. chunks: ['medicalTermMap', 'vendor', 'common'],
  86. inject: true,
  87. hash: true, //防止缓存
  88. minify: {
  89. removeAttributeQuotes: true, //压缩 去掉引号
  90. removeComments: true, //移除HTML中的注释
  91. collapseWhitespace: true //删除空白符与换行符
  92. }
  93. }),
  94. new MiniCssExtractPlugin({
  95. filename: 'css/[name].css',
  96. chunkFilename: '[id].css'
  97. }),
  98. new webpack.ProvidePlugin({
  99. $: 'jquery',
  100. jQuery: 'jquery',
  101. 'window.jQuery': 'jquery'
  102. }),
  103. new webpack.HotModuleReplacementPlugin(),
  104. new CleanWebpackPlugin(),
  105. new ExtractTextPlugin("stylesheets/[name].css"),
  106. ],
  107. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  108. minimizer: [
  109. new UglifyJsPlugin({
  110. uglifyOptions: {
  111. ie8: true,
  112. compress: {
  113. properties: false,
  114. warnings: false
  115. },
  116. mangle: {
  117. screw_ie8: false,
  118. except: ['e']
  119. },
  120. output: {
  121. beautify: true
  122. },
  123. sourceMap: false
  124. }
  125. })
  126. ],
  127. splitChunks: {
  128. cacheGroups: {
  129. commons: {
  130. chunks: "initial",
  131. name: "common",
  132. minChunks: 2,
  133. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  134. minSize: 0, // This is example is too small to create commons chunks
  135. reuseExistingChunk: true // 可设置是否重用该chunk
  136. }
  137. }
  138. }
  139. },
  140. module: {
  141. rules: [
  142. {
  143. test: /.js$/,
  144. enforce: 'post',
  145. loader: 'es3ify-loader'
  146. },
  147. {
  148. test: /\.m?js$/,
  149. exclude: /(node_modules|bower_components)/,
  150. use: {
  151. loader: 'babel-loader',
  152. options: {
  153. presets:['@babel/preset-env']
  154. }
  155. }
  156. },
  157. {
  158. test: /\.css$/,
  159. use:ExtractTextPlugin.extract({
  160. use: [
  161. // {
  162. // loader: MiniCssExtractPlugin.loader
  163. // },
  164. 'css-loader'
  165. ]
  166. })
  167. },
  168. {
  169. test: /\.less$/,
  170. use: ExtractTextPlugin.extract({
  171. fallback: 'style-loader',
  172. use: [
  173. // {
  174. // loader: 'style-loader', // creates style nodes from JS strings
  175. // },
  176. {
  177. loader: 'css-loader', // translates CSS into CommonJS
  178. },
  179. {
  180. loader: 'less-loader', // compiles Less to CSS
  181. },
  182. ],
  183. })
  184. },
  185. {
  186. test: /\.(png|jpg|jpeg|gif|svg)$/,
  187. use: {
  188. loader: 'file-loader',
  189. options: {
  190. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  191. limit: 10000,
  192. name: '[name].[ext]'
  193. }
  194. }
  195. }
  196. ]
  197. },
  198. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  199. mode: 'development',
  200. devServer: {
  201. contentBase: "./dist", //静态文件根目录
  202. proxy: {
  203. '/api': proxyHost
  204. },
  205. hot: true,
  206. openPage:'knowledgeGraph.html'
  207. }
  208. }