vue.config.js 6.0 KB

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