vue.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @description vue.config.js全局配置
  3. */
  4. const path = require("path");
  5. const proxy = require("./proxy.config.json");
  6. const {
  7. /* baseURL, */
  8. publicPath,
  9. assetsDir,
  10. outputDir,
  11. transpileDependencies,
  12. title,
  13. abbreviation,
  14. devPort,
  15. providePlugin,
  16. build7z,
  17. buildGzip,
  18. imageCompression,
  19. } = require("./src/config");
  20. const rely = require("vue-plugin-rely");
  21. const { webpackBarName, webpackBanner } = require("./vab.config");
  22. const { version, author } = require("./package.json");
  23. const Webpack = require("webpack");
  24. const WebpackBar = require("webpackbar");
  25. const FileManagerPlugin = require("filemanager-webpack-plugin");
  26. const dayjs = require("dayjs");
  27. const dateTime = dayjs().format("YYYY-M-D HH:mm:ss");
  28. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  29. const productionGzipExtensions = ["html", "js", "css", "svg"];
  30. process.env.VUE_APP_TITLE = title;
  31. process.env.VUE_APP_AUTHOR = author;
  32. process.env.VUE_APP_RANDOM = `${dayjs()}-${process.env.VUE_GITHUB_USER_NAME}`;
  33. process.env.VUE_APP_UPDATE_TIME = dateTime;
  34. process.env.VUE_APP_VERSION = version;
  35. process.env.VUE_APP_RELY = rely;
  36. const resolve = (dir) => {
  37. return path.join(__dirname, dir);
  38. };
  39. module.exports = {
  40. publicPath,
  41. assetsDir,
  42. outputDir,
  43. lintOnSave: false,
  44. transpileDependencies,
  45. devServer: {
  46. disableHostCheck: true,
  47. // allowedHosts: ['www.shbpms.com'],
  48. hot: true,
  49. port: devPort,
  50. open: false,
  51. proxy: proxy,
  52. noInfo: false,
  53. overlay: {
  54. warnings: false,
  55. errors: false,
  56. },
  57. host: "0.0.0.0",
  58. // 注释掉的地方是前端配置代理访问后端的示例
  59. // baseURL必须为/xxx,而不是后端服务器,请先了解代理逻辑,再设置前端代理
  60. // !!!一定要注意!!!
  61. // 1.这里配置了跨域及代理只针对开发环境生效
  62. // 2.不建议你在前端配置跨域,建议你后端配置Allow-Origin,Method,Headers,放行token字段,一步到位
  63. // 3.后端配置了跨域,就不需要前端再配置,会发生Origin冲突
  64. // proxy: {
  65. // [baseURL]: {
  66. // target: `http://你的后端接口地址`,
  67. // ws: true,
  68. // changeOrigin: true,
  69. // pathRewrite: {
  70. // ['^' + baseURL]: '',
  71. // },
  72. // },
  73. // },
  74. after: require("./mock"),
  75. },
  76. pwa: {
  77. workboxOptions: {
  78. skipWaiting: true,
  79. clientsClaim: true,
  80. },
  81. iconPaths: {
  82. favicon32: "favicon.ico",
  83. favicon16: "favicon.ico",
  84. appleTouchIcon: "favicon.ico",
  85. maskIcon: "favicon.ico",
  86. msTileImage: "favicon.ico",
  87. },
  88. themeColor: "#ffffff",
  89. msTileColor: "#ffffff",
  90. appleMobileWebAppCapable: "yes",
  91. appleMobileWebAppStatusBarStyle: "black",
  92. manifestOptions: {
  93. name: "生物样品库安全管理系统",
  94. short_name: "生物样品库安全管理系统",
  95. background_color: "#ffffff",
  96. },
  97. },
  98. configureWebpack() {
  99. return {
  100. resolve: {
  101. alias: {
  102. "~": resolve("."),
  103. "@": resolve("src"),
  104. "@/modules": resolve("src/modules"),
  105. },
  106. },
  107. plugins: [
  108. new Webpack.ProvidePlugin(providePlugin),
  109. new WebpackBar({
  110. name: webpackBarName,
  111. }),
  112. ],
  113. };
  114. },
  115. chainWebpack(config) {
  116. config.resolve.symlinks(true);
  117. config.module.rule("svg").exclude.add(resolve("src/icon"));
  118. config.module
  119. .rule("vabIcon")
  120. .test(/\.svg$/)
  121. .include.add(resolve("src/icon"))
  122. .end()
  123. .use("svg-sprite-loader")
  124. .loader("svg-sprite-loader")
  125. .options({ symbolId: "vab-icon-[name]" });
  126. config.when(process.env.NODE_ENV === "development", (config) => {
  127. config.devtool("source-map");
  128. });
  129. config.when(process.env.NODE_ENV === "production", (config) => {
  130. config.performance.set("hints", false);
  131. config.devtool("none");
  132. config.optimization.splitChunks({
  133. automaticNameDelimiter: "-",
  134. chunks: "all",
  135. cacheGroups: {
  136. chunk: {
  137. name: "vab-chunk",
  138. test: /[\\/]node_modules[\\/]/,
  139. minSize: 131072,
  140. maxSize: 524288,
  141. chunks: "async",
  142. minChunks: 2,
  143. priority: 10,
  144. },
  145. vue: {
  146. name: "vue",
  147. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  148. chunks: "initial",
  149. priority: 20,
  150. },
  151. elementUI: {
  152. name: "element-ui",
  153. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  154. priority: 30,
  155. },
  156. extra: {
  157. name: "vab-extra",
  158. test: resolve("src/extra"),
  159. priority: 40,
  160. },
  161. // 根据使用模块抽取公共代码
  162. echarts: {
  163. name: "echarts",
  164. test: /[\\/]node_modules[\\/](echarts|zrender|tslib)[\\/]/,
  165. priority: 50,
  166. },
  167. },
  168. });
  169. config
  170. .plugin("banner")
  171. .use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`]);
  172. if (imageCompression)
  173. config.module
  174. .rule("images")
  175. .use("image-webpack-loader")
  176. .loader("image-webpack-loader")
  177. .options({
  178. bypassOnDebug: true,
  179. })
  180. .end();
  181. if (buildGzip)
  182. config.plugin("compression").use(CompressionWebpackPlugin, [
  183. {
  184. filename: "[path][base].gz[query]",
  185. algorithm: "gzip",
  186. test: new RegExp(
  187. "\\.(" + productionGzipExtensions.join("|") + ")$"
  188. ),
  189. threshold: 8192,
  190. minRatio: 0.8,
  191. },
  192. ]);
  193. if (build7z)
  194. config.plugin("fileManager").use(FileManagerPlugin, [
  195. {
  196. events: {
  197. onEnd: {
  198. archive: [
  199. {
  200. source: `./${outputDir}`,
  201. destination: `./${outputDir}/${abbreviation}_${dayjs().unix()}.zip`,
  202. },
  203. ],
  204. },
  205. },
  206. },
  207. ]);
  208. });
  209. },
  210. runtimeCompiler: true,
  211. productionSourceMap: false,
  212. css: {
  213. requireModuleExtension: true,
  214. sourceMap: true,
  215. loaderOptions: {
  216. sass: {
  217. sassOptions: { outputStyle: "expanded" },
  218. additionalData(content, loaderContext) {
  219. const { resourcePath, rootContext } = loaderContext;
  220. const relativePath = path.relative(rootContext, resourcePath);
  221. if (
  222. relativePath.replace(/\\/g, "/") !==
  223. "src/vab/styles/variables/variables.scss"
  224. )
  225. return (
  226. '@import "~@/vab/styles/variables/variables.scss";' + content
  227. );
  228. return content;
  229. },
  230. },
  231. },
  232. },
  233. };