vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @description vue.config.js全局配置
  3. */
  4. const path = require("path");
  5. const {
  6. baseURL,
  7. publicPath,
  8. assetsDir,
  9. outputDir,
  10. lintOnSave,
  11. transpileDependencies,
  12. title,
  13. abbreviation,
  14. devPort,
  15. providePlugin,
  16. build7z,
  17. donation,
  18. } = require("./src/config/settings");
  19. const {
  20. webpackBarName,
  21. webpackBanner,
  22. donationConsole,
  23. } = require("zx-layouts");
  24. if (donation) donationConsole();
  25. const { version, author } = require("./package.json");
  26. const Webpack = require("webpack");
  27. const WebpackBar = require("webpackbar");
  28. const FileManagerPlugin = require("filemanager-webpack-plugin");
  29. const dayjs = require("dayjs");
  30. const date = dayjs().format("YYYY_M_D");
  31. const time = dayjs().format("YYYY-M-D HH:mm:ss");
  32. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  33. const productionGzipExtensions = ["html", "js", "css", "svg"];
  34. process.env.VUE_APP_TITLE = title || "vue-admin-beautiful";
  35. process.env.VUE_APP_AUTHOR = author || "chuzhixin";
  36. process.env.VUE_APP_UPDATE_TIME = time;
  37. process.env.VUE_APP_VERSION = version;
  38. const resolve = (dir) => {
  39. return path.join(__dirname, dir);
  40. };
  41. const mockServer = () => {
  42. if (process.env.NODE_ENV === "development") {
  43. return require("./mock/mockServer.js");
  44. } else {
  45. return "";
  46. }
  47. };
  48. module.exports = {
  49. publicPath,
  50. assetsDir,
  51. outputDir,
  52. lintOnSave,
  53. transpileDependencies,
  54. devServer: {
  55. hot: true,
  56. port: devPort,
  57. open: true,
  58. noInfo: false,
  59. overlay: {
  60. warnings: true,
  61. errors: true,
  62. },
  63. // 注释掉的地方是前端配置代理访问后端的示例
  64. // proxy: {
  65. // [baseURL]: {
  66. // target: `http://你的后端接口地址`,
  67. // ws: true,
  68. // changeOrigin: true,
  69. // pathRewrite: {
  70. // ["^/" + baseURL]: "",
  71. // },
  72. // },
  73. // },
  74. after: mockServer(),
  75. },
  76. configureWebpack() {
  77. return {
  78. resolve: {
  79. alias: {
  80. "@": resolve("src"),
  81. "*": resolve(""),
  82. },
  83. },
  84. plugins: [
  85. new Webpack.ProvidePlugin(providePlugin),
  86. new WebpackBar({
  87. name: webpackBarName,
  88. }),
  89. ],
  90. };
  91. },
  92. chainWebpack(config) {
  93. config.resolve.symlinks(true);
  94. config.module
  95. .rule("svg")
  96. .exclude.add(resolve("src/icon/remixIcon"))
  97. .add(resolve("src/icon/colorfulIcon"))
  98. .end();
  99. config.module
  100. .rule("remixIcon")
  101. .test(/\.svg$/)
  102. .include.add(resolve("src/icon/remixIcon"))
  103. .end()
  104. .use("svg-sprite-loader")
  105. .loader("svg-sprite-loader")
  106. .options({ symbolId: "remix-icon-[name]" })
  107. .end();
  108. config.module
  109. .rule("colorfulIcon")
  110. .test(/\.svg$/)
  111. .include.add(resolve("src/icon/colorfulIcon"))
  112. .end()
  113. .use("svg-sprite-loader")
  114. .loader("svg-sprite-loader")
  115. .options({ symbolId: "colorful-icon-[name]" })
  116. .end();
  117. config.when(process.env.NODE_ENV === "development", (config) => {
  118. config.devtool("source-map");
  119. });
  120. config.when(process.env.NODE_ENV !== "development", (config) => {
  121. config.performance.set("hints", false);
  122. config.devtool("none");
  123. config.optimization.splitChunks({
  124. chunks: "all",
  125. cacheGroups: {
  126. libs: {
  127. name: "vue-admin-beautiful-pro-libs",
  128. test: /[\\/]node_modules[\\/]/,
  129. priority: 10,
  130. chunks: "initial",
  131. },
  132. elementUI: {
  133. name: "vue-admin-beautiful-pro-element-ui",
  134. priority: 20,
  135. test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
  136. },
  137. },
  138. });
  139. config
  140. .plugin("banner")
  141. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  142. .end();
  143. config
  144. .plugin("compression")
  145. .use(CompressionWebpackPlugin, [
  146. {
  147. filename: "[path].gz[query]",
  148. algorithm: "gzip",
  149. test: new RegExp(
  150. "\\.(" + productionGzipExtensions.join("|") + ")$"
  151. ),
  152. threshold: 8192,
  153. minRatio: 0.8,
  154. },
  155. ])
  156. .end();
  157. });
  158. if (build7z) {
  159. config.when(process.env.NODE_ENV === "production", (config) => {
  160. config
  161. .plugin("fileManager")
  162. .use(FileManagerPlugin, [
  163. {
  164. onEnd: {
  165. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  166. archive: [
  167. {
  168. source: `./${outputDir}`,
  169. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  170. },
  171. ],
  172. },
  173. },
  174. ])
  175. .end();
  176. });
  177. }
  178. },
  179. runtimeCompiler: true,
  180. productionSourceMap: false,
  181. css: {
  182. requireModuleExtension: true,
  183. sourceMap: true,
  184. loaderOptions: {
  185. scss: {
  186. additionalData(content, loaderContext) {
  187. const { resourcePath, rootContext } = loaderContext;
  188. const relativePath = path.relative(rootContext, resourcePath);
  189. if (
  190. relativePath.replace(/\\/g, "/") !== "src/config/variables.scss"
  191. ) {
  192. return '@import "~@/config/variables.scss";' + content;
  193. }
  194. return content;
  195. },
  196. },
  197. },
  198. },
  199. };