vue.config.js 7.9 KB

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