vue.config.js 5.5 KB

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