vue.config.js 5.3 KB

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