vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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");
  15. const { webpackBarName, webpackBanner, donationConsole } = require("./layouts");
  16. if (donation) donationConsole();
  17. const { version, author } = require("./package.json");
  18. const Webpack = require("webpack");
  19. const WebpackBar = require("webpackbar");
  20. const FileManagerPlugin = require("filemanager-webpack-plugin");
  21. const dayjs = require("dayjs");
  22. const date = dayjs().format("YYYY_M_D");
  23. const time = dayjs().format("YYYY-M-D HH:mm:ss");
  24. process.env.VUE_APP_TITLE = title || "opsli-boot";
  25. process.env.VUE_APP_AUTHOR = "https://vue-admin-beautiful.com";
  26. process.env.VUE_APP_UPDATE_TIME = time;
  27. process.env.VUE_APP_VERSION = version;
  28. const resolve = (dir) => path.join(__dirname, dir);
  29. const mockServer = () => {
  30. if (process.env.NODE_ENV === "development") return require("./mock");
  31. else return "";
  32. };
  33. module.exports = {
  34. lintOnSave: false,
  35. publicPath,
  36. assetsDir,
  37. outputDir,
  38. transpileDependencies,
  39. devServer: {
  40. hot: true,
  41. port: devPort,
  42. open: true,
  43. noInfo: false,
  44. overlay: {
  45. warnings: true,
  46. errors: true,
  47. },
  48. after: mockServer(),
  49. },
  50. configureWebpack() {
  51. return {
  52. resolve: {
  53. alias: {
  54. "@": resolve("src"),
  55. },
  56. },
  57. plugins: [
  58. new Webpack.ProvidePlugin(providePlugin),
  59. new WebpackBar({
  60. name: webpackBarName,
  61. }),
  62. ],
  63. };
  64. },
  65. chainWebpack(config) {
  66. config.plugins.delete("preload");
  67. config.plugins.delete("prefetch");
  68. config.module
  69. .rule("svg")
  70. .exclude.add(resolve("src/remixIcon"))
  71. .add(resolve("src/colorfulIcon"))
  72. .end();
  73. config.module
  74. .rule("remixIcon")
  75. .test(/\.svg$/)
  76. .include.add(resolve("src/remixIcon"))
  77. .end()
  78. .use("svg-sprite-loader")
  79. .loader("svg-sprite-loader")
  80. .options({ symbolId: "remix-icon-[name]" })
  81. .end();
  82. config.module
  83. .rule("colorfulIcon")
  84. .test(/\.svg$/)
  85. .include.add(resolve("src/colorfulIcon"))
  86. .end()
  87. .use("svg-sprite-loader")
  88. .loader("svg-sprite-loader")
  89. .options({ symbolId: "colorful-icon-[name]" })
  90. .end();
  91. // config.when(process.env.NODE_ENV === "development", (config) => {
  92. // config.devtool("source-map");
  93. // });
  94. config.when(process.env.NODE_ENV !== "development", (config) => {
  95. config.performance.set("hints", false);
  96. config.devtool("none");
  97. config.optimization.splitChunks({
  98. chunks: "all",
  99. cacheGroups: {
  100. chunk: {
  101. name: "vab-chunk",
  102. test: /[\\/]node_modules[\\/]/,
  103. minSize: 131072,
  104. maxSize: 524288,
  105. chunks: "async",
  106. minChunks: 2,
  107. priority: 10,
  108. },
  109. vue: {
  110. name: "vue",
  111. test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
  112. chunks: "initial",
  113. priority: 20,
  114. },
  115. elementUI: {
  116. name: "element-ui",
  117. test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
  118. priority: 30,
  119. },
  120. extra: {
  121. name: "vab-layouts",
  122. test: resolve("src/layouts"),
  123. priority: 40,
  124. },
  125. },
  126. });
  127. config
  128. .plugin("banner")
  129. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  130. .end();
  131. });
  132. if (build7z) {
  133. config.when(process.env.NODE_ENV === "production", (config) => {
  134. config
  135. .plugin("fileManager")
  136. .use(FileManagerPlugin, [
  137. {
  138. onEnd: {
  139. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  140. archive: [
  141. {
  142. source: `./${outputDir}`,
  143. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  144. },
  145. ],
  146. },
  147. },
  148. ])
  149. .end();
  150. });
  151. }
  152. },
  153. runtimeCompiler: true,
  154. productionSourceMap: false,
  155. css: {
  156. requireModuleExtension: true,
  157. sourceMap: true,
  158. loaderOptions: {
  159. scss: {
  160. /*sass-loader 8.0语法 */
  161. //prependData: '@import "~@/styles/variables.scss";',
  162. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  163. additionalData(content, loaderContext) {
  164. const { resourcePath, rootContext } = loaderContext;
  165. const relativePath = path.relative(rootContext, resourcePath);
  166. if (
  167. relativePath.replace(/\\/g, "/") !== "src/styles/variables.scss"
  168. ) {
  169. return '@import "~@/styles/variables.scss";' + content;
  170. }
  171. return content;
  172. },
  173. },
  174. },
  175. },
  176. };