vue.config.js 5.6 KB

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