vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const path = require("path");
  2. const {
  3. publicPath,
  4. assetsDir,
  5. outputDir,
  6. lintOnSave,
  7. transpileDependencies,
  8. title,
  9. abbreviation,
  10. devPort,
  11. providePlugin,
  12. build7z,
  13. donation,
  14. } = require("./src/config/settings");
  15. const {
  16. webpackBarName,
  17. webpackBanner,
  18. donationConsole,
  19. } = require("zx-layouts");
  20. if (donation) donationConsole();
  21. const { version, author } = require("./package.json");
  22. const Webpack = require("webpack");
  23. const WebpackBar = require("webpackbar");
  24. const FileManagerPlugin = require("filemanager-webpack-plugin");
  25. const dayjs = require("dayjs");
  26. const date = dayjs().format("YYYY_M_D");
  27. const time = dayjs().format("YYYY-M-D HH:mm:ss");
  28. const productionGzipExtensions = ["html", "js", "css", "svg"];
  29. process.env.VUE_APP_TITLE = title || "vue-admin-beautiful";
  30. process.env.VUE_APP_AUTHOR = author || "chuzhixin 1204505056@qq.com";
  31. process.env.VUE_APP_UPDATE_TIME = time;
  32. process.env.VUE_APP_VERSION = version;
  33. const resolve = (dir) => path.join(__dirname, dir);
  34. const mockServer = () => {
  35. if (process.env.NODE_ENV === "development")
  36. return require("./mock/mockServer.js");
  37. else return "";
  38. };
  39. module.exports = {
  40. publicPath,
  41. assetsDir,
  42. outputDir,
  43. lintOnSave,
  44. transpileDependencies,
  45. devServer: {
  46. hot: true,
  47. port: devPort,
  48. open: true,
  49. noInfo: false,
  50. overlay: {
  51. warnings: true,
  52. errors: true,
  53. },
  54. after: mockServer(),
  55. },
  56. configureWebpack() {
  57. return {
  58. resolve: {
  59. alias: {
  60. "@": resolve("src"),
  61. },
  62. },
  63. plugins: [
  64. new Webpack.ProvidePlugin(providePlugin),
  65. new WebpackBar({
  66. name: webpackBarName,
  67. }),
  68. ],
  69. };
  70. },
  71. chainWebpack(config) {
  72. config.plugins.delete("preload");
  73. config.plugins.delete("prefetch");
  74. config.module
  75. .rule("svg")
  76. .exclude.add(resolve("src/remixIcon"))
  77. .add(resolve("src/colorfulIcon"))
  78. .end();
  79. config.module
  80. .rule("remixIcon")
  81. .test(/\.svg$/)
  82. .include.add(resolve("src/remixIcon"))
  83. .end()
  84. .use("svg-sprite-loader")
  85. .loader("svg-sprite-loader")
  86. .options({ symbolId: "remix-icon-[name]" })
  87. .end();
  88. config.module
  89. .rule("colorfulIcon")
  90. .test(/\.svg$/)
  91. .include.add(resolve("src/colorfulIcon"))
  92. .end()
  93. .use("svg-sprite-loader")
  94. .loader("svg-sprite-loader")
  95. .options({ symbolId: "colorful-icon-[name]" })
  96. .end();
  97. /* config.when(process.env.NODE_ENV === "development", (config) => {
  98. config.devtool("source-map");
  99. }); */
  100. config.when(process.env.NODE_ENV !== "development", (config) => {
  101. config.performance.set("hints", false);
  102. config.devtool("none");
  103. config.optimization.splitChunks({
  104. chunks: "all",
  105. cacheGroups: {
  106. libs: {
  107. name: "chunk-libs",
  108. test: /[\\/]node_modules[\\/]/,
  109. priority: 10,
  110. chunks: "initial",
  111. },
  112. elementUI: {
  113. name: "chunk-elementUI",
  114. priority: 20,
  115. test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
  116. },
  117. fortawesome: {
  118. name: "chunk-fortawesome",
  119. priority: 20,
  120. test: /[\\/]node_modules[\\/]_?@fortawesome(.*)/,
  121. },
  122. },
  123. });
  124. config
  125. .plugin("banner")
  126. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  127. .end();
  128. config.module
  129. .rule("images")
  130. .use("image-webpack-loader")
  131. .loader("image-webpack-loader")
  132. .options({
  133. bypassOnDebug: true,
  134. })
  135. .end();
  136. });
  137. if (build7z) {
  138. config.when(process.env.NODE_ENV === "production", (config) => {
  139. config
  140. .plugin("fileManager")
  141. .use(FileManagerPlugin, [
  142. {
  143. onEnd: {
  144. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  145. archive: [
  146. {
  147. source: `./${outputDir}`,
  148. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  149. },
  150. ],
  151. },
  152. },
  153. ])
  154. .end();
  155. });
  156. }
  157. },
  158. runtimeCompiler: true,
  159. productionSourceMap: false,
  160. css: {
  161. requireModuleExtension: true,
  162. sourceMap: true,
  163. loaderOptions: {
  164. scss: {
  165. /*sass-loader 8.0语法 */
  166. //prependData: '@import "~@/styles/variables.scss";',
  167. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  168. additionalData(content, loaderContext) {
  169. const { resourcePath, rootContext } = loaderContext;
  170. const relativePath = path.relative(rootContext, resourcePath);
  171. if (
  172. relativePath.replace(/\\/g, "/") !== "src/styles/variables.scss"
  173. ) {
  174. return '@import "~@/styles/variables.scss";' + content;
  175. }
  176. return content;
  177. },
  178. },
  179. },
  180. },
  181. };