vue.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const path = require('path');
  2. const { defineConfig } = require('@vue/cli-service');
  3. const webpack = require('webpack');
  4. const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
  5. const dayjs = require('dayjs');
  6. const TerserPlugin = require('terser-webpack-plugin');
  7. const resolve = (dir) => path.join(__dirname, dir); // 路径
  8. const pkg = require('./package.json');
  9. process.env.VUE_APP_VERSION = pkg.version;
  10. const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
  11. const IS_DEV = ['development'].includes(process.env.NODE_ENV);
  12. // port = 8098 npm run dev OR npm run dev --port = 8098
  13. const port = process.env.port || process.env.npm_config_port || 8201; // dev port
  14. const __APP_INFO__ = {
  15. pkg,
  16. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  17. };
  18. // https://next.cli.vuejs.org/
  19. module.exports = defineConfig({
  20. lintOnSave: false, //关闭eslint检查
  21. // publicPath: isDev ? '' : querystring.unescape('<%=request.getContextPath()%>'),
  22. //publicPath: process.env.BASE_URL,
  23. publicPath: '/web/',
  24. // filenameHashing: false,
  25. productionSourceMap: false,
  26. outputDir: "../src/main/resources/static/web",
  27. css: {
  28. loaderOptions: {
  29. // css: {
  30. // modules: {
  31. // auto: (path) => {
  32. // return path.includes('ant-design-vue/dist/antd.dark.css');
  33. // },
  34. // },
  35. // },
  36. less: {
  37. lessOptions: {
  38. javascriptEnabled: true,
  39. modifyVars: {},
  40. },
  41. additionalData: `
  42. @import "ant-design-vue/lib/style/themes/default.less";
  43. @import "~@/styles/variables.less";
  44. `,
  45. },
  46. // sass: {
  47. // additionalData: `
  48. // @use 'sass:math';
  49. // @import "@/styles/global.scss";`
  50. // }
  51. },
  52. },
  53. chainWebpack: (config) => {
  54. // 移除 preload 插件
  55. config.plugins.delete('preload');
  56. // 移除 prefetch 插件
  57. config.plugins.delete('prefetch');
  58. // 优化二次启动速度
  59. config.cache({
  60. // 将缓存类型设置为文件系统,默认是memory
  61. type: 'filesystem',
  62. buildDependencies: {
  63. // 更改配置文件时,重新缓存
  64. config: [__filename],
  65. },
  66. });
  67. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  68. config.optimization.runtimeChunk('single');
  69. config
  70. // https://webpack.js.org/configuration/devtool/#development
  71. .when(IS_DEV, (config) => config.devtool('cheap-source-map'));
  72. // 配置相关loader,支持修改,添加和替换相关的loader
  73. config.resolve.alias.set('@', resolve('src'));
  74. config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
  75. if (process.env.DEBUG_ANTDV) {
  76. console.info('DEBUG_ANTDV', process.env.DEBUG_ANTDV);
  77. config.resolve.alias.set('ant-design-vue/es/', 'ant-design-vue/components/');
  78. config.resolve.alias.set('ant-design-vue/lib/', 'ant-design-vue/components/');
  79. config.resolve.alias.set('vue', 'ant-design-vue/node_modules/vue');
  80. }
  81. config.plugin('html').tap((args) => {
  82. args[0].title = '惠州就业驿站管理系统';
  83. return args;
  84. });
  85. config.module
  86. .rule('css')
  87. .exclude.add(resolve('node_modules/ant-design-vue/dist/antd.dark.css'))
  88. .end();
  89. config.module.rule('raw-css').resourceQuery(/raw/).type('asset/source');
  90. // 忽略解析markdown文件
  91. config.module.noParse(/\.md$/);
  92. // config.module.rule('css').test(/\.ts$/).resourceQuery(/raw/).type('asset/source').end();
  93. // svg rule loader
  94. config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end();
  95. config.module
  96. .rule('icons')
  97. .test(/\.svg$/)
  98. .include.add(resolve('src/assets/icons'))
  99. .end()
  100. .use('svg-sprite-loader')
  101. .loader('svg-sprite-loader')
  102. .options({
  103. symbolId: 'svg-icon-[name]',
  104. });
  105. config.when(IS_PROD, (config) => {
  106. // split
  107. config.optimization.splitChunks({
  108. chunks: 'all', //指定哪些模块需要打包
  109. cacheGroups: {
  110. libs: {
  111. name: 'chunk-libs',
  112. test: /[\\/]node_modules[\\/]/,
  113. priority: 10,
  114. chunks: 'initial', // only package third parties that are initially dependent
  115. },
  116. antdv: {
  117. name: 'chunk-ant-design-vue', // split ant-design-vue into a single package
  118. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  119. test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/, // in order to adapt to cnpm
  120. },
  121. commons: {
  122. name: 'chunk-commons',
  123. test: resolve('src/components'), // can customize your rules
  124. minChunks: 3, // 被引用3次就提取出来
  125. priority: 5,
  126. reuseExistingChunk: true, // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
  127. },
  128. },
  129. });
  130. config.module
  131. .rule('md')
  132. .test(/\.md$/)
  133. .type('javascript/auto')
  134. .use('asset')
  135. .loader('asset')
  136. .options({
  137. limit: 100,
  138. esModule: false,
  139. generator: () => '',
  140. });
  141. });
  142. },
  143. configureWebpack: (config) => {
  144. // 开启顶级await
  145. config.experiments = {
  146. topLevelAwait: true,
  147. };
  148. config.resolve.fallback = { path: require.resolve('path-browserify') };
  149. config.devtool = 'source-map';
  150. config.plugins.push(
  151. // 定义全局变量
  152. new webpack.DefinePlugin({
  153. __APP_INFO__: JSON.stringify(__APP_INFO__),
  154. }),
  155. // 打包速度分析
  156. new SpeedMeasurePlugin(),
  157. // use defineOptions https://github.com/sxzz/unplugin-vue-define-options
  158. require('unplugin-vue-define-options/webpack')(),
  159. );
  160. if (IS_PROD) {
  161. // terser-webpack-plugin (https://webpack.docschina.org/plugins/terser-webpack-plugin/);
  162. const TerserPluginIndex = config.optimization.minimizer.findIndex(
  163. (n) => n.__pluginName === 'terser',
  164. );
  165. config.optimization.minimizer[TerserPluginIndex] = new TerserPlugin({
  166. terserOptions: {
  167. warnings: false,
  168. format: {
  169. comments: false,
  170. },
  171. compress: {
  172. drop_debugger: true, // 注释console
  173. drop_console: true,
  174. pure_funcs: ['console.log'], // 移除console
  175. },
  176. },
  177. extractComments: false, // 是否将注释提取到一个单独的文件中
  178. parallel: true, // 是否并⾏打包
  179. });
  180. }
  181. },
  182. devServer: {
  183. port,
  184. client: {
  185. progress: true,
  186. },
  187. // watchOptions: {
  188. // // 开发时,自动保存代码导致构建频繁且会报错,又不想手动保存,则可以开启延迟构建
  189. // aggregateTimeout: 1500,
  190. // ignored: /node_modules/,
  191. // },
  192. proxy: {
  193. // '/mock-api': {
  194. // target: `http://localhost:${port}`,
  195. // changeOrigin: true,
  196. // logLevel: 'debug',
  197. // pathRewrite: {
  198. // '^/mock-api': ''
  199. // }
  200. // },
  201. '^/api': {
  202. // target: process.env.VUE_APP_API_URL,
  203. target: 'http://localhost:8200',
  204. // target: 'http://localhost:7001 ',
  205. changeOrigin: true,
  206. logLevel: 'debug',
  207. pathRewrite: {
  208. '^/api': '/api',
  209. },
  210. },
  211. '^/n-api': {
  212. // target: process.env.VUE_APP_API_URL,
  213. target: 'http://localhost:8200',
  214. // target: 'http://localhost:7001 ',
  215. changeOrigin: true,
  216. logLevel: 'debug',
  217. pathRewrite: {
  218. '^/n-api': '/api',
  219. },
  220. },
  221. '^/ws-api': {
  222. target: 'wss://nest-api.buqiyuan.site',
  223. // target: 'http://localhost:7002',
  224. changeOrigin: true, //是否允许跨域
  225. wss: true,
  226. logLevel: 'debug',
  227. },
  228. },
  229. setupMiddlewares: require('./src/mock/mock-server.js'),
  230. },
  231. });