vue.config.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const path = require('path');
  2. const Setting = require('./src/setting.env');
  3. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  4. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  5. // 路径解析辅助函数
  6. const resolve = (dir) => path.join(__dirname, dir);
  7. // 环境变量
  8. const isProd = process.env.NODE_ENV === 'production';
  9. module.exports = {
  10. // 基础配置
  11. outputDir: Setting.outputDir,
  12. runtimeCompiler: true,
  13. productionSourceMap: false, // 关闭生产环境下的SourceMap映射文件
  14. publicPath: '/admin',
  15. assetsDir: 'system_static',
  16. indexPath: 'index.html',
  17. lintOnSave: false,
  18. // 开发服务器配置
  19. devServer: {
  20. port: 1617,
  21. },
  22. // webpack配置
  23. configureWebpack: (config) => {
  24. // 生产环境特定配置
  25. if (isProd) {
  26. // JS文件压缩配置
  27. config.plugins.push(
  28. new UglifyJsPlugin({
  29. uglifyOptions: {
  30. compress: {
  31. drop_debugger: true,
  32. drop_console: true, // 生产环境自动删除console
  33. pure_funcs: ['console.log'], // 移除console.log
  34. },
  35. },
  36. sourceMap: false,
  37. parallel: true, // 使用多进程并行运行来提高构建速度
  38. }),
  39. );
  40. // 代码分割配置(已注释,如需启用可取消注释)
  41. /*
  42. config.optimization = {
  43. runtimeChunk: 'single',
  44. splitChunks: {
  45. chunks: 'all',
  46. maxInitialRequests: Infinity,
  47. minSize: 20000,
  48. cacheGroups: {
  49. vendor: {
  50. test: /[\\/]node_modules[\\/]/,
  51. name(module) {
  52. const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  53. return `npm.${packageName.replace('@', '')}`;
  54. },
  55. },
  56. },
  57. },
  58. };
  59. */
  60. }
  61. },
  62. // webpack链式配置
  63. chainWebpack: (config) => {
  64. // 删除预加载
  65. config.plugins.delete('prefetch');
  66. // 设置路径别名
  67. config.resolve.alias.set('@', resolve('src')).set('_c', resolve('src/components')).set('@api', resolve('src/api'));
  68. // Vue文件规则配置
  69. config.module
  70. .rule('vue')
  71. .test(/\.vue$/)
  72. .end();
  73. // Node配置
  74. config.node.set('__dirname', true).set('__filename', true);
  75. // Monaco编辑器插件
  76. config.plugin('monaco').use(new MonacoWebpackPlugin());
  77. },
  78. };