vue.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const { webpackBarName, webpackBanner } = require("zx-layouts");
  2. const path = require("path");
  3. const {
  4. publicPath,
  5. assetsDir,
  6. outputDir,
  7. lintOnSave,
  8. transpileDependencies,
  9. title,
  10. abbreviation,
  11. devPort,
  12. providePlugin,
  13. build7z,
  14. } = require("./src/config/settings");
  15. const { version, author } = require("./package.json");
  16. const Webpack = require("webpack");
  17. const WebpackBar = require("webpackbar");
  18. const FileManagerPlugin = require("filemanager-webpack-plugin");
  19. const dayjs = require("dayjs");
  20. const date = new dayjs().format("YYYY_M_D");
  21. const time = new dayjs().format("YYYY-M-D HH:mm:ss");
  22. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  23. const productionGzipExtensions = ["html", "js", "css", "svg"];
  24. process.env.VUE_APP_TITLE = title || "vue-admin-beautiful";
  25. process.env.VUE_APP_AUTHOR = author || "chuzhixin";
  26. process.env.VUE_APP_UPDATE_TIME = time;
  27. process.env.VUE_APP_VERSION = version;
  28. function resolve(dir) {
  29. return path.join(__dirname, dir);
  30. }
  31. function mockServer() {
  32. if (process.env.NODE_ENV === "development") {
  33. const mockServer = require("./mock/mockServer.js");
  34. return mockServer;
  35. } else {
  36. return "";
  37. }
  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. "^": resolve("src/components"),
  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. commons: {
  124. name: "chunk-commons",
  125. test: resolve("src/components"),
  126. minChunks: 3,
  127. priority: 5,
  128. reuseExistingChunk: true,
  129. },
  130. },
  131. });
  132. config
  133. .plugin("banner")
  134. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  135. .end();
  136. config
  137. .plugin("compression")
  138. .use(CompressionWebpackPlugin, [
  139. {
  140. filename: "[path].gz[query]",
  141. algorithm: "gzip",
  142. test: new RegExp(
  143. "\\.(" + productionGzipExtensions.join("|") + ")$"
  144. ),
  145. threshold: 8192,
  146. minRatio: 0.8,
  147. },
  148. ])
  149. .end();
  150. config.module
  151. .rule("images")
  152. .use("image-webpack-loader")
  153. .loader("image-webpack-loader")
  154. .options({
  155. bypassOnDebug: true,
  156. })
  157. .end();
  158. });
  159. if (build7z) {
  160. config.when(process.env.NODE_ENV === "production", (config) => {
  161. config
  162. .plugin("fileManager")
  163. .use(FileManagerPlugin, [
  164. {
  165. onEnd: {
  166. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  167. archive: [
  168. {
  169. source: `./${outputDir}`,
  170. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  171. },
  172. ],
  173. },
  174. },
  175. ])
  176. .end();
  177. });
  178. }
  179. },
  180. runtimeCompiler: true,
  181. productionSourceMap: false,
  182. css: {
  183. requireModuleExtension: true,
  184. sourceMap: true,
  185. loaderOptions: {
  186. scss: {
  187. /*sass-loader 8.0语法 */
  188. //prependData: '@import "~@/styles/variables.scss";',
  189. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  190. additionalData(content, loaderContext) {
  191. const { resourcePath, rootContext } = loaderContext;
  192. const relativePath = path.relative(rootContext, resourcePath);
  193. if (
  194. relativePath.replace(/\\/g, "/") !== "src/styles/variables.scss"
  195. ) {
  196. return '@import "~@/styles/variables.scss";' + content;
  197. }
  198. return content;
  199. },
  200. },
  201. },
  202. },
  203. };