vue.config.js 5.5 KB

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