vue.config.js 4.6 KB

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