vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. const path = require("path");
  3. const defaultSettings = require("./src/settings.js");
  4. const CompressionPlugin = require("compression-webpack-plugin");
  5. const CopyWebpackPlugin = require("copy-webpack-plugin");
  6. function resolve(dir) {
  7. return path.join(__dirname, dir);
  8. }
  9. const name = defaultSettings.title; // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following methods:
  14. // port = 9528 npm run dev OR npm run dev --port = 9528
  15. const port = process.env.port || process.env.npm_config_port || 9590; // dev port
  16. const webpack = require("webpack");
  17. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  18. module.exports = {
  19. /**
  20. * You will need to set publicPath if you plan to deploy your site under a sub path,
  21. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  22. * then publicPath should be set to "/bar/".
  23. * In most cases please use '/' !!!
  24. * Detail: https://cli.vuejs.org/config/#publicpath
  25. */
  26. publicPath: process.env.NODE_ENV === "development" ? "/" : "/GCLS-Book",
  27. outputDir: "dist",
  28. assetsDir: "static",
  29. lintOnSave: false,
  30. productionSourceMap: false,
  31. devServer: {
  32. port: port,
  33. open: true,
  34. overlay: {
  35. warnings: false,
  36. errors: true,
  37. },
  38. proxy: {
  39. // change xxx-api/login => mock/login
  40. // detail: https://cli.vuejs.org/config/#devserver-proxy
  41. [process.env.VUE_APP_BASE_API]: {
  42. // target: `http://gcls.utschool.cn/`,
  43. target: `https://gcls.helxsoft.cn/`,
  44. changeOrigin: true,
  45. pathRewrite: {
  46. ["^" + process.env.VUE_APP_BASE_API]: "",
  47. },
  48. },
  49. [process.env.VUE_APP_PDF]: {
  50. target: "https://file-kf.helxsoft.cn/",
  51. // target: 'https://file-cs.helxsoft.cn/',
  52. changeOrigin: true,
  53. pathRewrite: {
  54. ["^" + process.env.VUE_APP_PDF]: "",
  55. },
  56. },
  57. [process.env.VUE_APP_PDF_API]: {
  58. target: `https://gcls.helxsoft.cn`,
  59. changeOrigin: true,
  60. pathRewrite: {
  61. ["^" + process.env.VUE_APP_PDF_API]: "/",
  62. },
  63. },
  64. },
  65. after: require("./mock/mock-server.js"),
  66. },
  67. configureWebpack: {
  68. // provide the app's title in webpack's name field, so that
  69. // it can be accessed in index.html to inject the correct title.
  70. name: name,
  71. resolve: {
  72. alias: {
  73. "@": resolve("src"),
  74. },
  75. },
  76. plugins: [
  77. new webpack.ProvidePlugin({
  78. jQuery: "jquery",
  79. $: "jquery",
  80. }),
  81. new CopyWebpackPlugin({
  82. patterns: [
  83. {
  84. from: "node_modules/book-ui/dist/img",
  85. to: "static/js/img",
  86. force: true,
  87. noErrorOnMissing: true,
  88. },
  89. ],
  90. }),
  91. new CompressionPlugin({
  92. algorithm: "gzip", // 使用gzip压缩
  93. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  94. minRatio: 0.8, // 压缩率小于0.8才会压缩
  95. threshold: 10240, // 对超过10k的数据压缩
  96. deleteOriginalAssets: false, // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  97. }),
  98. ],
  99. },
  100. chainWebpack(config) {
  101. // it can improve the speed of the first screen, it is recommended to turn on preload
  102. config.plugin("preload").tap(() => [
  103. {
  104. rel: "preload",
  105. // to ignore runtime.js
  106. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  107. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  108. include: "initial",
  109. },
  110. ]);
  111. // when there are many pages, it will cause too many meaningless requests
  112. config.plugins.delete("prefetch");
  113. // set svg-sprite-loader
  114. config.module.rule("svg").exclude.add(resolve("src/icons")).end();
  115. config.module
  116. .rule("icons")
  117. .test(/\.svg$/)
  118. .include.add(resolve("src/icons"))
  119. .end()
  120. .use("svg-sprite-loader")
  121. .loader("svg-sprite-loader")
  122. .options({
  123. symbolId: "icon-[name]",
  124. })
  125. .end();
  126. config.when(process.env.NODE_ENV !== "development", (config) => {
  127. config
  128. .plugin("ScriptExtHtmlWebpackPlugin")
  129. .after("html")
  130. .use("script-ext-html-webpack-plugin", [
  131. {
  132. // `runtime` must same as runtimeChunk name. default is `runtime`
  133. inline: /runtime\..*\.js$/,
  134. },
  135. ])
  136. .end();
  137. config.optimization.splitChunks({
  138. chunks: "all",
  139. cacheGroups: {
  140. libs: {
  141. name: "chunk-libs",
  142. test: /[\\/]node_modules[\\/]/,
  143. priority: 10,
  144. chunks: "initial", // only package third parties that are initially dependent
  145. },
  146. elementUI: {
  147. name: "chunk-elementUI", // split elementUI into a single package
  148. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  149. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  150. },
  151. commons: {
  152. name: "chunk-commons",
  153. test: resolve("src/components"), // can customize your rules
  154. minChunks: 3, // minimum common number
  155. priority: 5,
  156. reuseExistingChunk: true,
  157. },
  158. },
  159. });
  160. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  161. config.optimization.runtimeChunk("single");
  162. });
  163. },
  164. };